Skip to content

Commit 14a91cb

Browse files
committed
Fixed wrong game states
Reworked gameStates, mostly in internet connection
1 parent c4aef6a commit 14a91cb

File tree

8 files changed

+136
-105
lines changed

8 files changed

+136
-105
lines changed

src/cycles/clientGame.cpp

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ bool ClientGameCycle::inputMouseDown() {
3030
logAdditional("Saving field");
3131
}
3232
// Normal turn
33-
if (field.tryClickMultiplayerCurrent(mouse)) {
33+
if (field.tryClickClientCurrent(mouse)) {
3434
// Sending to opponent
3535
connection.sendConfirmed<Uint8>(ConnectionCode::GameTurn, field.getLastTurn(mouse));
3636
}
@@ -44,7 +44,7 @@ void ClientGameCycle::update() {
4444
switch (connection.updateMessages()) {
4545
case ConnectionCode::GameTurn:
4646
if (connection.lastPacket->isBytesAvaliable(3)) {
47-
field.clickMultiplayerOpponent(connection.lastPacket->getData<Uint8>(2));
47+
field.clickClientOpponent(connection.lastPacket->getData<Uint8>(2));
4848
logAdditional("Turn of opponent player to %u", connection.lastPacket->getData<Uint8>(2));
4949
}
5050
return;
@@ -55,19 +55,6 @@ void ClientGameCycle::update() {
5555
const Field f = Field((char*)(connection.lastPacket->getPointer())+2);
5656
// Setting it as current
5757
field.setNewField(&f, window);
58-
// Inverting game state
59-
switch (field.getState()) {
60-
case GameState::CurrentPlay:
61-
field.setState(GameState::OpponentPlay);
62-
break;
63-
64-
case GameState::OpponentPlay:
65-
field.setState(GameState::CurrentPlay);
66-
break;
67-
68-
default:
69-
break;
70-
}
7158

7259
// Making sound
7360
sounds.play(Sounds::Reset);
@@ -95,28 +82,24 @@ void ClientGameCycle::draw() const {
9582
// Draw game state
9683
switch (field.getState()) {
9784
case GameState::CurrentPlay:
98-
playersTurnsTexts[0].blit();
85+
playersTurnsTexts[1].blit();
9986
break;
10087

10188
case GameState::OpponentPlay:
102-
playersTurnsTexts[1].blit();
89+
playersTurnsTexts[0].blit();
10390
break;
10491

10592
case GameState::CurrentWin:
106-
winText.blit();
93+
looseText.blit();
10794
break;
10895

10996
case GameState::OpponentWin:
110-
looseText.blit();
97+
winText.blit();
11198
break;
11299

113100
case GameState::NobodyWin:
114101
nobodyWinText.blit();
115102
break;
116-
117-
case GameState::WaitState:
118-
waitText.blit();
119-
break;
120103
}
121104
// Drawing upper dashboard
122105
exitButton.blit();

src/cycles/coopGame.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ bool TwoPlayerGameCycle::inputMouseDown() {
4444
return true;
4545
} else {
4646
// Normal turn
47-
field.tryClickTwo(mouse);
47+
field.tryClickCoop(mouse);
4848
}
4949
return false;
5050
}
@@ -86,11 +86,11 @@ void TwoPlayerGameCycle::draw() const {
8686
break;
8787

8888
case GameState::CurrentWin:
89-
secondWinText.blit();
89+
firstWinText.blit();
9090
break;
9191

9292
case GameState::OpponentWin:
93-
firstWinText.blit();
93+
secondWinText.blit();
9494
break;
9595

9696
case GameState::NobodyWin:

src/cycles/serverGame.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bool ServerGameCycle::inputMouseDown() {
5050
return true;
5151
} else {
5252
// Normal turn
53-
if (field.tryClickMultiplayerCurrent(mouse)) {
53+
if (field.tryClickServerCurrent(mouse)) {
5454
// Sending to opponent
5555
connection.sendConfirmed<Uint8>(ConnectionCode::GameTurn, field.getLastTurn(mouse));
5656
}
@@ -84,7 +84,7 @@ void ServerGameCycle::update() {
8484
case ConnectionCode::GameTurn:
8585
if (connection.lastPacket->isBytesAvaliable(3)) {
8686
// Making turn
87-
field.clickMultiplayerOpponent(connection.lastPacket->getData<Uint8>(2));
87+
field.clickServerOpponent(connection.lastPacket->getData<Uint8>(2));
8888

8989
// Making sound
9090
sounds.play(Sounds::Turn);
@@ -117,11 +117,11 @@ void ServerGameCycle::draw() const {
117117
break;
118118

119119
case GameState::CurrentWin:
120-
secondWinText.blit();
120+
firstWinText.blit();
121121
break;
122122

123123
case GameState::OpponentWin:
124-
firstWinText.blit();
124+
secondWinText.blit();
125125
break;
126126

127127
case GameState::NobodyWin:

src/cycles/singleplayerGame.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ void SinglePlayerGameCycle::draw() const {
8585
break;
8686

8787
case GameState::CurrentWin:
88-
secondWinText.blit();
88+
firstWinText.blit();
8989
break;
9090

9191
case GameState::OpponentWin:
92-
firstWinText.blit();
92+
secondWinText.blit();
9393
break;
9494

9595
case GameState::NobodyWin:

src/game/field.cpp

Lines changed: 25 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55

66
#include "field.hpp"
7-
#include "menu/selectingMenu.hpp"
87

98

109
const float Field::cellSide = 100;
@@ -63,6 +62,10 @@ GameState Field::getState() const {
6362
return gameState;
6463
}
6564

65+
bool Field::isStarted() const {
66+
return count > 0;
67+
}
68+
6669
const char* Field::getSaveTime() const {
6770
// Get time
6871
SDL_DateTime time;
@@ -132,7 +135,6 @@ bool Field::clickSingle(SDL_Point p) {
132135
if (gameState < GameState::CurrentWin) {
133136
AImove();
134137
}
135-
checkEnd();
136138
return true;
137139
}
138140
return false;
@@ -158,13 +160,8 @@ bool Field::clickTwo(SDL_Point p) {
158160
}
159161
count++;
160162

161-
// Making sound
162-
sounds.play(Sounds::Turn);
163-
music.startFromCurrent(Music::MainCombat);
164-
165163
// Checking for win
166164
gameState = checkWin(p);
167-
checkEnd();
168165
return true;
169166
}
170167
return false;
@@ -189,45 +186,33 @@ bool Field::clickMultiplayerCurrent(SDL_Point p) {
189186
}
190187
count++;
191188

192-
// Making sound
193-
sounds.play(Sounds::Turn);
194-
music.startFromCurrent(Music::MainCombat);
195-
196189
// Checking for win
197190
gameState = checkWin(p);
198-
checkEnd();
199191
return true;
200192
}
201193
return false;
202194
}
203195

204196
void Field::clickMultiplayerOpponent(SDL_Point p) {
205-
if (gameState == GameState::OpponentPlay) {
206-
switch (gameState) {
207-
case GameState::CurrentPlay:
208-
setCell(p, Cell::Current);
209-
gameState = GameState::OpponentPlay;
210-
break;
211-
212-
case GameState::OpponentPlay:
213-
setCell(p, Cell::Opponent);
214-
gameState = GameState::CurrentPlay;
215-
break;
216-
217-
default:
218-
break;
219-
}
220-
count++;
197+
switch (gameState) {
198+
case GameState::CurrentPlay:
199+
setCell(p, Cell::Current);
200+
gameState = GameState::OpponentPlay;
201+
break;
221202

222-
// Making sound
223-
sounds.play(Sounds::Turn);
224-
music.startFromCurrent(Music::MainCombat);
203+
case GameState::OpponentPlay:
204+
setCell(p, Cell::Opponent);
205+
gameState = GameState::CurrentPlay;
206+
break;
225207

226-
// Checking for win
227-
gameState = checkWin(p);
228-
checkEnd();
229-
return;
208+
default:
209+
break;
230210
}
211+
count++;
212+
213+
// Checking for win
214+
gameState = checkWin(p);
215+
return;
231216
}
232217

233218
// Recursivly solve, where cell need to be placed
@@ -320,7 +305,7 @@ GameState Field::checkWin(SDL_Point p) {
320305
}
321306

322307
if (state) {
323-
return (GameState)(state+2);
308+
return (GameState)(state+1);
324309
}
325310
}
326311

@@ -334,7 +319,7 @@ GameState Field::checkWin(SDL_Point p) {
334319
}
335320

336321
if (state) {
337-
return (GameState)(state+2);
322+
return (GameState)(state+1);
338323
}
339324
}
340325

@@ -346,7 +331,7 @@ GameState Field::checkWin(SDL_Point p) {
346331
state &= (Uint8)data[t + (p.y + t - p.x) * width];
347332
}
348333
if (state) {
349-
return (GameState)(state+2);
334+
return (GameState)(state+1);
350335
}
351336
}
352337

@@ -362,7 +347,7 @@ GameState Field::checkWin(SDL_Point p) {
362347
pos -= (width-1);
363348
}
364349
if (state) {
365-
return (GameState)(state+2);
350+
return (GameState)(state+1);
366351
}
367352
}
368353
} else {
@@ -376,7 +361,7 @@ GameState Field::checkWin(SDL_Point p) {
376361
pos -= (width-1);
377362
}
378363
if (state) {
379-
return (GameState)(state+2);
364+
return (GameState)(state+1);
380365
}
381366
}
382367
}
@@ -387,30 +372,6 @@ GameState Field::checkWin(SDL_Point p) {
387372
return gameState;
388373
}
389374

390-
void Field::checkEnd() {
391-
switch (gameState) {
392-
case GameState::CurrentWin:
393-
sounds.play(Sounds::Loose);
394-
logAdditional("Opponent win");
395-
break;
396-
397-
case GameState::OpponentWin:
398-
sounds.play(Sounds::Win);
399-
logAdditional("Current win");
400-
break;
401-
402-
case GameState::NobodyWin:
403-
sounds.play(Sounds::Loose);
404-
logAdditional("Nobody win");
405-
break;
406-
407-
default:
408-
return;
409-
}
410-
// Setting start menu for next game
411-
SelectingMenu::open();
412-
}
413-
414375
void Field::blit(const Window& window) const {
415376
// Rendering cells with their background
416377
for (int y=0; y < width; ++y) {

src/game/field.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class Field {
4646
Cell getCell(SDL_Point p) const;
4747
void setCell(SDL_Point p, Cell state);
4848

49-
void checkEnd(); // Function for do actions at game end
5049
void AImove(); // Move of computer
5150
int recursivelySolve(Uint8 round); // Function for solve game in singleplayer recursively
5251
GameState checkWin(SDL_Point p); // Check, if anyone win after his turn, return who win
@@ -68,6 +67,9 @@ class Field {
6867
GameState getState() const;
6968
void setState(GameState state);
7069
const char* getSaveTime() const;
70+
bool isStarted() const;
71+
72+
// Save system
7173
void updateSaveInfo();
7274
const Array<char> getSave() const;
7375
char getCheckSum() const;

0 commit comments

Comments
 (0)