-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (98 loc) · 2.62 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
function game(board)
{
//Printing the board
console.log("\nTic Tac Toe:\n")
console.log(board[0][0]+" | "+board[0][1]+" | "+board[0][2]+"\n"+
"----------\n"+
board[1][0]+" | "+board[1][1]+" | "+board[1][2]+"\n"+
"----------\n"+
board[2][0]+" | "+board[2][1]+" | "+board[2][2]+"\n"
)
var vert_result = check_vert(board)
var hoz_result = check_hoz(board)
var diag_result = check_diag(board)
if(vert_result == 1 || hoz_result == 1 || diag_result == 1){
console.log("x Wins")
}
else if(vert_result == -1 || hoz_result == -1 || diag_result == -1){
console.log("o Wins")
}
else {
console.log("No Winner")
}
}
function check_vert(board)
{
for(var i = 0; i < board.length; i++) {
var x_score = 0;
var o_score = 0;
for(var j = 0; j < board.length; j++) {
if(board[i][j]=="x"){
x_score += 1;
} else if(board[i][j]=="o"){
o_score +=1;
}
}
if(x_score==3){
return 1
} else if(o_score==3){
return -1
}
}
return 0;
}
function check_hoz(board)
{
for(var i = 0; i < board.length; i++) {
var x_score = 0;
var o_score = 0;
for(var j = 0; j < board.length; j++) {
if(board[j][i]=="x"){
x_score += 1;
} else if(board[j][i]=="o"){
o_score +=1;
}
}
if(x_score==3){
return 1
} else if(o_score==3){
return -1
}
}
return 0;
}
function check_diag(board)
{
if(board[0][0] == ('x') && board[1][1] == ('x') && board [2][2] == ('x'))
{
return 1
}
if(board[0][0] == ('o') && board[1][1] == ('o') && board [2][2] == ('o'))
{
return -1
}
if (board[0][2] == ('x') && board[1][1] == ('x') && board [2][0] == ('x'))
{
return 1
}
if(board[0][2] == ('o') && board[1][1] == ('o') && board [2][0] == ('o'))
{
return -1
}
return 0;
}
//DIFFERENT BoARD RESULTS
const x_win_vert_board = [ ["x","x","x"], ["o","x","x"], ["x","o","o"] ]
const o_win_vert_board = [ ["x","","x"], ["o","o","o"], ["","","x"] ]
const x_win_hoz_board = [ ["x","","x"], ["x","o","o"], ["x","","x"] ]
const o_win_hoz_board = [ ["x","o","x"], ["o","o","o"], ["","o","x"] ]
const o_win_diag_board = [ ["o","o","x"], ["x","o","o"], ["","x","o"] ]
const x_win_diag_board = [ ["x","o","x"], ["o","x","o"], ["","o","x"] ]
const no_win = [ ["x","o","x"], ["o","x","o"], ["o","x","o"] ]
game(x_win_vert_board)
game(o_win_vert_board)
game(x_win_hoz_board)
game(o_win_hoz_board)
game(x_win_diag_board)
game(o_win_diag_board)
game(no_win)