-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
128 lines (109 loc) · 2.3 KB
/
game.cpp
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
// Lucas Wagler
// Dr. Khadka
// 2020-03-11
// Tic-Tac-Toe with Victory Lines
#include "game.h"
game::game()
{
}
void game::click(int button)
{
if (!state[button]) {
state[button] = turn;
turn = (turn == 1) ? 2 : 1;
}
}
int *game::getState()
{
return this->state;
}
int game::getTurn()
{
return turn;
}
int game::winner()
{
// -1 = tie
// 0 = ongoing game
// 1 = player 1 wins
// 2 = player 2 wins
// check horizontal
for (int i = 0; i < 9; i += 3)
{
if ( state[i] == state[i+1] and state[i] == state[i+2] and state[i] != 0)
{
return state[i];
}
}
// check vertical
for (int i = 0; i < 3; i++)
{
if ( state[i] == state[i+3] and state[i] == state[i+6] and state[i] != 0)
{
return state[i];
}
}
// check diagonal NW-SE
if ( state[0] == state[4] and state[0] == state[8] and state[0] != 0 )
{
return state[0];
}
// check diagonal NE-SW
if ( state[2] == state[4] and state[2] == state[6] and state[2] != 0)
{
return state[2];
}
// check tie
for (int i = 0; i < 9; i++)
{
if (!state[i])
{
break;
}
if (i == 8)
{
return -1;
}
}
return 0;
}
// Dirty implementation for line drawing; could be combined with above winner()
int game::winResult()
{
// -1 = error
// 0 = horizontal top
// 3 = horizontal mid
// 6 = horizontal bottom
// 10 = vertical left
// 11 = vertical mid
// 12 = vertical right
// 20 = diagonal NW-SE
// 21 = diagonal NE-SW
// check horizontal 0
for (int i = 0; i < 9; i += 3)
{
if ( state[i] == state[i+1] and state[i] == state[i+2] and state[i] != 0)
{
return i;
}
}
// check vertical
for (int i = 0; i < 3; i++)
{
if ( state[i] == state[i+3] and state[i] == state[i+6] and state[i] != 0)
{
return i+10;
}
}
// check diagonal NW-SE
if ( state[0] == state[4] and state[0] == state[8] and state[0] != 0 )
{
return 20;
}
// check diagonal NE-SW
if ( state[2] == state[4] and state[2] == state[6] and state[2] != 0)
{
return 21;
}
return -1;
}