-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
174 lines (151 loc) · 4.76 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var readline = require("readline-sync");
const resultArray = [[0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6], [3, 4, 5], [6, 7, 8], [0, 1, 2]];
var xArray = [];
var yArray = [];
class TicTacToeGame {
makeArray(length) {
var matrixArray = [];
for (var i = 0; i < length; i++) {
matrixArray[i] = i
}
this.printMatrix(matrixArray)
return matrixArray;
}
startGame() {
let tictactoe = [];
let length = 9;
let result =undefined;
tictactoe = this.makeArray(length);
for (var i = 0; i < length; i++) {
var x;
var symbol;
if (i % 2 == 0) {
console.log("Enter position of player 1");
x = readline.question("enter position ");
symbol = "x";
if (!this.checkInput(tictactoe, x, length)) {
i--;
continue;
}
else {
xArray.push(x);
}
tictactoe = this.initSymbol(tictactoe, x, symbol)
}
else {
console.log("Enter position of player 2 ");
x = readline.question("enter position ");
symbol = "o";
if (!this.checkInput(tictactoe, x, length)) {
i--;
continue;
}
else {
yArray.push(x);
}
tictactoe = this.initSymbol(tictactoe, x, symbol)
}
this.printMatrix(tictactoe);
result = this.compareCondition( symbol);
if (result == "player 1" || result == "player 2") {
//this.makeArray(9);
var restart = readline.question("press y if you want to restart: ");
if(restart == "y"){
xArray = [];
yArray = []
this.startGame();
}
break;
}
}
if( i >= 8 ){
var restart = readline.question("press y if you want to restart: ");
if(restart == "y"){
xArray = [];
yArray = []
this.startGame();
}
}
if ( i >= 8 && result ==undefined)
{
console.log('MATCH IS DRAW');
return;
}
}
printMatrix(tictactoe) {
for (let index = 0; index < tictactoe.length; index += 3) {
var temp = "";
for (let j = index; j < index + 3; j++) {
temp = temp + tictactoe[j] + " ";
}
console.log(temp)
console.log();
}
}
initSymbol(arr, x, symbol) {
arr[x] = symbol;
return arr;
}
checkInput(arr, x, length) {
if ((x >= length - 1 || x <= -1) || x === '') {
console.log("position out of matrix range")
console.log("Please enter again")
return false;
}
if (arr[x] == "x" || arr[x] == "o") {
console.log("This coordinates has already filled by the values")
console.log("Please enter again")
return false;
}
return true;
}
compareCondition( symbol) {
var res;
if (symbol == "x") {
res = this.winnerCheck(xArray, symbol);
}
else if (symbol == "o") {
res = this.winnerCheck(yArray, symbol);
}
return res
}
checkArray(arr, ele) {
for (let index = 0; index < arr.length; index++) {
if (arr[index] == ele) {
return true
}
}
return false
}
winnerCheck(array, symbol) {
var count = 0;
var flag = 1;
if (array.length >= 3) {
for (let index = 0; index < resultArray.length; index++) {
count = 0;
flag = 1;
for (let index1 = 0; index1 < array.length; index1++) {
var element = array[index1]
if (this.checkArray(resultArray[index], element))
count++;
if (count == 3) {
flag = 0;
break;
}
}
if (flag == 0) {
break;
}
}
}
if (symbol == "x" && count == 3) {
console.log("PLAYER ONE IS THE WINNER");
return "player 1";
}
else if (count == 3 && symbol == "o") {
console.log("PLAYER TWO IS THE WINNER");
return "player 2";
}
}
}
module.exports = { TicTacToeGame };