Skip to content

Commit 5601260

Browse files
committed
* Migrating connection from TCP to UDP
* Rewrite net code * Rewrite end game system
1 parent 3f2db45 commit 5601260

File tree

16 files changed

+368
-360
lines changed

16 files changed

+368
-360
lines changed

build/Game.ico

-4.19 KB
Binary file not shown.

src/define.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
// Internet numbers
1818
#define BASE_PORT 2000 // Base port to create/connect
1919
#define INTERNET_BUFFER 3 // Size of data to send/recieve by connecion
20-
#define MESSAGE_TIMEOUT 5000 // Time after which connection is considered lost
21-
#define MESSAGE_NULL 2000 // Time to send NULL message to keep connecion
20+
#define MESSAGE_GET_TIMEOUT 2000 // Time after which connection is considered lost
21+
#define MESSAGE_NULL_TIMEOUT 800 // Time to send NULL message to keep connecion
22+
#define MESSAGE_APPLY_TIMEOUT 400 // Time to apply arriving message
2223

2324
// Base file names
2425
#define SETTING_FILE "settings4.ini" // File with all starting data (width, height...)

src/entity.cpp

Lines changed: 19 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "structs.hpp"
44

55
#include "dataLoader.hpp"
6-
#include "gameServer.hpp"
6+
#include "gameSingle.hpp"
77

88
Field::Field()
99
{
@@ -20,27 +20,18 @@ void Field::reset(){
2020
for(Uint8 i=0; i < fieldWidth * fieldWidth; ++i){
2121
data[i] = CELL_EMPTY;
2222
}
23+
count = 0;
2324
}
2425

2526
void Field::clickSingle(coord x, coord y){
2627
// Checking, if cell empty
2728
if(data[x + y * fieldWidth] == CELL_EMPTY){
2829
data[x + y * fieldWidth] = CELL_MY;
30+
count++;
2931

3032
//Checking for win
31-
switch (checkWin(x, y))
32-
{
33-
case CELL_MY:
34-
winning = true;
35-
break;
33+
gameState = checkWin(x, y);
3634

37-
case CELL_ENEMY:
38-
loosing = true;
39-
break;
40-
case 3:
41-
nobody = true;
42-
break;
43-
}
4435
AImove();
4536
}
4637
}
@@ -50,53 +41,24 @@ void Field::clickTwo(coord x, coord y){
5041
if(data[x + y * fieldWidth] == CELL_EMPTY){
5142
data[x + y * fieldWidth] = CELL_MY + player;
5243

53-
player = (player+1)%2; // Changing player
44+
player = (player + 1) % 2; // Changing player
45+
count++;
5446

5547
//Checking for win
56-
switch (checkWin(x, y))
57-
{
58-
case CELL_MY:
59-
winning = true;
60-
break;
61-
62-
case CELL_ENEMY:
63-
loosing = true;
64-
break;
65-
66-
case 3:
67-
nobody = true;
68-
break;
69-
}
48+
gameState = checkWin(x, y);
7049
}
7150
}
7251

73-
bool Field::clickMulti(coord x, coord y, TCPsocket sendPlace, Uint64* timer){
52+
bool Field::clickMulti(coord x, coord y){
7453
// Checking, if cell empty
7554
if(data[x + y * fieldWidth] == CELL_EMPTY){
7655
data[x + y * fieldWidth] = CELL_MY + player;
7756

78-
// Sending data to another player
79-
Uint8 sendData[INTERNET_BUFFER] = {MES_TURN, x, y};
80-
SDLNet_TCP_Send(sendPlace, sendData, INTERNET_BUFFER);
81-
*timer = SDL_GetTicks64() + MESSAGE_NULL;
82-
8357
player = (player + 1) % 2; // Changing player
58+
count++;
8459

8560
//Checking for win
86-
switch (checkWin(x, y))
87-
{
88-
case CELL_MY:
89-
winning = true;
90-
break;
91-
92-
case CELL_ENEMY:
93-
loosing = true;
94-
break;
95-
96-
case 3:
97-
nobody = true;
98-
break;
99-
}
61+
gameState = checkWin(x, y);
10062
return true;
10163
}
10264
return false;
@@ -111,6 +73,7 @@ Sint8 Field::recSolve(Uint8 round){
11173
if(data[y * fieldWidth + x] == CELL_EMPTY){
11274
// Trying set cell and think, what happen next
11375
data[y * fieldWidth + x] = (round % 2) + 1; // Set player cell on odd rounds and enemy cell in even
76+
count++;
11477
switch (checkWin(x, y))
11578
{
11679
case CELL_MY:
@@ -136,6 +99,7 @@ Sint8 Field::recSolve(Uint8 round){
13699
result -= recSolve(round+1);
137100
};
138101
data[y * fieldWidth + x] = CELL_EMPTY;
102+
count--;
139103
}
140104
}
141105
return result;
@@ -149,31 +113,22 @@ void Field::AImove(){
149113
for(coord i=0; i < fieldWidth * fieldWidth; ++i){
150114
if(data[i] == CELL_EMPTY){
151115
data[i] = CELL_ENEMY;
116+
count++;
152117
Sint8 cur = recSolve(1);
153118
if(cur > maxScore){
154119
maxScore = cur;
155120
maxPos = i;
156121
}
122+
count--;
157123
data[i] = CELL_EMPTY;
158124
}
159125
}
160126
//
161127
data[maxPos] = CELL_ENEMY;
128+
count++;
162129

163130
// Checking for win
164-
switch (checkWin(maxPos % fieldWidth, maxPos / fieldWidth))
165-
{
166-
case CELL_MY:
167-
winning = true;
168-
break;
169-
170-
case CELL_ENEMY:
171-
loosing = true;
172-
break;
173-
case 3:
174-
nobody = true;
175-
break;
176-
}
131+
gameState = checkWin(maxPos % fieldWidth, maxPos / fieldWidth);
177132
}
178133

179134
// New checkWin function
@@ -234,17 +189,10 @@ Uint8 Field::checkWin(const coord X, const coord Y){
234189
}
235190

236191
// Checking, is field full
237-
bool c = true;
238-
for(coord t=0; t < fieldWidth * fieldWidth; ++t){
239-
if(data[t] == CELL_EMPTY){
240-
c = false;
241-
break;
242-
}
243-
}
244-
if(c){
245-
return 3;
192+
if(count == fieldWidth * fieldWidth){
193+
return END_NOBODY;
246194
}
247-
return 0;
195+
return END_NONE;
248196
}
249197

250198
void Field::blit(){

src/entity.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ class Field
1515
private:
1616
Uint8 *data; // Grid of cells type from cellTypes
1717
Sint8 recSolve(Uint8 round); // Function for solve game in singleplayer recursivly
18+
Uint16 count; // Counter of filled cells
1819
public:
1920
Field();
2021
~Field();
2122
void reset();
2223

2324
void clickSingle(Uint8 x, Uint8 y); // Clicking in singleplayer mode
2425
void clickTwo(Uint8 x, Uint8 y); // Clicking in two-player mode
25-
bool clickMulti(Uint8 x, Uint8 y, TCPsocket sendPlace, Uint64* timer); // Clicking in multiplayer mode
26+
bool clickMulti(Uint8 x, Uint8 y); // Clicking in multiplayer mode, return if have turn
2627

2728
void AImove(); // Move of computer
2829
Uint8 checkWin(const Uint8 X, const Uint8 Y); // Check, if anyone win after his turn, return how win or 3 if nobody

0 commit comments

Comments
 (0)