Skip to content

Instantly share code, notes, and snippets.

@anu1097
Last active July 24, 2020 11:47
Show Gist options
  • Save anu1097/07db7779c5ad309714146323a95faee8 to your computer and use it in GitHub Desktop.
Save anu1097/07db7779c5ad309714146323a95faee8 to your computer and use it in GitHub Desktop.
Tic Tac Toe Node.Js Terminal Game
#!/usr/bin/env node
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
// usage represents the help guide
function usage() {
const usageText = `
Tic Tac Toe application
usage:
tic tac toe <command>
commands can be:
new: used to create a new tic tac toe
help: used to print the usage guide
`
console.log(usageText)
}
const args = process.argv
const new_board = [['-','-','-'],['-','-','-'],['-','-','-']];
let current_board;
let turn;
switch(args[2]) {
case 'help':
usage();
break;
default:
getNewBoard();
}
//Initialises with new board
function getNewBoard(){
current_board = new_board;
turn = 0;
console.log("New boad created");
printTable();
console.log("");
getUserInput();
}
//Gets user Input
function getUserInput(){
if(turn%2 === 0){
userInput('O');
// userOInput();
}
else{
userInput('X');
// userXInput();
}
}
//Checks if board is Full
function checkIfBoardIsFull(){
let isFull = true;
current_board.forEach(row => {
row.forEach(item => {
if(item === '-'){
isFull = false;
}
})
});
return isFull;
}
//Prints Table
function printTable(){
current_board.forEach(row => {
console.log(row);
});
}
//Gets 2D coordinates
function getCoordinates(location) {
location -= 1;
let x = Math.floor(location/3);
let y = location%3;
return {x, y};
}
//Checks if the location is empty
function checkIfEmpty(x,y){
return current_board[x][y] === '-';
}
//Enters the input in Table
function addInputToTable(location,input){
if(location>9 || location<1){
console.log('Invalid Input. Kindly enter between 1 to 9');
}
else{
let {x, y} = getCoordinates(location);
if(checkIfEmpty(x, y)){
current_board[x][y] = input;
turn+=1;
printTable();
console.log("");
if(checkIfUserWon(x,y,input)){
console.log(`User ${input} Won`);
return 0;
}
if(checkIfBoardIsFull()){
console.log("Board is Full. Game Over");
return 0;
}
}
else{
console.log('Invalid Input. Kindly enter another empty location');
}
}
return 1;
}
function checkStraightDiagonal(input){
return (current_board[0][0] === input) && (current_board[1][1] === input) && (current_board[2][2] === input);
}
function checkCrossDiagonal(input){
return (current_board[0][2] === input) && (current_board[1][1] === input) && (current_board[2][0] === input);
}
function checkRowsAndColums(x,y, input){
return ((current_board[x][0] === input) && (current_board[x][1] === input) && (current_board[x][2] === input)) ||
((current_board[0][y] === input) && (current_board[1][y] === input) && (current_board[2][y] === input));
}
function checkIfUserWon(x,y, input){
if(checkRowsAndColums(x,y, input)){
return true;
}
else{
if(x==1 && y==1){
if(checkStraightDiagonal(input) || checkCrossDiagonal(input)){
return true;
}
}
else if(x===y){
if(checkStraightDiagonal(input)){
return true;
}
}
else if(x+y === 2){
if(checkCrossDiagonal(input)){
return true;
}
}
}
}
function userInput(input) {
readline.question(`User {input} print its value - `, location => {
if(addInputToTable(location, input)){
getUserInput();
}
else{
readline.close();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment