I made this two player TicTacToe game as a challenge i was given in class. The game starts where the first player chooses where their X will be placed. Each area in the array has been given a number starting from 1 and ending at 9. After X chooses their area, the game will let the user know who's turn it is, and they will keep playing until one of them wins.
As of right now, the game doesn't let you know that nobody won, but that's another update i have to make.
Please do review the code and let me know how i can make it more efficient and i am also open for suggestions, so please be honest as much as possible as this is the first time i write in Javascript using Nodejs, and i would love to get better.
//user input and format
let input = process.stdin;
input.setEncoding("utf-8");
console.log("You will start with X");
console.log("Please choose a box number from 1 to 9 to place your X");
//initializing an array of 9 fields where each one contains * as a start
let gameboard = []; // = ["*", "*", "*", "*", "*", "*", "*", "*", "*"];
let symbol = "*";
for (let i = 0; i < 9; i++) {
gameboard[i] = symbol;
}
//determining who's turn it is and the players' symbols
let player1isplaying = true;
let player1 = "X";
let player2 = "O";
//Game loop
input.on("data", function(data) {
console.clear();
//bad input
if (parseInt(data) > 9 || parseInt(data) < 1 || isNaN(data)) {
console.log("Please enter a number between 1 and 9");
} else if (gameboard[parseInt(data) - 1] != symbol) {
console.log("The spot you're trying to fill is already taken.");
}
//which player's turn
else if (player1isplaying) {
gameboard[parseInt(data) - 1] = player1;
player1isplaying = false;
} else if (!player1isplaying) {
gameboard[parseInt(data) - 1] = player2;
player1isplaying = true;
}
showboard(gameboard);
checkforXwin(gameboard);
checkforOwin(gameboard);
if (player1isplaying) {
console.log(player1 + "'s turn");
} else {
console.log(player2 + "'s turn");
}
});
//winner validation
function checkforXwin(array) {
if (
(array[0] == player1 && array[1] == player1 && array[2] == player1) ||
(array[3] == player1 && array[4] == player1 && array[5] == player1) ||
(array[6] == player1 && array[7] == player1 && array[8] == player1) ||
(array[0] == player1 && array[4] == player1 && array[8] == player1) ||
(array[2] == player1 && array[4] == player1 && array[6] == player1) ||
(array[0] == player1 && array[3] == player1 && array[6] == player1) ||
(array[1] == player1 && array[4] == player1 && array[7] == player1) ||
(array[2] == player1 && array[5] == player1 && array[8] == player1)
) {
console.log(player1 + " wins");
process.exit();
}
}
//winner validation
function checkforOwin(array) {
if (
(array[0] == player2 && array[1] == player2 && array[2] == player2) ||
(array[3] == player2 && array[4] == player2 && array[5] == player2) ||
(array[6] == player2 && array[7] == player2 && array[8] == player2) ||
(array[0] == player2 && array[4] == player2 && array[8] == player2) ||
(array[2] == player2 && array[4] == player2 && array[6] == player2) ||
(array[0] == player2 && array[3] == player2 && array[6] == player2) ||
(array[1] == player2 && array[4] == player2 && array[7] == player2) ||
(array[2] == player2 && array[5] == player2 && array[8] == player2)
) {
console.log(player2 + " wins");
process.exit();
}
}
//showing the board in 3 rows and 3 columns
function showboard(array) {
console.log("---------");
console.log(array.slice(0, 3).join(" "));
console.log("");
console.log(array.slice(3, 6).join(" "));
console.log("");
console.log(array.slice(6, 9).join(" "));
console.log("---------");
}