-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathchess.cpp
104 lines (98 loc) · 1.49 KB
/
chess.cpp
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
// #include "grader.h"
#include <iostream>
using namespace std;
class chessboard
{
public:
enum piece
{ Rook, Knight, Bishop, Queen, King, Pawn };
public:
enum color
{ Black, White };
public:
piece ** board;
public:
int size;
chessboard (int size)
{
this->size = size;
board = new piece *[size];
for (int i = 0; i < size; i++)
{
board[i] = new piece[size];
}
}
~chessboard ()
{
for (int i = 0; i < size; i++)
{
delete[]board[i];
}
delete[]board;
}
int place (int x, int y, char color, piece p)
{
if (x < 0 || x > size || y < 0 || y > size)
{
return -1;
}
if (color != Black && color != White)
{
return -4;
}
if (p != Rook && p != King && p != Bishop && p != Queen
&& p != Pawn && p != Knight)
{
return -5;
}
if (board[x][y] != ' ')
{
return -3;
}
board[x][y] = p;
return 1;
}
int get (int x, int y, char &color, piece & p)
{
if (x < 0 || x > size || y < 0 || y > size)
{
return -1;
}
if (board[x][y] == ' ')
{
return -3;
}
p = board[x][y];
color = color;
return 1;
}
};
struct iterator
{
int x;
int y;
chessboard *board;
iterator (chessboard * board)
{
this->board = board;
x = 0;
y = 0;
}
void next ()
{
if (y == board->size - 1)
{
x++;
y = 0;
}
else
{
y++;
}
}
void xy (int &x, int &y)
{
x = this->x;
y = this->y;
}
};