-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.cpp
More file actions
127 lines (122 loc) · 3.87 KB
/
TicTacToe.cpp
File metadata and controls
127 lines (122 loc) · 3.87 KB
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
/**
*cpp file of class TicTacToe
*Authors Alexey Titov and Shir Bentabou
*Version 2.0
**/
//libraries
#include "TicTacToe.h"
#include <string>
//Constructor
TicTacToe::TicTacToe(int s):gameBoard(s){
this->size = s;
}
//This functions checks if the game is over - if a player won or if there was a tie (board is full)
int TicTacToe::gameover(char ch){
uint i,j;
//int i,j;
int victory = 1;
//rows
for(i = 0; i< this->size; i++){
victory = 1;
//left-right
for(j = 0; j<this->size; j++){
if(this->gameBoard[{i,j}] != ch){
victory = 0;
break;
}
}
if(victory)
return 1;
victory = 1;
//up-down
for(j = 0; j<this->size; j++){
if(this->gameBoard[{j,i}] != ch){
victory = 0;
break;
}
}
if(victory)
return 1;
}
victory = 1;
//diagonal upleft - downright
for(i = 0; i< this->size; i++){
if(this->gameBoard[{i,i}] != ch){
victory = 0;
break;
}
}
if(victory)
return 1;
victory = 1;
//diagonal upright - downleft
for(i = 0; i< this->size; i++){
if(this->gameBoard[{this->size-i-1,i}] != ch){
victory = 0;
break;
}
}
return victory;
}
//This function executes a whole game of tic-tac-toe, and uses player and winner functions of this class.
void TicTacToe::play(Player& xPlayer, Player& oPlayer){
int board_size = this->size*this->size;
xPlayer.setChar('X');
oPlayer.setChar('O');
Coordinate c(0,0);
this->gameBoard ='.';
for (int i=0; i<board_size;){
i++;
try{
c.setCoors(xPlayer.play(this->gameBoard));
if(this->gameBoard[c]=='.')
this->gameBoard[c] = xPlayer.getChar();
else{
this->champ = &oPlayer;
return;
}
}catch(const string& msg){
//after fix 86-91
//if(this->gameBoard[{0,0}]=='.')
// this->gameBoard[{0,0}] = xPlayer.getChar();
this->champ = &oPlayer;
return;
}
if (gameover('X')){
this->champ=&xPlayer;
return;
}
i++;
if (i<board_size){
try{
c.setCoors(oPlayer.play(this->gameBoard));
if(this->gameBoard[c]=='.')
this->gameBoard[c] = oPlayer.getChar();
else{
this->champ = &xPlayer;
return;
}
}catch(const string& msg){
//after fix 86-91
//if(this->gameBoard[{0,0}]=='.')
// this->gameBoard[{0,0}] = oPlayer.getChar();
this->champ = &xPlayer;
return;
}
if (gameover('O')){
this->champ=&oPlayer;
return;
}
}
}
//if it's a tie, oPlayer wins
this->champ=&oPlayer;
}
//This function returns the board when the game has ended
Board TicTacToe::board() const{
return this->gameBoard;
}
//This function returns the player that won the game
Player& TicTacToe::winner() const{
return *this->champ;
}