class TestProducePathBetweenTwoSquares: from Model import Board test_board = Board() a1 = test_board.dict_of_64_squares["a1"] f1 = test_board.dict_of_64_squares["f1"] d4 = test_board.dict_of_64_squares["d4"] b6 = test_board.dict_of_64_squares["b6"] b7 = test_board.dict_of_64_squares["b7"] test_path_one = [ test_board.dict_of_64_squares["b1"], test_board.dict_of_64_squares["c1"], test_board.dict_of_64_squares["d1"], test_board.dict_of_64_squares["e1"], ] test_path_two = [ test_board.dict_of_64_squares["b2"], test_board.dict_of_64_squares["c3"], ] test_path_three = [ test_board.dict_of_64_squares["c5"], ] test_path_four = [] @pytest.mark.parametrize( "test_input_1, test_input_2, test_input_3, expected", [ (a1, f1, test_board, test_path_one), (a1, d4, test_board, test_path_two), (d4, b6, test_board, test_path_three), (b6, b7, test_board, test_path_four), (a1, b6, test_board, test_path_four), ]) def test_works_for_expected_input(self, test_input_1, test_input_2, test_input_3, expected): # Returns expected value when passed valid input. result = produce_path_between_two_squares(test_input_1, test_input_2, test_input_3) assert set(result) == set( expected ), "Expected path to match expected set of Squares but it did not."
class TestIsLegalMoveCastling: white_player = Player(1) black_player = Player(0) board_one = Board() board_one.pieces_into_specified_positions({ "a1": Rook(None, white_player), "b1": Knight(None, white_player), "c1": Bishop(None, white_player), "d1": Queen(None, white_player), "e1": King(None, white_player), "c4": Bishop(None, white_player), "f3": Knight(None, white_player), "h1": Rook(None, white_player), "a2": Pawn(None, white_player), "b2": Pawn(None, white_player), "c2": Pawn(None, white_player), "d2": Pawn(None, white_player), "e4": Pawn(None, white_player), "f2": Pawn(None, white_player), "g2": Pawn(None, white_player), "h2": Pawn(None, white_player), "a7": Pawn(None, black_player), "b7": Pawn(None, black_player), "c7": Pawn(None, black_player), "d6": Pawn(None, black_player), "e5": Pawn(None, black_player), "f7": Pawn(None, black_player), "g7": Pawn(None, black_player), "h7": Pawn(None, black_player), "a8": Rook(None, black_player), "b8": Knight(None, black_player), "g4": Bishop(None, black_player), "d8": Queen(None, black_player), "e8": King(None, black_player), "f8": Bishop(None, black_player), "g8": Knight(None, black_player), "h8": Rook(None, black_player), }) board_two = Board() board_two.pieces_into_specified_positions({ "a1": Rook(None, white_player), "b1": Knight(None, white_player), "c1": Bishop(None, white_player), "d1": Queen(None, white_player), "e1": King(None, white_player, True), "c4": Bishop(None, white_player), "f3": Knight(None, white_player), "h1": Rook(None, white_player), "a2": Pawn(None, white_player), "b2": Pawn(None, white_player), "c2": Pawn(None, white_player), "d2": Pawn(None, white_player), "e4": Pawn(None, white_player), "f2": Pawn(None, white_player), "g2": Pawn(None, white_player), "h2": Pawn(None, white_player), "a7": Pawn(None, black_player), "b7": Pawn(None, black_player), "c7": Pawn(None, black_player), "d6": Pawn(None, black_player), "e5": Pawn(None, black_player), "f7": Pawn(None, black_player), "g7": Pawn(None, black_player), "h7": Pawn(None, black_player), "a8": Rook(None, black_player), "b8": Knight(None, black_player), "g4": Bishop(None, black_player), "d8": Queen(None, black_player), "e8": King(None, black_player), "f8": Bishop(None, black_player), "g8": Knight(None, black_player), "h8": Rook(None, black_player), }) board_three = Board() board_three.pieces_into_specified_positions({ "a1": Rook(None, white_player), "b1": Knight(None, white_player), "c1": Bishop(None, white_player), "d1": Queen(None, white_player), "e1": King(None, white_player), "c4": Bishop(None, white_player), "f3": Knight(None, white_player), "h1": Rook(None, white_player, True), "a2": Pawn(None, white_player), "b2": Pawn(None, white_player), "c2": Pawn(None, white_player), "d2": Pawn(None, white_player), "e4": Pawn(None, white_player), "f2": Pawn(None, white_player), "g2": Pawn(None, white_player), "h2": Pawn(None, white_player), "a7": Pawn(None, black_player), "b7": Pawn(None, black_player), "c7": Pawn(None, black_player), "d6": Pawn(None, black_player), "e5": Pawn(None, black_player), "f7": Pawn(None, black_player), "g7": Pawn(None, black_player), "h7": Pawn(None, black_player), "a8": Rook(None, black_player), "b8": Knight(None, black_player), "g4": Bishop(None, black_player), "d8": Queen(None, black_player), "e8": King(None, black_player), "f8": Bishop(None, black_player), "g8": Knight(None, black_player), "h8": Rook(None, black_player), }) board_four = Board() board_four.pieces_into_specified_positions({ "a1": Rook(None, white_player), "b1": Knight(None, white_player), "c1": Bishop(None, white_player), "d1": Queen(None, white_player), "e1": King(None, white_player), "f3": Knight(None, white_player), "h1": Rook(None, white_player), "a2": Pawn(None, white_player), "b2": Pawn(None, white_player), "c2": Pawn(None, white_player), "d2": Pawn(None, white_player), "e4": Pawn(None, white_player), "f2": Pawn(None, white_player), "g2": Pawn(None, white_player), "h2": Pawn(None, white_player), "a7": Pawn(None, black_player), "b6": Pawn(None, black_player), "c7": Pawn(None, black_player), "d7": Pawn(None, black_player), "e5": Pawn(None, black_player), "f7": Pawn(None, black_player), "g7": Pawn(None, black_player), "h7": Pawn(None, black_player), "a8": Rook(None, black_player), "b8": Knight(None, black_player), "a6": Bishop(None, black_player), "d8": Queen(None, black_player), "e8": King(None, black_player), "f8": Bishop(None, black_player), "g8": Knight(None, black_player), "h8": Rook(None, black_player), }) board_five = Board() board_five.pieces_into_specified_positions({ "a1": Rook(None, white_player), "b1": Knight(None, white_player), "c1": Bishop(None, white_player), "d1": Queen(None, white_player), "e1": King(None, white_player), "f3": Knight(None, white_player), "h1": Rook(None, white_player), "a2": Pawn(None, white_player), "b2": Pawn(None, white_player), "c2": Pawn(None, white_player), "d2": Pawn(None, white_player), "e4": Pawn(None, white_player), "f2": Pawn(None, white_player), "g2": Pawn(None, white_player), "h2": Pawn(None, white_player), "a7": Pawn(None, black_player), "b6": Pawn(None, black_player), "c7": Pawn(None, black_player), "d7": Pawn(None, black_player), "e5": Pawn(None, black_player), "f7": Pawn(None, black_player), "g7": Pawn(None, black_player), "h7": Pawn(None, black_player), "a8": Rook(None, black_player), "b8": Knight(None, black_player), "f1": Bishop(None, black_player), "d8": Queen(None, black_player), "e8": King(None, black_player), "f8": Bishop(None, black_player), "g8": Knight(None, black_player), "h8": Rook(None, black_player), }) board_six = Board() board_six.pieces_into_specified_positions({ "a1": Rook(None, white_player), "b1": Knight(None, white_player), "c1": Bishop(None, white_player), "d1": Queen(None, white_player), "e1": King(None, white_player), "f1": Bishop(None, white_player), "f3": Knight(None, white_player), "h1": Rook(None, white_player), "a2": Pawn(None, white_player), "b2": Pawn(None, white_player), "c2": Pawn(None, white_player), "d2": Pawn(None, white_player), "e2": Pawn(None, white_player), "f2": Pawn(None, white_player), "g2": Pawn(None, white_player), "h2": Pawn(None, white_player), "a7": Pawn(None, black_player), "b7": Pawn(None, black_player), "c7": Pawn(None, black_player), "d7": Pawn(None, black_player), "e7": Pawn(None, black_player), "f7": Pawn(None, black_player), "g7": Pawn(None, black_player), "h7": Pawn(None, black_player), "a8": Rook(None, black_player), "b8": Knight(None, black_player), "c8": Bishop(None, black_player), "d8": Queen(None, black_player), "e8": King(None, black_player), "f8": Bishop(None, black_player), "g8": Knight(None, black_player), "h8": Rook(None, black_player), }) board_seven = Board() board_seven.pieces_into_specified_positions({ "a1": Rook(None, white_player), "a3": Knight(None, white_player), "e3": Bishop(None, white_player), "d3": Queen(None, white_player), "e1": King(None, white_player), "f1": Bishop(None, white_player), "f3": Knight(None, white_player), "h1": Rook(None, white_player), "a2": Pawn(None, white_player), "b2": Pawn(None, white_player), "c2": Pawn(None, white_player), "d4": Pawn(None, white_player), "e2": Pawn(None, white_player), "f2": Pawn(None, white_player), "g2": Pawn(None, white_player), "h2": Pawn(None, white_player), "a7": Pawn(None, black_player), "b7": Pawn(None, black_player), "c6": Pawn(None, black_player), "d5": Pawn(None, black_player), "e6": Pawn(None, black_player), "f7": Pawn(None, black_player), "g7": Pawn(None, black_player), "h7": Pawn(None, black_player), "a8": Rook(None, black_player), "b8": Knight(None, black_player), "c8": Bishop(None, black_player), "a5": Queen(None, black_player), "e8": King(None, black_player), "f8": Bishop(None, black_player), "g8": Knight(None, black_player), "h8": Rook(None, black_player), }) board_eight = Board() board_eight.pieces_into_specified_positions({ "a1": Rook(None, white_player), "e3": Bishop(None, white_player), "d2": Queen(None, white_player), "e1": King(None, white_player), "f1": Bishop(None, white_player), "f3": Knight(None, white_player), "h1": Rook(None, white_player), "a2": Pawn(None, white_player), "b2": Pawn(None, white_player), "d4": Pawn(None, white_player), "e2": Pawn(None, white_player), "f2": Pawn(None, white_player), "g2": Pawn(None, white_player), "h2": Pawn(None, white_player), "a7": Pawn(None, black_player), "b7": Pawn(None, black_player), "c7": Pawn(None, black_player), "d6": Pawn(None, black_player), "e7": Pawn(None, black_player), "f7": Pawn(None, black_player), "g7": Pawn(None, black_player), "h7": Pawn(None, black_player), "a8": Rook(None, black_player), "b8": Knight(None, black_player), "b1": Bishop(None, black_player), "d8": Queen(None, black_player), "e8": King(None, black_player), "f8": Bishop(None, black_player), "g8": Knight(None, black_player), "h8": Rook(None, black_player), }) turn_one = Turn(starting_board=board_one, player=white_player, starting_square=board_one.dict_of_64_squares["e1"], ending_square=board_one.dict_of_64_squares["g1"]) turn_two = Turn(starting_board=board_one, player=white_player, starting_square=board_one.dict_of_64_squares["h1"], ending_square=board_one.dict_of_64_squares["f1"]) turn_three = Turn(starting_board=board_one, player=white_player, starting_square=board_one.dict_of_64_squares["e1"], ending_square=board_one.dict_of_64_squares["f1"]) turn_four = Turn(starting_board=board_one, player=white_player, starting_square=board_one.dict_of_64_squares["e1"], ending_square=board_one.dict_of_64_squares["e2"]) turn_five = Turn(starting_board=board_two, player=white_player, starting_square=board_two.dict_of_64_squares["e1"], ending_square=board_two.dict_of_64_squares["g1"]) turn_six = Turn(starting_board=board_three, player=white_player, starting_square=board_three.dict_of_64_squares["e1"], ending_square=board_three.dict_of_64_squares["g1"]) turn_seven = Turn(starting_board=board_four, player=white_player, starting_square=board_four.dict_of_64_squares["e1"], ending_square=board_four.dict_of_64_squares["g1"]) turn_eight = Turn(starting_board=board_five, player=white_player, starting_square=board_five.dict_of_64_squares["e1"], ending_square=board_five.dict_of_64_squares["g1"]) turn_nine = Turn(starting_board=board_six, player=white_player, starting_square=board_six.dict_of_64_squares["e1"], ending_square=board_six.dict_of_64_squares["g1"]) turn_ten = Turn(starting_board=board_seven, player=white_player, starting_square=board_seven.dict_of_64_squares["e1"], ending_square=board_seven.dict_of_64_squares["g1"]) turn_eleven = Turn(starting_board=board_eight, player=white_player, starting_square=board_eight.dict_of_64_squares["e1"], ending_square=board_eight.dict_of_64_squares["g1"]) @pytest.mark.parametrize( "test_input, expected", [ (turn_one, True), # No complications (turn_two, False), # Rook selected to move instead of King (turn_three, False), # King moved one space over instead of two (turn_four, False), # King moved to different row (turn_five, False), # King was moved in a previous turn (turn_six, False), # Rook was moved in a previous turn (turn_seven, False), # King passes through check while castling (turn_eight, False), # King's path is blocked by enemy piece (turn_nine, False), # King's path is blocked by own piece (turn_ten, False), # King starts off in check (turn_eleven, False), # Rook's path is blocked by enemy piece ]) def test_works_for_expected_input(self, test_input, expected): # Returns expected value when passed valid input. result = test_input.is_legal_move_castling() assert result == expected, "Expected Turn to identify legal castling move."
def __init__(self): self.gameState = 1 #State of game: 1 if playing, 0 if game over self.player = 'white' #Current player: white player always starts self.board = Board() #The board representation self.control = Control() #The control that handles user input self.sp = Speech() #The verbal feedback given by the computer
class CF: from globaldb import global_conn, global_cache from Model import Section,Status,UserInfo,UserAuth,\ Board,Post,Action,ReadMark,Mail,Permissions,UserSign,\ Favourite, Clipboard, Disgest,FreqControl, Team, Admin,\ Query, Deny, Notify, Notice from perm import ArgoTeam db = global_conn ch = global_cache section = Section() status = Status() userinfo = UserInfo() usersign = UserSign() board = Board() post = Post() mail = Mail() readmark = ReadMark(post=post) perm = Permissions() favourite = Favourite() clipboard = Clipboard() disgest = Disgest() freq = FreqControl() notify = Notify() notice = Notice() team = Team() userperm = ArgoTeam(team, perm) auth = UserAuth(table=userinfo, status=status, userperm=userperm, favourite=favourite, team=team) action = Action(board, status, post, mail, userinfo, readmark, notify) deny = Deny() admin = Admin(board, userperm, post, section, deny, userinfo, mail) query = Query(board=board, userperm=userperm, perm=perm, favourite=favourite, section=section, post=post, userinfo=userinfo, team=team) loads = [ section, status, userinfo, auth, board, post, readmark, mail, usersign, favourite, clipboard, disgest, freq, perm, team, userperm, admin, deny, notify, notice ] use = { "section": section, "status": status, "userinfo": userinfo, "auth": auth, "board": board, "post": post, "action": action, "readmark": readmark, "perm": perm, "usersign": usersign, "favourite": favourite, "mail": mail, "clipboard": clipboard, "disgest": disgest, "freq": freq, "userperm": userperm, "team": team, "admin": admin, "query": query, "deny": deny, "notify": notify, "notice": notice, } @classmethod def load(cls): for model in cls.loads: model.bind(db=cls.db, ch=cls.ch)
class Game(): def __init__(self): self.gameState = 1 #State of game: 1 if playing, 0 if game over self.player = 'white' #Current player: white player always starts self.board = Board() #The board representation self.control = Control() #The control that handles user input self.sp = Speech() #The verbal feedback given by the computer def play(self): #What happens when the game is started: self.sp.engine.say("Welcome to Wizard's Chess!") self.sp.engine.runAndWait() print "" print "*~*~*~*~*~*~*~*~*~* Welcome to Wizard's Chess! *~*~*~*~*~*~*~*~*~*" print "*~*~*~*~*~*~*~*~*~* Input is of the form 'Knight 1 to H3' *~*~*~*~*~*~*~*~*~*\n" self.board.updateBoard() self.board.printBoard() #Print the board so the player can make their first move #The game loop: runs continuously until the game is over while self.gameState == 1: self.board.updatePosMoves(self.player) #Update the possible moves of the player's pieces #move = self.control.getKeyboardInput(self.player.capitalize()) #Get the desired move from user (keyboard) input move = self.control.getVoiceInput(self.player.capitalize()) if move: #If the user input is valid: success = self.board.movePiece(self.player,move) #Try to move the piece while not move or not success: #While the user input isn't valid or the piece can't be moved: #move = self.control.getKeyboardInput(self.player.capitalize()) #Get new user input until both conditions are satisfied move = self.control.getVoiceInput(self.player.capitalize()) if move: success = self.board.movePiece(self.player,move) self.board.updateBoard() #Once input is valid and piece is successfully moved: self.board.printBoard() #Print the board to the console self.board.updatePosMoves(self.player) #Update the possible moves of the player's pieces self.player = self.other(self.player) #Switch players self.gameState = self.board.continuePlay(self.player) #Check to see if the game is over #Terminate the program once the game is over self.sp.engine.say(self.player + " player loses!") self.sp.engine.runAndWait() print self.player.capitalize() + " player loses!" sys.exit(0) def other(self,player): #Return the name of the other player if player == 'white': return 'black' if player == 'black': return 'white'