kriegspiel 0.9.0b1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kriegspiel-0.9.0b1/LICENSE +21 -0
- kriegspiel-0.9.0b1/PKG-INFO +51 -0
- kriegspiel-0.9.0b1/README.md +23 -0
- kriegspiel-0.9.0b1/kriegspiel/__init__.py +7 -0
- kriegspiel-0.9.0b1/kriegspiel/berkeley.py +287 -0
- kriegspiel-0.9.0b1/kriegspiel/move.py +213 -0
- kriegspiel-0.9.0b1/kriegspiel.egg-info/PKG-INFO +51 -0
- kriegspiel-0.9.0b1/kriegspiel.egg-info/SOURCES.txt +12 -0
- kriegspiel-0.9.0b1/kriegspiel.egg-info/dependency_links.txt +1 -0
- kriegspiel-0.9.0b1/kriegspiel.egg-info/requires.txt +1 -0
- kriegspiel-0.9.0b1/kriegspiel.egg-info/top_level.txt +1 -0
- kriegspiel-0.9.0b1/setup.cfg +4 -0
- kriegspiel-0.9.0b1/setup.py +109 -0
- kriegspiel-0.9.0b1/tests/test_berkeley.py +649 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Kriegspiel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: kriegspiel
|
|
3
|
+
Version: 0.9.0b1
|
|
4
|
+
Summary: Python Kriegspiel Game
|
|
5
|
+
Home-page: https://github.com/Kriegspiel/ks-game
|
|
6
|
+
Author: Alexander Filatov
|
|
7
|
+
Author-email: alexander@kriegspiel.org
|
|
8
|
+
License: MIT
|
|
9
|
+
Keywords: kriegspiel chess variant
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Topic :: Games/Entertainment
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.5
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: python-chess[uci]
|
|
18
|
+
Dynamic: author
|
|
19
|
+
Dynamic: author-email
|
|
20
|
+
Dynamic: classifier
|
|
21
|
+
Dynamic: description
|
|
22
|
+
Dynamic: home-page
|
|
23
|
+
Dynamic: keywords
|
|
24
|
+
Dynamic: license
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
Dynamic: requires-dist
|
|
27
|
+
Dynamic: summary
|
|
28
|
+
|
|
29
|
+
# Python Kriegspiel Game
|
|
30
|
+
|
|
31
|
+
Supported Kriegspiel rules:
|
|
32
|
+
|
|
33
|
+
1. Berkeley + Public Any
|
|
34
|
+
|
|
35
|
+
In plan:
|
|
36
|
+
|
|
37
|
+
1. Pure Berkeley
|
|
38
|
+
2. Crazykrieg
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
### How-to
|
|
42
|
+
|
|
43
|
+
Run tests
|
|
44
|
+
``` bash
|
|
45
|
+
# clone
|
|
46
|
+
git clone https://github.com/Kriegspiel/ks-game.git
|
|
47
|
+
# install dependencies
|
|
48
|
+
cd ks-game && pip install -r requirements.txt
|
|
49
|
+
# run tests
|
|
50
|
+
python -m pytest tests/
|
|
51
|
+
```
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Python Kriegspiel Game
|
|
2
|
+
|
|
3
|
+
Supported Kriegspiel rules:
|
|
4
|
+
|
|
5
|
+
1. Berkeley + Public Any
|
|
6
|
+
|
|
7
|
+
In plan:
|
|
8
|
+
|
|
9
|
+
1. Pure Berkeley
|
|
10
|
+
2. Crazykrieg
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### How-to
|
|
14
|
+
|
|
15
|
+
Run tests
|
|
16
|
+
``` bash
|
|
17
|
+
# clone
|
|
18
|
+
git clone https://github.com/Kriegspiel/ks-game.git
|
|
19
|
+
# install dependencies
|
|
20
|
+
cd ks-game && pip install -r requirements.txt
|
|
21
|
+
# run tests
|
|
22
|
+
python -m pytest tests/
|
|
23
|
+
```
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
import enum
|
|
4
|
+
|
|
5
|
+
import chess
|
|
6
|
+
|
|
7
|
+
from kriegspiel.move import KriegspielMove as KSMove
|
|
8
|
+
from kriegspiel.move import QuestionAnnouncement as QA
|
|
9
|
+
|
|
10
|
+
from kriegspiel.move import KriegspielAnswer as KSAnswer
|
|
11
|
+
from kriegspiel.move import MainAnnouncement as MA
|
|
12
|
+
from kriegspiel.move import SpecialCaseAnnouncement as SCA
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
HALFMOVE_CLOCK_LIMIT = 2000
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class BerkeleyGame(object):
|
|
19
|
+
'''docstring for BerkeleyGame'''
|
|
20
|
+
|
|
21
|
+
def __init__(self):
|
|
22
|
+
super(BerkeleyGame).__init__()
|
|
23
|
+
self._board = chess.Board()
|
|
24
|
+
self._must_use_pawns = False
|
|
25
|
+
self._game_over = False
|
|
26
|
+
self._generate_possible_to_ask_list()
|
|
27
|
+
|
|
28
|
+
def ask_for(self, move):
|
|
29
|
+
if not isinstance(move, KSMove):
|
|
30
|
+
raise TypeError
|
|
31
|
+
result = self._ask_for(move)
|
|
32
|
+
# Regenerate possbile to ask list if move is done
|
|
33
|
+
if result.move_done:
|
|
34
|
+
self._generate_possible_to_ask_list()
|
|
35
|
+
# Remove non-pawn-captures is there is any pawn captures
|
|
36
|
+
if result.main_announcement == MA.HAS_ANY:
|
|
37
|
+
self._possible_to_ask = list(
|
|
38
|
+
set(self._possible_to_ask) -
|
|
39
|
+
(set(self._possible_to_ask) - set(self._generate_posible_pawn_captures()))
|
|
40
|
+
)
|
|
41
|
+
# Remove pawn captures is there is no pawn captures
|
|
42
|
+
if result.main_announcement == MA.NO_ANY:
|
|
43
|
+
self._possible_to_ask = list(set(self._possible_to_ask) - set(self._generate_posible_pawn_captures()))
|
|
44
|
+
# Possible to ask about a move only once
|
|
45
|
+
if result.main_announcement in (MA.ILLEGAL_MOVE, MA.NO_ANY):
|
|
46
|
+
# For `has any` already deleted
|
|
47
|
+
self._possible_to_ask.remove(move)
|
|
48
|
+
return result
|
|
49
|
+
|
|
50
|
+
def _ask_for(self, move):
|
|
51
|
+
'''
|
|
52
|
+
return (MoveAnnouncement, captured_square, SpecialCaseAnnouncement)
|
|
53
|
+
'''
|
|
54
|
+
if move not in self.possible_to_ask:
|
|
55
|
+
return KSAnswer(MA.IMPOSSIBLE_TO_ASK)
|
|
56
|
+
|
|
57
|
+
if move.question_type == QA.COMMON:
|
|
58
|
+
# Player asks about common move
|
|
59
|
+
if self._is_legal_move(move.chess_move):
|
|
60
|
+
# Move is legal in normal chess
|
|
61
|
+
if not self._check_if_must_use_pawns_rule(move.chess_move):
|
|
62
|
+
# Move is illigal, because player have asked
|
|
63
|
+
# about any possible pawn captures and there
|
|
64
|
+
# are some pawn captures, but now tries to
|
|
65
|
+
# play another piece.
|
|
66
|
+
return KSAnswer(MA.ILLEGAL_MOVE)
|
|
67
|
+
# Perform normal move
|
|
68
|
+
captured_square = self._make_move(move.chess_move)
|
|
69
|
+
special_case = self._check_special_cases()
|
|
70
|
+
if captured_square is not None:
|
|
71
|
+
# If it was capture
|
|
72
|
+
return KSAnswer(MA.CAPTURE_DONE,
|
|
73
|
+
capture_at_square=captured_square,
|
|
74
|
+
special_announcement=special_case
|
|
75
|
+
)
|
|
76
|
+
# If it was regular move
|
|
77
|
+
return KSAnswer(MA.REGULAR_MOVE,
|
|
78
|
+
special_announcement=special_case
|
|
79
|
+
)
|
|
80
|
+
# If move is illegal
|
|
81
|
+
return KSAnswer(MA.ILLEGAL_MOVE)
|
|
82
|
+
elif move.question_type == QA.ASK_ANY:
|
|
83
|
+
# Any Rule. Asking for any available pawn captures.
|
|
84
|
+
# Possible to ask once a turn
|
|
85
|
+
if self._has_any_pawn_captures():
|
|
86
|
+
self._must_use_pawns = True
|
|
87
|
+
return KSAnswer(MA.HAS_ANY)
|
|
88
|
+
else:
|
|
89
|
+
return KSAnswer(MA.NO_ANY)
|
|
90
|
+
else:
|
|
91
|
+
raise TypeError
|
|
92
|
+
|
|
93
|
+
def _check_if_must_use_pawns_rule(self, move):
|
|
94
|
+
if self._must_use_pawns:
|
|
95
|
+
return self._board.piece_type_at(move.from_square) == chess.PAWN
|
|
96
|
+
return True
|
|
97
|
+
|
|
98
|
+
def is_game_over(self):
|
|
99
|
+
if self._game_over:
|
|
100
|
+
return True
|
|
101
|
+
if self._board.is_stalemate():
|
|
102
|
+
return True
|
|
103
|
+
if self._board.is_insufficient_material():
|
|
104
|
+
return True
|
|
105
|
+
if self._board.is_checkmate():
|
|
106
|
+
return True
|
|
107
|
+
if self._board.halfmove_clock == HALFMOVE_CLOCK_LIMIT:
|
|
108
|
+
return True
|
|
109
|
+
return False
|
|
110
|
+
|
|
111
|
+
def _check_special_cases(self):
|
|
112
|
+
def same_rank(from_sq, to_sq):
|
|
113
|
+
# Or same row
|
|
114
|
+
return chess.rank_index(from_sq) == chess.rank_index(to_sq)
|
|
115
|
+
def same_file(from_sq, to_sq):
|
|
116
|
+
# Or same column
|
|
117
|
+
return chess.file_index(from_sq) == chess.file_index(to_sq)
|
|
118
|
+
def sw_ne_diagonal(from_sq, to_sq):
|
|
119
|
+
# Or on one lower-left upper-right diagonal
|
|
120
|
+
# Parallel to A1H8
|
|
121
|
+
return ((chess.rank_index(from_sq) - chess.rank_index(to_sq)) ==
|
|
122
|
+
(chess.file_index(from_sq) - chess.file_index(to_sq)))
|
|
123
|
+
def nw_se_diagonal(from_sq, to_sq):
|
|
124
|
+
# Or on one upper-left lower-right diagonal
|
|
125
|
+
# Parallel to A8H1
|
|
126
|
+
return ((chess.rank_index(from_sq) - chess.rank_index(to_sq)) ==
|
|
127
|
+
-(chess.file_index(from_sq) - chess.file_index(to_sq)))
|
|
128
|
+
def is_short_diagonal(from_sq, to_sq):
|
|
129
|
+
'''
|
|
130
|
+
return True is diagonal is short
|
|
131
|
+
'''
|
|
132
|
+
if (((chess.rank_index(to_sq) <= 3) and (chess.file_index(to_sq) <= 3)) or
|
|
133
|
+
((chess.rank_index(to_sq) > 3) and (chess.file_index(to_sq) > 3))):
|
|
134
|
+
# This means that King is in lower-left quadrant or
|
|
135
|
+
# in upper-right quadrant
|
|
136
|
+
# In this quadrants NW_SE_diagonals are shortest
|
|
137
|
+
if nw_se_diagonal(from_sq, to_sq):
|
|
138
|
+
return True
|
|
139
|
+
elif sw_ne_diagonal(from_sq, to_sq):
|
|
140
|
+
return False
|
|
141
|
+
else:
|
|
142
|
+
raise KeyError
|
|
143
|
+
else:
|
|
144
|
+
# Other two quadrants. And diagonals are vise-versa.
|
|
145
|
+
if nw_se_diagonal(from_sq, to_sq):
|
|
146
|
+
return False
|
|
147
|
+
elif sw_ne_diagonal(from_sq, to_sq):
|
|
148
|
+
return True
|
|
149
|
+
else:
|
|
150
|
+
raise KeyError
|
|
151
|
+
def kind_of_check(attacker_square, king_square):
|
|
152
|
+
if same_file(attacker_square, king_square):
|
|
153
|
+
return SCA.CHECK_FILE
|
|
154
|
+
elif same_rank(attacker_square, king_square):
|
|
155
|
+
return SCA.CHECK_RANK
|
|
156
|
+
elif (sw_ne_diagonal(attacker_square, king_square) or
|
|
157
|
+
nw_se_diagonal(attacker_square, king_square)):
|
|
158
|
+
if is_short_diagonal(attacker_square, king_square):
|
|
159
|
+
return SCA.CHECK_SHORT_DIAGONAL
|
|
160
|
+
else:
|
|
161
|
+
return SCA.CHECK_LONG_DIAGONAL
|
|
162
|
+
else:
|
|
163
|
+
return SCA.CHECK_KNIGHT
|
|
164
|
+
|
|
165
|
+
if self.is_game_over():
|
|
166
|
+
self._game_over = True
|
|
167
|
+
if self._board.is_stalemate():
|
|
168
|
+
return SCA.DRAW_STALEMATE
|
|
169
|
+
if self._board.is_insufficient_material():
|
|
170
|
+
return SCA.DRAW_INSUFFICIENT
|
|
171
|
+
if self._board.is_checkmate():
|
|
172
|
+
if self._board.result() == '1-0':
|
|
173
|
+
return SCA.CHECKMATE_WHITE_WINS
|
|
174
|
+
elif self._board.result() == '0-1':
|
|
175
|
+
return SCA.CHECKMATE_BLACK_WINS
|
|
176
|
+
if self._board.halfmove_clock == HALFMOVE_CLOCK_LIMIT:
|
|
177
|
+
return SCA.DRAW_TOOMANYREVERSIBLEMOVES
|
|
178
|
+
|
|
179
|
+
if self._board.is_check():
|
|
180
|
+
sq = self._board.pieces(chess.KING, self._board.turn)
|
|
181
|
+
king_square = sq.pop()
|
|
182
|
+
attackers = self._board.attackers(not self._board.turn, king_square)
|
|
183
|
+
attackers_squares = list(attackers)
|
|
184
|
+
if len(attackers_squares) == 2:
|
|
185
|
+
first = kind_of_check(attackers_squares[0], king_square)
|
|
186
|
+
second = kind_of_check(attackers_squares[1], king_square)
|
|
187
|
+
return SCA.CHECK_DOUBLE, [first, second]
|
|
188
|
+
elif len(attackers_squares) == 1:
|
|
189
|
+
attacker_square = attackers_squares[0]
|
|
190
|
+
return kind_of_check(attacker_square, king_square)
|
|
191
|
+
else:
|
|
192
|
+
raise RuntimeError
|
|
193
|
+
return SCA.NONE
|
|
194
|
+
|
|
195
|
+
def _get_captured_square(self, move):
|
|
196
|
+
if not self._board.is_en_passant(move):
|
|
197
|
+
return move.to_square
|
|
198
|
+
else:
|
|
199
|
+
if self._board.turn == chess.WHITE:
|
|
200
|
+
return move.to_square - 8
|
|
201
|
+
else:
|
|
202
|
+
return move.to_square + 8
|
|
203
|
+
|
|
204
|
+
def _make_move(self, move):
|
|
205
|
+
self._must_use_pawns = False
|
|
206
|
+
captured_square = None
|
|
207
|
+
if self._board.is_capture(move):
|
|
208
|
+
captured_square = self._get_captured_square(move)
|
|
209
|
+
self._board.push(move)
|
|
210
|
+
return captured_square
|
|
211
|
+
|
|
212
|
+
def _has_any_pawn_captures(self):
|
|
213
|
+
pawn_squares = self._board.pieces(chess.PAWN, self._board.turn)
|
|
214
|
+
for move in self._board.legal_moves:
|
|
215
|
+
if move.from_square in pawn_squares:
|
|
216
|
+
if self._board.is_capture(move):
|
|
217
|
+
return True
|
|
218
|
+
return False
|
|
219
|
+
|
|
220
|
+
def _is_legal_move(self, move):
|
|
221
|
+
return move in self._board.legal_moves
|
|
222
|
+
|
|
223
|
+
def _prepare_players_board(self):
|
|
224
|
+
players_board = self._board.copy(stack=False)
|
|
225
|
+
for square in chess.SQUARES:
|
|
226
|
+
if players_board.piece_at(square) is not None:
|
|
227
|
+
if players_board.piece_at(square).color is not self._board.turn:
|
|
228
|
+
players_board.remove_piece_at(square)
|
|
229
|
+
self._players_board = players_board
|
|
230
|
+
|
|
231
|
+
def _generate_posible_pawn_captures(self):
|
|
232
|
+
possibilities = list()
|
|
233
|
+
for square in list(self._board.pieces(chess.PAWN, self._board.turn)):
|
|
234
|
+
for attacked in list(self._players_board.attacks(square)):
|
|
235
|
+
if self._players_board.piece_at(attacked) is None:
|
|
236
|
+
if chess.rank_index(attacked) in (0, 7):
|
|
237
|
+
# If capture is promotion for pawn.
|
|
238
|
+
possibilities.extend([
|
|
239
|
+
KSMove(QA.COMMON, chess.Move(square, attacked, promotion=chess.QUEEN)),
|
|
240
|
+
KSMove(QA.COMMON, chess.Move(square, attacked, promotion=chess.BISHOP)),
|
|
241
|
+
KSMove(QA.COMMON, chess.Move(square, attacked, promotion=chess.KNIGHT)),
|
|
242
|
+
KSMove(QA.COMMON, chess.Move(square, attacked, promotion=chess.ROOK))
|
|
243
|
+
])
|
|
244
|
+
else:
|
|
245
|
+
# If capture is not promotion for pawn
|
|
246
|
+
possibilities.append(KSMove(QA.COMMON, chess.Move(square, attacked)))
|
|
247
|
+
return possibilities
|
|
248
|
+
|
|
249
|
+
def _generate_possible_to_ask_list(self):
|
|
250
|
+
# Very slow. :(
|
|
251
|
+
if self._game_over:
|
|
252
|
+
self._possible_to_ask = list()
|
|
253
|
+
return
|
|
254
|
+
# Make a board that current player see
|
|
255
|
+
self._prepare_players_board()
|
|
256
|
+
# Now players_board is equal to board that current player see
|
|
257
|
+
possibilities = list()
|
|
258
|
+
# First collect all possible moves keeping in mind castling rules
|
|
259
|
+
possibilities.extend([
|
|
260
|
+
KSMove(QA.COMMON, chess_move)
|
|
261
|
+
for chess_move in self._players_board.legal_moves
|
|
262
|
+
])
|
|
263
|
+
# Always possible to ask ANY?
|
|
264
|
+
possibilities.append(KSMove(QA.ASK_ANY))
|
|
265
|
+
# Second add possible pawn captures
|
|
266
|
+
possibilities.extend(self._generate_posible_pawn_captures())
|
|
267
|
+
# And remove finally — remove duplicates
|
|
268
|
+
self._possible_to_ask = list(set(possibilities))
|
|
269
|
+
|
|
270
|
+
@property
|
|
271
|
+
def possible_to_ask(self):
|
|
272
|
+
return self._possible_to_ask
|
|
273
|
+
|
|
274
|
+
@property
|
|
275
|
+
def game_over(self):
|
|
276
|
+
return self._game_over
|
|
277
|
+
|
|
278
|
+
@property
|
|
279
|
+
def must_use_pawns(self):
|
|
280
|
+
return self._must_use_pawns
|
|
281
|
+
|
|
282
|
+
@property
|
|
283
|
+
def turn(self):
|
|
284
|
+
return self._board.turn
|
|
285
|
+
|
|
286
|
+
def is_possible_to_ask(self, move):
|
|
287
|
+
return move in self.possible_to_ask
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
import enum
|
|
4
|
+
|
|
5
|
+
import chess
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@enum.unique
|
|
9
|
+
class QuestionAnnouncement(enum.Enum):
|
|
10
|
+
COMMON = 1
|
|
11
|
+
ASK_ANY = 2
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class KriegspielMove(object):
|
|
15
|
+
'''docstring for KriegdpielMove'''
|
|
16
|
+
def __init__(self, question_type, chess_move=None):
|
|
17
|
+
super(KriegspielMove, self).__init__()
|
|
18
|
+
# Validation
|
|
19
|
+
if not isinstance(question_type, QuestionAnnouncement):
|
|
20
|
+
raise TypeError
|
|
21
|
+
if (question_type == QuestionAnnouncement.COMMON and
|
|
22
|
+
not isinstance(chess_move, chess.Move)):
|
|
23
|
+
raise TypeError
|
|
24
|
+
self.question_type = question_type
|
|
25
|
+
self.chess_move = chess_move
|
|
26
|
+
|
|
27
|
+
def __str__(self):
|
|
28
|
+
return '<KriegspielMove: {QA}, move={chess_move}>'.format(
|
|
29
|
+
QA=self.question_type,
|
|
30
|
+
chess_move=self.chess_move
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
def __repr__(self):
|
|
34
|
+
return self.__str__()
|
|
35
|
+
|
|
36
|
+
def __eq__(self, other):
|
|
37
|
+
if not isinstance(other, KriegspielMove):
|
|
38
|
+
return False
|
|
39
|
+
if (self.chess_move == other.chess_move and
|
|
40
|
+
self.question_type == other.question_type):
|
|
41
|
+
return True
|
|
42
|
+
else:
|
|
43
|
+
return False
|
|
44
|
+
|
|
45
|
+
def __ne__(self, other):
|
|
46
|
+
return not self.__eq__(other)
|
|
47
|
+
|
|
48
|
+
def __lt__(self, other):
|
|
49
|
+
if self.question_type.value < other.question_type.value:
|
|
50
|
+
return True
|
|
51
|
+
if self.question_type.value == other.question_type.value:
|
|
52
|
+
if self.chess_move.uci() < other.chess_move.uci():
|
|
53
|
+
return True
|
|
54
|
+
return False
|
|
55
|
+
|
|
56
|
+
def __hash__(self):
|
|
57
|
+
return hash(self.__str__())
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@enum.unique
|
|
61
|
+
class MainAnnouncement(enum.Enum):
|
|
62
|
+
'''docstring for MoveAnnouncement'''
|
|
63
|
+
IMPOSSIBLE_TO_ASK = 0 #enum.auto()
|
|
64
|
+
ILLEGAL_MOVE = 1 #enum.auto()
|
|
65
|
+
REGULAR_MOVE = 2 #enum.auto()
|
|
66
|
+
CAPTURE_DONE = 3 #enum.auto()
|
|
67
|
+
|
|
68
|
+
HAS_ANY = 4 #enum.auto()
|
|
69
|
+
NO_ANY = 5 #enum.auto()
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
MOVE_DONE = [
|
|
73
|
+
MainAnnouncement.REGULAR_MOVE,
|
|
74
|
+
MainAnnouncement.CAPTURE_DONE
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@enum.unique
|
|
79
|
+
class SpecialCaseAnnouncement(enum.Enum):
|
|
80
|
+
'''docstring for SpecialCaseAnnouncement'''
|
|
81
|
+
NONE = -1
|
|
82
|
+
|
|
83
|
+
DRAW_TOOMANYREVERSIBLEMOVES = 1 #enum.auto()
|
|
84
|
+
DRAW_STALEMATE = 2 #enum.auto()
|
|
85
|
+
DRAW_INSUFFICIENT = 3 #enum.auto()
|
|
86
|
+
|
|
87
|
+
CHECKMATE_WHITE_WINS = 4 #enum.auto()
|
|
88
|
+
CHECKMATE_BLACK_WINS = 5 #enum.auto()
|
|
89
|
+
|
|
90
|
+
CHECK_RANK = 6 #enum.auto()
|
|
91
|
+
CHECK_FILE = 7 #enum.auto()
|
|
92
|
+
CHECK_LONG_DIAGONAL = 8 #enum.auto()
|
|
93
|
+
CHECK_SHORT_DIAGONAL = 9 #enum.auto()
|
|
94
|
+
CHECK_KNIGHT = 10 #enum.auto()
|
|
95
|
+
CHECK_DOUBLE = 11 #enum.auto()
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class KriegspielAnswer(object):
|
|
99
|
+
'''docstring for KriegdpielMove'''
|
|
100
|
+
def __init__(self, main_announcement, **kwargs):
|
|
101
|
+
super(KriegspielAnswer, self).__init__()
|
|
102
|
+
# Validation
|
|
103
|
+
if not isinstance(main_announcement, MainAnnouncement):
|
|
104
|
+
raise TypeError
|
|
105
|
+
|
|
106
|
+
self._main_announcement = main_announcement
|
|
107
|
+
self._capture_at_square = None
|
|
108
|
+
self._special_announcement = SpecialCaseAnnouncement.NONE
|
|
109
|
+
self._move_done = False
|
|
110
|
+
self._check_1 = None
|
|
111
|
+
self._check_2 = None
|
|
112
|
+
|
|
113
|
+
if main_announcement == MainAnnouncement.CAPTURE_DONE:
|
|
114
|
+
if not isinstance(kwargs.get('capture_at_square'), int):
|
|
115
|
+
raise TypeError
|
|
116
|
+
self._capture_at_square = kwargs['capture_at_square']
|
|
117
|
+
|
|
118
|
+
if 'special_announcement' in kwargs:
|
|
119
|
+
sca = kwargs['special_announcement']
|
|
120
|
+
if isinstance(sca, SpecialCaseAnnouncement):
|
|
121
|
+
self._special_announcement = sca
|
|
122
|
+
elif isinstance(sca, tuple):
|
|
123
|
+
if sca[0] == SpecialCaseAnnouncement.CHECK_DOUBLE:
|
|
124
|
+
self._special_announcement = SpecialCaseAnnouncement.CHECK_DOUBLE
|
|
125
|
+
for check in sca[1]:
|
|
126
|
+
if not isinstance(check, SpecialCaseAnnouncement):
|
|
127
|
+
raise TypeError
|
|
128
|
+
self._check_1 = sca[1][0]
|
|
129
|
+
self._check_2 = sca[1][1]
|
|
130
|
+
else:
|
|
131
|
+
raise TypeError
|
|
132
|
+
else:
|
|
133
|
+
raise TypeError
|
|
134
|
+
|
|
135
|
+
if self._main_announcement in MOVE_DONE:
|
|
136
|
+
self._move_done = True
|
|
137
|
+
|
|
138
|
+
@property
|
|
139
|
+
def main_announcement(self):
|
|
140
|
+
return self._main_announcement
|
|
141
|
+
|
|
142
|
+
@property
|
|
143
|
+
def capture_at_square(self):
|
|
144
|
+
return self._capture_at_square
|
|
145
|
+
|
|
146
|
+
@property
|
|
147
|
+
def special_announcement(self):
|
|
148
|
+
return self._special_announcement
|
|
149
|
+
|
|
150
|
+
@property
|
|
151
|
+
def move_done(self):
|
|
152
|
+
return self._move_done
|
|
153
|
+
|
|
154
|
+
@property
|
|
155
|
+
def check_1(self):
|
|
156
|
+
return self._check_1
|
|
157
|
+
|
|
158
|
+
@property
|
|
159
|
+
def check_2(self):
|
|
160
|
+
return self._check_2
|
|
161
|
+
|
|
162
|
+
def __str__(self):
|
|
163
|
+
capture_at = None
|
|
164
|
+
if isinstance(self._capture_at_square, int):
|
|
165
|
+
capture_at = chess.SQUARE_NAMES[self._capture_at_square]
|
|
166
|
+
|
|
167
|
+
main_data = [
|
|
168
|
+
'capture_at={CA}'.format(CA=capture_at),
|
|
169
|
+
'special_case={SC}'.format(SC=self._special_announcement)
|
|
170
|
+
]
|
|
171
|
+
|
|
172
|
+
if self._check_1 is not None or self._check_2 is not None:
|
|
173
|
+
extra_data = 'check_1={c1}, check_2={c2}'.format(
|
|
174
|
+
c1=self._check_1,
|
|
175
|
+
c2=self._check_2
|
|
176
|
+
)
|
|
177
|
+
main_data.append(extra_data)
|
|
178
|
+
|
|
179
|
+
main_data = ', '.join(main_data)
|
|
180
|
+
return '<KriegspielAnswer: {MA}, {data}>'.format(
|
|
181
|
+
MA=self._main_announcement,
|
|
182
|
+
data=main_data
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
def __repr__(self):
|
|
186
|
+
return self.__str__()
|
|
187
|
+
|
|
188
|
+
def __eq__(self, other):
|
|
189
|
+
if not isinstance(other, KriegspielAnswer):
|
|
190
|
+
return False
|
|
191
|
+
if (self._main_announcement == other._main_announcement and
|
|
192
|
+
self._capture_at_square == other._capture_at_square and
|
|
193
|
+
self._special_announcement == other._special_announcement):
|
|
194
|
+
return True
|
|
195
|
+
else:
|
|
196
|
+
return False
|
|
197
|
+
|
|
198
|
+
def __ne__(self, other):
|
|
199
|
+
return not self.__eq__(other)
|
|
200
|
+
|
|
201
|
+
def __lt__(self, other):
|
|
202
|
+
if self._main_announcement.value < other._main_announcement.value:
|
|
203
|
+
return True
|
|
204
|
+
if self._main_announcement.value == other._main_announcement.value:
|
|
205
|
+
if self._capture_at_square < other._capture_at_square:
|
|
206
|
+
return True
|
|
207
|
+
if self._capture_at_square == other._capture_at_square:
|
|
208
|
+
if self._special_announcement.value < other._special_announcement.value:
|
|
209
|
+
return True
|
|
210
|
+
return False
|
|
211
|
+
|
|
212
|
+
def __hash__(self):
|
|
213
|
+
return hash(self.__str__())
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: kriegspiel
|
|
3
|
+
Version: 0.9.0b1
|
|
4
|
+
Summary: Python Kriegspiel Game
|
|
5
|
+
Home-page: https://github.com/Kriegspiel/ks-game
|
|
6
|
+
Author: Alexander Filatov
|
|
7
|
+
Author-email: alexander@kriegspiel.org
|
|
8
|
+
License: MIT
|
|
9
|
+
Keywords: kriegspiel chess variant
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Topic :: Games/Entertainment
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.5
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: python-chess[uci]
|
|
18
|
+
Dynamic: author
|
|
19
|
+
Dynamic: author-email
|
|
20
|
+
Dynamic: classifier
|
|
21
|
+
Dynamic: description
|
|
22
|
+
Dynamic: home-page
|
|
23
|
+
Dynamic: keywords
|
|
24
|
+
Dynamic: license
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
Dynamic: requires-dist
|
|
27
|
+
Dynamic: summary
|
|
28
|
+
|
|
29
|
+
# Python Kriegspiel Game
|
|
30
|
+
|
|
31
|
+
Supported Kriegspiel rules:
|
|
32
|
+
|
|
33
|
+
1. Berkeley + Public Any
|
|
34
|
+
|
|
35
|
+
In plan:
|
|
36
|
+
|
|
37
|
+
1. Pure Berkeley
|
|
38
|
+
2. Crazykrieg
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
### How-to
|
|
42
|
+
|
|
43
|
+
Run tests
|
|
44
|
+
``` bash
|
|
45
|
+
# clone
|
|
46
|
+
git clone https://github.com/Kriegspiel/ks-game.git
|
|
47
|
+
# install dependencies
|
|
48
|
+
cd ks-game && pip install -r requirements.txt
|
|
49
|
+
# run tests
|
|
50
|
+
python -m pytest tests/
|
|
51
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
setup.py
|
|
4
|
+
kriegspiel/__init__.py
|
|
5
|
+
kriegspiel/berkeley.py
|
|
6
|
+
kriegspiel/move.py
|
|
7
|
+
kriegspiel.egg-info/PKG-INFO
|
|
8
|
+
kriegspiel.egg-info/SOURCES.txt
|
|
9
|
+
kriegspiel.egg-info/dependency_links.txt
|
|
10
|
+
kriegspiel.egg-info/requires.txt
|
|
11
|
+
kriegspiel.egg-info/top_level.txt
|
|
12
|
+
tests/test_berkeley.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
python-chess[uci]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
kriegspiel
|