-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
95 lines (83 loc) · 2.98 KB
/
game.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
# Connect 4
# Player 1 is 'X', player 2 is 'O'
from board import Board
def get_difficulty_input():
# gets the difficulty from the user and returns as an integer
proper_input = False
user_input = input(f'Choose your difficulty level (between 1 and 5): ')
while not proper_input:
try:
user_input = int(user_input)
if user_input < 0 or user_input > 5: # 0, -1 for testing
user_input = input("Difficulty must be an integer between 1 and 5: ")
proper_input = False
break
proper_input = True
except ValueError:
user_input = input("Difficulty must be an integer between 1 and 5: ")
return user_input
def get_input():
# get the slot number from the user and return as an integer
proper_input = False
user_input = input(f'Select a column: ')
while not proper_input:
try:
user_input = int(user_input)
if user_input < -1 or user_input > 7: # 0 and -1 are allowed for debugging purposes. (in make_test_board)
user_input = input("Input must be an integer between 1 and 7: ")
proper_input = False
break
proper_input = True
except ValueError:
user_input = input("Input must be an integer between 1 and 7: ")
return user_input
def play_human():
# to play against your friends
board = Board()
player = 2
while not board.check_for_win():
if player == 2:
player = 1
else:
player = 2
board.printBoard()
user_input = get_input(player)
board.place(user_input-1, player)
board.printBoard()
print(f'Congratulations Player {player}, you win!')
def play_computer(difficulty):
# to play against the AI
board = Board()
player = 1
while not board.check_for_win()[0]:
# display board, allow user to choose a column, show new board to user
board.printBoard()
user_input = get_input()
board.place(user_input-1, player)
board.printBoard()
# get minimax selection from computer
board = board.minimax(difficulty, True)[1]
board.printBoard()
if board.check_for_win()[1] == 'O':
print('The AI wins!')
else:
print(f'Congratulations! You beat the AI at difficulty level {difficulty}')
def make_test_board():
# used for debugging purposes. Allows me to make a board using the CLI.
board = Board()
user_input = -10
player = 1
while not board.check_for_win()[0]:
board.printBoard()
if user_input == -1:
player = 2 if player == 1 else 1
user_input = get_input()
if user_input != -1 and user_input != 0:
board.place(user_input-1, player)
board.printBoard()
return board
def main():
difficulty = get_difficulty_input()
play_computer(difficulty)
if __name__ == "__main__":
main()