-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent_code.py
More file actions
129 lines (106 loc) · 4.81 KB
/
student_code.py
File metadata and controls
129 lines (106 loc) · 4.81 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
import random
import importlib
import copy
import sys
import konane
# class for individual player. student and grader players should be identical except for:
# - implementation of getMinimaxMove() and getAlphabetaMove(), and
# - any helper functions and/or members implemented by student
class player:
def __init__(self, b,s,depth,algo):
self.b = b # board to be played for test
self.s = s # save 'x' or 'o' designation
self.depth = depth # maximum depth for search (in number fo plies)
self.algo = algo # name of algorithm for player
self.prior_move = 'L' # helper variable for first/last deterministic player algo
# should not be needed for autograder, but include to help development
def makeFirstMove(self,r,c):
self.b.firstMove(self.s,r,c)
# returns list of available moves for player as list of [[x_from][y_from],[x_to][y_to]] items
def getNextMoves(self):
return(self.b.possibleNextMoves(self.s))
# makes move specified by move expressed as [[x_from][y_from],[x_to][y_to]]
def makeNextMove(self,move):
self.b.nextMove(self.s,move)
######
# next few methods get the next move for each of the available algorithms
# get the first move of the list of available moves
def getFirstMove(self):
moves = self.b.possibleNextMoves(self.s)
return moves[0]
# alternative between taking the first and last available move
def getFirstLastMove(self):
moves = self.b.possibleNextMoves(self.s)
if self.prior_move == 'L':
move = moves[0]
self.prior_move = 'F'
else:
move = moves[len(moves)-1]
self.prior_move = 'L'
return move
# randomly choose one of the available moves
def getRandomMove(self):
moves = self.b.possibleNextMoves(self.s)
selected = random.randint(0,len(moves)-1)
return moves[selected]
# ask a human player for a move
def getHumanMove(self):
print("Possible moves:" , self.b.possibleNextMoves(self.s))
origin = self._promptForPoint("Choose a piece to move (in the format 'row column'): ")
destination = self._promptForPoint("Choose a destination for (%s, %s) -> " % (origin[0], origin[1]))
if (origin, destination) in self.b.possibleNextMoves(self.s):
return (origin, destination)
else:
print("Invalid move.", (origin, destination))
return self.getHumanMove()
# help for prompting human player
def _promptForPoint(self, prompt):
raw = raw_input(prompt)
(r, c) = raw.split()
return (int(r), int(c))
# minimax algorithm to be completed by students
# note: you may add parameters to this function call
def getMinimaxMove(self):
# to be completed by students
return []
# alphabeta algorithm to be completed by students
# note: you may add parameters to this function call
def getAlphaBetaMove(self):
# to be completed by students
return []
def opposite(self,s):
if s == 'x':
return 'o'
else:
return 'x'
def heuristic(self, board, player):
score = len(board.possibleNextMoves(self.s)) - len(board.possibleNextMoves(self.opposite(self.s))) + \
int(board.state[0][0]==self.s) + \
int(board.state[0][board.size-1]==self.s) + \
int(board.state[board.size-1][0]==self.s) + \
int(board.state[board.size-1][board.size-1]==self.s)
return score
# member function called by test() which specifies move to be made for player's turn, with move
# expressed as [[x_from][y_from],[x_to][y_to]]
# if no moves available, return Python 'None' value
def takeTurn(self):
moves = self.b.possibleNextMoves(self.s)
#hello finley
# return Python 'None' if no moves available
if len(moves) == 0:
return [True,None]
if self.algo == 'First Move': # select first avaliable move
move = self.getFirstMove()
if self.algo == 'First/Last Move': # alternate first and last moves
move = self.getFirstLastMove()
if self.algo == 'Random': # select random move Note: not determinisic, just used to exercise code
move = self.getRandomMove()
if self.algo == 'MiniMax': # player must select best move based upon MiniMax algorithm
move = self.getMinimaxMove()
if self.algo == 'AlphaBeta': # player must select best move based upon AlphaBeta algorithm
move = self.getAlphaBetaMove()
if self.algo == 'Human':
move = self.getHumanMove()
# makes move on board being used for evaluation
self.makeNextMove(move)
return [False,move]