-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFour_in_a_Row.py
More file actions
112 lines (102 loc) · 3.9 KB
/
Copy pathFour_in_a_Row.py
File metadata and controls
112 lines (102 loc) · 3.9 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
import sys
PLAYER_X = 'X'
PLAYER_O = 'O'
BOARD_WIDTH = 7
BOARD_HEIGHT = 6
COLUMN_LABELS = ('1','2','3','4','5','6','7')
assert len(COLUMN_LABELS) == BOARD_WIDTH
def main():
print('''
Two players take turns dropping tiles into one of seven columns, trying to make four in a row horizontally,vertically, or diagonally.
''')
gameBoard = getNewBoard()
playerTurn = PLAYER_X
while True:
displayBoard(gameBoard)
playerMove = askForPlayerMove(playerTurn,gameBoard)
gameBoard[playerMove] = playerTurn
if isWinner(playerTurn,gameBoard):
displayBoard(gameBoard)
print('Player ' + playerTurn + ' has won!')
sys.exit90
elif isFull(gameBoard):
displayBoard(gameBoard)
print('There is a tie!')
sys.exit()
if playerTurn == PLAYER_X:
playerTurn = PLAYER_O
elif playerTurn == PLAYER_O:
playerTurn = PLAYER_X
def getNewBoard():
board = {}
for columnIndex in range(BOARD_WIDTH):
for rowIndex in range(BOARD_HEIGHT):
board[(columnIndex,rowIndex)] = EMPTY_SPACE
return board
def displayBoard(board):
titleChars = []
for rowIndex in range(BOARD_HEIGHT):
for columnIndex in range(BOARD_WIDTH):
tilesChars.append(board[(columnIndex,rowIndex)])
print("""
1234567
+------+
|{}{}{}{}{}{}{}{}|
""".format(*tileChars))
def askForPlayerMove(playerTile,board):
while True:
print('Player {}, enter a column or QUIT:'.format(playerTile))
response = input('> ').upper().strip()
if response == 'QUIT':
print('Thanks for playing')
sys.exit()
if response not in COLUMN_LABELS:
print('Enter a number from 1 to {}'.format(BOARD_WIDTH))
continue
columnIndex =int(response)
if board[(columnIndex,0)] != EMPTY_SPACE:
print('That column is full, select another one.')
continue
for rowIndex in range(BOARD_HEIGHT -1,-1,-1):
if board[(columnIndex,rowIndex)] == EMPTY_SPACE:
return (columnIndex,rowIndex)
def isFull(board):
for rowIndex in range(BOARD_HEIGHT):
for columnIndex in range(BOARD_WIDTH):
if board[(columnIndex,rowIndex)] == EMPTY_SPACE:
return False
return True
def isWinner(playerTile,board):
for columnIndex in range(BOARD_WIDTH -3):
for rowIndex in range(BOARD_HEIGHT):
tile1 =board[(columnIndex,rowIndex)]
tile2 =board[(columnIndex +1,rowIndex)]
tile3 =board[(columnIndex +2,rowIndex)]
tile4 =board[(columnIndex +3,rowIndex)]
if tile1 == tile2 == tile3 == tile4 == playerTile:
return True
for columnIndex in range(BOARD_WIDTH):
for rowIndex in range(BOARD_HEIGHT - 3):
tile1 = board[(columnIndex, rowIndex)]
tile2 = board[(columnIndex, rowIndex + 1)]
tile3 = board[(columnIndex, rowIndex + 2)]
tile4 = board[(columnIndex, rowIndex + 3)]
if tile1 == tile2 == tile3 == tile4 == playerTile:
return True
for columnIndex in range(BOARD_WIDTH - 3):
for rowIndex in range(BOARD_HEIGHT - 3):
tile1 = board[(columnIndex,rowIndex)]
tile2 = board[(columnIndex +1,rowIndex + 1)]
tile3 = board[(columnIndex +2,rowIndex + 2)]
tile4 = board[(columnIndex +3,rowIndex + 3)]
if tile1 == tile2 == tile3 == tile4 == playerTile:
return True
tile1 =board[(columnIndex +3,rowIndex)]
tile2 = board[(columnIndex +2,rowIndex + 1)]
tile3 = board[(columnIndex +1,rowIndex + 2)]
tile4 = board[(columnIndex,rowIndex +3)]
if tile1 == tile2 == tile3 == tile4 == playerTile:
return True
return False
if __name__ == '__main__':
main()