def setUp(self): self.b = Board(9) self.v = View(self.b)
class ViewTest(unittest.TestCase): def setUp(self): self.b = Board(9) self.v = View(self.b) def test_init(self): self.assertEqual(self.v._board, self.b) self.assertEqual(self.v._cursor, (1, 1)) self.assertEqual(self.v._hoshis, ( (3, 3), (7, 3), (3, 7), (7, 7), (5, 5), )) e = str(Board.EMPTY) h = View.HOSHI self.assertEqual(self.v._array, [ [e, e, e, e, e, e, e, e, e], [e, e, e, e, e, e, e, e, e], [e, e, h, e, e, e, h, e, e], [e, e, e, e, e, e, e, e, e], [e, e, e, e, h, e, e, e, e], [e, e, e, e, e, e, e, e, e], [e, e, h, e, e, e, h, e, e], [e, e, e, e, e, e, e, e, e], [e, e, e, e, e, e, e, e, e], ]) def test_get_hoshis(self): self.assertEqual(self.v._get_hoshis(19), ( (4, 4), (16, 4), (4, 16), (16, 16), (10, 10), (4, 10), (10, 4), (16, 10), (10, 16), )) self.assertEqual(self.v._get_hoshis(18), ( (4, 4), (15, 4), (4, 15), (15, 15), )) self.assertEqual(self.v._get_hoshis(13), ( (4, 4), (10, 4), (4, 10), (10, 10), (7, 7), )) def test_in_width(self): self.assertEqual(self.v._in_width(100), 9) self.assertEqual(self.v._in_width(-123), 1) self.assertEqual(self.v._in_width(3), 3) def test_in_height(self): self.assertEqual(self.v._in_height(100), 9) self.assertEqual(self.v._in_height(-123), 1) self.assertEqual(self.v._in_height(3), 3) def test_cursor_movement(self): self.assertEqual(self.v.cursor, (1, 1)) self.v.cursor_up() self.v.cursor_left() self.assertEqual(self.v.cursor, (1, 1)) self.v.cursor_right() self.v.cursor_right() self.v.cursor_down() self.assertEqual(self.v.cursor, (3, 2)) self.v.cursor_up() self.v.cursor_left() self.v.cursor_down() self.v.cursor_down() self.v.cursor_down() self.assertEqual(self.v.cursor, (2, 4)) for i in range(12): self.v.cursor_right() self.v.cursor_down() self.assertEqual(self.v.cursor, (9, 9)) def test_str(self): self.assertEqual( str(self.v), 'X . . . . . . . .\n' '. . . . . . . . .\n' '. . + . . . + . .\n' '. . . . . . . . .\n' '. . . . + . . . .\n' '. . . . . . . . .\n' '. . + . . . + . .\n' '. . . . . . . . .\n' '. . . . . . . . .')
def main(): # Get arguments parser = argparse.ArgumentParser(description='Starts a game of go in the terminal.') parser.add_argument('-s', '--size', type=int, default=19, help='size of board') args = parser.parse_args() if args.size < 7 or args.size > 19: sys.stdout.write('Board size must be between 7 and 19!\n') sys.exit(0) # Initialize board and view board = Board(args.size) view = View(board) err = None # User actions def move(): """ Makes a move at the current position of the cursor for the current turn. """ board.move(*view.cursor) view.redraw() def undo(): """ Undoes the last move. """ board.undo() view.redraw() def redo(): """ Redoes an undone move. """ board.redo() view.redraw() def exit(): """ Exits the game. """ sys.exit(0) # Action keymap KEYS = { 'w': view.cursor_up, 's': view.cursor_down, 'a': view.cursor_left, 'd': view.cursor_right, ' ': move, 'u': undo, 'r': redo, '\x1b': exit, } # Main loop while True: # Print board clear() sys.stdout.write('{0}\n'.format(view)) sys.stdout.write('Black: {black} <===> White: {white}\n'.format(**board.score)) sys.stdout.write('{0}\'s move... '.format(board.turn)) if err: sys.stdout.write('\n' + err + '\n') err = None # Get action key c = getch() try: # Execute selected action KEYS[c]() except BoardError as be: # Board error (move on top of other piece, suicidal move, etc.) err = be.message except KeyError: # Action not found, do nothing pass
class ViewTest(unittest.TestCase): def setUp(self): self.b = Board(9) self.v = View(self.b) def test_init(self): self.assertEqual(self.v._board, self.b) self.assertEqual(self.v._cursor, (1, 1)) self.assertEqual(self.v._hoshis, ( (3, 3), (7, 3), (3, 7), (7, 7), (5, 5), )) e = str(Board.EMPTY) h = View.HOSHI self.assertEqual(self.v._array, [ [e, e, e, e, e, e, e, e, e], [e, e, e, e, e, e, e, e, e], [e, e, h, e, e, e, h, e, e], [e, e, e, e, e, e, e, e, e], [e, e, e, e, h, e, e, e, e], [e, e, e, e, e, e, e, e, e], [e, e, h, e, e, e, h, e, e], [e, e, e, e, e, e, e, e, e], [e, e, e, e, e, e, e, e, e], ]) def test_get_hoshis(self): self.assertEqual(self.v._get_hoshis(19), ( (4, 4), (16, 4), (4, 16), (16, 16), (10, 10), (4, 10), (10, 4), (16, 10), (10, 16), )) self.assertEqual(self.v._get_hoshis(18), ( (4, 4), (15, 4), (4, 15), (15, 15), )) self.assertEqual(self.v._get_hoshis(13), ( (4, 4), (10, 4), (4, 10), (10, 10), (7, 7), )) def test_in_width(self): self.assertEqual(self.v._in_width(100), 9) self.assertEqual(self.v._in_width(-123), 1) self.assertEqual(self.v._in_width(3), 3) def test_in_height(self): self.assertEqual(self.v._in_height(100), 9) self.assertEqual(self.v._in_height(-123), 1) self.assertEqual(self.v._in_height(3), 3) def test_cursor_movement(self): self.assertEqual(self.v.cursor, (1, 1)) self.v.cursor_up() self.v.cursor_left() self.assertEqual(self.v.cursor, (1, 1)) self.v.cursor_right() self.v.cursor_right() self.v.cursor_down() self.assertEqual(self.v.cursor, (3, 2)) self.v.cursor_up() self.v.cursor_left() self.v.cursor_down() self.v.cursor_down() self.v.cursor_down() self.assertEqual(self.v.cursor, (2, 4)) for i in range(12): self.v.cursor_right() self.v.cursor_down() self.assertEqual(self.v.cursor, (9, 9)) def test_str(self): self.assertEqual( str(self.v), 'X . . . . . . . .\n' '. . . . . . . . .\n' '. . + . . . + . .\n' '. . . . . . . . .\n' '. . . . + . . . .\n' '. . . . . . . . .\n' '. . + . . . + . .\n' '. . . . . . . . .\n' '. . . . . . . . .' )
def main(): # Get arguments """ parser = argparse.ArgumentParser(description='Starts a game of go in the terminal.') parser.add_argument('-s', '--size', type=int, default=19, help='size of board') args = parser.parse_args() if args.size < 7 or args.size > 19: sys.stdout.write('Board size must be between 7 and 19!\n') sys.exit(0) """ # Initialize board and view board = Board(7) view = View(board) err = None board_history = deque(maxlen=8) # User actions def move(): """ Makes a move at the current position of the cursor for the current turn. """ print("Getting our input for x and y") x = input("Input x:") y = input("Input y:") view.set_coordinates(x,y) board.move(*view.cursor) view.redraw() print("Board state") #print(*board._state) #This is list board_state = [] #Creating the board state for item in board._state[0]: row = [] for elem in item: #print(type(elem)) if elem == Location('empty'): row.append(0) elif elem == Location('black'): row.append(1) else: row.append(2) board_state.append(row) board_history.append(board_state) print(board_state) ctr = 0 for item in board_history: print(ctr," ",item) ctr = ctr + 1 #print("\n".join(board_history)) def undo(): """ Undoes the last move. """ board.undo() view.redraw() def redo(): """ Redoes an undone move. """ board.redo() view.redraw() def exit(): """ Exits the game. """ sys.exit(0) # Action keymap KEYS = { 'w': view.cursor_up, 's': view.cursor_down, 'a': view.cursor_left, 'd': view.cursor_right, ' ': move, 'u': undo, 'r': redo, '\x1b': exit, } # Main loop while True: # Print board #clear() sys.stdout.write('{0}\n'.format(view)) sys.stdout.write('Black: {black} <===> White: {white}\n'.format(**board.score)) sys.stdout.write('{0}\'s move... '.format(board.turn)) if err: sys.stdout.write('\n' + err + '\n') err = None # Get action key # c = getch() try: # Execute selected action #KEYS[c]() move() except BoardError as be: # Board error (move on top of other piece, suicidal move, etc.) print("Illegal Move") #err = be.message except KeyError: # Action not found, do nothing pass