-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.cpp
More file actions
137 lines (123 loc) · 4.09 KB
/
TicTacToe.cpp
File metadata and controls
137 lines (123 loc) · 4.09 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
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <iostream>
#include "TicTacToe.h"
using namespace std;
Board TicTacToe::board() const
{
return game;
}
Player& TicTacToe::winner() const
{
return *Winner;
}
void TicTacToe::play(Player& xPlayer, Player& oPlayer)
{
size_t count = 0;
size_t TableSize = sizeBoard * sizeBoard;
xPlayer.setChar('X');
oPlayer.setChar('O');
if(xPlayer.name()=="Inna , Arbel and Anna"){
Champion* c =dynamic_cast<Champion*>(&xPlayer);
c->getRival(oPlayer.name(),1);
Player* temp=dynamic_cast<Player*>(c);
xPlayer=*temp;
}
else if(oPlayer.name()=="Inna , Arbel and Anna"){
Champion* c =dynamic_cast<Champion*>(&oPlayer);
c->getRival(xPlayer.name(),0);
Player* temp=dynamic_cast<Player*>(c);
oPlayer=*temp;
}
while (count < TableSize)
{
if (turn('X', xPlayer, oPlayer)) // X won.
return;
count++;
if (turn('O', xPlayer, oPlayer) && count < TableSize) // O won.
return;
count++;
}
//draw case.
Winner = &oPlayer;
}
bool TicTacToe::turn(char player, Player& xPlayer, Player& oPlayer) {
try
{
Coordinate c;
player == 'X' ? c = xPlayer.play(board()) : c = oPlayer.play(board());
if (game[c] != '.')
throw std::string("Illegal Player");
game[c] = player;
if (GameWinned(board(), c, player))
{
player == 'X' ? Winner = &xPlayer : Winner = &oPlayer;
return true;
}
return false;
}
catch (...) // All the exception are caught.
{
player == 'X' ? Winner = &oPlayer : Winner = &xPlayer;
return true;
}
}
bool TicTacToe::GameWinned(Board board, Coordinate c, char player) {
Count up = {1, true};
Count down = {0, true};
Count right = {1, true};
Count left = {0, true};
Count diagonalLeftDown = {1, true};
Count diagonalLeftUp = {1, true};
Count diagonalRightDown = {0, true};
Count diagonalRightUp = {0, true};
for (int i = 1; i < board.size(); i++) {
if (isPlayer({c.getRow(), c.getCol() + i}, player) && up.flag)
up.sum++;
else
up.flag = false;
if (isPlayer({c.getRow(), c.getCol() - i}, player) && down.flag)
down.sum++;
else
down.flag = false;
if (isPlayer({c.getRow() + i, c.getCol()}, player) && right.flag)
right.sum++;
else
right.flag = false;
if (isPlayer({c.getRow() - i, c.getCol()}, player) && left.flag)
left.sum++;
else
left.flag = false;
if (isPlayer({c.getRow() - i, c.getCol() - i}, player) && diagonalLeftDown.flag)
diagonalLeftDown.sum++;
else
diagonalLeftDown.flag = false;
if (isPlayer({c.getRow() - i, c.getCol() + i}, player) && diagonalLeftUp.flag)
diagonalLeftUp.sum++;
else
diagonalLeftUp.flag = false;
if (isPlayer({c.getRow() + i, c.getCol() - i}, player) && diagonalRightDown.flag)
diagonalRightDown.sum++;
else
diagonalRightDown.flag = false;
if (isPlayer({c.getRow() + i, c.getCol() + i}, player) && diagonalRightUp.flag)
diagonalRightUp.sum++;
else
diagonalRightUp.flag = false;
}
int sumHigh = up.sum + down.sum;
int sumSide = right.sum + left.sum;
int sumFirstDiagonal = diagonalLeftDown.sum + diagonalRightUp.sum;
int sumSecondDiagonal = diagonalLeftUp.sum + diagonalRightDown.sum;
if (sumHigh == board.size() || sumSide == board.size() || sumFirstDiagonal == board.size() || sumSecondDiagonal == board.size()) {
return true;
}
return false;
}
bool TicTacToe::isPlayer(Coordinate c, char player) {
if (c.getRow() < 0 || c.getRow() >= sizeBoard || c.getCol()< 0 || c.getCol() >= sizeBoard)
return false;
char cell = game[c].get();
if (player == cell)
return true;
return false;
}