Skip to content
Snippets Groups Projects
Commit 9c02b167 authored by Sean Paeglis's avatar Sean Paeglis
Browse files

p2

parent a5de58b0
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ class Player: ...@@ -4,7 +4,7 @@ class Player:
def __init__(self, colour): def __init__(self, colour):
self.colour = colour self.colour = colour
self.board = Board.Board() self.board = Board.Board(self.colour)
def on_board(self, piece, move): def on_board(self, piece, move):
if piece.pos[0] + move[0] < -3 or piece.pos[0] + move[0] > 3: if piece.pos[0] + move[0] < -3 or piece.pos[0] + move[0] > 3:
...@@ -16,12 +16,10 @@ class Player: ...@@ -16,12 +16,10 @@ class Player:
return True return True
def action(self): def action(self):
for piece in self.board.pieces: moves = self.board.get_moves(self.colour)
if piece.colour == self.colour: moves[0] = self.board.convert_coords_ref(moves[0])
print(piece.pos) print(moves[0])
for move in self.board.moves: return(moves[0])
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])))
def update(self, colour, action): def update(self, colour, action):
self.board.move(action) self.board.move(action)
No preview for this file type
...@@ -2,14 +2,14 @@ from Snek.util import Piece ...@@ -2,14 +2,14 @@ from Snek.util import Piece
class Board: class Board:
# List of move directions # List of move directions
moves = [(-1,0), (0,-1), (1,-1), (1,0), (0,1), (-1,1)] directions = [(-1,0), (0,-1), (1,-1), (1,0), (0,1), (-1,1)]
def __init__(self): def __init__(self, colour):
self.colour = colour
# List of exit tiles for each colour # List of exit tiles for each colour
self.exit_tiles = {'red':[(3,-3), (3,-2), (3,-1), (3,0)], 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)]}
'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 = [[None for i in range(7)] for j in range(7)]
self.tiles[0][0] = 0 self.tiles[0][0] = 0
...@@ -26,27 +26,93 @@ class Board: ...@@ -26,27 +26,93 @@ class Board:
self.tiles[6][6] = 0 self.tiles[6][6] = 0
# List of pieces on the board # List of pieces on the board
self.pieces = [] self.pieces = {'red':[], 'green':[], 'blue':[]}
for i in range(0,4):
self.pieces.append(Piece.Piece((-3,i),"red")) for i in range(3,7):
self.pieces['red'].append(Piece.Piece((0,i),"red"))
for i in range(0,4): for i in range(3,7):
self.pieces.append(Piece.Piece((i,-3),"green")) self.pieces['green'].append(Piece.Piece((i,0),"green"))
j = 3 j = 6
for i in range(0,4): for i in range(3,7):
self.pieces.append(Piece.Piece((i,j),"blue")) self.pieces['blue'].append(Piece.Piece((i,j),"blue"))
j = j - 1 j = j - 1
for piece in self.pieces: for piece in self.pieces['red']:
self.tiles[piece.pos[1]+3][piece.pos[0]+3] = piece 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 #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): def move(self, action):
if action[0] == 'MOVE' or action == 'JUMP': if action[0] == 'MOVE' or action == 'JUMP':
piece = self.tiles[action[1][0][1]+3][action[1][0][0]+3] piece = self.tiles[action[1][0][1]][action[1][0][0]]
self.tiles[action[1][0][1]+3][action[1][0][0]+3] = None self.tiles[action[1][0][1]][action[1][0][0]] = None
self.tiles[action[1][1][1]+3][action[1][1][0]+3] = piece self.tiles[action[1][1][1]][action[1][1][0]] = piece
self.tiles[action[1][1][1]+3][action[1][1][0]+3].pos = action[1][1] self.tiles[action[1][1][1]][action[1][1][0]].pos = action[1][1]
\ No newline at end of file
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
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment