sample code: Boxes

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

CSS , Javascript

A game with blocks

Make a simple game that takes place on a 3 × 3 grid as shown on the below image. Blocks are denoted by letters from A to H and are initially shuffled. The player can drag a chosen block to the empty field. Only one block can reside on a field at any time. When blocks are sorted alphabetically A – H the game is finished.

sample code:
<!DOCTYPE html>
<html>

<title>Boxes</title>

<style>

.box {
display: flex;
float: left;
width: 100px;
height: 100px;
border: 2px solid red;
justify-content: center;
align-items: center;
margin: 5px;
}
.item {

display: flex;
width: 90px;
height: 90px;
background-color: seagreen;
justify-content: center;
align-items: center;
font-size: 50px;
color: yellow;
user-select: none;
}

</style>

<script>

function itemDragStart(event) {
console.log("Dragging", event.target.id);
event.dataTransfer.setData("text", event.target.id);
}

 

function itemDragOver(event) {
var targetBox = event.target;
console.log("drag over", targetBox.id);
event.preventDefault();
}

 

function itemDrop(event) {
console.log("dropped");
var targetBox = event.target;
targetBox.style.backgroundColor = "";
var draggedId = event.dataTransfer.getData("text");
var draggedItem = document.getElementById(draggedId);
targetBox.appendChild(draggedItem);
event.preventDefault();
}


function itemDragEnter(event) {
var targetBox = event.target;
targetBox.style.backgroundColor = "silver";
event.preventDefault();
}

 

function itemDragLeave(event) {
var targetBox = event.target;
targetBox.style.backgroundColor = "";
event.preventDefault();
}

 

</script>

<body>

<div id="box1" class="box" ondragover="itemDragOver(event)" ondrop="itemDrop(event)">
<div id="item1" class="item" draggable="true" ondragstart="itemDragStart(event)">A</div>
</div>

<div id="box2" class="box" ondragover="itemDragOver(event)" ondrop="itemDrop(event)">
<div id="item2" class="item" draggable="true" ondragstart="itemDragStart(event)">B</div>
</div>


</div>

<script>

for (i = 3; i <= 3; i++) {
document.write(
`<div id="box${i}"
class="box"
ondragover="itemDragOver(event)"
ondrop="itemDrop(event)"
ondragenter="itemDragEnter(event)"
ondragleave="itemDragLeave(event)"
></div>`
);
}

 

</script>

</body>
</html>

C
B
H
F
E
A
D
Transcribed Image Text:C B H F E A D
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY