Skip to content

Commit

Permalink
adverse_ai working and winning
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilesh95 committed Mar 24, 2018
1 parent 23b91fb commit da6306d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
21 changes: 20 additions & 1 deletion ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ def computeMove(self, marked_tiles, currHead, oppHead, direction, oppDirection):
self.head = currHead
self.oppHead = oppHead

playerScores = []
oppScores = []
board = [[]]
head = self.head
score = 0
Expand All @@ -298,8 +300,25 @@ def computeMove(self, marked_tiles, currHead, oppHead, direction, oppDirection):
#Update board with 1 move from Opponent's Head
oppHead = self.moveHead(oppHead, oppMove)
if (not oppHead): continue
oppScores.append(self.getScore(board, oppHead))
oppScore = self.getScore(board, oppHead)/100

#Check 4 possible moves of the player and get the max score
playerScores = []
for playerMove in MOVES:
head2 = head
if(playerMove != oppDir(move)):
head2 = self.moveHead(head2, playerMove)
if (not head2): continue
#Compute score for this board
playerScore = self.getScore(board, head2)/100
playerScores.append(playerScore)

oppScores.append(oppScore)
# print (score)
if (oppScores): score += min(oppScores)
# print (score)
if (playerScores) : score += max(playerScores)
# print (score)
if (score >= max_score):
max_score = score
max_score_dir = move
Expand Down
2 changes: 1 addition & 1 deletion experiments.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#Run the file 100 times
import time
start = time.time()
numSim = 100
numSim = 1000
open('results.txt', 'w').close()
for i in range(numSim):
execfile("snakebattle.py")
Expand Down
4 changes: 2 additions & 2 deletions snakebattle.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@



def_fps = 10#00000
def_fps = 10000000

## start arguments
arg_parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -122,7 +122,7 @@ def turn(self):
## Define which AI for which player
# Create AI objects
player1_ai = Adverse_ai(UP, marked_tiles, (p1.x,p1.y), (p2.x,p2.y))
player2_ai = Simple_ai(UP, marked_tiles, (p2.x,p2.y), (p1.x,p1.y))
player2_ai = Original_ai(UP, marked_tiles, (p2.x,p2.y), (p1.x,p1.y))
#player2_ai = Adverse_ai(UP, marked_tiles, (p2.x,p2.y), (p1.x,p1.y))


Expand Down

0 comments on commit da6306d

Please sign in to comment.