-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect4.py
More file actions
72 lines (61 loc) · 1.98 KB
/
Copy pathconnect4.py
File metadata and controls
72 lines (61 loc) · 1.98 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
# coding=utf-8
__author__ = 'Israel Estévez Hatchuell'
import games
import heu
def next_state(state, game):
while True:
try:
column = int(raw_input("Select a column into the range: "))
break
except ValueError:
print "ATENTION: This input is not a valid number, try again!"
return game.make_move(column, state)
def difficulty():
while True:
try:
dif = int(raw_input("Choose difficulty you are willing to tolerate (Easy=1, Medium=2, Hard=3): "))
if dif == 1:
return 0
elif dif == 2:
return 2
elif dif == 3:
return 4
except ValueError:
print "Write 1 to easy, 2 to medium or 3 to hard level!"
def players():
while True:
answer = raw_input("Would you like the computer as your adversary?(yes/no): ")
if answer == 'yes':
return True
elif answer == 'no':
print "Ok, you will play with your friend. Enjoy!"
return False
print "Just write yes or no!"
game = games.ConnectFour()
state = game.initial
player = game.to_move(state)
welcome = "New Game of {0} and begin the player {1}"
print welcome.format(game, player).center(80, "*")
adversary_computer = players()
if adversary_computer:
depth = difficulty()
while True:
print "It's up to: ", game.to_move(state)
print "Legal moves: ", game.legal_moves(state)
if player == 'X':
if adversary_computer:
move = games.alphabeta_search(state, game, d=depth, eval_fn=heu.heuristic)
state = game.make_move(move, state)
else:
state = next_state(state, game)
else:
state = next_state(state, game)
game.display(state)
print "-------------------"
if game.terminal_test(state):
if state.utility != 0:
print "The winner is: ", player
else:
print "Draw!"
break
player = game.to_move(state)