Skip to content

Instantly share code, notes, and snippets.

@Adarain
Created May 31, 2015 14:31
Show Gist options
  • Save Adarain/88ee2b83c6e889b07a52 to your computer and use it in GitHub Desktop.
Save Adarain/88ee2b83c6e889b07a52 to your computer and use it in GitHub Desktop.
import random
import copy
#vars
var = []
player = 1
INITIAL_DEPTH = 0
ITERATIONS = 100
#functions
#sets the passed board variable to the initial field
def resetBoard(board):
board = [[0 for i in range(5)] for j in range(5)]
board[0][1] = 2
board[0][3] = 2
board[1][0] = 2
board[1][4] = 2
board[3][0] = 2
board[3][4] = 2
board[4][1] = 2
board[4][3] = 2
board[2][2] = 1
return board
#checks whether a given move on a board is legal. returns 1 if legal and 0 if illegal
def testMove(player, x, y, direction, board):
if (x == 0 and direction == -2) or (x == 4 and direction == 2) or (y == 0 and direction == -1) or (y == 4 and direction == 1):
return 0
if (direction == 1 and board[y+1][x] != 0) or (direction == -1 and board[y-1][x] != 0) or (direction == 2 and board[y][x+1] != 0) or (direction == -2 and board[y][x-1] != 0):
return 0
#todo: forbid moving back for stag
return 1
#moves a piece on the passed board. does not check for legality of move (program will break if it is illegal)
def move(player, x, y, direction, board):
if direction == 1:
board[y+1][x] = player
elif direction == 2:
board[y][x+1] = player
elif direction == -1:
board[y-1][x] = player
elif direction == -2:
board[y][x-1] = player
board[y][x] = 0
return board
#toggles the state of the passed player variable between 1 and 2
def changePlayer(player):
if player == 1:
return 2
else:
return 1
#returns a list containing the (x, y) coordinates of all pieces of the current player on the board
def findPieces(board, player):
pieceList = []
for y in range(5):
for x in range(5):
if board[y][x] == player:
pieceList.append((x, y))
return pieceList
#ckecks which moves of the selected piece are legal and selects one at random. returns 0 if there are no legal moves, and the chosen direction otherwise.
def chooseDirection(piece, player, board):
legalDirections = []
for i in (-2, -1, 1, 2):
if testMove(player, piece[0], piece[1], i, board) == 1:
legalDirections.append(i)
if legalDirections == []:
return 0
else:
direction = random.choice(legalDirections)
return direction
#selects a piece at random out of a list of given pieces
def choosePieces(pieces):
return random.choice(pieces)
#selects what move is to be done on the given board. if depth is set to 0, it will select moves at random until it finds a legal one and start the function to move it, then return the board in its new state
#if depth is 1, it will start a number of games with depth == 0 (set by the ITERATIONS constant) for every legal move and select the one where the current player won most games
#if depth is >1, it will start one game for every possible move with depth decreased by one and select the one where the current player won most games
#for small values of depth (~2), this should stop the computer from making extremely stupid moves (like hindering the other player from winning in the next move if it is possible)
#for large values of depth, this should create an AI that bruteforces the best possible move out of the entire move tree, at a cost of speed
#for large values of ITERATIONS, this returns better moves at a cost of speed
def chooseMove(board, player, depth):
if depth == 0:
pieces = findPieces(board, player)
if player == 1:
piece = pieces[0]
direction = chooseDirection(piece, 1, board)
if direction == 0:
return 0 #player 1 looses
else:
return move(player, piece[0], piece[1], direction, board)
elif player == 2:
stag = findPieces(board, 1)[0]
if stag[0] == 0 or stag[0] == 4 or stag[1] == 0 or stag[1] == 4:
return 0 #player 2 looses
while True:
piece = choosePieces(pieces)
direction = chooseDirection(piece, 2, board)
if direction != 0:
break
return move(player, piece[0], piece[1], direction, board)
elif depth == 1:
#todo much more complicated code
pass
else:
#todo slightly less complicated (but still complex) code than above
pass
#main code (testing grounds)
var = resetBoard(var)
for i in range(20):
var = chooseMove(copy.deepcopy(var), player, 0)
if var == 0:
#todo end game
break
player = changePlayer(player)
for y in range(5):
print(var[y])
print('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment