-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectfour_basics.py
158 lines (108 loc) · 3.75 KB
/
connectfour_basics.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
154
155
156
157
158
#Sue Ji 33337876
import connectfour
def printboard(board:[[int]])->None:
'''
prints the visual board
'''
for num in range (1,connectfour.BOARD_COLUMNS+1):
if num==connectfour.BOARD_COLUMNS:
print(num)
else:
print(num,end=' ')
for row in range(connectfour.BOARD_ROWS):
tile=''
for col in range(connectfour.BOARD_COLUMNS):
if board[col][row]==1:
tile+='R '
if board[col][row]==2:
tile+='Y '
if board[col][row]==connectfour.NONE:
tile+='. '
print(tile[:-1])
def _poppable(gamestate:tuple)->list:
'''
takes the gamestate and checks if it is empty tile at the bottom
returns a list of all the non-empty tiles at the bottom
'''
valid=[]
for col in range(len(gamestate.board)):
if gamestate.board[col][-1]==gamestate.turn:
valid.append(col)
return valid
def column_command()->int:
'''
takes and returns the column number user wants to drop or pop
'''
col=None
while col==None:
try:
col=int(input('Please input the column number\
you want to move your disc\n'))-1
if col<0 or col>6:
print('Please enter a valid number\n')
col=None
else:
return col
except ValueError:
print('Please enter a valid number\n')
col=None
return col
def pop_or_drop()->str:
'''
asks the user to pop or drop and returns pop or drop
'''
request=False
while request==False:
command=input('Would you like to pop or drop your disc?\n')
if command.lower()=='drop':
request=True
return 'DROP'
if command.lower()=='pop':
request=True
return 'POP'
else:
print('Please type "pop" or "drop"')
request=False
def update_gamestate(gamestate:connectfour.GameState,move:str)->"Connectfour.GameState":
'''
while playing the game and the exception is not raised after proceeding one move,
update the board
'''
if move.startswith('DROP'):
move_col=int(move[5])
gamestate=connectfour.drop(gamestate,move_col)
else:
move_col=int(move[4])
gamestate=connectfour.pop(gamestate,move_col)
return gamestate
def get_valid_move(gamestate:connectfour.GameState)->str:
'''
asks the player to input a move and test if it is a valid move,
if yes, return the move type and col as one str
'''
while True:
try:
move_type=pop_or_drop()
move_col=column_command()
if move_type=='DROP':
gamestate=connectfour.drop(gamestate,move_col)
else:
gamestate=connectfour.pop(gamestate,move_col)
if gamestate!=None:
return '{} {}'.format(move_type,move_col)
except KeyboardInterrupt:
quit()
except:
print('Invalid move. Please try again!')
def print_move(move:str)->str:
'''
get the move col and add 1 onto it before sending the
move to the server or print out in the interface,
since the server reads on 1-base and the move right now is 0-based
'''
if move.startswith('D'):
col=int(move[5])+1
return "{}{}".format(move[:5],col)
else:
col=int(move[4])+1
return "{}{}".format(move[:4],col)