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

Project Start

parents
No related branches found
No related tags found
No related merge requests found
Pipeline #488 failed
File added
File added
File added
File added
class Block:
def __init__(self, pos):
self.pos = pos
self.visited = []
Board.py 0 → 100644
import Tile
class Board:
directions = ['l', 'tl', 'tr', 'r', 'br', 'bl']
moves = {'l':(-1,0), 'tl':(0,-1), 'tr':(1,-1), 'r':(1,0),
'br':(0,1), 'bl':(-1,1)}
tiles = [[None for i in range(7)] for j in range(7)]
def __init__(self):
r = -3
for q in range(0, 4):
self.new_tile(q,r)
r = -2
for q in range(-1, 4):
self.new_tile(q,r)
r = -1
for q in range(-2, 4):
self.new_tile(q,r)
r = 0
for q in range(-3, 4):
self.new_tile(q,r)
r = 1
for q in range(-3, 3):
self.new_tile(q,r)
r = 2
for q in range(-3, 2):
self.new_tile(q,r)
r = 3
for q in range(-3, 1):
self.new_tile(q,r)
def new_tile(self, q, r):
tile = Tile.Tile((q,r))
self.tiles[r+3][q+3] = tile
def move(self, t_from, t_to):
tile_from = self.tiles[t_from[1]+3][t_from[0]+3]
tile_to = self.tiles[t_to[1]+3][t_to[0]+3]
tile_from.piece.pos = t_to
tile_to.piece = tile_from.piece
tile_from.piece = None
def distance(self, start, end):
q1 = start.pos[0]
q2 = end.pos[0]
r1 = start.pos[1]
r2 = end.pos[1]
return (abs(q1 - q2) + abs(q1 + r1 - q2 - r2) + abs(r1 - r2))/2
class Piece:
def __init__(self, pos):
self.pos = pos
self.visited = [self.pos]
State.py 0 → 100644
class State:
def __init__(self, pieces, blocks):
self.pieces = pieces
self.blocks = blocks
def apply(self, move):
for piece in self.pieces:
if piece.pos[0] == move[0] and piece.pos[1] == move[1]:
piece.pos[0] += move[0]
piece.pos[1] += move[1]
Tile.py 0 → 100644
import Piece
import Block
class Tile:
def __init__(self, pos):
self.pos = pos
self.piece = None
self.block = None
def add_piece(self, piece):
self.piece = piece
self.piece.pos = self.pos
def add_block(self, block):
self.block = block
self.block.pos = self.pos
File added
File added
File added
File added
File added
File added
game.py 0 → 100644
import sys
import json
import Board
import Piece
import Block
board = Board.Board()
def main():
with open(sys.argv[1]) as file:
data = json.load(file)
bdict = {}
for p in data['pieces']:
piece = Piece.Piece(tuple(p))
for row in board.tiles:
for tile in row:
if tile is not None and tile.pos == piece.pos:
tile.add_piece(piece)
for b in data['blocks']:
block = Block.Block(tuple(b))
for row in board.tiles:
for tile in row:
if tile is not None and tile.pos == block.pos:
tile.add_block(block)
bdict = make_bdict()
print_board(bdict)
board.move((0,0),(0,1))
bdict = make_bdict()
print_board(bdict)
print(board.distance(board.tiles[3][3], board.tiles[2][6]))
def make_bdict():
bdict = {}
for row in board.tiles:
for tile in row:
if tile is not None and tile.piece is not None:
bdict[tile.piece.pos] = 'p'
elif tile is not None and tile.block is not None:
bdict[tile.block.pos] = 'b'
return bdict
def print_board(board_dict, message="", debug=False, **kwargs):
"""
Helper function to print a drawing of a hexagonal board's contents.
Arguments:
* `board_dict` -- dictionary with tuples for keys and anything printable
for values. The tuple keys are interpreted as hexagonal coordinates (using
the axial coordinate system outlined in the project specification) and the
values are formatted as strings and placed in the drawing at the corres-
ponding location (only the first 5 characters of each string are used, to
keep the drawings small). Coordinates with missing values are left blank.
Keyword arguments:
* `message` -- an optional message to include on the first line of the
drawing (above the board) -- default `""` (resulting in a blank message).
* `debug` -- for a larger board drawing that includes the coordinates
inside each hex, set this to `True` -- default `False`.
* Or, any other keyword arguments! They will be forwarded to `print()`.
"""
# Set up the board template:
if not debug:
# Use the normal board template (smaller, not showing coordinates)
template = """# {0}
# .-'-._.-'-._.-'-._.-'-.
# |{16:}|{23:}|{29:}|{34:}|
# .-'-._.-'-._.-'-._.-'-._.-'-.
# |{10:}|{17:}|{24:}|{30:}|{35:}|
# .-'-._.-'-._.-'-._.-'-._.-'-._.-'-.
# |{05:}|{11:}|{18:}|{25:}|{31:}|{36:}|
# .-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-.
# |{01:}|{06:}|{12:}|{19:}|{26:}|{32:}|{37:}|
# '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'
# |{02:}|{07:}|{13:}|{20:}|{27:}|{33:}|
# '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'
# |{03:}|{08:}|{14:}|{21:}|{28:}|
# '-._.-'-._.-'-._.-'-._.-'-._.-'
# |{04:}|{09:}|{15:}|{22:}|
# '-._.-'-._.-'-._.-'-._.-'"""
else:
# Use the debug board template (larger, showing coordinates)
template = """# {0}
# ,-' `-._,-' `-._,-' `-._,-' `-.
# | {16:} | {23:} | {29:} | {34:} |
# | 0,-3 | 1,-3 | 2,-3 | 3,-3 |
# ,-' `-._,-' `-._,-' `-._,-' `-._,-' `-.
# | {10:} | {17:} | {24:} | {30:} | {35:} |
# | -1,-2 | 0,-2 | 1,-2 | 2,-2 | 3,-2 |
# ,-' `-._,-' `-._,-' `-._,-' `-._,-' `-._,-' `-.
# | {05:} | {11:} | {18:} | {25:} | {31:} | {36:} |
# | -2,-1 | -1,-1 | 0,-1 | 1,-1 | 2,-1 | 3,-1 |
# ,-' `-._,-' `-._,-' `-._,-' `-._,-' `-._,-' `-._,-' `-.
# | {01:} | {06:} | {12:} | {19:} | {26:} | {32:} | {37:} |
# | -3, 0 | -2, 0 | -1, 0 | 0, 0 | 1, 0 | 2, 0 | 3, 0 |
# `-._,-' `-._,-' `-._,-' `-._,-' `-._,-' `-._,-' `-._,-'
# | {02:} | {07:} | {13:} | {20:} | {27:} | {33:} |
# | -3, 1 | -2, 1 | -1, 1 | 0, 1 | 1, 1 | 2, 1 |
# `-._,-' `-._,-' `-._,-' `-._,-' `-._,-' `-._,-'
# | {03:} | {08:} | {14:} | {21:} | {28:} |
# | -3, 2 | -2, 2 | -1, 2 | 0, 2 | 1, 2 | key:
# `-._,-' `-._,-' `-._,-' `-._,-' `-._,-' ,-' `-.
# | {04:} | {09:} | {15:} | {22:} | | input |
# | -3, 3 | -2, 3 | -1, 3 | 0, 3 | | q, r |
# `-._,-' `-._,-' `-._,-' `-._,-' `-._,-'"""
# prepare the provided board contents as strings, formatted to size.
ran = range(-3, +3+1)
cells = []
for qr in [(q,r) for q in ran for r in ran if -q-r in ran]:
if qr in board_dict:
cell = str(board_dict[qr]).center(5)
else:
cell = " " # 5 spaces will fill a cell
cells.append(cell)
# fill in the template to create the board drawing, then print!
board = template.format(message, *cells)
print(board, **kwargs)
if __name__ == '__main__':
main()
{
"colour": "red",
"pieces": [[0,0],[0,-1],[-2,1]],
"blocks": [[-1,0],[-1,1],[1,1],[3,-1]]
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment