-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
114 lines (93 loc) · 1.96 KB
/
Board.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
#include "Board.h"
#include <iostream>
using namespace std;
//default constructor
Board::Board()
{
board_display = new Coordinate*[BOARD_SIZE];
for (int i = 0; i < BOARD_SIZE; ++i)
{
board_display[i] = new Coordinate[BOARD_SIZE];
for (int j = 0; j < BOARD_SIZE; ++j)
{
board_display[i][j].setValue(AVAILABLE);
board_display[i][j].setX(j);
board_display[i][j].setY(i);
}
}
//player 1 starts
player_turn = 0;
}
//copy constructor
Board::Board(Board ©)
{
Coordinate** temp = copy.getBoard();
board_display = new Coordinate*[BOARD_SIZE];
for (int i = 0; i < BOARD_SIZE; ++i)
{
board_display[i] = new Coordinate[BOARD_SIZE];
for (int j = 0; j < BOARD_SIZE; ++j)
{
board_display[i][j].setValue(temp[i][j].getValue());
board_display[i][j].setX(j);
board_display[i][j].setY(i);
}
}
player_turn = copy.getPlayerTurn();
}
//destructor
Board::~Board()
{
for (int i = 0; i < BOARD_SIZE; ++i)
delete [] board_display[i];
delete [] board_display;
}
//accessors
Coordinate** Board::getBoard()
{
return board_display;
}
int Board::getPlayerTurn()
{
return player_turn;
}
void Board::setPlayerTurn(int player_turn)
{
this->player_turn = player_turn;
}
//board methods
void Board::displayBoard()
{
for (int i = 0; i < BOARD_SIZE; ++i)
{
cout << " " << (i + 1);
}
cout << endl;
for (int i = 0; i < BOARD_SIZE; ++i)
{
char row = 65 + i;
cout << row << " ";
for (int j = 0; j < BOARD_SIZE; j++)
cout << board_display[i][j].getValue() << " ";
cout << endl;
}
}
void Board::executeMove(Coordinate move)
{
//at this point, the coordinate is valid
if (player_turn == 0)
{
board_display[move.getY()][move.getX()].setValue(PLAYER_ONE);
board_display[move.getY() + 1][move.getX()].setValue(PLAYER_ONE);
}
else
{
board_display[move.getY()][move.getX()].setValue(PLAYER_TWO);
board_display[move.getY()][move.getX() + 1].setValue(PLAYER_TWO);
}
}
//other
void Board::changePlayerTurn()
{
player_turn = (player_turn + 1) % 2;
}