-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.cpp
161 lines (130 loc) · 4.23 KB
/
GameManager.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Roni Fultheim, ID: 313465965
* GameManager.cpp
*/
#include "GameManager.h"
#include <iostream>
using namespace std;
GameManager::GameManager(ViewGame* view, Board* b, Player* black, Player* white, MoveLogic* log):
board_(b), currPlayer_(black), oppPlayer_(white), logic_(log), view_(view) {}
void GameManager::playGame() {
//declare flag - in first turn game has not been played, current player has moves
bool noMoves = false;
view_->messageBoard();
view_->printBoard(board_->getBoard(), board_->size());
/* General explanation - First, build a list containing all the
* empty cells on the board. then, checking what might be a possible
* move dor the player, and putting all the options into a vector.
* The user select a point, and the board update acoording to the
* selected point.
*/
//while game is not over - keep playing
while (!board_->isBoardFull())
{
//display current turn
view_->messageForTurn(currPlayer_);
//initialize moves for black and white players
logic_->updateMoveOptions(currPlayer_, board_);
//if current player can play his turn
if (logic_->canPlayTurn(currPlayer_)) {
//show possible moves
view_->messagePossibleMoves(currPlayer_->getPossibleMoves());
//get next player's move
Location move = currPlayer_->getNextMove(view_, logic_, board_, oppPlayer_);
//check that move is allowed
//while move isn't legal - get another move from player
while (!logic_->isMoveAllowed(move, currPlayer_, board_))
{
view_->messageIllegalMove();
move = currPlayer_->getNextMove(view_, logic_, board_, oppPlayer_);
}
//call logic to play move
logic_->playMove(move, currPlayer_, board_);
//message of last turn
view_->messagePlayerMove(move, currPlayer_);
//update flag
noMoves = false;
}
//if current player cannot play his turn
else
{
//if the second player cannot play - show message and switch turns
if (!noMoves) {
view_->messageSwitchTurns();
noMoves = true;
}
else
{
//if both players did not play - game is over, there are no more moves left in game
view_->messageNoMoves();
break;
}
}
//switch players
Player* temp = currPlayer_;
currPlayer_ = oppPlayer_;
oppPlayer_ = temp;
//show board and last moves
view_->messageBoard();
view_->printBoard(board_->getBoard(), board_->size());
}
showWinner();
}
bool GameManager::playTurn(Player* playing, Player* other) {
//if the playing player cannot play - notify, update and return false
if (!logic_->canPlayTurn(playing)) {
//print message and newline, using cin methods to implement waiting for enter
//(cin>>variable does not work - must press enter in addition)
cout << "No possible moves. Play passes back to the other player. Press ENTER to continue... ";
cin.ignore();
cin.get();
cout << endl;
//update other player's move - changed from his last turn
logic_->updateMoveOptions(other, board_);
//and return false (mark that player didn't play)
return false;
}
//otherwise, print possible moves
cout << "Your possible moves: ";
//get moves and declare constant iterator
vector<Location>::const_iterator i;
vector<Location> moves = playing->getPossibleMoves();
//iterate over given moves and print them
for(i=moves.begin(); i != moves.end()-1; ++i){
//print location
cout << (*i) << " , ";
}
//print last location
cout << (*(moves.end()-1)) << endl;
//get player's choice of move
Location move = playing->getNextMove(view_, logic_, board_, other);
//while move isn't legal - get another move from player
while (!logic_->isMoveAllowed(move, playing, board_))
{
view_->messageIllegalMove();
move = playing->getNextMove(view_, logic_, board_, other);
}
//call logic to play move
logic_->playMove(move, playing, board_);
//update other player's options after playing move
logic_->updateMoveOptions(other, board_);
// TODO : YAEL - ask Roni
//save location of last move made in game
//lastMove_.set(move);
//mark that player has played
return true;
}
void GameManager::showWinner()
{
if (currPlayer_->getScore() > oppPlayer_->getScore()) {
view_->messageWinner(currPlayer_);
}
else if (currPlayer_->getScore() < oppPlayer_->getScore())
{
view_->messageWinner(oppPlayer_);
}
else
{
view_->messageTiko();
}
}