-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
130 lines (107 loc) · 3.77 KB
/
game.py
File metadata and controls
130 lines (107 loc) · 3.77 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
#!/usr/bin/python
import os
import sys
import constants
from board import Board
import board_analyzer
class Game(object):
def __init__(self):
"""
Initializes new game.
"""
#Create new game board
self._board = Board()
#Record whose turn it is
self._currentPlayer = constants.WHITE_PLAYER
#Print welcome text
os.system('clear')
welcome = "Welcome to Solidarity Bros. Chess!\n" \
"by Chris Wang, Dmitriy Chukhin, and Jim Ladd\n" \
"\n" \
"To move a piece, give the starting and ending coordinates.\n" \
"To move the white knight for example, type 'b1c3'.\n" \
"To quit, type 'quit'.\n\n" \
"Enjoy!\n\n"
print welcome
def play(self):
"""
Main game loop.
"""
#Print initial board
self._board.printBoard()
while True:
self._nextTurn()
def _nextTurn(self):
"""
Contains logic for executing a player's turn.
Exits when game is finished.
"""
#Get next move from player
move = self._getPlayersNextMove()
move = self._board._moveConverter(move)
while self._board.isLegalMove(self._currentPlayer, move) != True:
move = self._getPlayersNextMove()
move = self._board._moveConverter(move)
#Executes move
self._board.movePiece(self._currentPlayer, move)
#Prints board
os.system('clear')
self._board.printBoard()
#Creates variable other_player
if self._currentPlayer == constants.WHITE_PLAYER:
self._otherPlayer = constants.BLACK_PLAYER
else:
self._otherPlayer = constants.WHITE_PLAYER
#End game conditions
if board_analyzer.isCheckMate(self._board, self._otherPlayer) == True:
print "Player",self._currentPlayer,"has won the game!"
choice = None
while choice != 'quit':
choice = raw_input("Type 'quit' to exit. ")
else:
sys.exit(0)
#Switches players
if self._currentPlayer == constants.WHITE_PLAYER:
self._currentPlayer = constants.BLACK_PLAYER
else:
self._currentPlayer = constants.WHITE_PLAYER
def _getPlayersNextMove(self):
"""
Retrieves player's next move. (e.g. "b1d4").
Ensures that move is well-formed.
@return: Player's next move (e.g. "b1d4")
"""
while True:
playerColor = ""
if self._currentPlayer == constants.WHITE_PLAYER:
playerColor = "White"
else:
playerColor = "Black"
prompt = "%s> " % playerColor
response = raw_input(prompt)
#Check response length
if len(response) != 4:
self._printHelp()
continue
#Does user want to quit?
if response.lower() == 'quit':
sys.exit(0)
#Check row/column values
validColumns = "abcdefgh"
validRows = "12345678"
col1 = response[0]
row1 = response[1]
col2 = response[2]
row2 = response[3]
if (col1 not in validColumns) or \
(row1 not in validRows) or \
(col2 not in validColumns) or \
(row2 not in validRows):
self._printHelp()
continue
return response
def _printHelp(self):
helpText = "\n Type the starting and ending coordinates.\n" \
" For example, 'b1c3'.\n\n" \
" Type 'quit' to quit.\n"
print helpText