def main(stdscr): global windowTooSmall h, w = stdscr.getmaxyx() H_LIM, W_LIM = 39, 120 if h < H_LIM or w < W_LIM: windowTooSmall = True curses.endwin() else: board = Board(18, 22) game = Game(board, stdscr) game.start()
def play(card_stack): """ driver function for the game program """ board = Board() display_board(board) while (True): popped_card = card_stack.pop() print("current card:", popped_card.value) if popped_card.value in "kqj": if popped_card.value == "k": if board.data[0][0].value != "-" and board.data[0][ 3].value != "-" and board.data[3][ 0].value != "-" and board.data[3][3].value != "-": print("you lost!") return if popped_card.value == "q": if board.data[1][0].value != "-" and board.data[2][ 0].value != "-" and board.data[1][ 3].value != "-" and board.data[2][3].value != "-": print("you lost!") return if popped_card.value == "j": if board.data[0][1].value != "-" and board.data[0][ 2].value != "-" and board.data[3][ 1].value != "-" and board.data[3][2].value != "-": print("you lost!") return usr_row = -1 usr_col = -1 while (coordinate_invalid(usr_row, usr_col, board, popped_card)): # avoid re-wrtitting values if they exist if (board.is_full()): print("board is full") delete_pairs(board) display_board(board) print("current card: ", popped_card.value) try: usr_row = int( input(Colors.BOLD + "enter row: " + Colors.NORMAL)) - 1 except: print("invalid please enter an int") while (row_invalid(usr_row)): try: usr_row = int( input(Colors.BOLD + "enter row: " + Colors.NORMAL)) - 1 except: print("invalid please enter an int") if (row_invalid(usr_row)): print("out of range; try again") try: usr_col = int( input(Colors.BOLD + "enter col: " + Colors.NORMAL)) - 1 except: print("invalid please enter an int") while (col_invalid(usr_col)): try: usr_col = int( input(Colors.BOLD + "enter col: " + Colors.NORMAL)) - 1 except: print("invalid please enter an int") if (col_invalid(usr_col)): print("out of range; try again") if (coordinate_invalid(usr_row, usr_col, board, popped_card) and not board.is_full()): print("invalid location") board.data[usr_row][usr_col] = popped_card board.add_count() board.add_card(popped_card) display_board(board) if (board.game_won()): print("you won!!!") return if (board.game_lost()): print("you lost...") return
def test_invalid_diagonal(self): invalid_grid = '4..5..6.2..........61..7..9...1...6315.......8.79.2......4...575..21.........32..' invalid_board = Board(invalid_grid) self.assertFalse(agent.solve(invalid_board.get_puzzle_dict()))
class TestDiagonalSudoku(unittest.TestCase): diagonal_grid = '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3' solved_diag_sudoku = { 'G7': '8', 'G6': '9', 'G5': '7', 'G4': '3', 'G3': '2', 'G2': '4', 'G1': '6', 'G9': '5', 'G8': '1', 'C9': '6', 'C8': '7', 'C3': '1', 'C2': '9', 'C1': '4', 'C7': '5', 'C6': '3', 'C5': '2', 'C4': '8', 'E5': '9', 'E4': '1', 'F1': '1', 'F2': '2', 'F3': '9', 'F4': '6', 'F5': '5', 'F6': '7', 'F7': '4', 'F8': '3', 'F9': '8', 'B4': '7', 'B5': '1', 'B6': '6', 'B7': '2', 'B1': '8', 'B2': '5', 'B3': '3', 'B8': '4', 'B9': '9', 'I9': '3', 'I8': '2', 'I1': '7', 'I3': '8', 'I2': '1', 'I5': '6', 'I4': '5', 'I7': '9', 'I6': '4', 'A1': '2', 'A3': '7', 'A2': '6', 'E9': '7', 'A4': '9', 'A7': '3', 'A6': '5', 'A9': '1', 'A8': '8', 'E7': '6', 'E6': '2', 'E1': '3', 'E3': '4', 'E2': '8', 'E8': '5', 'A5': '4', 'H8': '6', 'H9': '4', 'H2': '3', 'H3': '5', 'H1': '9', 'H6': '1', 'H7': '7', 'H4': '2', 'H5': '8', 'D8': '9', 'D9': '2', 'D6': '8', 'D7': '1', 'D4': '4', 'D5': '3', 'D2': '7', 'D3': '6', 'D1': '5' } board = Board(diagonal_grid) def test_solve(self): self.assertEqual(agent.solve(self.board.get_puzzle_dict()), self.solved_diag_sudoku) def test_invalid_diagonal(self): invalid_grid = '4..5..6.2..........61..7..9...1...6315.......8.79.2......4...575..21.........32..' invalid_board = Board(invalid_grid) self.assertFalse(agent.solve(invalid_board.get_puzzle_dict()))
class TestBoard(unittest.TestCase): puzzle = '2.............62....1....7...6..8...3...9...7...6..4...4....8....52.............3' puzzle_dict = { 'A1': '2', 'A2': '123456789', 'A3': '123456789', 'A4': '123456789', 'A5': '123456789', 'A6': '123456789', 'A7': '123456789', 'A8': '123456789', 'A9': '123456789', 'B1': '123456789', 'B2': '123456789', 'B3': '123456789', 'B4': '123456789', 'B5': '123456789', 'B6': '6', 'B7': '2', 'B8': '123456789', 'B9': '123456789', 'C1': '123456789', 'C2': '123456789', 'C3': '1', 'C4': '123456789', 'C5': '123456789', 'C6': '123456789', 'C7': '123456789', 'C8': '7', 'C9': '123456789', 'D1': '123456789', 'D2': '123456789', 'D3': '6', 'D4': '123456789', 'D5': '123456789', 'D6': '8', 'D7': '123456789', 'D8': '123456789', 'D9': '123456789', 'E1': '3', 'E2': '123456789', 'E3': '123456789', 'E4': '123456789', 'E5': '9', 'E6': '123456789', 'E7': '123456789', 'E8': '123456789', 'E9': '7', 'F1': '123456789', 'F2': '123456789', 'F3': '123456789', 'F4': '6', 'F5': '123456789', 'F6': '123456789', 'F7': '4', 'F8': '123456789', 'F9': '123456789', 'G1': '123456789', 'G2': '4', 'G3': '123456789', 'G4': '123456789', 'G5': '123456789', 'G6': '123456789', 'G7': '8', 'G8': '123456789', 'G9': '123456789', 'H1': '123456789', 'H2': '123456789', 'H3': '5', 'H4': '2', 'H5': '123456789', 'H6': '123456789', 'H7': '123456789', 'H8': '123456789', 'H9': '123456789', 'I1': '123456789', 'I2': '123456789', 'I3': '123456789', 'I4': '123456789', 'I5': '123456789', 'I6': '123456789', 'I7': '123456789', 'I8': '123456789', 'I9': '3' } board = Board(puzzle) def test_board_declaration(self): puzzle = self.puzzle + '.1' self.assertRaises(AssertionError, Board, puzzle) def test_board_puzzle_dict(self): self.assertEqual(self.board.get_puzzle_dict(), self.puzzle_dict) def test_update_board_1(self): # test update with invalid grid invalid_grid = self.puzzle = '..4' self.assertRaises(AssertionError, self.board.update_board_with_grid, invalid_grid) def test_update_board_2(self): new_grid = '267945381853716249491823576576438192384192657129657438642379815935281764718564923' self.board.update_board_with_grid(new_grid) self.assertEqual(self.board.puzzle, new_grid)
def __init__(self, surface, pageName): Page.__init__(self, surface, pageName) # | Flag to indicate whether the game is over or not self.gameOver = False # | Flag to indicate if the snake has hit an item (and therefore another needs to be placed) self.itemHit = False # | Time that is between each movement of the snake (in seconds) self.movementInterval = 1 / 14 # | board # |-------- boardXpos = 20 boardYpos = 20 boardDimensions = {'width': 48, 'height': 28} boardSpaceSideLength = 17 boardSpaceBuffer = 1 self.board = Board(boardXpos, boardYpos, boardDimensions, boardSpaceSideLength, boardSpaceBuffer) # | snake # |-------- snakeColumn = self.board.width // 2 snakeRow = self.board.height // 2 snakeSquares = self.board self.snake = Snake(snakeColumn, snakeRow, snakeSquares) # | ttlScore # |-------- self.playerScore = 0 ttlScoreXpos = 100 ttlScoreYpos = 550 ttlScoreText = "Score: " + str(self.playerScore) ttlScoreTextSize = 28 self.ttlScore = Title(ttlScoreXpos, ttlScoreYpos, ttlScoreText, ttlScoreTextSize) # | btnPause # |----------- btnPauseXpos = self.surface.get_width() // 2 btnPauseYpos = self.surface.get_height() - 40 btnPauseDimensions = {'width': 50, 'height': 50} btnPauseColour = colours.buttonColour btnPauseHoverColour = colours.buttonHoverColour btnPauseAction = "Pause" btnPauseText = "||" btnPauseTextSize = 34 btnPauseTextColour = colours.textColour self.btnPause = Button(btnPauseXpos, btnPauseYpos, btnPauseDimensions, btnPauseColour, btnPauseHoverColour, btnPauseAction, btnPauseText, btnPauseTextSize, btnPauseTextColour) self.addToObjects([self.board, self.ttlScore, self.btnPause]) self.addToButtons(self.btnPause) # | Place the first item self.placeNewItem() # | Start thread that moves the snake snakeMover = threading.Thread(target=self.moveSnake) snakeMover.setDaemon(True) # | Daemonic thread to prevent this thread keeping the program alive snakeMover.start() # | Start the thread that places items in the board itemPlacer = threading.Thread(target=self.placeItems) itemPlacer.setDaemon(True) # | Daemonic thread to prevent this thread keeping the program alive itemPlacer.start()
class Play(Page): def __init__(self, surface, pageName): Page.__init__(self, surface, pageName) # | Flag to indicate whether the game is over or not self.gameOver = False # | Flag to indicate if the snake has hit an item (and therefore another needs to be placed) self.itemHit = False # | Time that is between each movement of the snake (in seconds) self.movementInterval = 1 / 14 # | board # |-------- boardXpos = 20 boardYpos = 20 boardDimensions = {'width': 48, 'height': 28} boardSpaceSideLength = 17 boardSpaceBuffer = 1 self.board = Board(boardXpos, boardYpos, boardDimensions, boardSpaceSideLength, boardSpaceBuffer) # | snake # |-------- snakeColumn = self.board.width // 2 snakeRow = self.board.height // 2 snakeSquares = self.board self.snake = Snake(snakeColumn, snakeRow, snakeSquares) # | ttlScore # |-------- self.playerScore = 0 ttlScoreXpos = 100 ttlScoreYpos = 550 ttlScoreText = "Score: " + str(self.playerScore) ttlScoreTextSize = 28 self.ttlScore = Title(ttlScoreXpos, ttlScoreYpos, ttlScoreText, ttlScoreTextSize) # | btnPause # |----------- btnPauseXpos = self.surface.get_width() // 2 btnPauseYpos = self.surface.get_height() - 40 btnPauseDimensions = {'width': 50, 'height': 50} btnPauseColour = colours.buttonColour btnPauseHoverColour = colours.buttonHoverColour btnPauseAction = "Pause" btnPauseText = "||" btnPauseTextSize = 34 btnPauseTextColour = colours.textColour self.btnPause = Button(btnPauseXpos, btnPauseYpos, btnPauseDimensions, btnPauseColour, btnPauseHoverColour, btnPauseAction, btnPauseText, btnPauseTextSize, btnPauseTextColour) self.addToObjects([self.board, self.ttlScore, self.btnPause]) self.addToButtons(self.btnPause) # | Place the first item self.placeNewItem() # | Start thread that moves the snake snakeMover = threading.Thread(target=self.moveSnake) snakeMover.setDaemon(True) # | Daemonic thread to prevent this thread keeping the program alive snakeMover.start() # | Start the thread that places items in the board itemPlacer = threading.Thread(target=self.placeItems) itemPlacer.setDaemon(True) # | Daemonic thread to prevent this thread keeping the program alive itemPlacer.start() # | update() # |-------------------------------------------------- # | Code to update the page. Essentially code that # | is neither drawing the page, or handling # | events, but code that still needs to # | be ran each loop of the game. # |------------------------- def update(self): # | If there is less than one collectible on the board, place another if self.board.collectibles < 1: self.placeNewItem() if self.playerScore != self.snake.getScore(): self.playerScore = self.snake.getScore() self.ttlScore.changeText("Score: " + str(self.playerScore)) # | handleEvent() # |------------------------------------------------------------ # | Checks for the special case event of the snake dying, and # | returns the appropriate value to indicate what happened. # |------------------------------------------------------- def handleEvent(self, event): action = None if event.type == Globals.DEADSNAKE: action = "GameOver" elif event.type == pygame.KEYDOWN: self.changeSnakeDirection(event) else: action = Page.handleEvent(self, event) return action # | placeNewItem() # |------------------------------------------------------ # | Places a new item in a random location on the board. # |--------------------------------------------------- def placeNewItem(self): spaceOccupied = True # | Ensure that the new item is placed in an empty space while spaceOccupied: column = random.randint(0, self.board.width - 1) row = random.randint(0, self.board.height - 1) # | If the square is empty if self.board.getSpace(column, row).isEmpty(): spaceOccupied = False item = Apple(column, row) self.board.giveItem(item) # | Reset the flag to place an item self.itemHit = False # | moveSnake() # |----------------------------------------------------- # | Method to be called in a thread to move the snake. # |------------------------------------------------ def moveSnake(self): while self.snake.isAlive: if not self.paused: self.snake.move(self.board) time.sleep(self.movementInterval) # | changeSnakeDirection() # |---------------------------------------- # | Changes the direction the snake moves # | based on the keys that are pressed. # |--------------------------------- def changeSnakeDirection(self, event): if event.key == pygame.K_DOWN: self.snake.changeDirection('down') elif event.key == pygame.K_UP: self.snake.changeDirection('up') elif event.key == pygame.K_RIGHT: self.snake.changeDirection('right') elif event.key == pygame.K_LEFT: self.snake.changeDirection('left') # | placeItem() # |---------------------------------------------------- # | Makes a random decision to place an item on the # | board every random interval (between 1 and 5 # | seconds, with a 1 in 5 chance each time). # |------------------------------------- def placeItems(self): while self.snake.isAlive: if not self.paused: timeToWait = random.randint(1, 5) chance = random.randint(1, 5) if chance == 1: self.placeNewItem() time.sleep(timeToWait)
from utils import agent from utils.utils import history from utils.puzzles import puzzle_choices from objects.Board import Board # Declare units if __name__ == "__main__": sudoku_puzzle = '' if len(sys.argv) == 2: sudoku_puzzle = str(sys.argv[1]) else: sudoku_puzzle = puzzle_choices[random.randrange(6)] # random.randrange(4) board = Board(sudoku_puzzle) board.display_board() # run agent solution = agent.solve(board.get_puzzle_dict()) if not solution: sys.exit('Invalid diagonal puzzle!') # update and display solved board board.update_board_with_dict(solution) board.display_board() try: import PySudoku play_board = Board(sudoku_puzzle)