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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
| #include <Windows.h>
#include <windowsx.h>
#include "resource.h"
#include "GameBoard.h"
/*#pragma comment(linker,"/manifestdependency:"type='win32' \
name='Microsoft.Windows.Common-Controls' \
version='6.0.0.0' processorArchitecture='*'\
publicKeyToken='6595b64144ccf1df' language='*'"")*/
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND PlayerBoard[CELL][CELL],
OpponentBoard[CELL][CELL];
GameBoard *Player = new GameBoard();
GameBoard *Computer = new GameBoard();
bool formations = false, ShotPlayer = false, ShotComputer = false;
enum SizeShip { SingleDeck, Destroyer, Submarine, BattleShip };
enum Direction { Up, Down, Left, Right };
TCHAR message_1[] = L" Расставьте корабли";
TCHAR message_2[] = L" Нажмите In Game";
TCHAR message_3[] = L" Ваш ход";
TCHAR message_4[] = L"Компьютер ходит первым";
TCHAR message_5[] = L"Вы ходите первым";
TCHAR message_6[] = L"Попадание";
BOOL InitApplication(HINSTANCE hInstance) {
WNDCLASSEX wcx;
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpszClassName = L"SeaBattle";
wcx.lpszMenuName = L"SeaBattleMenu";
wcx.cbClsExtra = NULL;
wcx.cbWndExtra = NULL;
wcx.lpfnWndProc = WndProc;
wcx.hInstance = hInstance;
wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wcx)) {
MessageBox(NULL, L"Error Register class", L"Fatal error", MB_OK | MB_ICONERROR);
return FALSE;
}
return RegisterClassEx(&wcx);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {
HWND hMainWnd, DirectionBox, TypeBox, PlayerBox, InGame, OpponentBox, Direction[DIRECTION], TypeDeck[TYPEDECK];
HFONT hFont;
int index = 0, down_style = 0, right_style = 0, index_jump = 9, count = 0;
hMainWnd = CreateWindow(L"SeaBattle", L"Sea Battle", WS_OVERLAPPEDWINDOW | WS_VSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, (HWND)HWND_DESKTOP, (HMENU)NULL, hInstance, (LPVOID)NULL);
if (!hMainWnd) {
MessageBox(NULL, L"Error initialiation window", L"Fatal error", MB_OK | MB_ICONERROR);
return FALSE;
}
hFont = CreateFont(-11, 0, 0, 0, 0, TRUE, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, L"Calibri");
DirectionBox = CreateWindow(L"button", L"Положение Корабля", WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
5, 5, 120, 120, hMainWnd, NULL, hInstance, NULL);
TypeBox = CreateWindow(L"button", L"Тип Корабля", WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
130, 5, 120, 120, hMainWnd, NULL, hInstance, NULL);
PlayerBox = CreateWindow(L"button", L"Игровое Поле", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 5, 130, 305, 320, hMainWnd, NULL, hInstance, NULL);
OpponentBox = CreateWindow(L"button", L"Игровое Поле Оппонента", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 460, 130, 305, 320, hMainWnd, NULL, hInstance, NULL);
CreateWindow(L"button", L"Сообщение", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 450, 0, 200, 100, hMainWnd, (HMENU)NULL, hInstance, (LPVOID)NULL);
InGame = CreateWindow(L"button", L"In Game", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 345, 100, 80, 30, hMainWnd, (HMENU)INGAME, hInstance, (LPVOID)NULL);
Direction[index++] = CreateWindow(L"button", L"Вверх", WS_VISIBLE | WS_CHILD | WS_GROUP | BS_AUTORADIOBUTTON,
10, 20, 110, 30, hMainWnd, (HMENU)UP, hInstance, (LPVOID)NULL);
Direction[index++] = CreateWindow(L"button", L"Вниз", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON,
10, 42, 110, 30, hMainWnd, (HMENU)DOWN, hInstance, (LPVOID)NULL);
Direction[index++] = CreateWindow(L"button", L"Влево", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON,
10, 64, 110, 30, hMainWnd, (HMENU)LEFT, hInstance, (LPVOID)NULL);
Direction[index] = CreateWindow(L"button", L"Вправо", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON,
10, 86, 110, 30, hMainWnd, (HMENU)RIGHT, hInstance, (LPVOID)NULL);
index = 0;
TypeDeck[index++] = CreateWindow(L"button", L"Однопалубный", WS_CHILD | WS_VISIBLE | WS_GROUP | BS_AUTORADIOBUTTON,
135, 20, 110, 30, hMainWnd, (HMENU)SINGLEDECK, hInstance, (LPVOID)NULL);
TypeDeck[index++] = CreateWindow(L"button", L"Двухпалубный", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
135, 42, 110, 30, hMainWnd, (HMENU)DESTROYER, hInstance, (LPVOID)NULL);
TypeDeck[index++] = CreateWindow(L"button", L"Трехпалубный", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
135, 64, 110, 30, hMainWnd, (HMENU)SUBMARINE, hInstance, (LPVOID)NULL);
TypeDeck[index] = CreateWindow(L"button", L"Четырехпалубный", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
135, 86, 110, 30, hMainWnd, (HMENU)BATTLESHIP, hInstance, (LPVOID)NULL);
for (index = 0; index < CELL; index++) {
for (int index_j = 0; index_j < CELL; index_j++) {
count++;
PlayerBoard[index][index_j] = CreateWindow(L"button", L"", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_BITMAP,
10 + right_style, 150 + down_style, 25, 25, hMainWnd, (HMENU)(IDI_PLAYERBUTTON + count), hInstance, NULL);
right_style += 30;
if (index_j == index_jump) {
down_style += 30;
right_style = 0;
}
}
}
right_style = 0;
down_style = 0;
count = 1;
for (index = 0; index < CELL; index++) {
for (int index_j = 0; index_j < CELL; index_j++) {
OpponentBoard[index][index_j] = CreateWindow(L"button", L"", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_BITMAP,
465 + right_style, 150 + down_style, 25, 25, hMainWnd, (HMENU)(IDI_OPPONENTBUTTON + count), hInstance, NULL);
right_style += 30;
if (index_j == index_jump) {
down_style += 30;
right_style = 0;
}
}
}
SendMessage(PlayerBoard[0][0], BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)L"Image.bmp");
SendMessage(DirectionBox, WM_SETFONT, (WPARAM)hFont, NULL);
SendMessage(TypeBox, WM_SETFONT, (WPARAM)hFont, NULL);
for (index = 0; index < DIRECTION; index++)
SendMessage(Direction[index], WM_SETFONT, (WPARAM)hFont, NULL);
for (index = 0; index < TYPEDECK; index++)
SendMessage(TypeDeck[index], WM_SETFONT, (WPARAM)hFont, NULL);
index = 0;
SendMessage(Direction[index], BM_SETCHECK, (WPARAM)hFont, NULL);
SendMessage(TypeDeck[index], BM_SETCHECK, (WPARAM)hFont, NULL);
int Error = GetLastError();
char buf[11];
itoa(Error, buf, 10);
MessageBoxA(NULL, buf, "ERROR", MB_OK);
ShowWindow(hMainWnd, nCmdShow);
UpdateWindow(hMainWnd);
return TRUE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lCmdLine, int nCmdShow) {
MSG msg;
InitApplication(hInstance);
InitInstance(hInstance, nCmdShow);
while (GetMessage(&msg, NULL, NULL, NULL)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
int Error = GetLastError();
char buf[11];
itoa(Error, buf, 10);
MessageBoxA(NULL, buf, "ERROR", MB_OK);
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
HBRUSH hBackground = CreateSolidBrush(RGB(0, 0, 255));
HFONT hFont, oldFont;
RECT rect;
PAINTSTRUCT ps;
rect = { 1020,0,80,80 };
int index = 0, index_j = -1, index_jump = 10, _Direction = -1, _SizeShip = -1, count = 1, Compare_count = 0,_count = 0;
bool ReturnValue = false, CaseBan = false;
switch (uMsg) {
//case WM_CREATE:
// hFont = CreateFont(-11, 0, 0, 0, 0, TRUE, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, L"Calibri");
//break;
case WM_COMMAND:
if (!formations) {
HWND _Up = GetDlgItem(hWnd, UP);
HWND _Down = GetDlgItem(hWnd, DOWN);
HWND _Left = GetDlgItem(hWnd, LEFT);
HWND _Right = GetDlgItem(hWnd, RIGHT);
HWND _SingleDeck = GetDlgItem(hWnd, SINGLEDECK);
HWND _Destroyer = GetDlgItem(hWnd, DESTROYER);
HWND _Submarine = GetDlgItem(hWnd, SUBMARINE);
HWND _BattleShip = GetDlgItem(hWnd, BATTLESHIP);
HWND _ReturnValue;
HWND _CellBan;
HWND _BanChoice;
HINSTANCE hInstance = GetModuleHandle(NULL);
HBITMAP ShipCreate = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
for (; count <= 100; count++) {
index_j++;
if (index_j == index_jump) {
index_j = 0;
index++;
}
if (LOWORD(wParam) == IDI_PLAYERBUTTON + count) {
if (SendMessage(_Up, BM_GETCHECK, 0, 0))
_Direction = Up;
else if (SendMessage(_Down, BM_GETCHECK, 0, 0))
_Direction = Down;
else if (SendMessage(_Left, BM_GETCHECK, 0, 0))
_Direction = Left;
else if (SendMessage(_Right, BM_GETCHECK, 0, 0))
_Direction = Right;
if (SendMessage(_SingleDeck, BM_GETCHECK, 0, 0))
_SizeShip = SingleDeck;
else if (SendMessage(_Destroyer, BM_GETCHECK, 0, 0))
_SizeShip = Destroyer;
else if (SendMessage(_Submarine, BM_GETCHECK, 0, 0))
_SizeShip = Submarine;
else if (SendMessage(_BattleShip, BM_GETCHECK, 0, 0))
_SizeShip = BattleShip;
ReturnValue = Player->SetShipPlayer(index, index_j, _SizeShip, _Direction);
_count = 0;
for (index = 0; index < CELL; index++) {
for (index_j = 0; index_j < CELL; index_j++) {
_count++;
Compare_count = Player->ReturnVariable(index, index_j);
if (Compare_count == 1) {
_ReturnValue = GetDlgItem(hWnd, IDI_PLAYERBUTTON + _count);
SendMessage(_ReturnValue, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)ShipCreate);
UpdateWindow(_ReturnValue);
}
else if (Compare_count == -1) {
_CellBan = GetDlgItem(hWnd, IDI_PLAYERBUTTON + _count);
EnableWindow(_CellBan, FALSE);
UpdateWindow(_CellBan);
}
}
}
CaseBan = Player->BlockCase(_SizeShip);
if (CaseBan) {
_BanChoice = GetDlgItem(hWnd, SHIP + _SizeShip);
EnableWindow(_BanChoice, FALSE);
SendMessage(_BanChoice, BM_SETCHECK, NULL, NULL);
}
}
}
formations = Player->ReturnPlayerShip();
}
if (formations) {
InvalidateRect(hWnd, &rect, TRUE);
UpdateWindow(hWnd);
}
break;
case WM_PAINT:
if (!formations) {
BeginPaint(hWnd, &ps);
HDC hDC = GetDC(hWnd);
SetBkMode(hDC, TRANSPARENT);
hFont = CreateFont(-15, 0, 0, 0, 0, TRUE, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, L"Calibri");
oldFont = (HFONT)SelectObject(hDC, hFont);
DrawText(hDC, message_1, ARRAYSIZE(message_1), &rect, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
EndPaint(hWnd, &ps);
}
if (formations) {
BeginPaint(hWnd, &ps);
HDC hDC = GetDC(hWnd);
SetBkMode(hDC, TRANSPARENT);
hFont = CreateFont(-15, 0, 0, 0, 0, TRUE, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, L"Calibri");
oldFont = (HFONT)SelectObject(hDC, hFont);
DrawText(hDC, message_2, ARRAYSIZE(message_2), &rect, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
EndPaint(hWnd, &ps);
}
break;
case WM_CLOSE:
DestroyWindow(hWnd);
PostQuitMessage(NULL);
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
} |