Skip to content

Instantly share code, notes, and snippets.

@PkBadger
Created November 23, 2019 03:09
Show Gist options
  • Save PkBadger/9f2b08c1dce430856dd3c07491e21454 to your computer and use it in GitHub Desktop.
Save PkBadger/9f2b08c1dce430856dd3c07491e21454 to your computer and use it in GitHub Desktop.
import numpy as np
class Square:
def __init__(self, value= None):
self.value = np.random.randint(1,16) if not value else value
self.visited = False
self.testVisited = False
class Board:
def __init__(self):
value = 1
self.board = []
for i in range(8):
self.board.append([])
for k in range(8):
self.board[-1].append(Square(value))
value += 1
def resetTest(self):
for i in range(8):
for k in range(8):
self.board[i][k].testVisited = False
def __repr__(self):
array = []
testVisited = []
for i in range(8):
array.append([])
for k in range(8):
array[-1].append(self.board[i][k].value)
for i in range(8):
testVisited.append([])
for k in range(8):
testVisited[-1].append(self.board[i][k].testVisited)
string = ''
for sub in array:
string += str(sub) + '\n'
string += 'TestVisited\n'
for sub in testVisited:
string += str(sub) + '\n'
return string
def setTestVisited(self,x,y):
self.board[x][y].testVisited = True
np.random.seed(100)
board = Board()
player1 = {
'position': {
'x': 0,
'y': 0,
}
}
player2 = {
'position': {
'x': 7,
'y': 7,
}
}
def validacion(movimiento, posicion, board):
x = posicion['x']
y = posicion['y']
if movimiento == 1 and y - 1 > 0: #arriba
y = y - 1
x = x
elif movimiento == 2 and x + 1 < 7: #derecha
x = x + 1
y = y
elif movimiento == 3 and y + 1 < 7: #abajo
y = y + 1
x = x
elif movimiento == 4 and x - 1 > 0: #izquierda
x = x - 1
y = y
else:
return False
if board.board[x][y].testVisited:
return False
return True
def getLeaf(board, currentPlayer, player1, player2, totalDepth, values, depth=0):
player = player1 if currentPlayer == 0 else player2
siguiente = 0 if currentPlayer == 1 else 1
localValues = []
isLast = depth >= totalDepth
if(validacion(1, player.get('temporalPosition'), board)):
player['temporalPosition'] = {
'x': player['temporalPosition']['x'],
'y': player['temporalPosition']['y'] - 1
}
board.setTestVisited(player['temporalPosition']['x'], player['temporalPosition']['y'] - 1)
if not isLast:
getLeaf(board, siguiente, player1, player2, totalDepth, values, depth+1)
else:
localValues.append(board.board[player['temporalPosition']['x']][player['temporalPosition']['y']].value)
if(validacion(2, player.get('temporalPosition'), board)):
player['temporalPosition'] = {
'x': player['temporalPosition']['x'] +1,
'y': player['temporalPosition']['y']
}
board.setTestVisited(player['temporalPosition']['x'], player['temporalPosition']['y'])
if not isLast:
getLeaf(board, siguiente, player1, player2, totalDepth, values, depth+1)
else:
localValues.append(board.board[player['temporalPosition']['x']][player['temporalPosition']['y']].value)
if(validacion(3, player.get('temporalPosition'), board)):
player['temporalPosition'] = {
'x': player['temporalPosition']['x'],
'y': player['temporalPosition']['y'] + 1
}
board.setTestVisited(player['temporalPosition']['x'], player['temporalPosition']['y'])
if not isLast:
getLeaf(board, siguiente, player1, player2, totalDepth,values, depth+1)
else:
localValues.append(board.board[player['temporalPosition']['x']][player['temporalPosition']['y']].value)
if(validacion(4, player.get('temporalPosition'), board)):
player['temporalPosition'] = {
'x': player['temporalPosition']['x'] -1,
'y': player['temporalPosition']['y']
}
board.setTestVisited(player['temporalPosition']['x'], player['temporalPosition']['y'])
if not isLast:
getLeaf(board, siguiente, player1, player2, totalDepth,values, depth+1)
else:
localValues.append(board.board[player['temporalPosition']['x']][player['temporalPosition']['y']].value)
if(isLast):
values.append(localValues)
#print('last')
player1['temporalPosition'] = {
'x': player1['position']['x'],
'y': player1['position']['y']
}
player2['temporalPosition'] = {
'x': player2['position']['x'],
'y': player2['position']['y']
}
values = []
board.setTestVisited(player1['temporalPosition']['x'], player1['temporalPosition']['y'])
board.setTestVisited(player2['temporalPosition']['x'], player2['temporalPosition']['y'])
print(getLeaf(board, 0, player1, player2, 0, values, 0))
print(values)
print(board)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment