-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (130 loc) · 5.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
let readlineSync = require('readline-sync');
let userScore = 0;
let computerScore = 0;
let boardValues = [[" "," "," "], [" "," "," "], [" "," "," "]]
const referenceValues = [[0,1,2], [3,4,5], [6,7,8]];
const positionsObject = {
0: [0,0],
1: [0,1],
2: [0,2],
3: [1,0],
4: [1,1],
5: [1,2],
6: [2,0],
7: [2,1],
8: [2,2]
}
const userName = readlineSync.question("Please enter your name: \n");
console.log(`Hi there ${userName}, welcome to command line based Tic Tac Toe! \n`);
function startOfGame () {
boardValues = [[" "," "," "], [" "," "," "], [" "," "," "]];
const userFlip = readlineSync.question("Input H for heads, or T for tails to see who's going first: \n"
, {limit: ["H","T"], limitMessage: "Incorrect input, please enter either 'H' for heads or 'T' for tails \n"});
process.stdout.write('\033c'); // this clears the command line text above it
const computerFlip = Math.floor(Math.random()*2);
if (userFlip === "H" && computerFlip == 1) {
game("User");
}
else if (userFlip === "T" && computerFlip == 0) {
game("User");
}
else {
game("Computer");
};
}
function tttBoard (boardValue = boardValues) { // this function draws the board with its updated values
const row1 = ` ${boardValue[0][0]} | ${boardValue[0][1]} | ${boardValue[0][2]} \n`;
const row2 = `___|___|___\n`;
const row3 = ` ${boardValue[1][0]} | ${boardValue[1][1]} | ${boardValue[1][2]} \n`;
const row4 = `___|___|___\n`;
const row5 = ` ${boardValue[2][0]} | ${boardValue[2][1]} | ${boardValue[2][2]} \n`;
const row6 = ` | | `;
console.log(row1+row2+row3+row4+row5+row6 + '\n');
};
function game (firstToAct) {
let freePositions = [0,1,2,3,4,5,6,7,8];
if (firstToAct == "User") {
tttBoard(referenceValues);
console.log("You're first to act! You will have 'X' and the computer will have 'O'. \n");
gameLoop(freePositions);
}
else {
tttBoard(referenceValues);
console.log("You lost the flip, you are second to go and you will have 'O' \n")
let compPos = freePositions[Math.floor(Math.random()*freePositions.length)];
boardValues[positionsObject[compPos][0]][positionsObject[compPos][1]] = 'O';
let indexToRemove2 = freePositions.indexOf(compPos);
freePositions.splice(indexToRemove2,1);
tttBoard();
gameLoop(freePositions);
}
if (freePositions.length == 0 && checkWin() == undefined) {
console.log("IT'S A TIE!!!!!")
restart();
}
}
function gameLoop (freePositions) {
while (freePositions.length > 0) {
let location = parseInt(readlineSync.question("Choose a number from 0 to 8 to specify the placement of your tic: \n",
{limit: freePositions, limitMessage: "Incorrect input, make sure to input a single integer in the range {0,8}, "
+"not including already occupied positions."}));
console.log('\n');
boardValues[positionsObject[location][0]][positionsObject[location][1]] = 'X';
if (checkWin() !== undefined) {
break;
}
let indexToRemove1 = freePositions.indexOf(location);
freePositions.splice(indexToRemove1,1);
if (freePositions.length == 0) {
break;
}
let compPos = freePositions[Math.floor(Math.random()*freePositions.length)];
boardValues[positionsObject[compPos][0]][positionsObject[compPos][1]] = 'O';
let indexToRemove2 = freePositions.indexOf(compPos);
freePositions.splice(indexToRemove2,1);
if (checkWin() !== undefined) {
break;
}
tttBoard();
}
}
function checkWin() {
let winningCombos = [
[boardValues[0][0],boardValues[0][1],boardValues[0][2]],
[boardValues[1][0],boardValues[1][1],boardValues[1][2]],
[boardValues[2][0],boardValues[2][1],boardValues[2][2]],
[boardValues[0][0],boardValues[1][0],boardValues[2][0]],
[boardValues[0][1],boardValues[1][1],boardValues[2][1]],
[boardValues[0][2],boardValues[1][2],boardValues[2][2]],
[boardValues[0][0],boardValues[1][1],boardValues[2][2]],
[boardValues[0][2],boardValues[1][1],boardValues[2][0]]
];
for (let i = 0; i < winningCombos.length; i++) {
if (winningCombos[i].join('') === 'XXX') {
userScore++;
console.log(' YOU WON!!!!!');
tttBoard();
restart();
return "User";
}
else if (winningCombos[i].join('') === 'OOO') {
computerScore++;
console.log(' YOU LOST!!!!!');
tttBoard();
restart();
return "Computer";
};
};
}
function restart () {
console.log(` User score: ${userScore} | Computer score: ${computerScore}`);
const toRestartOrNot = readlineSync.question("Would you like to play again? Enter Y for Yes or N for No: \n",
{limit: ['Y','N'],limitMessage: "Incorrect input, please enter either 'Y' for yes or 'N' for no \n"});
if (toRestartOrNot == 'Y') {
startOfGame();
}
else {
return console.log(' GAME OVER \n');
}
}
startOfGame();