-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpuFour.py
126 lines (114 loc) · 3.7 KB
/
cpuFour.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
# cpuFour.py
# Connect Four MiniMax AI
# CS 51 Final Project
# Evan Sandhoefner, Ryan Kerr, Milan Ravenell, Matthew Tesfalul
# run by typing "python cpufour.py" at command line
# import external modules
import time
import sys
# import our scripts
from evaluate import *
from ab_pruning import *
from board_functions import *
# declare global variables
global ai2_turn
global board
# instantiate empty board
board = [["." for y in range(ROWS)] for x in range(COLUMNS)]
# take input originating from user, exit program if input is "q"
def exit_if(user_input):
if user_input == "q":
sys.exit()
# print intro, ask for first player, ask cpu difficulty, print starting board,
# assign to ai2_turn, call move(diff1, diff2)
def init():
global ai2_turn
print ("\nHello! My name is Rondo.\nI'm about to play Connect Four with my"
" friend Carlisle.\nYou can enter 'q' at any prompt if you decide"
" that you don't want to watch.")
first = raw_input("Carlisle will be yellow (Y)."
"\nShould he go first? (y/n):\n").lower()
while (first != "y") & (first != "n") & (first != "q"):
first = raw_input("\nSorry, I don't understand! "
"Please type 'y' or 'n':\n").lower()
exit_if(first)
difficulty1 = raw_input("\nWhat difficulty should Rondo be? "
" Choose a number 1 (easy) - 5 (hard):\n").lower()
while difficulty1 not in ["1","2","3","4","5","q"]:
difficulty1 = raw_input("\nSorry, I don't understand! "
"Please type a number 1-5:\n").lower()
exit_if(difficulty1)
difficulty2 = raw_input("\nWhat difficulty should Carlisle be? "
" Choose a number 1 (easy) - 5 (hard):\n").lower()
exit_if(difficulty1)
while difficulty2 not in ["1","2","3","4","5","q"]:
difficulty2 = raw_input("\nSorry, I don't understand! "
"Please type a number 1-5:\n").lower()
exit_if(difficulty2)
difficulty1 = int(difficulty1)
difficulty2 = int(difficulty2)
print '\nStarting board:'
printBoard()
if first == "n":
ai2_turn = False
else:
ai2_turn = True
move(difficulty1, difficulty2)
# check for game over, change last move to lowercase,
# reassign ai2_turn, call moveAI1 or moveAI2
def move(diff1, diff2):
global ai2_turn
global board
if game_won(board, "R"):
print "\nGame over! I win!"
sys.exit()
elif game_won(board, "Y"):
print "\nGame over! Carlisle Wins!"
sys.exit()
elif full(board):
print "\nGame over! It's a tie!"
sys.exit()
for row in range(ROWS):
for column in range(COLUMNS):
if board[column][row] == "R":
board[column][row] = "r"
if board[column][row] == "Y":
board[column][row] = "y"
if ai2_turn:
ai2_turn = False
moveAI2(diff1, diff2)
else:
ai2_turn = True
moveAI1(diff1, diff2)
# sleep if AI too fast, compute optimal move & modify board in memory,
# print board, call move()
def moveAI1(diff1, diff2):
global board
print "\nRondo is thinking...."
if diff1 < 4:
time.sleep(1)
board = go_next(board, minimax_ab(board, diff1, "R"), "R")
print "Rondo's move:"
printBoard()
move(diff1, diff2)
# sleep if AI too fast, compute optimal move & modify board in memory,
# print board, call move()
def moveAI2(diff1, diff2):
global board
print "\nCarlisle is thinking...."
if diff2 < 4:
time.sleep(1)
board = go_next(board, minimax_ab(board, diff2, "Y"), "Y")
print "Carlisle's move:"
printBoard()
move(diff1, diff2)
# print ASCII board to terminal window
def printBoard():
for column in range(COLUMNS):
print str(1 + column),
print ""
for row in range(ROWS):
for column in range(COLUMNS):
print board[column][ROWS - row - 1],
print ""
init()