-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameSapper.cpp
More file actions
125 lines (118 loc) · 6.33 KB
/
Copy pathGameSapper.cpp
File metadata and controls
125 lines (118 loc) · 6.33 KB
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
#include "stdafx.h"
#include <sstream>
#include "GameSapper.h"
GameSapper::Game::Game(){
Recreate( 10, 600 );
}
// ** Функция создания игрового поля
void GameSapper::Game::Recreate( unsigned new_size, unsigned new_time ){
// ~~ Check into values
if( new_size > 32 )
;// THROW EXCEPTION : OVERLY LONG SIZE OF FIELD;
// ~~ Initialize
uSize = new_size;
uTimeLimit = new_time;
delete pCells;
pCells = gcnew array<Cell^,2>( uSize, uSize );
for( unsigned i = 0; i < uSize; i++ )
for( unsigned j = 0; j < uSize; j++ )
pCells[i, j] = gcnew Cell;
}
// ** Функция, инициализирующая новую игру
void GameSapper::Game::NewGame(){
// ~~ Initialize game values
uTime = uTimeLimit;
iMarked = uSize * uSize / 7 + ( uSize / 10 - 1 ) * uSize;
uBombs = iMarked;
uTrueMarked = 0;
// ~~ Closing all cells
for( unsigned i = 0; i < uSize; i++ )
for( unsigned j = 0; j < uSize; j++ ){
pCells[i, j]->Value = 0;
pCells[i, j]->Flag = FLAG::CLOSED;
}
// ~~ Randomizing bombs
Random R;
for( int i = 0; i < iMarked; i++ ){
int x = R.Next( uSize );
int y = R.Next( uSize );
// ~~ In Cell[x,y] already exist bomb
if( pCells[x, y]->Value == BOMB )
i--;
else{
// ~~ Set bomb
pCells[x, y]->Value = BOMB;
// ~~ Calculate in neighbor cells
for( int xf = -1; xf < 2; xf++ ){
for( int yf = -1; yf < 2; yf++ ){
int a = x + xf;
int b = y + yf;
if( a >= 0 && a < int( uSize ) && b >= 0 && b < int( uSize ) )
if( pCells[x + xf, y + yf]->Value != BOMB )
pCells[x + xf, y + yf]->Value++;
}
}
}
}
}
System::String^ GameSapper::Game::ToString(){
int m = uTime / 60, s = uTime % 60;
//std::stringstream str;
//str << m << ":" << s;
DateTime^ date = gcnew DateTime( 1, 1, 1, 0, m, s );
return gcnew String( date->Minute.ToString() + ":" + date->Second.ToString() );
}
// ** Функция, отвечающая за открытие клетки поля
int GameSapper::Game::Open( unsigned x, unsigned y ){
#define p pCells[x,y]
// ~~ Not open these cells
if( p->Flag == FLAG::OPEN || p->Flag == FLAG::MARK )
return -2;
// ~~ If bomb then defeat
if( p->Value == BOMB ){
return BOMB;
}
// ~~ If empty then open & open near
else if( p->Value == 0 ){
// ~~ Open current
p->Flag = FLAG::OPEN;
// ~~ Open near
for( int xf = -1; xf < 2; xf++ ){
for( int yf = -1; yf < 2; yf++ ){
int a = x + xf;
int b = y + yf;
if( a >= 0 && a < int( uSize ) && b >= 0 && b < int( uSize ) )
if( pCells[a,b]->Flag == FLAG::CLOSED )
Open( a, b );
}
}
}
// ~~ If number then open
else if( p->Value < BOMB ){
p->Flag = FLAG::OPEN;
}
return -1;
#undef p
}
// ** Функция, отвечающая за отметку клетки игрового поля, как содержащего мину
bool GameSapper::Game::Mark( unsigned x, unsigned y ){
#define p pCells[x,y]->Flag
if( p != FLAG::OPEN ){
if( p != FLAG::MARK ){
p = FLAG::MARK;
iMarked--;
if( pCells[x,y]->Value == BOMB )
uTrueMarked++;
}
else{
p = FLAG::CLOSED;
iMarked++;
if( pCells[x, y]->Value == BOMB )
uTrueMarked--;
}
}
if( uTrueMarked == uBombs && iMarked == 0 )
return true;
return false;
#undef p
}