-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommandLineY.py
More file actions
154 lines (131 loc) · 5.83 KB
/
Copy pathcommandLineY.py
File metadata and controls
154 lines (131 loc) · 5.83 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import graph as grph
import node as nde
import game as gm
# finding graph size
def graphSize(n):
spaces = 3 * (n ** 2 - n) // 2
print(' Your board of base ' + str(n) + " has " + str(spaces) + " spaces")
return grph.generateNSizedGraph(n)
# human or AI
def playertype(ptb, ptw):
if ptb == "human":
ptbString = "a human"
else:
ptbString = "an AI"
if ptw == "human":
ptwString = "a human"
else:
ptwString = "an AI"
print(" Black is controlled by " + ptbString + " and white is controlled by " + ptwString)
# startup
print('Welcome to Command Line Y!')
while True:
try:
n = int(input('Please enter the base size of the board: '))
if n >= 2:
break
if n < 2:
print(' Error: base must be an integer greater than 1')
except:
print(' Error: base must be an integer greater than 1')
g = graphSize(n)
# choosing player types
while True:
try:
ptb = str(input('Please enter the player type for black (human or AI): '))
if ptb == "human" or ptb == "AI":
break
else:
print(' Error: player type must be a string reading \"human\" or \"AI\"')
except:
print(' Error: player type must be a string reading \"human\" or \"AI\"')
while True:
try:
ptw = str(input('Please enter the player type for white (human or AI): '))
if ptw == "human" or ptw == "AI":
break
else:
print(' Error: player type must be a string reading \"human\" or \"AI\"')
except:
print(' Error: player type must be a string reading \"human\" or \"AI\"')
playertype(ptb, ptw)
# AI type selecting
if ptb == "AI":
while True:
try:
bTypeInput = str(input('Please enter the type of AI for black — random, negamax, or montecarlo (type \"help\" to learn more): '))
if bTypeInput == "random":
ptb = "random"
print(" Black is controlled by a random AI")
break
elif bTypeInput == "negamax":
ptb = "negamax"
print(" Black is controlled by a negamax AI")
break
elif bTypeInput == "montecarlo":
ptb = "montecarlo"
print(" Black is controlled by a montecarlo tree search AI")
break
elif bTypeInput == "help":
print(" The random AI makes its moves randomly. The negamax AI makes its moves based on what it evaluates as the best. The montecarlo AI is a learned tree search that makes its moves based on previous winning moves")
else:
print(' Error: AI type must be \"random,\" \"negamax,\" or \"montecarlo\"')
except:
print(' Error: AI type must be a string reading \"random,\" \"negamax,\" or \"montecarlo\"')
if ptw == "AI":
while True:
try:
wTypeInput = str(input('Please enter the type of AI for white — random, negamax, or montecarlo (type \"help\" to learn more): '))
if wTypeInput == "random":
ptw = "random"
print(" White is controlled by a random AI")
break
elif wTypeInput == "negamax":
ptw = "negamax"
print(" White is controlled by a negamax AI")
break
elif wTypeInput == "montecarlo":
ptw = "montecarlo"
print(" White is controlled by a montecarlo tree search AI")
break
elif wTypeInput == "help":
print(" The random AI makes its moves randomly. The negamax AI makes its moves based on what it evaluates as the best. The montecarlo AI is a learned tree search that makes its moves based on previous winning moves")
else:
print(' Error: AI type must be \"random,\" \"negamax,\" or \"montecarlo\"')
except:
print(' Error: AI type must be a string reading \"random,\" \"negamax,\" or \"montecarlo\"')
# level selecting for negamax
if ptb == "negamax":
while True:
bLevelInput = input('Please enter the difficulty level, 1 to 5, for the black AI (type \"help\" to learn more): ')
if bLevelInput.isdigit() and 1 <= int(bLevelInput) <= 5:
bLevel = int(bLevelInput)
print(" The AI difficulty for black is set to level " + str(bLevel))
break
elif bLevelInput.isdigit() and ( int(bLevelInput) < 1 or int(bLevelInput) > 5 ):
print(' Error: AI level must be an integer between 1 and 5')
elif bLevelInput == "help":
print(" Levels 1 and 2 are easy, levels 3 and 4 are medium, and level 5 is hard. The higher the level, the longer the AI takes to make decisions")
else:
print(' Error: AI level must be an integer between 1 and 5')
else:
bLevel = 1 # placeholder level if human
if ptw == "negamax":
while True:
wLevelInput = input('Please enter the difficulty level, 1 to 5, for the white AI (type \"help\" to learn more): ')
if wLevelInput.isdigit() and 1 <= int(wLevelInput) <= 5:
wLevel = int(wLevelInput)
print(" The AI difficulty for white is set to level " + str(wLevel))
break
elif wLevelInput.isdigit() and ( int(wLevelInput) < 1 or int(wLevelInput) > 5 ):
print(' Error: AI level must be an integer between 1 and 5')
elif wLevelInput == "help":
print(" Levels 1 and 2 are easy, levels 3 and 4 are medium, and level 5 is hard. The higher the level, the longer the AI takes to make decisions")
else:
print(' Error: AI level must be an integer between 1 and 5')
else:
wLevel = 1 # placeholder level if human
# node creation
game = gm.Game(g)
game.run(ptb, ptw, bLevel, wLevel)
# gameplay