-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuser.py
More file actions
127 lines (94 loc) · 3.43 KB
/
user.py
File metadata and controls
127 lines (94 loc) · 3.43 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
import move
def indexToCoords(x, y):
return(chr(x+97), 8-y)
def coordsToIndex(x, y):
return(ord(x)-97, 8-y)
class util:
clear = "\x1b[2J"
reset_cursor = "\x1b[H"
red_text = "\x1b[31m"
reset = "\x1b[0m"
class User():
def __init__(self, manager):
self.manager = manager
self.board = manager.getBoard()
self.error = ""
def verifyCoordinateInput(self, inputCoords):
if(len(inputCoords) != 2):
self.error = "Input coordinates must be two characters long"
return False
if(not inputCoords[1].isnumeric()):
self.error = "The second character must be a digit from 1-7"
return False
coords = coordsToIndex(inputCoords[0], int(inputCoords[1]))
if(coords[1] < 0 or coords[1] >= 8):
self.error = "The second character must be a digit from 1-7"
return False
if(coords[0] < 0 or coords[0] >= 8):
self.error = "The first character must be a lowercase letter a - h"
return False
return coords
def resetWithMessage(self, message, moves, selected=None):
print(util.clear + util.reset_cursor)
self.board.print(moves, selected)
if(self.manager.status == "ongoing"):
if(self.manager.isInCheck(self.manager.getColorTurn())):
turn = "White" if self.manager.getColorTurn() == 'w' else "Black"
print(util.red_text + turn + " is in check" + util.reset)
print(message)
def getMove(self):
while True:
color = self.manager.getColorTurn()
if(color == "w"):
turn = "white"
else:
turn = "black"
self.resetWithMessage(self.error, set())
inputCoords = input("It is " + turn + "'s turn, please enter a move in form of 'a1', 'f3', etc. ")
coords = self.verifyCoordinateInput(inputCoords)
if(coords == False):
continue
piece = self.board.getPiece(coords)
if(piece.getColor() != self.manager.getColorTurn()):
self.error = "You do not control the chosen piece"
continue
moves = piece.getLegalMoves(self.manager)
self.resetWithMessage("Piece " + inputCoords + " selected", moves, coords)
inputCoords = input("Select a destination: ")
destination = self.verifyCoordinateInput(inputCoords)
if(destination == False):
continue
legal = False
special = None
for obj in moves:
if(obj.toPos == destination):
legal = True
if(obj.special != None):
if(obj.special == 'c' or obj.special == 'en'):
special = obj.special
else:
self.resetWithMessage("What would you like to promote your pawn into?\n 1: Queen\n 2: Bishop\n 3: Knight\n 4: Rook", [], coords)
num = input()
if(not num.isnumeric()):
self.error = "Input must be numeric"
break
num = int(num)
if(num > 4 or num <= 0):
self.error = "Input must be 1-4"
break
match(num):
case 1:
special = 'q'
case 2:
special = 'b'
case 3:
special = 'kn'
case 4:
special = 'r'
break
if(legal == False):
self.error = inputCoords + " is not a valid destination"
continue
self.error = ""
finalMove = move.Move(coords, destination, special)
return finalMove