-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·236 lines (196 loc) · 6.59 KB
/
main.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <stdio.h>
#include <ncurses.h>
#include "game.h"
#include "global.h"
#include "draw.h"
#include "net.h"
int setUpScreen();
void freeMemory();
void changeGameState( const char newState );
int main() {
//============================
// Variables
//============================
char gameIsDone = 0;
char whoPlayer; // Server or Client
char whoFirst; // Порядок хода
//============================
// Setup
//============================
gameState = GS_MENU;
game_initVariables();
setUpScreen();
//============================
// Game
//============================
while ( !gameIsDone ) {
if ( gameState == GS_MENU ) {
switch ( game_mainMenu( &whoPlayer ) ) {
case GS_NET_SETUP:
changeGameState( GS_NET_SETUP );
break;
case GS_END_GAME:
changeGameState( GS_END_GAME );
break;
}
} else if ( gameState == GS_NET_SETUP ) {
net_setup( whoPlayer );
net_createSocket( whoPlayer );
net_connectOpponent( whoPlayer );
changeGameState( GS_SETUP_SHIP );
} else if ( gameState == GS_SETUP_SHIP ) {
draw_labels();
game_setUpShips();
changeGameState( GS_WAIT_ENEMY );
} else if ( gameState == GS_WAIT_ENEMY ) {
whoFirst = game_initGame( whoPlayer );
if ( whoFirst == 1 ) { // 1 - ходит первым, 2 - вторым
changeGameState( GS_SHOOT );
} else if ( whoFirst == 2 ) {
changeGameState( GS_WAIT_STEP );
} else {
draw_ERROR( "main", "Wrong argument whoPlayer" );
}
} else if ( gameState == GS_SHOOT ) {
switch ( game_doStep() ) {
case SHOOT_BIT:
break;
case SHOOT_MISS:
changeGameState( GS_WAIT_STEP );
break;
}
switch( game_checkWin( whoPlayer ) ) {
case GAME_WIN:
changeGameState( GS_YOU_WIN );
break;
case GAME_LOSE:
changeGameState( GS_YOU_LOSE );
break;
case GAME_NOTHING:
break;
}
} else if ( gameState == GS_WAIT_STEP ) {
switch ( game_waitStep() ) {
case SHOOT_BIT:
break;
case SHOOT_MISS:
changeGameState( GS_SHOOT );
break;
}
switch( game_checkWin( whoPlayer ) ) {
case GAME_WIN:
changeGameState( GS_YOU_WIN );
break;
case GAME_LOSE:
changeGameState( GS_YOU_LOSE );
break;
case GAME_NOTHING:
break;
}
} else if ( gameState == GS_YOU_WIN ) {
draw_loadFullScreen( "You WIN!" );
getch();
draw_closeLoadScreen();
changeGameState( GS_END_GAME );
} else if ( gameState == GS_YOU_LOSE ) {
draw_loadFullScreen( "You LOSE!" );
getch();
draw_closeLoadScreen();
changeGameState( GS_END_GAME );
} else if ( gameState == GS_END_GAME ) {
net_closeConnection( whoPlayer );
gameIsDone = 1;
} else {
draw_ERROR( "main", "Wrong argument gameState" );
}
}
freeMemory();
return 0;
}
/*
* Name: setUpScreen
* Description: Инициализирует настройки экрана
* */
int setUpScreen() {
initscr(); // Инициализируем экран
int row, col;
getmaxyx( stdscr, row, col );
winFstPlayer = subwin( stdscr, 10, 19, 2, 3 ); // Создаем экран первого игрока
winSecPlayer = subwin( stdscr, 10, 19, 2, 26 + kSPACE_BETWEEN ); // Создаем экран второго игрока
winHelp = subwin( stdscr, 1, col, row - 1, 0 ); // Создаем экран для вывода дополнительной информации
start_color(); // Включаем цвет
init_pair( 1, COLOR_WHITE, COLOR_BLUE ); // Создаем пару СИМВОЛ и ФОН для обычных символов
init_pair( 2, COLOR_BLUE, COLOR_WHITE ); // Пара для текущего положения курсора
init_pair( 3, COLOR_RED, COLOR_WHITE ); // Пара для совпадения детали текущего и уже существующего корабля
init_pair( 4, COLOR_WHITE, COLOR_BLACK ); // Пара для текста подсказки
bkgdset( COLOR_PAIR( 1 ) ); // Устанавливаем пару как стандартную
wbkgdset( winFstPlayer, COLOR_PAIR( 1 ) ); // Ставим пару цветов для окна первого ирока
wbkgdset( winSecPlayer, COLOR_PAIR( 1 ) ); // Ставим пару цветов для окна второго игрока
wbkgdset( winHelp, COLOR_PAIR( 1 ) ); // Ставим пару цветов для окна помощи
noecho(); // Отключаем отображение символов при нажатии в консоли
curs_set( 0 ); // Отключаем отображение курсора
keypad( stdscr, true ); // Включаем управление вводом с клавиатуры
return 0;
}
/*
* Name: freeMemory
* Description: Очистка памяти
* */
void freeMemory() {
delwin( winFstPlayer );
delwin( winSecPlayer );
endwin();
}
/*
* Name: changeGameState
* Description: Меняет состояние игры
* */
void changeGameState( const char newState ) {
if ( gameState == GS_MENU ) {
draw_help( "Leave GS_MENU state!" );
} else if ( gameState == GS_NET_SETUP ) {
draw_help( "Leave GS_NET_SETUP state!" );
} else if ( gameState == GS_SETUP_SHIP ) {
draw_help( "Leave GS_SETUP_SHIP state!" );
} else if ( gameState == GS_SHOOT ) {
draw_help( "Leave GS_SHOOT state!" );
} else if ( gameState == GS_END_GAME ) {
draw_help( "Leave GS_END_GAME state!" );
} else if ( gameState == GS_WAIT_ENEMY ) {
draw_help( "Leave GS_WAIT_ENEMY state!" );
} else if ( gameState == GS_WAIT_STEP ) {
draw_help( "Leave GS_WAIT_STEP state!" );
} else if ( gameState == GS_YOU_WIN ) {
draw_help( "Leave GS_YOU_WIN state!" );
} else if ( gameState == GS_YOU_LOSE ) {
draw_help( "Leave GS_YOU_LOSE state!" );
} else {
draw_ERROR( "changeGameState", "Wrong argument gameState" );
}
/*else if ( gameState == ) {
}*/
//getch();
gameState = newState;
if ( gameState == GS_MENU ) {
draw_help( "Set GS_MENU state!" );
} else if ( gameState == GS_NET_SETUP ) {
draw_help( "Set GS_NET_SETUP state!" );
} else if ( gameState == GS_SETUP_SHIP ) {
draw_help( "Set GS_SETUP_SHIP state!" );
} else if ( gameState == GS_SHOOT ) {
draw_help( "Set GS_SHOOT state!" );
} else if ( gameState == GS_END_GAME ) {
draw_help( "Set GS_END_GAME state!" );
} else if ( gameState == GS_WAIT_ENEMY ) {
draw_help( "Set GS_WAIT_ENEMY state!" );
} else if ( gameState == GS_WAIT_STEP ) {
draw_help( "Set GS_WAIT_STEP state!" );
} else if ( gameState == GS_YOU_WIN ) {
draw_help( "Set GS_YOU_WIN state!" );
} else if ( gameState == GS_YOU_LOSE ) {
draw_help( "Set GS_YOU_LOSE state!" );
} else {
draw_ERROR( "changeGameState", "Wrong argument gameState" );
}
//getch();
}