|
| 1 | +/* |
| 2 | +First time? Check out the tutorial game: |
| 3 | +https://sprig.hackclub.com/gallery/getting_started |
| 4 | +
|
| 5 | +@title: TicTacToe |
| 6 | +@author: Benjamin |
| 7 | +@tags: ['tictactoe','tic-tac-toe'] |
| 8 | +@addedOn: 2024-07-01 |
| 9 | +*/ |
| 10 | + |
| 11 | +const player = "p"; |
| 12 | +const x = "x"; |
| 13 | +const o = "o"; |
| 14 | +const background = "b"; |
| 15 | +let turn = false; |
| 16 | +let board = [ |
| 17 | + ["", "", ""], |
| 18 | + ["", "", ""], |
| 19 | + ["", "", ""] |
| 20 | +]; |
| 21 | +gameover = false; |
| 22 | + |
| 23 | +setLegend( |
| 24 | + [player, bitmap` |
| 25 | +................ |
| 26 | +................ |
| 27 | +................ |
| 28 | +................ |
| 29 | +................ |
| 30 | +.....000000..... |
| 31 | +.....0....0..... |
| 32 | +.....0.00.0..... |
| 33 | +.....0.00.0..... |
| 34 | +.....0....0..... |
| 35 | +.....000000..... |
| 36 | +................ |
| 37 | +................ |
| 38 | +................ |
| 39 | +................ |
| 40 | +................`], |
| 41 | + [x, bitmap` |
| 42 | +................ |
| 43 | +................ |
| 44 | +................ |
| 45 | +................ |
| 46 | +................ |
| 47 | +.....3....3..... |
| 48 | +......3..3...... |
| 49 | +.......33....... |
| 50 | +.......33....... |
| 51 | +......3..3...... |
| 52 | +.....3....3..... |
| 53 | +................ |
| 54 | +................ |
| 55 | +................ |
| 56 | +................ |
| 57 | +................`], |
| 58 | + [o, bitmap` |
| 59 | +................ |
| 60 | +................ |
| 61 | +................ |
| 62 | +................ |
| 63 | +................ |
| 64 | +.....555555..... |
| 65 | +.....5....5..... |
| 66 | +.....5....5..... |
| 67 | +.....5....5..... |
| 68 | +.....5....5..... |
| 69 | +.....555555..... |
| 70 | +................ |
| 71 | +................ |
| 72 | +................ |
| 73 | +................ |
| 74 | +................`], |
| 75 | + [background, bitmap` |
| 76 | +0000000000000000 |
| 77 | +0..............0 |
| 78 | +0..............0 |
| 79 | +0..............0 |
| 80 | +0..............0 |
| 81 | +0..............0 |
| 82 | +0..............0 |
| 83 | +0..............0 |
| 84 | +0..............0 |
| 85 | +0..............0 |
| 86 | +0..............0 |
| 87 | +0..............0 |
| 88 | +0..............0 |
| 89 | +0..............0 |
| 90 | +0..............0 |
| 91 | +0000000000000000`] |
| 92 | +); |
| 93 | + |
| 94 | + |
| 95 | +let level = map` |
| 96 | +... |
| 97 | +.p. |
| 98 | +...`; |
| 99 | + |
| 100 | +setMap(level); |
| 101 | + |
| 102 | +setSolids([x, o]); |
| 103 | +setBackground(background); |
| 104 | + |
| 105 | +function gameOver(winner) { |
| 106 | + gameover = true |
| 107 | + if (winner == "x") { |
| 108 | + addText(`Game over. X won!`, { |
| 109 | + x: 2, |
| 110 | + y: 6, |
| 111 | + color: color`3` |
| 112 | + }) |
| 113 | + } else if (winner == "o") { |
| 114 | + addText(`Game over. O won!`, { |
| 115 | + x: 2, |
| 116 | + y: 6, |
| 117 | + color: color`5` |
| 118 | + }) |
| 119 | + } else { |
| 120 | + addText(`Game over.`, { |
| 121 | + x: 4, |
| 122 | + y: 6, |
| 123 | + color: color`9` |
| 124 | + }) |
| 125 | + addText(`It was a draw!`, { |
| 126 | + x: 3, |
| 127 | + y: 8, |
| 128 | + color: color`9` |
| 129 | + }) |
| 130 | + } |
| 131 | + addText(`Press J to restart`, { |
| 132 | + x: 1, |
| 133 | + y: 10, |
| 134 | + color: color`D` |
| 135 | + }) |
| 136 | +} |
| 137 | + |
| 138 | +const restartGame = async () => { |
| 139 | + clearText(); |
| 140 | + gameover = false |
| 141 | + // Remove all x's |
| 142 | + getAll(x).forEach((x) => { |
| 143 | + x.remove(); |
| 144 | + }); |
| 145 | + // Remove all o's |
| 146 | + getAll(o).forEach((o) => { |
| 147 | + o.remove(); |
| 148 | + }); |
| 149 | + |
| 150 | + // Clear Board |
| 151 | + board = [ |
| 152 | + ["", "", ""], |
| 153 | + ["", "", ""], |
| 154 | + ["", "", ""] |
| 155 | + ]; |
| 156 | +} |
| 157 | + |
| 158 | +// inputs for player movement control |
| 159 | +onInput("w", () => { |
| 160 | + if (!gameover) { |
| 161 | + getFirst(player).y -= 1; |
| 162 | + } |
| 163 | +}); |
| 164 | + |
| 165 | +onInput("a", () => { |
| 166 | + if (!gameover) { |
| 167 | + getFirst(player).x -= 1; |
| 168 | + } |
| 169 | +}); |
| 170 | + |
| 171 | +onInput("s", () => { |
| 172 | + if (!gameover) { |
| 173 | + getFirst(player).y += 1; |
| 174 | + } |
| 175 | +}); |
| 176 | + |
| 177 | +onInput("d", () => { |
| 178 | + if (!gameover) { |
| 179 | + getFirst(player).x += 1; |
| 180 | + } |
| 181 | +}); |
| 182 | + |
| 183 | +onInput("j", () => { |
| 184 | + if (gameover) { |
| 185 | + restartGame(); |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + let playerPosition = [getFirst(player).x, getFirst(player).y]; |
| 190 | + |
| 191 | + // Check if can place in tile |
| 192 | + if (getTile(playerPosition[0], playerPosition[1]).length > 1) { |
| 193 | + return; |
| 194 | + } |
| 195 | + |
| 196 | + if (!turn) { |
| 197 | + // Place X |
| 198 | + addSprite(playerPosition[0], playerPosition[1], x); |
| 199 | + board[playerPosition[1]][playerPosition[0]] = "x"; |
| 200 | + } else { |
| 201 | + // Place Os |
| 202 | + addSprite(playerPosition[0], playerPosition[1], o); |
| 203 | + board[playerPosition[1]][playerPosition[0]] = "o"; |
| 204 | + } |
| 205 | + |
| 206 | + // Check for winner |
| 207 | + console.log("board", board) |
| 208 | + // Check rows) |
| 209 | + if (board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][2] == "x") { |
| 210 | + gameOver("x"); |
| 211 | + } else if (board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][2] == "o") { |
| 212 | + gameOver("o"); |
| 213 | + } |
| 214 | + if (board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][2] == "x") { |
| 215 | + gameOver("x"); |
| 216 | + } else if (board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][2] == "o") { |
| 217 | + gameOver("o"); |
| 218 | + } |
| 219 | + if (board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][2] == "x") { |
| 220 | + gameOver("x"); |
| 221 | + } else if (board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][2] == "o") { |
| 222 | + gameOver("o"); |
| 223 | + } |
| 224 | + // Check columns |
| 225 | + if (board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[2][0] == "x") { |
| 226 | + gameOver("x"); |
| 227 | + } else if (board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[2][0] == "o") { |
| 228 | + gameOver("o"); |
| 229 | + } |
| 230 | + if (board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[2][1] == "x") { |
| 231 | + gameOver("x"); |
| 232 | + } else if (board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[2][1] == "o") { |
| 233 | + gameOver("o"); |
| 234 | + } |
| 235 | + if (board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[2][2] == "x") { |
| 236 | + gameOver("x"); |
| 237 | + } else if (board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[2][2] == "o") { |
| 238 | + gameOver("o"); |
| 239 | + } |
| 240 | + //Check diagonals |
| 241 | + if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == "x") { |
| 242 | + gameOver("x"); |
| 243 | + } else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == "o") { |
| 244 | + gameOver("o"); |
| 245 | + } |
| 246 | + if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] == "x") { |
| 247 | + gameOver("x"); |
| 248 | + } else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] == "o") { |
| 249 | + gameOver("o"); |
| 250 | + } |
| 251 | + |
| 252 | + //Check for draw |
| 253 | + if (!gameover) { |
| 254 | + counter = 0 |
| 255 | + for (let i = 0; i < 3; i++) { |
| 256 | + for (let j = 0; j < 3; j++) { |
| 257 | + if (board[i][j] == "") { |
| 258 | + counter++ |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | + if (counter == 0) { |
| 263 | + gameOver("draw") |
| 264 | + } |
| 265 | + } |
| 266 | + turn = !turn; |
| 267 | +}); |
0 commit comments