include ncurses include cstdlib include ctime include iostream include

  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
#include <ncurses.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
class Board {
public:
Board(int w, int h):
width(w), height(h),
pos_x(0), pos_y(0),
sel_x(0), sel_y(0),
selected(false),
count_close(width*height)
{
int i;
cards = new card*[width];
for( i=0 ; i<width ; i++ )
cards[i] = new card[height];
fillBoard();
refresh();
}
/********************************************/
~Board()
{
int i;
for( i=0 ; i<width ; i++ )
delete[] cards[i];
delete[] cards;
}
/********************************************/
void touchCard()
{
if( selected )
{
if( !cards[pos_x][pos_y].opened )
{
selected = false;
cards[pos_x][pos_y].opened = true;
if( cards[pos_x][pos_y].ch==cards[sel_x][sel_y].ch )
{
cards[pos_x][pos_y].opened = true;
count_close -= 2;
refresh();
}
else
{
refresh();
cards[pos_x][pos_y].opened = false;
cards[sel_x][sel_y].opened = false;
}
}
}
else
{
if( !cards[pos_x][pos_y].opened )
{
selected = true;
sel_x = pos_x;
sel_y = pos_y;
cards[sel_x][sel_y].opened = true;
refresh();
}
}
}
/********************************************/
inline int countClose()
{ return count_close; }
/********************************************/
inline int getPosX()
{ return pos_x; }
/********************************************/
inline int getPosY()
{ return pos_y; }
/********************************************/
void setPos(int x, int y)
{
if( trueCoor(x, y) )
{
pos_x = x;
pos_y = y;
refresh();
}
}
protected:
struct card {
char ch;
bool opened;
card():
ch(' '), opened(false)
{}
};
/********************************************/
bool trueCoor(int x, int y)
{ return (x>=0) && (x<width) && (y>=0) && (y<height); }
/********************************************/
void fillBoard()
{
card *free_list[width*height];
int count_free = 0;
int x, y;
for( y=0 ; y<height ; y++ )
for( x=0 ; x<width ; x++ )
free_list[count_free++] = &cards[x][y];
while( count_free!=0 )
{
char ch = rand()%('Z'-'A'+1)+'A';
// первая карта
int index = rand()%count_free;
free_list[index]->ch = ch;
free_list[index] = free_list[--count_free];
// вторая карта
index = rand()%count_free;
free_list[index]->ch = ch;
free_list[index] = free_list[--count_free];
}
}
/********************************************/
void refresh()
{
int x, y;
for( y=0 ; y<height ; y++ )
{
move(y, 0);
for( x=0 ; x<width ; x++ )
{
char cell[4];
cell[3] = 0;
if( (x==pos_x) && (y==pos_y) )
{
cell[0] = '[';
cell[2] = ']';
}
else
if( selected && (x==sel_x) && (y==sel_y) )
{
cell[0] = '(';
cell[2] = ')';
}
else
{
cell[0] = ' ';
cell[2] = ' ';
}
if( cards[x][y].opened )
cell[1] = cards[x][y].ch;
else
cell[1] = '_';
printw(cell);
}
}
}
/********************************************/
int width, height;
int pos_x, pos_y;
int sel_x, sel_y;
bool selected;
int count_close;
card **cards;
};
int main()
{
initscr();
noecho();
keypad(stdscr, TRUE);
bool prog_exit = false;
Board board(10, 10);
while( !prog_exit )
{
switch( getch() )
{
case 'q':
prog_exit = true;
break;
case KEY_LEFT:
board.setPos( board.getPosX()-1 , board.getPosY() );
break;
case KEY_RIGHT:
board.setPos( board.getPosX()+1 , board.getPosY() );
break;
case KEY_UP:
board.setPos( board.getPosX() , board.getPosY()-1 );
break;
case KEY_DOWN:
board.setPos( board.getPosX() , board.getPosY()+1 );
break;
case ' ':
board.touchCard();
if( board.countClose()==0 )
prog_exit = true;
}
}
endwin();
std::cout << "Game Over!" << std::endl;
return 0;
}