Last active
March 5, 2022 20:00
-
-
Save aakhilv/af7edb6175ac4e545de616561b85c827 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let chalk = require("chalk"); | |
let prompt = require("prompt-sync")(); | |
let board = [ | |
[" ", " ", " "], | |
[" ", " ", " "], | |
[" ", " ", " "] | |
]; | |
let player = true, marker, icon, win = false; | |
function promptplayer() { | |
let playername; | |
if (player) { | |
playername = "Player 1"; | |
icon = "⨯"; | |
} else if (!player) { | |
playername = "Player 2"; | |
icon = "⚬"; | |
}; | |
marker = prompt(`${chalk.red(playername)} (${chalk.magenta(icon)}) please enter your marker's placement: `); | |
console.log(); | |
}; | |
function changeboard() { | |
if (!(/([0-2],[a-c])/g.test(marker))) { | |
if (player) { | |
player = false; | |
} else if (!player) { | |
player = true; | |
}; | |
return console.log("Invalid format, try again.\n"); | |
}; | |
let newm = marker.split(","); | |
if (newm[1] == "a") { | |
newm[1] = 0; | |
} else if (newm[1] == "b") { | |
newm[1] = 1; | |
} else if (newm[1] == "c") { | |
newm[1] = 2; | |
}; | |
if (player) { | |
icon = "⨯"; | |
} else if (!player) { | |
icon = "⚬"; | |
}; | |
if (board[newm[0]][newm[1]] == " ") { | |
board[newm[0]][newm[1]] = icon; | |
} else { | |
nextplayer(); | |
console.log("That spot's taken! Try again.\n"); | |
}; | |
}; | |
function nextplayer() { | |
player = !player; | |
}; | |
function printboard() { | |
console.log(` a b c\n0 ${chalk.magenta(board[0][0])} | ${chalk.magenta(board[0][1])} | ${chalk.magenta(board[0][2])}\n -----------\n1 ${chalk.magenta(board[1][0])} | ${chalk.magenta(board[1][1])} | ${chalk.magenta(board[1][2])}\n -----------\n2 ${chalk.magenta(board[2][0])} | ${chalk.magenta(board[2][1])} | ${chalk.magenta(board[2][2])}\n`); | |
}; | |
function checkwin() { | |
let winner; | |
if (board[0][0] == "⨯" && board[0][1] == "⨯" && board[0][2] == "⨯") { | |
winner = "⨯"; | |
win = true; | |
} else if (board[0][0] == "⚬" && board[0][1] == "⚬" && board[0][2] == "⚬") { | |
winner = "⚬"; | |
win = true; | |
} else if (board[1][0] == "⨯" && board[1][1] == "⨯" && board[1][2] == "⨯") { | |
winner = "⨯"; | |
win = true; | |
} else if (board[1][0] == "⚬" && board[1][1] == "⚬" && board[1][2] == "⚬") { | |
winner = "⚬"; | |
win = true; | |
} else if (board[2][0] == "⨯" && board[2][1] == "⨯" && board[2][2] == "⨯") { | |
winner = "⨯"; | |
win = true; | |
} else if (board[2][0] == "⚬" && board[2][1] == "⚬" && board[2][2] == "⚬") { | |
winner = "⚬"; | |
win = true; | |
} else if (board[0][0] == "⨯" && board[1][0] == "⨯" && board[2][0] == "⨯") { | |
winner = "⨯"; | |
win = true; | |
} else if (board[0][0] == "⚬" && board[1][0] == "⚬" && board[2][0] == "⚬") { | |
winner = "⚬"; | |
win = true; | |
} else if (board[0][1] == "⨯" && board[1][1] == "⨯" && board[2][1] == "⨯") { | |
winner = "⨯"; | |
win = true; | |
} else if (board[0][1] == "⚬" && board[1][1] == "⚬" && board[2][1] == "⚬") { | |
winner = "⚬"; | |
win = true; | |
} else if (board[0][2] == "⨯" && board[1][2] == "⨯" && board[2][2] == "⨯") { | |
winner = "⨯"; | |
win = true; | |
} else if (board[0][2] == "⚬" && board[1][2] == "⚬" && board[2][2] == "⚬") { | |
winner = "⚬"; | |
win = true; | |
} else if (board[0][0] == "⨯" && board[1][1] == "⨯" && board[2][2] == "⨯") { | |
winner = "⨯"; | |
win = true; | |
} else if (board[0][0] == "⚬" && board[1][1] == "⚬" && board[2][2] == "⚬") { | |
winner = "⚬"; | |
win = true; | |
} else if (board[2][0] == "⨯" && board[1][1] == "⨯" && board[0][2] == "⨯") { | |
winner = "⨯"; | |
win = true; | |
} else if (board[2][0] == "⚬" && board[1][1] == "⚬" && board[0][2] == "⚬") { | |
winner = "⚬"; | |
win = true; | |
} else if (!board.some(row => row.includes(" "))) { | |
win = true; | |
return console.log(`It's a draw! ${chalk.blue("∞")}`); | |
}; | |
if (win) console.log(`${chalk.magenta(winner)} won the game!`); | |
}; | |
console.log(`Welcome to Tic-Tac-Toe!\n\n${chalk.red("Player 1")} will go first as ${chalk.magenta("⨯")}, and ${chalk.red("Player 2")} will go second as ${chalk.magenta("⚬")}.\n\nHere is the starting board:\n`); | |
printboard(); | |
console.log(`Please format your marker placements as ${chalk.blue("number")},${chalk.blue("letter")} coordinates including the comma between them.\n\nOn that note, let the games begin!\n`); | |
while (!win) { | |
promptplayer(); | |
changeboard(); | |
nextplayer(); | |
printboard(); | |
checkwin(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment