-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChessGUI_text.py
113 lines (90 loc) · 3.34 KB
/
ChessGUI_text.py
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
#! /usr/bin/env python
"""
Project: Python Chess
File name: ChessGUI_text.py
Description: Draws a text based chess board in the console window.
Gets user input through text entry.
Copyright (C) 2009 Steve Osborne, srosborne (at) gmail.com
http://yakinikuman.wordpress.com/
"""
from ChessRules import ChessRules
class ChessGUI_text:
def __init__(self):
self.Rules = ChessRules()
# def GetGameSetupParams(self):
#MOVED FUNCTIONALITY TO CHESSGAMEPARAMS.PY
# player1Name = raw_input("Player 1 name: ")
# player1Color = 'red'
# while not player1Color == 'black' and not player1Color == 'white':
# player1Color = raw_input(" Player 1 color ('white' or 'black'): ")
# if player1Color == 'white':
# player2Color = 'black'
# else:
# player2Color = 'white'
# player1Type = 'monkey'
# while not player1Type == 'human' and not player1Type == 'AI':
# player1Type = raw_input(" Is player 1 'human' or 'AI'? ")
# player2Name = raw_input("Player 2 name: ");
# player2Type = 'monkey'
# while not player2Type == 'human' and not player2Type == 'AI':
# player2Type = raw_input(" Is player 2 'human' or 'AI'? ")
# print "Setting up game..."
# print "Player 1:", player1Name, player1Color, player1Type
# print "Player 2:", player2Name, player2Color, player2Type
# return (player1Name,player1Color,player1Type,player2Name,player2Color,player2Type)
def Draw(self,board):
print " c0 c1 c2 c3 c4 c5 c6 c7 "
print " ----------------------------------------"
for r in range(8):
print "r"+str(r)+"|",
for c in range(8):
if board[r][c] != 'e':
print str(board[r][c]), "|",
else:
print " |",
if c == 7:
print #to get a new line
print " ----------------------------------------"
def EndGame(self,board):
self.Draw(board)
def GetPlayerInput(self,board,color):
fromTuple = self.GetPlayerInput_SquareFrom(board,color)
toTuple = self.GetPlayerInput_SquareTo(board,color,fromTuple)
return (fromTuple,toTuple)
def GetPlayerInput_SquareFrom(self,board,color):
ch = "?"
cmd_r = 0
cmd_c = 0
while (ch not in board[cmd_r][cmd_c] or self.Rules.GetListOfValidMoves(board,color,(cmd_r,cmd_c))==[]):
print "Player", color
cmd_r = int(raw_input(" From row: "))
cmd_c = int(raw_input(" From col: "))
if color == "black":
ch = "b"
else:
ch = "w"
if (board[cmd_r][cmd_c] == 'e'):
print " Nothing there!"
elif (ch not in board[cmd_r][cmd_c]):
print " That's not your piece!"
elif self.Rules.GetListOfValidMoves(board,color,(cmd_r,cmd_c)) == []:
print " No valid moves for that piece!"
return (cmd_r,cmd_c)
def GetPlayerInput_SquareTo(self,board,color,fromTuple):
toTuple = ('x','x')
validMoveList = self.Rules.GetListOfValidMoves(board,color,fromTuple)
print "List of valid moves for piece at",fromTuple,": ", validMoveList
while (not toTuple in validMoveList):
cmd_r = int(raw_input(" To row: "))
cmd_c = int(raw_input(" To col: "))
toTuple = (cmd_r,cmd_c)
if not toTuple in validMoveList:
print " Invalid move!"
return toTuple
def PrintMessage(self,message):
print message
if __name__ == "__main__":
from ChessBoard import ChessBoard
cb = ChessBoard(0)
gui = ChessGUI_text()
gui.Draw(cb.GetState())