This repository was archived by the owner on Feb 28, 2018. It is now read-only.
forked from klausthul/kibitz
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChessMove.m
More file actions
210 lines (185 loc) · 5.2 KB
/
ChessMove.m
File metadata and controls
210 lines (185 loc) · 5.2 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
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
/*
$Id$
Copyright 2006 Klaus Thul (klaus.thul@mac.com)
This file is part of Kibitz.
Kibitz is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
Kibitz is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Kibitz; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#import "ChessMove.h"
@implementation ChessMove
+ (ChessMove *) moveFromStyle12: (NSArray *) data
{
int i, j, k;
NSString *pieces = @"-PNBRQK pnbrqk";
ChessMove *cm;
if ((cm = [[ChessMove alloc] init]) != nil) {
cm->movePrint = [data[29] retain];
cm->timeUsed = [data[28] retain];
cm->gameRelationship = [data[19] intValue];
for (i = 0; i < 8; i++) {
NSString *s = data[i + 1];
for (j = 0; j < 8; j++) {
unichar c = [s characterAtIndex: j];
for (k = 0; k < 15; k++)
if (c == [pieces characterAtIndex: k]) {
cm->fields[(7-i)*8 + j] = k;
break;
}
}
}
if ([data[9] characterAtIndex:0] == 'B')
cm->sideToMove = BLACK;
else
cm->sideToMove = WHITE;
cm->runningClock = (cm->sideToMove == WHITE) ? WHITE_CLOCK_RUNS : BLACK_CLOCK_RUNS;
cm->enPassantLine = [data[10] intValue];
cm->castleRights = [data[11] intValue] * WHITE_SHORT;
cm->castleRights += [data[12] intValue] * WHITE_LONG;
cm->castleRights += [data[13] intValue] * BLACK_SHORT;
cm->castleRights += [data[14] intValue] * BLACK_LONG;
cm->moveCounter50Rule = [data[15] intValue];
cm->whiteMaterial = [data[22] intValue];
cm->blackMaterial = [data[23] intValue];
cm->whiteRemainingTime = [data[24] intValue];
cm->blackRemainingTime = [data[25] intValue];
cm->nextMoveNumber = [data[26] intValue];
cm->lastTimeUpdate = time(NULL);
}
return [cm autorelease];
}
+ (ChessMove *) initialPosition
{
ChessMove *cm = [[ChessMove alloc] init];
if (cm != nil) {
int i, j, k, l;
const char firstrow[] = { ROOK, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK };
for (i = 0, j = 56, k = 8, l = 48; i < 8; i++, j++, k++, l++) {
cm->fields[i] = firstrow[i] | WHITE;
cm->fields[j] = firstrow[i] | BLACK;
cm->fields[k] = PAWN | WHITE;
cm->fields[l] = PAWN | BLACK;
}
cm->enPassantLine = -1;
cm->sideToMove = WHITE;
cm->nextMoveNumber = 1;
cm->castleRights = WHITE_LONG | WHITE_SHORT | BLACK_LONG | BLACK_SHORT;
}
cm->whiteRemainingTime = cm->blackRemainingTime = -1;
return [cm autorelease];
}
- (void) dealloc
{
[movePrint release];
[timeUsed release];
[super dealloc];
}
- (enum GameRelationship) gameRelationship
{
return gameRelationship;
}
- (enum Color) sideToMove
{
return sideToMove;
}
- (int) pieceLine: (int) l row: (int) r
{
return fields[l + 8*r - 9];
}
- (int) flipPieceLine: (int) l row: (int) r
{
return fields[63 - l - 8*r + 9];
}
- (int) pieceOnField: (struct ChessField) field
{
if (field.line == -1)
return (passedPieces[field.row] >= 1) ? field.row : 0;
else
return fields[field.row * 8 + field.line - 9];
}
- (enum ValidationResult) validateMoveFrom: (struct ChessField) from to: (struct ChessField) to
{
if (from.line == -1) {
if ((from.row < 0) || (from.row > 15)
|| (passedPieces[from.row] <= 0)
|| (to.row < 1) || (to.row > 8) || (to.line < 1) || (to.line > 8))
return INVALID;
else
return VALID;
} else {
if (((to.row == 1) || (to.row == 8)) && GETPIECE([self pieceOnField:from]) == PAWN)
return REQUIRES_PROMOTION;
return VALID;
}
}
- (int) whiteCurrentTime
{
if (runningClock == WHITE_CLOCK_RUNS)
return MAX(0, whiteRemainingTime - difftime(time(NULL), lastTimeUpdate));
else
return whiteRemainingTime;
}
- (int) blackCurrentTime
{
if (runningClock == BLACK_CLOCK_RUNS)
return MAX(0, blackRemainingTime - difftime(time(NULL), lastTimeUpdate));
else
return blackRemainingTime;
}
- (void) stopClock
{
runningClock = NO_CLOCK_RUNS;
}
- (int) moveNumber
{
return (sideToMove == BLACK) ? nextMoveNumber : nextMoveNumber - 1;
}
- (enum Color) moveColor
{
return (sideToMove == BLACK) ? WHITE : BLACK;
}
- (NSString *) movePrint
{
return (movePrint != nil) ? movePrint : @"";
}
- (int) passedPieces: (int) piece
{
return passedPieces[piece];
}
- (void) passedPiecesWhite: (NSString *) white black: (NSString *) black
{
int i, j, l;
char *pieceNames = "\0PNBRQK\0";
white = white.uppercaseString;
black = black.uppercaseString;
l = white.length;
hasPassedPieces = 0;
for (i = 0; i < 8; i++) {
passedPieces[i] = 0;
char c = pieceNames[i];
if (c != 0)
for (j = 0; j < l; j++)
if (c == [white characterAtIndex: j]) {
passedPieces[i]++;
hasPassedPieces = YES;
}
}
l = black.length;
for (i = 8; i < 16; i++) {
passedPieces[i] = 0;
char c = pieceNames[i - 8];
if (c != 0)
for (j = 0; j < l; j++)
if (c == [black characterAtIndex: j]) {
passedPieces[i]++;
hasPassedPieces = YES;
}
}
}
- (bool) hasPassedPieces {
return hasPassedPieces;
}
@end