d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Assistance With A Js Assignment
Add Reply New Topic New Poll
Member
Posts: 11,499
Joined: Sep 5 2011
Gold: 11,376.21
Apr 21 2016 09:31am
will compensate, pm me for details
Member
Posts: 11,499
Joined: Sep 5 2011
Gold: 11,376.21
Apr 21 2016 03:06pm
Code
<!DOCTYPE html>
<html>
<head>
<title> 1D Lights Out </title>
<style>
div {
width: 200px;
height: 50px;
border: 1px solid black;
}
</style>
<script>
// Create a global variable to keep track of the number of "clicks":
var clicks = 0;


// Create an init() function that sets the background color on all the "l*"
// divs to red.
function init() {

l6.style.backgroundColor = 'red';
l5.style.backgroundColor = 'red';
l4.style.backgroundColor = 'red';
l3.style.backgroundColor = 'red';
l2.style.backgroundColor = 'red';
l1.style.backgroundColor = 'red';
l0.style.backgroundColor = 'red';
}

// This function toggles the background color of the div "l<n>". Use the color
// "red" for your test. If it's red, then make it white, if it's not red,
// then make it red.
function toggle(n) {

n = "l" + n;
if (eval(n).style.backgroundColor === 'red') {
eval(n).style.backgroundColor = 'white';
} else if (eval(n).style.backgroundColor === 'white') {
eval(n).style.backgroundColor = 'red';
}
}

function checkforwin() {
win = true;
if (l0.style.backgroundColor == 'red') {
win = false;
}
if (l1.style.backgroundColor == 'red') {
win = false;
}
if (l2.style.backgroundColor == 'red') {
win = false;
}
if (l3.style.backgroundColor == 'red') {
win = false;
}
if (l4.style.backgroundColor == 'red') {
win = false;
}
if (l5.style.backgroundColor == 'red') {
win = false;
}
if (l6.style.backgroundColor == 'red') {
win = false;
}


// If the boolean variable is true, then call the window.alert() function to
// print that "You win!".
if (win) {
window.alert("You win!");
}
}

// Called when we click on an element. n is the number of the div we clicked
// on:
function set(n) {
if (eval("l" + n).style.backgroundColor == "red") {
eval("l" + n).style.backgroundColor = "white";
} else {
eval("l" + n).style.backgroundColor = "red";
}
// If n is > 0, then toggle the n below this one (n-1):
console.log(n);
var o = n;
if (n > 0) {
n = n - 1;
toggle(n);
}
// Toggle this element (n)

// If n is < 6, then toggle the n above this one (n+1):
if (o < 6) {
o = o + 1;
toggle(o);
}

// Increment the counter that keeps track of the number of clicks:
clicks += 1;
// Update the clicks display:
document.getElementById("clicks").innerHTML = clicks;

// Call the checkforwin() function to see if we've won:
checkforwin();
}
</script>
</head>
<body onload='init();'>
<table>
<tr>
<td>
<div id='l0' onclick='set(0);'></div>
<div id='l1' onclick='set(1);'></div>
<div id='l2' onclick='set(2);'></div>
<div id='l3' onclick='set(3);'></div>
<div id='l4' onclick='set(4);'></div>
<div id='l5' onclick='set(5);'></div>
<div id='l6' onclick='set(6);'></div>
<td>
# of clicks:
<td id='clicks'> 0
</table>
</body>
</html>


what I have so far for a basic lights game, just need to make it 5x5 boxes now instead of only 6 boxes
Member
Posts: 1,741
Joined: Feb 8 2008
Gold: 2,186.00
Apr 22 2016 07:05pm
Something like this? -> http://unhabited.planet.ee/labor/lights.html (just check the source)

I'm not quite sure if i understood the point of this game. You click on a square and the color of that square as well as colors of all squares around it change - correct? And you'll win if you can change the colors of all squares? Well, you can always win with 4 easy clicks then. (You can change which squares are affected easily by removing the corrsponding 'changeBg(row...);' from the code.)

I also changed your code a bit. Most importantly - i removed all global scope variables (by wraping everything into function that runs when dom is loaded) and embed js in html (by adding event handlers) what i consider to be bad practice. I also used table cells instead div-s, changed the check for win system, new game is automatically started after you win now etc.

If you have any questions - leave them here or just pm me.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll