-
Notifications
You must be signed in to change notification settings - Fork 7
/
Player.py
153 lines (126 loc) · 4.5 KB
/
Player.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
This module contains functions relating to user interaction,
including automated players.
"""
import random
import Canvas
import utils
import board
import utils
import globVar
import copy
import time
def turn():
utils.potenial_moves()
Canvas.drawBoard()
if (globVar.noPlayers or globVar.numPlayers == 0):
ai_turn()
elif (globVar.numPlayers == 1 and globVar.player == "b"):
ai_turn()
elif (globVar.numPlayers == 2 or globVar.player == "W" and
globVar.numPlayers == 1):
human_turn()
globVar.removed = False
def ai_turn():
runagain = True
if globVar.numPlayers == 1 or globVar.slow_speed:
time.sleep(0.5)
while runagain:
utils.clearAllOptions()
if globVar.player == "W":
rand_pc = random.choice(globVar.w_pieces)
else:
rand_pc = random.choice(globVar.b_pieces)
fromSqr = board.Grid(rand_pc.row, rand_pc.col)
fpc = copy.deepcopy(fromSqr.piece)
availMoves = fromSqr.piece.scan()
globVar.r_avail = copy.deepcopy(availMoves)
globVar.r_avail_Num = len(globVar.r_avail)
availMoves = utils.mark_invalid_moves(availMoves, fromSqr.piece)
runagain = not utils.hasMoves(availMoves)
if runagain:
board.Grid(fromSqr.row, fromSqr.col).piece.selected = False
am = fromSqr.piece.scan()
utils.resetAvailMoves(am)
Canvas.drawBoard()
utils.un_resetAvailMoves(availMoves)
choice = randChoose(availMoves)
utils.record_user_move(fpc, availMoves, choice)
pc = utils.move(fromSqr, availMoves, choice)
# Check for check
utils.check_king()
# revert back if still in check
if ((globVar.w_check and globVar.player == "W") or
(globVar.b_check and globVar.player == "b")):
utils.undo_user_move()
ai_turn()
utils.check_pawn(pc)
def human_turn():
runagain = True
while runagain:
utils.clearAllOptions()
fromSqr = select()
fpc = copy.deepcopy(fromSqr.piece)
availMoves = fromSqr.piece.scan()
globVar.r_avail = copy.deepcopy(availMoves)
globVar.r_avail_Num = len(globVar.r_avail)
# remove invalid moves
availMoves = utils.mark_invalid_moves(availMoves, fromSqr.piece)
runagain = not utils.hasMoves(availMoves)
if runagain:
board.Grid(fromSqr.row, fromSqr.col).piece.selected = False
am = fromSqr.piece.scan()
utils.resetAvailMoves(am)
Canvas.chooseAvailableMessage()
Canvas.drawBoard()
utils.un_resetAvailMoves(availMoves)
choice = choose(availMoves)
utils.record_user_move(fpc, availMoves, choice)
pc = utils.move(fromSqr, availMoves, choice)
# Check for check
utils.check_king()
# revert back if still in check
if ((globVar.w_check and globVar.player == "W") or
(globVar.b_check and globVar.player == "b")):
Canvas.getouttacheckMessage()
utils.undo_user_move()
# utils.undoMove()
human_turn()
utils.check_pawn(pc)
def select():
print(" Select which piece to move.")
selecting = True
while selecting:
col = utils.r_c('c')
row = utils.r_c('r')
if board.Grid(row,col).piece.color != globVar.player:
Canvas.selectError()
else:
board.Grid(row,col).piece.selected = True # display fromSqr as selected
selecting = False
return board.Grid(row,col)
def choose(availMoves):
Canvas.drawBoard()
choice = Canvas.chooseMove(len(availMoves))
return choice
def randChoose(availMoves):
if globVar.numPlayers == 1 or globVar.slow_speed:
Canvas.drawBoard()
time.sleep(0.5)
if globVar.aggressive:
collisions = []
for p in availMoves:
if p.pieceStatus:
collisions.append(p)
if collisions:
return collisions[(random.randint(0, 100) % len(collisions))].option
if globVar.chill:
no_collisions = []
for p in availMoves:
if not p.pieceStatus:
no_collisions.append(p)
if no_collisions:
return no_collisions[(random.randint(0, 100) % len(no_collisions))].option
# if there are no pieces that fit the criteria
# or if the user is playing on Normal, return a random choice
return random.randint(0, 100) % len(availMoves)