-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathengine.py
More file actions
106 lines (88 loc) · 3.39 KB
/
engine.py
File metadata and controls
106 lines (88 loc) · 3.39 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
#!/usr/bin/env python3
import piece
from exceptions import *
class Game:
def __init__(self, player_color="white", netinfo=None):
"""
player_color may be "white" or "black".
netinfo is the (ip, port) of the remote server, or None for local game.
"""
self.player1_color = player_color
self.player2_color = "white" if player_color == "black" else "black"
self.board = [[None] * 8] * 8 # board[column][line]
self.turn = "white"
self.finished = False
self.netinfo = netinfo
self.init_board()
def play(self):
"""
Plays a turn of the game, returns None.
If the game is finished, returns the color of the winner.
"""
valid_move = False
while not valid_move:
move = get_move()
origin = move[0]
piece = self.at(*origin)
valid_move = piece.valid(move) and piece.color == self.turn
self.move((origin, destination))
self.turn = "white" if self.turn == "black" else "black"
if self.finished:
if self.turn == self.player1_color:
return self.player1_color
else:
return self.player2_color
return None
def init_board(self):
"""
Initializes the board with the classical chess starting position
"""
self.board[0][0] = piece.Rook("white")
self.board[1][0] = piece.Knight("white")
self.board[2][0] = piece.Bishop("white")
self.board[3][0] = piece.Queen("white")
self.board[4][0] = piece.King("white")
self.board[5][0] = piece.Bishop("white")
self.board[6][0] = piece.Knight("white")
self.board[7][0] = piece.Rook("white")
self.board[0][7] = piece.Rook("black")
self.board[1][7] = piece.Knight("black")
self.board[2][7] = piece.Bishop("black")
self.board[3][7] = piece.Queen("black")
self.board[4][7] = piece.King("black")
self.board[5][7] = piece.Bishop("black")
self.board[6][7] = piece.Knight("black")
self.board[7][7] = piece.Rook("black")
for i in range(8):
self.board[i][1] = piece.Pawn("white")
self.board[i][1] = piece.Pawn("black")
def get_move(self):
"""
Returns a move = (origin, destination).
This is only a debug implementation and may be overriden by the client.
"""
o_column = input("column: ")
o_line = input("line: ")
d_column = input("column: ")
d_line = input("line: ")
return ((o_column-1, o_line-1),
(d_column-1, d_line-1))
def at(column, line):
"""
Returns the piece at given position.
This function interfaces with position tuples nicer than
the array access.
"""
return self.board[column][line]
def move(origin, destination):
"""
Moves a piece from origin to destination
Raises a InvalidMoveException if no piece is present at the given
location
"""
opiece = self.at(*origin)
if (not opiece.is_move_valid(origin, destination)
or opiece.is_ally(destination)):
raise InvalidMoveException
self.board[destination[0], destination[1]] = self.at(*origin)
self.board[origin[0], orign[1]] = self.at(*origin)