Chessboard¶
This is what the chessboard looks like:
* 1 2 3 4 5 6 7 8 9 A
1|X| | | | | | | | | |
2| |O| | | | | | | | |
3| | |X| | | | | | | |
4| | | |O| | | | | | |
5| | | | |X| | | | | |
6| | | | | |O| | | | |
7| | | | | | |X| | | |
8| | | | | | | |O| | |
9| | | | | | | | |X| |
A| | | | | | | | | |O|
Init¶
The Chessboard class
chessboard.Chessboard(
board_size=3,
win=3,
ch_off='O',
ch_def='X',
ch_blank=' ',
user_number=2,
game_name=None,
pos=None,
nested=False
)
board_sizedefines the size of the chessboardwindefines the number of chess pieces to win in a linech_offdefines the character of offensive playerch_defdefines the character of defensive playerch_blackdefines the character of default placeuser_numberdefines the number of players (No use)game_namedefines the built-in game_name (default None)
Instance Methods¶
Some methods to operate the chessboard is listed
self.set_pos(pos, check=False)
posare the coordinates of chess.checkwhether to check winner after this step- return
Trueif the current user wins, else, return the current coordinates
self.print_pos(coordinates=None, pos=None)
- Print the chessboard, if
posis given, printpos, else, printself.pos coordinatesis alistof coordinates which will be printed in specific color.
self.rotate_board(angle, unit='radian')
- Rotate the chessboard anticlockwise for
angledegree/radian (based onunit), using the center of the chessboard as the center of rotation, e.g.,
* 1 2 3
1|O|X| |
2| | | |
3| | | |
becomes
* 1 2 3
1| | |O|
2| | |X|
3| | | |
when call self.rotate_board(270, 'angle')
self.handle_input(input_str, check=False, place=True)
- Handle the input of user, can be coordinates or commands.
input_strThe input string.checkWhether to check winner.placeWhether to place a chess or only process the input- return same as
self.set_posifplaceisTrue, else, return the current coordinates only.
self.validate_pos(pos)
- Validate the coordinates.
posshould be in form(x, y)
self.validate_input(input_str, val_pos=True)
Validate the user input.
input_str, valid user input isx, yu, 1x(only for game fourinarow)
(
xandyare the one-letter coordinates)
val_posindicate whether to validate the coodinates
self.undo(times=1)
- Undo
timesUndo times, default 1
An example: comgames¶
Installation¶
pip install comgames
Usage¶
comgames
- Several kinds of board games are built-in.
- fourinarow
- Gomoku
- tictactoe
- Reversi
- normal
- When normal, players are asked to input the size of the board and the number of winnings. Max size: 61 Max winning: < size
fourinarow¶
* 1 2 3 4 5 6 7
1| | | | | | | |
2| | | | | | | |
3| | | | | | | |
4| | | |O| | | |
5| | |O|X| | | |
6| |O|X|O| | | |
7|O|X|X|O|X| | |
Gomoku¶
* 1 2 3 4 5 6 7 8 9 A B C D E F
1| | | | | | | | | | | | | | | |
2| | | | | | | | | | | | | | | |
3| | | | | | | | | | | | | | | |
4| | | | | | | | | | | | | | | |
5| | | | | | | | | | | | | | | |
6| | | | | | | | | | | | | | | |
7| | | | | | |O| | | | | | | | |
8| | | | | | |X|O| | | | | | | |
9| | | | | | | |X|O| | | | | | |
A| | | | | | | | | |O|X|X| | | |
B| | | | | | | | | | |O| | | | |
C| | | | | | | | | | | | | | | |
D| | | | | | | | | | | | | | | |
E| | | | | | | | | | | | | | | |
F| | | | | | | | | | | | | | | |
tictactoe¶
* 1 2 3
1|O|X|O|
2|X|O|X|
3|X|O|O|
Reversi¶
* 1 2 3 4 5 6 7 8
1| | | | | | | | |
2| | | | | | | | |
3| | | | | | | | |
4| | | |O|X| | | |
5| | | |X|O| | | |
6| | | | | | | | |
7| | | | | | | | |
8| | | | | | | | |