33#include " structs.hpp"
44
55#include " dataLoader.hpp"
6- #include " gameServer .hpp"
6+ #include " gameSingle .hpp"
77
88Field::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
2526void 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
250198void Field::blit (){
0 commit comments