-
Notifications
You must be signed in to change notification settings - Fork 0
/
CController.cpp
152 lines (112 loc) · 3.41 KB
/
CController.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
#include "CController.h"
#include "CMainMenu.h"
#include "CDifficultyMenu.h"
#include "CColorMenu.h"
#include "CCommand.h"
#include "CPlayer.h"
#include "CAbstractMenuScreen.h"
#include "CGameSession.h"
#include "CKing.h"
CController::CController(){
menu = new CMainMenu();
}
void CController::deleteMenus(){
CAbstractMenuScreen * tmp = menu;
CAbstractMenuScreen * tmp2 = tmp;
if(menu == NULL) // vse uz je smazano (treba proto ze jsem BACKoval zpatky v menu)
return;
while(tmp->nextMenu != NULL){ // mazu od zacatku do konce
tmp = tmp->nextMenu;
delete tmp2;
tmp2 = tmp;
}
delete tmp;
menu = NULL;
}
CController::~CController(){
deleteMenus();
}
void CController::showMenus(){
CAbstractMenuScreen * tmpmenu = menu;
while(tmpmenu != NULL){
tmpmenu->show();
cout << "vybrano: "<< tmpmenu->readInput()<<endl;
tmpmenu->setStuff(this);
tmpmenu->setNextMenu();
if( tmpmenu -> nextMenu == NULL && tmpmenu -> prevMenu == NULL ){
deleteMenus();
break;
}
else{
tmpmenu = tmpmenu->nextMenu;
}
}
}
void CController::startGame(){
if(!game.gameReady()){ //jsme na main menu a vybrali Exit
game.end();
return;
}
else
game.start();
gameLoop();
game.end();
}
void CController::gameLoop(){
cin.ignore();
if(game.whosTurn == WHITE){
if(game.player1->getPlayerColor() == WHITE)
game.currPlayerPtr = game.player1;
else
game.currPlayerPtr = game.player2;
}
else{
if(game.player1->getPlayerColor() == BLACK)
game.currPlayerPtr = game.player1;
else
game.currPlayerPtr = game.player2;
}
while(true){
// cout<<"===================="<<endl;
// game.gameBoard.printDebug();
// game.gameBoard.printBoard();
// cout<<"===================="<<endl;
CCommand command;
if(game.isCheckMate()){
command.command = SURRENDER;
command.executeCommand(game);
return;
}
else if(game.isTie()){
command.command = TIE;
command.executeCommand(game);
return;
}
else if(game.currPlayerPtr->kingIsChecked(game)){
CCommand checkcom;
checkcom.command = CHECK;
checkcom.executeCommand(game);
}
while(command.command == UNKNOWN){
if(game.currPlayerPtr == game.player2)
cout << "Čekám na tah protihráče..." << endl;
command = game.currPlayerPtr-> getCommand(game);
}
command.executeCommand(game);
if(game.exitRequest)
break;
if(game.movePerformed){
if(game.onlineGame && game.currPlayerPtr == game.player1){ // hral jsem ja, poslu svuj tah druhymu
game.networking.sendCommand(command,game.player2->getSocket());
}
game.switchPlayers();
game.movePerformed = false;
}
}
}
void CController::endGame(){
game.end();
}
CGameSession& CController::getGameSess() {
return game;
}