-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect4.py
65 lines (50 loc) · 1.7 KB
/
connect4.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
import connect4board
import sys
PLAYER1_CHARACTER = 'R'
PLAYER2_CHARACTER = 'B'
PLAYER1 = 1
PLAYER2 = 2
INPUT_OFFSET = 1
PLAYER_TURN = 1
board = connect4board.Connect4Board()
TURN_TO_PLAYER = {PLAYER1 : PLAYER1_CHARACTER, PLAYER2 : PLAYER2_CHARACTER}
PROVIDE_VALID_INT_MSG = "Please provide a valid integer between " + str(1) + " and " + str(board.getColumns())
THANKS = "Thanks for playing!"
QUIT = "quit"
print r'Enter "quit" any time to stop the game'
while 1:
board.printBoard()
if board.isWinner(PLAYER1_CHARACTER):
print "Player " + PLAYER1_CHARACTER + " has won!"
break
if board.isWinner(PLAYER2_CHARACTER):
print "Player " + PLAYER2_CHARACTER + " has won!"
break
if board.getSpacesLeft() == 0:
print "No more spaces left, play again"
break
print "It is Player " + str(TURN_TO_PLAYER[PLAYER_TURN]) + " turn"
try:
rawInput = raw_input()
if rawInput == QUIT:
print THANKS
break
rawInput = int(rawInput)
playerInput = board.checkColumnInput(rawInput-INPUT_OFFSET)
except Exception,e:
print PROVIDE_VALID_INT_MSG
continue
if playerInput > board.ERROR:
print "Player " + TURN_TO_PLAYER[PLAYER_TURN] + " picked column " + str(rawInput)
row = board.getAvailableRow(board, playerInput)
if row > board.ERROR:
if PLAYER_TURN == PLAYER1:
board.markBoard(row,playerInput,PLAYER1_CHARACTER)
PLAYER_TURN = PLAYER2
else:
board.markBoard(row,playerInput,PLAYER2_CHARACTER)
PLAYER_TURN = PLAYER1
else:
print "Sorry column " + str(rawInput) + " is full, pick again."
else:
print "Sorry but " + str(rawInput) + " is not between " + str(1) + " and " + str(board.getColumns())