def computeRepetitiveHopRecursion(board, origini, originj, hopi, hopj, pastPosition, possibleMoveBoard): hopMoves = findLegalHop(board, hopi, hopj) if hopMoves is not None: for hopMove in hopMoves: (nexti, nextj) = hopMove if (nexti, nextj) not in pastPosition: #print "piece " + str(hopi) + " " + str(hopj) + " going " + str(nexti) + " " + str(nextj) pastPosition[(nexti, nextj)] = 1 futureBoard = copy.deepcopy(board.board) futureBoard[hopi][hopj] = 0 futureBoard[nexti][nextj] = 1 possibleMoveBoard.append((origini, originj, nexti, nextj)) if board.fullGame == 0: futureboard = boardState(options='smallGame', inputBoard=futureBoard) elif board.fullGame == 1: futureboard = boardState(options='fullGame', inputBoard=futureBoard) else: futureboard = boardState(options='midGame', inputBoard=futureBoard) #futureboard.printBoard() computeRepetitiveHopRecursion(board, origini, originj, nexti, nextj, pastPosition, possibleMoveBoard)
def __init__(self, parent, options = 0): Frame.__init__(self, parent) if options == 0: self.full = 0 self.game = boardState(options = 'smallGame') self.HEIGHT = pieceSize * self.game.height + 2 * margin self.WIDTH = pieceSize * self.game.mid_width_max + 2 * margin elif options == 1: self.full = 1 self.game = boardState(options = 'fullGame') self.HEIGHT = pieceSize * self.game.height + 2 * margin self.WIDTH = pieceSize * self.game.mid_width_max + 2 * margin self.row, self.col = -1, -1 self.parent = parent self.initUI()
def computeRepetitiveHopRecursion(board, origini, originj, hopi, hopj, pastPosition, possibleMoveBoard): hopMoves= findLegalHop(board, hopi, hopj) if hopMoves is not None: for hopMove in hopMoves: (nexti, nextj) = hopMove if (nexti, nextj) not in pastPosition: #print "piece " + str(hopi) + " " + str(hopj) + " going " + str(nexti) + " " + str(nextj) pastPosition[(nexti, nextj)] = 1 futureBoard = copy.deepcopy(board.board) futureBoard[hopi][hopj] = 0 futureBoard[nexti][nextj] = 1 possibleMoveBoard.append((origini, originj,nexti, nextj)) if board.fullGame == 0: futureboard = boardState(options = 'smallGame', inputBoard = futureBoard) else: futureboard = boardState(options = 'fullGame', inputBoard = futureBoard) #futureboard.printBoard() computeRepetitiveHopRecursion(board, origini, originj, nexti, nextj, pastPosition, possibleMoveBoard)
def computeRepetitiveHop(board, hopi, hopj): possibleMoveBoard = [] pastPosition = {} pastPosition[(hopi, hopj)] = 1 for (basei, basej) in board.allPosition: hopMove= findLegalHop(board, hopi, hopj, basei, basej) if hopMove is not None: (nexti, nextj) = hopMove if (nexti, nextj) not in pastPosition: pastPosition[(nexti, nextj)] = 1 futureBoard = copy.deepcopy(board.board) futureBoard[hopi][hopj] = 0 futureBoard[nexti][nextj] = 1 possibleMoveBoard.append(((hopi,hopj),(nexti, nextj))) if board.fullGame == 0: futureboard = boardState(options = 'smallGame', inputBoard = futureBoard) else: futureboard = boardState(options = 'fullGame', inputBoard = futureBoard) computeRepetitiveHopRecursion(board, hopi, hopj, nexti, nextj, pastPosition, possibleMoveBoard) return possibleMoveBoard
from boardState import * from computeLegalMove import * from computeFeatures import * board1 = boardState(options = 'fullGame') print "Orginal Board" board1.printBoard() print computeFeaturesFull(board1, 1) print computeFeaturesFull(board1, 2)
from boardState import * from computeLegalMove import * from computeFeatures import * from computeMinimax import * import numpy as np import copy import time print "################################################################################" boardStart = boardState(options='smallGame') # fullGame, smallGame print "Orginal Board" boardStart.printBoard() boardNow = boardStart player = 1 turn = 0 weights = np.ones(5) weights[0] = -1.69853196 weights[1] = 0.06633986 weights[2] = 0.09230491 weights[3] = -0.01052756 weights[4] = 0.23913056 cantGo1 = [] cantGo2 = [] while ((not boardNow.isEnd()) and turn < 100): turn = turn + 1 timeStart = time.time() features = computeFeaturesFull(boardNow) scoreRaw = np.inner(features, weights) if (player == 1): (scoreMiniMax, moveList, recursions) = computeMinimax(boardNow, player, weights, 4, cantGo1)
print('\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n') from boardState import * from computeLegalMove import * from computeFeatures import * from computeMinimax import * import numpy as np import copy import time from randomMove import * weights = np.ones(3) # weights[0] = 10 depth = 4 stplength = 0.1 boardStart = boardState(options = 'midGame') # fullGame, smallGame, midGame boardboard = boardStart.board boardboard[0][5] = 2 boardboard[1][4] = 2 boardboard[1][6] = 2 boardboard[2][3] = 2 boardboard[2][5] = 2 boardboard[2][7] = 2 boardboard[8][3] = 1 boardboard[8][5] = 1 boardboard[8][7] = 0 boardboard[9][4] = 1 boardboard[9][6] = 1 boardboard[10][5] = 1 boardboard[5][2] = 1
Player One's weights: ''' aaaaa = [10, 1, 1] weights1 = np.array(aaaaa) weights1 = weights1 / np.linalg.norm(weights1) ''' Player Two's weights: ''' aaaaa = [10, 1, 1] weights2 = np.array(aaaaa) weights2 = weights2 / np.linalg.norm(weights2) while gameCount[0] < totalGames: print "################################################################################" gameCount[0] += 1 boardNow = boardState( options='midGame') # options can be fullGame, midGame, smallGame turn = 0 player = 1 timeGameStart = time.time() while ((not boardNow.isEnd()) and turn < 250): turn = turn + 1 timeTurnStart = time.time() print('\nGame No. ' + str(gameCount[0]) + ', turn No. ' + str(turn)) if (player == 1): # move = findMove_GreedyRandom(boardNow, player, 1) move = findMove_GreedyRandom(boardNow, player, 2) # move = findMove_MiniMax(boardNow, player, weights1, 2, cantGo1) ## Minimax strategy throughout #move = findMove_Advanced(boardNow, player, weights1, 2, cantGo2) ## Most advanced strategy cantGo1.append(move) if (len(cantGo1) >= 5): cantGo1.pop(0)
board[0][8] = 0 board[2][10] = 0 board[4][10] = 2 board[7][7] = 2 board[9][9] = 2 board[16][8] = 0 board[14][10] = 0 board[13][11] = 0 board[13][9] = 0 board[13][5] = 0 board[12][10] = 1 board[12][8] = 1 board[12][6] = 1 board[11][5] = 1 board[10][6] = 1 board1 = boardState(options='fullGame', inputBoard=board) board1.printBoard() boardNow = board1 print( '\n ##################### \n Endgame Begins!!!! \n #####################') player = 2 while ((not boardNow.isEnd())): print('\n\n') print('player = {}'.format(player)) print('\n') timeStart = time.time() print('All possible moves = {}'.format(computeLegalMove(boardNow, player))) print('legal move forward = {}'.format( computeLegalMoveForward(boardNow, player, 0))) (scoreGreedy, moveList, recursions) = findMoveGreedy(boardNow, player, 3)
# This file is used to test a weight. To be specific, both players play the game with this weights, and this file will keep track of each step print "################################################################################" from boardState import * from computeLegalMove import * from computeFeatures import * from strategies import * from strategiesHelpers import * import numpy as np import copy import time boardNow = boardState(options='smallGame') # fullGame, smallGame, midGame player = 1 turn = 0 aaaaa = [10, 1, 1] weights = np.array(aaaaa) weights = weights / np.linalg.norm(weights) cantGo1 = [] cantGo2 = [] depth = 2 #Can only be even numbers: 2, 4, 6, ... ## Startgame begins! while (turn < 249): turn = turn + 1 timeStart = time.time() print('\n\n') print('turn = {}'.format(turn)) print('player = {}'.format(player))
from boardState import * from computeLegalMove import * from computeFeatures import * from computeMinimax import * from randomMove import * board1 = boardState(options='fullGame') print randomMove(board1, 1)
from boardState import * from computeFeatures import * from computeLegalMove import * x = boardState(options='midGame') x.printBoard() # x.board[2,3] = 2; x.board[1,2] = 0; # x.board[3,2] = 1; x.board[5,2] = 0; # x.board[4,6] = 2; x.board[3,7] = 0; # x.board[12,6] = 1; x.board[13,7] = 0; ''' isEnd testing''' # x.printBoard() # print 'game ends? {}'.format(x.isEnd()) ''' feature testing ''' print 'position One {}'.format(x.PositionOne) print 'position Two {}'.format(x.PositionTwo) print 'possible moves for player 1:' print computeLegalMove(x, 1) print 'possible moves for player 2:' print computeLegalMove(x, 2) # x.board[2,3] = 1; x.board[1,2] = 0; # x.board[3,2] = 2; x.board[5,2] = 0; # x.printBoard() # x.PositionOne = []; x.PositionTwo = [] # for i in range(x.height): # for j in range(x.mid_width_max): # if x.board[i,j] == 1: # x.PositionOne.append((i,j)) # if x.board[i,j] == 2: # x.PositionTwo.append((i,j))
""" from new_eleusis import * from boardState import * from God import God from standAlone import * from attribute import * from trainingData import * import collections import random from random import randint from math import * god = God() board = boardState() # create Instance of attribute class a = attribute() # Generate Rule Map ruleMap = getMap() keyRule = ruleMap.keys() """ =============================== Initialize all the variables =========================================""" cardDeck = [] cardsPlayed = 0 firstPlay = False godRule = "" playerRule = "" attributeMap = {} gotRule = False myrulelist = [] __boardState = []
weightsSum = np.zeros(3) depth = 2 #Can only be even numbers: 2, 4, 6, ... totalGames = 40 # Learn the weights using linear regression gameCount = 0 AARVec = [] RSquareVec = [] weightsGradVec = [] predErrorVec = [] while gameCount < totalGames: print "################################################################################" gameCount += 1 print('Game = No. {}'.format(gameCount)) boardNow = boardState(options = 'smallGame') # fullGame, smallGame, midGame error = 0 player = 1 turn = 0 cantGo1 = [] cantGo2 = [] print ('weightsNow = {}'.format(weights)) while ((not boardNow.isEnd()) and turn < 249) : turn = turn + 1 # print('\nGame No. ' + str(gameCount) + ', turn No. ' + str(turn)) # boardNow.printBoard() timeStart = time.time() if (player == 1) : (scoreMaxiMax, move, recursions) = computeMaximax(boardNow, player, weights, depth / 2, cantGo1) cantGo1.append(move)
from boardState import * from computeFeatures import * from computeLegalMove import * x = boardState(options = 'midGame') x.printBoard() # x.board[2,3] = 2; x.board[1,2] = 0; # x.board[3,2] = 1; x.board[5,2] = 0; # x.board[4,6] = 2; x.board[3,7] = 0; # x.board[12,6] = 1; x.board[13,7] = 0; ''' isEnd testing''' # x.printBoard() # print 'game ends? {}'.format(x.isEnd()) ''' feature testing ''' print 'position One {}'.format(x.PositionOne) print 'position Two {}'.format(x.PositionTwo) print 'possible moves for player 1:' print computeLegalMove(x,1) print 'possible moves for player 2:' print computeLegalMove(x,2) # x.board[2,3] = 1; x.board[1,2] = 0; # x.board[3,2] = 2; x.board[5,2] = 0; # x.printBoard() # x.PositionOne = []; x.PositionTwo = [] # for i in range(x.height): # for j in range(x.mid_width_max): # if x.board[i,j] == 1: # x.PositionOne.append((i,j)) # if x.board[i,j] == 2:
from boardState import * from computeLegalMove import * from computeFeatures import * from computeMinimax import * import numpy as np import copy import time weights = np.ones(4) weights[0] = 10 weights = weights / np.linalg.norm(weights) stplength = 0.1 boardStart = boardState(options = 'smallGame') # fullGame, smallGame print "Orginal Board" boardStart.printBoard() errors = [] boardNow = boardStart player = 1 turn = 0 weights1 = weights cantGo1 = [] cantGo2 = [] while ((not boardNow.isEnd()) and turn < 100) : turn = turn + 1 timeStart = time.time() features = computeFeaturesFull(boardNow) scoreRaw = np.inner(features, weights)
from boardState import * from computeFeatures import * from computeLegalMove import * x = boardState(options='smallGame') x.printBoard() # x.board[2,3] = 2; x.board[1,2] = 0; # x.board[3,2] = 1; x.board[5,2] = 0; # x.board[4,6] = 2; x.board[3,7] = 0; # x.board[12,6] = 1; x.board[13,7] = 0; ''' isEnd testing''' # x.printBoard() # print 'game ends? {}'.format(x.isEnd()) ''' feature testing ''' print 'position One {}'.format(x.PositionOne) print 'position Two {}'.format(x.PositionTwo) x.board[2, 3] = 1 x.board[1, 2] = 0 x.board[3, 2] = 2 x.board[5, 2] = 0 x.printBoard() x.PositionOne = [] x.PositionTwo = [] for i in range(x.height): for j in range(x.mid_width_max): if x.board[i, j] == 1: x.PositionOne.append((i, j)) if x.board[i, j] == 2: x.PositionTwo.append((i, j)) x.allPosition = x.PositionOne + x.PositionTwo # x.PositionOne[1] = (2,3);
## used by Siun to test his code ###################################################################################################################### ## You can run the file, but do NOT change if you are not sijun ###################################################################################################################### from boardState import * x = boardState(options = 'smallGame') print "Orginal Board" x.printBoard() possibleMoveBoard = x.computeLegalMove() for move in possibleMoveBoard: print move print possibleMoveBoard[2] ((oldi, oldj),(newi, newj)) = possibleMoveBoard[2] test = x.takeMove(oldi, oldj, newi, newj) test.printBoard() twoSteps = test.computeLegalMove() for twoStep in twoSteps: print twoStep
board[0][8] = 0 board[2][10] = 0 board[4][10] = 2 board[7][7] = 2 board[10][8] = 2 board[16][8] = 0 board[14][10] = 0 board[13][11] = 0 board[13][9] = 0 board[13][5] = 0 board[12][10] = 1 board[12][8] = 1 board[12][6] = 1 board[11][5] = 1 board[10][6] = 1 board1 = boardState(options = 'fullGame', inputBoard = board) board1.printBoard() boardNow = board1 print('\n ##################### \n Endgame Begins!!!! \n #####################') player = 2 while ((not boardNow.isEnd())) : print('\n\n') print('player = {}'.format(player)) print('\n') timeStart = time.time() print ('All possible moves = {}'.format(computeLegalMove(boardNow, player))) print ('legal move forward = {}'.format(computeLegalMoveForward(boardNow, player, 0))) (scoreGreedy, moveList, recursions) = findMoveGreedy(boardNow, player, 3) timeEnd = time.time() print('scoreGreedy = {}'.format(scoreGreedy))