diff options
| author | Leo Goetz <dev@leogtz.de> | 2026-03-21 16:16:36 +0100 |
|---|---|---|
| committer | Leo Goetz <dev@leogtz.de> | 2026-03-21 16:16:36 +0100 |
| commit | 82969cac5d6f9626bef681ef7d5cea60ed686eb0 (patch) | |
| tree | e5a110d2d6de0e47f65802d073f570ca4d5aacef /src/game.js | |
Diffstat (limited to 'src/game.js')
| -rw-r--r-- | src/game.js | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/game.js b/src/game.js new file mode 100644 index 0000000..0820f8c --- /dev/null +++ b/src/game.js @@ -0,0 +1,118 @@ +import { getInput } from "./utils.js"; + +let game = { + player1: { + symbol: "X", + name: undefined, + }, + player2: { + symbol: "O", + name: undefined, + }, + turn: "player1", + state: "setup", + board: [" ", " ", " ", " ", " ", " ", " ", " ", " "], +}; + +const winningPositions = [ + [0, 1, 2], + [0, 3, 6], + [0, 4, 8], + [1, 4, 7], + [2, 5, 8], + [2, 4, 6], + [3, 4, 5], + [6, 7, 8], +]; + +function checkForWin() { + let winner = ""; + for (let position of winningPositions) { + if ( + // ignore field empty + game.board[position[0]] != " " && + game.board[position[1]] != " " && + game.board[position[2]] != " " && + // check if three in a row + game.board[position[0]] == game.board[position[1]] && + game.board[position[1]] == game.board[position[2]] + ) { + winner = + game.player1.symbol == game.board[position[0]] + ? game.player1.name + : game.player2.name; + } + } + return winner; +} + +async function gameLoop() { + while (game.state == "running") { + outputBoard(); + + let winner = checkForWin(); + if (winner) { + console.log(`${winner} has won!`); + game.state = "finish"; + return; + } + + await questionLoop(); + } + + async function questionLoop() { + let playerMove = await getInput( + `Where would you want to place (${game[game.turn].name})?: `, + ); + + while (game.board[playerMove - 1] != " ") { + console.log("\nThere is already a Symbol. Try again."); + playerMove = await getInput( + `Where would you want to place (${game[game.turn].name})?: `, + ); + } + + if (game.turn == "player2") { + move(game.player2, playerMove); + game.turn = "player1"; + } else { + move(game.player1, playerMove); + game.turn = "player2"; + } + } +} + +async function setupGame() { + console.log("Let's Start a Game of Tic Tac Toe! \n"); + game.player1.name = await getInput("Name of Player 1: "); + game.player2.name = await getInput("Name of Player 2: "); + game.state = "running"; +} + +function move(player, position) { + game.board[position - 1] = player.symbol; +} + +export default async function start() { + if (game.state == "setup") { + await setupGame(); + } + + if (game.state == "running") { + await gameLoop(); + } + + if (game.state == "finish") { + process.exit(); + } +} + +function outputBoard() { + console.log("\n"); + console.log(`${game.board[0]} | ${game.board[1]} | ${game.board[2]}`); + console.log("---------"); + console.log(`${game.board[3]} | ${game.board[4]} | ${game.board[5]}`); + console.log("---------"); + console.log(`${game.board[6]} | ${game.board[7]} | ${game.board[8]}`); + console.log("\n"); +} |
