-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.cpp
97 lines (93 loc) · 2.95 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
#include "Game.h"
Game::Game() {
for(int i=0; i<COLUMN_COUNT; ++i) {
for(int j=0; j<ROW_COUNT; ++j) {
columns[i][j] = 0;
}
}
for(int i=0; i<ROW_COUNT; ++i) {
for(int j=0; j<COLUMN_COUNT; ++j) {
rows[i][j] = 0;
}
}
for(int i=0; i<DIAGONAL_COUNT; ++i) {
for(int j=0; j<DIAGONAL_LENGTH; ++j) {
diagLDR[i][j] = 0;
diagRDL[i][j] = 0;
}
}
}
bool Game::takeTurn(Move turn) {
if(turn.column < 0 || turn.column >= COLUMN_COUNT) {
output.badMove();
return false;
}
int pos = -1;
if(columns[turn.column][ROW_COUNT-1]==0) {
pos=ROW_COUNT-1;
}
else while(columns[turn.column][pos+1]==0) {
pos++;
}
if(pos==-1) {
output.badMove();
return false;
}
columns[turn.column][pos] = turn.player;
rows[pos][turn.column] = turn.player;
//Funcky formulas to set up diagonals from left to right
if((turn.column+(ROW_COUNT-pos-1)) < 6) {
diagLDR[turn.column+(ROW_COUNT-pos-1)][turn.column] = turn.player;
}
else {
diagLDR[turn.column+(ROW_COUNT-pos-1)][pos] = turn.player;
}
//Funcky formulas to set up diagonals from right to left
if((COLUMN_COUNT-turn.column-1)+(ROW_COUNT-pos-1) < 6) {
diagRDL[(COLUMN_COUNT-turn.column-1)+(ROW_COUNT-pos-1)][COLUMN_COUNT-turn.column-1] = turn.player;
}
else {
diagRDL[(COLUMN_COUNT-turn.column-1)+(ROW_COUNT-pos-1)][pos] = turn.player;
}
return true;
}
int Game::isWin() {
int posCounter=0;
for(int i=0; i<ROW_COUNT; ++i) {
for(int j=0; j<COLUMN_COUNT; ++j) {
if(rows[i][j] != 0) {
posCounter++;
//Check row? for win condition
if(j<=(COLUMN_COUNT-4) && (rows[i][j]==rows[i][j+1] && rows[i][j]==rows[i][j+2] && rows[i][j]==rows[i][j+3])) {
return rows[i][j];
}
//Check column? for win condition
else if(i<=(ROW_COUNT-4) && (rows[i][j]==rows[i+1][j] && rows[i][j]==rows[i+2][j] && rows[i][j]==rows[i+3][j])) {
return rows[i][j];
}
}
//Check diagonal for win condition
if(j<=2) {
if(diagLDR[i+3][j]!=0 && (diagLDR[i+3][j]==diagLDR[i+3][j+1] && diagLDR[i+3][j]==diagLDR[i+3][j+2] && diagLDR[i+3][j]==diagLDR[i+3][j+3])) {
return diagLDR[i+3][j];
}
if(diagRDL[i+3][j]!=0 && (diagRDL[i+3][j]==diagRDL[i+3][j+1] && diagRDL[i+3][j]==diagRDL[i+3][j+2] && diagRDL[i+3][j]==diagRDL[i+3][j+3])) {
return diagRDL[i+3][j];
}
}
}
}
if(posCounter==42) {
return 3;
}
return 0;
}
GameState Game::getGameState() {
GameState gs;
for(int i=0; i<ROW_COUNT; ++i) {
for(int j=0; j<COLUMN_COUNT; ++j) {
gs.board[i][j] = rows[i][j];
}
}
return gs;
}