-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
92 lines (80 loc) · 2.25 KB
/
Copy pathBoard.cpp
File metadata and controls
92 lines (80 loc) · 2.25 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
#include "Board.h"
#include<iostream>
using namespace std;
Board::Board()
{
for (int i = 0;i < 3;i++)
for (int j = 0;j < 3;j++)
board[i][j] = " ";
}
Board::Board(const Board & aux)
{
//copy
for (int i = 0;i < 3;i++)
for (int j = 0;j < 3;j++)
board[i][j] = aux.board[i][j];
}
Board::~Board()
{
//delete[] &board;
}
Board & Board::operator=(const Board &aux)
{
if (this == &aux) //Self assigment guard
return *this;
//copy
for (int i = 0;i < 3;i++)
for (int j = 0;j < 3;j++)
board[i][j] = aux.board[i][j];
return *this;
}
void Board::printBoard()
{
cout << "+---+---+---+\n";
cout << "| " << board[0][0] << " | " << board[0][1] << " | "<<board[0][2]<<" |\n";
cout << "+---+---+---+\n";
cout << "| " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " |\n";
cout << "+---+---+---+\n";
cout << "| " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " |\n";
cout << "+---+---+---+\n";
}
string Board::checkSymbol(int i, int j)
{
return board[i][j];
}
void Board::placeSymbol(string aux, int i, int j)
{
board[i][j] = aux;
}
bool Board::checkWinner()
{
// Left to Right same value
if (board[0][0] == board[0][1] && board[0][0] == board[0][2] && board[0][0] != " ")
return true;
else if (board[1][0] == board[1][1] && board[1][0] == board[1][2] && board[1][0] != " ")
return true;
else if (board[2][0] == board[2][1] && board[2][0] == board[2][2] && board[2][0] != " ")
return true;
// upwards & downwards same value
else if (board[0][0] == board[1][0] && board[0][0] == board[2][0] && board[0][0] != " ")
return true;
else if (board[0][1] == board[1][1] && board[0][1] == board[2][1] && board[0][1] != " ")
return true;
else if (board[0][2] == board[1][2] && board[0][2] == board[2][2] && board[0][2] != " ")
return true;
// Diagonal same value
else if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != " ")
return true;
else if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] != " ")
return true;
else
return false;
}
bool Board::checkTie()
{
for (int i = 0; i < 3;i++)
for (int j = 0; j < 3; j++)
if (board[i][j] == " ")
return false;
return true;
}