diff --git a/Snek/player/Player.py b/Snek/player/Player.py index 8f7828b8c1b0c0671dc632e5b804b48ea5f66f53..422d3ca62a64e49714f000f86567b1838cb0676e 100644 --- a/Snek/player/Player.py +++ b/Snek/player/Player.py @@ -4,7 +4,7 @@ class Player: def __init__(self, colour): self.colour = colour - self.board = Board.Board() + self.board = Board.Board(self.colour) def on_board(self, piece, move): if piece.pos[0] + move[0] < -3 or piece.pos[0] + move[0] > 3: @@ -16,12 +16,10 @@ class Player: return True def action(self): - for piece in self.board.pieces: - if piece.colour == self.colour: - print(piece.pos) - for move in self.board.moves: - if self.on_board(piece, move) and self.board.tiles[piece.pos[1]+3+move[1]][piece.pos[0]+3+move[0]] is None: - return ("MOVE",((piece.pos),(piece.pos[0]+move[0], piece.pos[1]+move[1]))) + moves = self.board.get_moves(self.colour) + moves[0] = self.board.convert_coords_ref(moves[0]) + print(moves[0]) + return(moves[0]) def update(self, colour, action): self.board.move(action) diff --git a/Snek/player/__pycache__/Player.cpython-36.pyc b/Snek/player/__pycache__/Player.cpython-36.pyc index 91ad61beb090206b21aedef69659145b8eb9a9d3..1c5f91be7a2d1e9018a546b768e4a7af0e07b61d 100644 Binary files a/Snek/player/__pycache__/Player.cpython-36.pyc and b/Snek/player/__pycache__/Player.cpython-36.pyc differ diff --git a/Snek/util/Board.py b/Snek/util/Board.py index 89fecd57a9661d656f93461835805b2e34546ae9..e73cbc9cfe851b296bec0e7a544ca8516de0cbde 100644 --- a/Snek/util/Board.py +++ b/Snek/util/Board.py @@ -1,52 +1,118 @@ from Snek.util import Piece class Board: - # List of move directions - moves = [(-1,0), (0,-1), (1,-1), (1,0), (0,1), (-1,1)] - - def __init__(self): - - # List of exit tiles for each colour - self.exit_tiles = {'red':[(3,-3), (3,-2), (3,-1), (3,0)], - 'green':[(-3,3),(-2,3),(-1,3),(0,3)], - 'blue':[(0,-3),(-1,-2),(-2,-1),(-3,0)]} - - self.tiles = [[None for i in range(7)] for j in range(7)] - self.tiles[0][0] = 0 - self.tiles[0][1] = 0 - self.tiles[0][2] = 0 - self.tiles[1][0] = 0 - self.tiles[1][1] = 0 - self.tiles[2][0] = 0 - self.tiles[4][6] = 0 - self.tiles[5][5] = 0 - self.tiles[5][6] = 0 - self.tiles[6][4] = 0 - self.tiles[6][5] = 0 - self.tiles[6][6] = 0 - - # List of pieces on the board - self.pieces = [] - for i in range(0,4): - self.pieces.append(Piece.Piece((-3,i),"red")) - - for i in range(0,4): - self.pieces.append(Piece.Piece((i,-3),"green")) - - j = 3 - for i in range(0,4): - self.pieces.append(Piece.Piece((i,j),"blue")) - j = j - 1 - - for piece in self.pieces: - self.tiles[piece.pos[1]+3][piece.pos[0]+3] = piece - - #print self.tiles - - - def move(self, action): - if action[0] == 'MOVE' or action == 'JUMP': - piece = self.tiles[action[1][0][1]+3][action[1][0][0]+3] - self.tiles[action[1][0][1]+3][action[1][0][0]+3] = None - self.tiles[action[1][1][1]+3][action[1][1][0]+3] = piece - self.tiles[action[1][1][1]+3][action[1][1][0]+3].pos = action[1][1] \ No newline at end of file + # List of move directions + directions = [(-1,0), (0,-1), (1,-1), (1,0), (0,1), (-1,1)] + + def __init__(self, colour): + + self.colour = colour + + # List of exit tiles for each colour + self.exit_tiles = {'red':[(6,0), (6,1), (6,2), (6,3)], 'green':[(0,6),(1,6),(2,6),(3,6)],'blue':[(3,0),(2,1),(1,2),(0,3)]} + + self.tiles = [[None for i in range(7)] for j in range(7)] + self.tiles[0][0] = 0 + self.tiles[0][1] = 0 + self.tiles[0][2] = 0 + self.tiles[1][0] = 0 + self.tiles[1][1] = 0 + self.tiles[2][0] = 0 + self.tiles[4][6] = 0 + self.tiles[5][5] = 0 + self.tiles[5][6] = 0 + self.tiles[6][4] = 0 + self.tiles[6][5] = 0 + self.tiles[6][6] = 0 + + # List of pieces on the board + self.pieces = {'red':[], 'green':[], 'blue':[]} + + for i in range(3,7): + self.pieces['red'].append(Piece.Piece((0,i),"red")) + + for i in range(3,7): + self.pieces['green'].append(Piece.Piece((i,0),"green")) + + j = 6 + for i in range(3,7): + self.pieces['blue'].append(Piece.Piece((i,j),"blue")) + j = j - 1 + + for piece in self.pieces['red']: + self.tiles[piece.pos[1]][piece.pos[0]] = piece + + for piece in self.pieces['green']: + self.tiles[piece.pos[1]][piece.pos[0]] = piece + + for piece in self.pieces['blue']: + self.tiles[piece.pos[1]][piece.pos[0]] = piece + + j = 6 + for i in range(3,7): + print(self.tiles[i][0]) + print(self.tiles[0][i]) + print(self.tiles[j][i]) + j = j - 1 + + #print self.tiles + + + def get_moves(self, colour): + valid_moves = [] + for piece in self.pieces[colour]: + + # If the piece is on an exit tile, return that piece + if piece.pos in self.exit_tiles[piece.colour]: + valid_moves.append(('EXIT', piece.pos)) + p_r = piece.pos[1] + p_q = piece.pos[0] + + # Otherwise, try all moves and return valid ones + for direction in self.directions: + if not self.on_board(piece, direction): + continue + + if self.tiles[p_r + direction[1]][p_q + direction[0]] is None: + valid_moves.append(('MOVE',((p_q, p_r),(p_q + direction[0], p_r + direction[1])))) + continue + + direction = (direction[0]*2, direction[1]*2) + if self.on_board(piece, direction): + if self.tiles[p_r + direction[1]][p_q + direction[0]] is None: + valid_moves.append(('JUMP',((p_q, p_r),(p_q + direction[0], p_r + direction[1])))) + + return valid_moves + + def on_board(self, piece, direction): + if piece.pos[0] + direction[0] < 0 or piece.pos[0] + direction[0] > 6: + return False + + elif piece.pos[1] + direction[1] < 0 or piece.pos[1] + direction[1] > 6: + return False + + return True + + def convert_coords_local(self, action): + if action[0] == 'EXIT': + return (action[0], (action[1][0], action[1][1])) + + return (action[0], ((action[1][0][0]+3, action[1][0][1]+3), (action[1][1][0]+3, action[1][1][1]+3))) + + def convert_coords_ref(self, action): + if action[0] == 'EXIT': + return (action[0], (action[1][0], action[1][1])) + + return (action[0], ((action[1][0][0]-3, action[1][0][1]-3), (action[1][1][0]-3, action[1][1][1]-3))) + + def move(self, action): + + if action[0] == 'MOVE' or action == 'JUMP': + piece = self.tiles[action[1][0][1]][action[1][0][0]] + self.tiles[action[1][0][1]][action[1][0][0]] = None + self.tiles[action[1][1][1]][action[1][1][0]] = piece + self.tiles[action[1][1][1]][action[1][1][0]].pos = action[1][1] + + elif action[0] == 'EXIT': + piece = self.tiles[action[1][0][1]][action[1][0][0]] + self.tiles[action[1][0][1]][action[1][0][0]] = None diff --git a/Snek/util/__pycache__/Board.cpython-36.pyc b/Snek/util/__pycache__/Board.cpython-36.pyc index d7b6d407082d4b6a3dd1d816ff2019a5e701dd54..803df5803e155c9ffd9182a00dfdeb7b5bf7d4e1 100644 Binary files a/Snek/util/__pycache__/Board.cpython-36.pyc and b/Snek/util/__pycache__/Board.cpython-36.pyc differ