|
| 1 | +// Set up variables for the game. |
| 2 | +let loose = false; |
| 3 | +let greenSpots = []; |
| 4 | +let redSpots = []; |
| 5 | + |
| 6 | +function placeGreen() { |
| 7 | + if (greenSpots.length > score) { |
| 8 | + return; |
| 9 | + } |
| 10 | + if (redSpots.length + greenSpots.length > 13) { |
| 11 | + return; |
| 12 | + } |
| 13 | + let greencoordinates = {}; |
| 14 | + placeGreenLoop: while (typeof greencoordinates.column === 'undefined') { |
| 15 | + const random = randomElement(); |
| 16 | + for (let g = 0; g < greenSpots.length; g += 1) { |
| 17 | + if (greenSpots[g].column === random.column && greenSpots[g].row === random.row) { |
| 18 | + continue placeGreenLoop; |
| 19 | + } |
| 20 | + } |
| 21 | + for (let g = 0; g < redSpots.length; g += 1) { |
| 22 | + if (redSpots[g].column === random.column && redSpots[g].row === random.row) { |
| 23 | + continue placeGreenLoop; |
| 24 | + } |
| 25 | + } |
| 26 | + const randomHealth = randomRange(15, 50); |
| 27 | + greencoordinates = { column: random.column, row: random.row, health: randomHealth }; |
| 28 | + } |
| 29 | + greenSpots.push(greencoordinates); |
| 30 | +} |
| 31 | + |
| 32 | +function placeRed() { |
| 33 | + if (redSpots.length > score) { |
| 34 | + return; |
| 35 | + } |
| 36 | + if (redSpots.length + greenSpots.length > 13) { |
| 37 | + return; |
| 38 | + } |
| 39 | + let redcoordinates = {}; |
| 40 | + placeRedLoop: while (typeof redcoordinates.column === 'undefined') { |
| 41 | + const random = randomElement(); |
| 42 | + for (let g = 0; g < greenSpots.length; g += 1) { |
| 43 | + if (greenSpots[g].column === random.column && greenSpots[g].row === random.row) { |
| 44 | + continue placeRedLoop; |
| 45 | + } |
| 46 | + } |
| 47 | + for (let g = 0; g < redSpots.length; g += 1) { |
| 48 | + if (redSpots[g].column === random.column && redSpots[g].row === random.row) { |
| 49 | + continue placeRedLoop; |
| 50 | + } |
| 51 | + } |
| 52 | + const randomHealth = randomRange(25, 50); |
| 53 | + redcoordinates = { column: random.column, row: random.row, health: randomHealth }; |
| 54 | + } |
| 55 | + redSpots.push(redcoordinates); |
| 56 | +} |
| 57 | + |
| 58 | +function userActionClick(element) { |
| 59 | + for (let g = 0; g < greenSpots.length; g += 1) { |
| 60 | + if (greenSpots[g].column === element.column && greenSpots[g].row === element.row) { |
| 61 | + greenSpots.splice(g, 1); |
| 62 | + score += 1; |
| 63 | + } |
| 64 | + } |
| 65 | + for (let g = 0; g < redSpots.length; g += 1) { |
| 66 | + if (redSpots[g].column === element.column && redSpots[g].row === element.row) { |
| 67 | + loose = true; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// Decrease the health of all counters. If any counter reaches 0 then remove it from the grid. |
| 73 | +function decreaseHealth() { |
| 74 | + for (let g = 0; g < greenSpots.length; g += 1) { |
| 75 | + greenSpots[g].health -= 1; |
| 76 | + if (greenSpots[g].health === 0) { |
| 77 | + greenSpots.splice(g, 1); |
| 78 | + } |
| 79 | + } |
| 80 | + for (let r = 0; r < redSpots.length; r += 1) { |
| 81 | + redSpots[r].health -= 1; |
| 82 | + if (redSpots[r].health === 0) { |
| 83 | + redSpots.splice(r, 1); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +function init() { |
| 89 | + placeGreen(); |
| 90 | + placeRed(); |
| 91 | +} |
| 92 | + |
| 93 | +function update() { |
| 94 | + if (loose === true) { |
| 95 | + // Crashed! |
| 96 | + // Flush the screen. |
| 97 | + cls(); |
| 98 | + // Display the score. |
| 99 | + displayScore(score); |
| 100 | + // Reset some variables. |
| 101 | + score = 0; |
| 102 | + |
| 103 | + greenSpots = []; |
| 104 | + redSpots = []; |
| 105 | + |
| 106 | + placeGreen(); |
| 107 | + placeRed(); |
| 108 | + |
| 109 | + // Turn off the win state. |
| 110 | + loose = false; |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + // Randomly place a counter. |
| 115 | + let actions = [ |
| 116 | + placeGreen, |
| 117 | + placeRed, |
| 118 | + ]; |
| 119 | + shuffle(actions); |
| 120 | + actions[0](); |
| 121 | + |
| 122 | + decreaseHealth(); |
| 123 | +} |
| 124 | + |
| 125 | +function draw() { |
| 126 | + cls(); |
| 127 | + for (let i = 0; i < gridMaxX; i += 1) { |
| 128 | + for (let j = 0; j < gridMaxY; j += 1) { |
| 129 | + const element = grid[i][j]; |
| 130 | + for (let g = 0; g < greenSpots.length; g += 1) { |
| 131 | + if (greenSpots[g].column === element.column && greenSpots[g].row === element.row) { |
| 132 | + fillBox(element, 'green'); |
| 133 | + } |
| 134 | + } |
| 135 | + for (let g = 0; g < redSpots.length; g += 1) { |
| 136 | + if (redSpots[g].column === element.column && redSpots[g].row === element.row) { |
| 137 | + fillBox(element, 'red'); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments