예제 #1
0
    def test_validate_3_in_column(self):
        """This method testing a function that returns true if
        there are 3 of Cross or Circle in one column"""
        # Clean board
        self.reset_board()

        # Make column of 3 circles in the first column and check if returns true
        Tic_Tac_Toe.board[0][0] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[1][0] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[2][0] = Tic_Tac_Toe.CIRCLE
        self.assertTrue(Tic_Tac_Toe.validate_3_in_column(
            0, Tic_Tac_Toe.CIRCLE))

        # Check wrong line
        self.assertFalse(
            Tic_Tac_Toe.validate_3_in_column(1, Tic_Tac_Toe.CIRCLE))

        # Check wrong sign
        self.assertFalse(Tic_Tac_Toe.validate_3_in_column(
            0, Tic_Tac_Toe.CROSS))

        # Make column of 3 crosses in the last column and check if results are even
        Tic_Tac_Toe.board[0][2] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[1][2] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[2][2] = Tic_Tac_Toe.CROSS
        self.assertEqual(
            Tic_Tac_Toe.validate_3_in_column(0, Tic_Tac_Toe.CIRCLE),
            Tic_Tac_Toe.validate_3_in_column(2, Tic_Tac_Toe.CROSS))
    def init_players_data(self, player1str: str, player2str: str = ""):
        player1 = Tic_Tac_Toe.Player()
        player2 = Tic_Tac_Toe.Player()
        player1.playerName = player1str
        if (player2str == ""):
            player2.playerName = "Computer"
            player2.isPc = True

        else:
            player2.playerName = player2str

        player1.playerShape = "X"
        player2.playerShape = "O"
        self.gameData.player1 = player1
        self.gameData.player2 = player2
        print(player1)
예제 #3
0
    def test_is_draw(self):
        """This method will check if 'is draw' function is working as we want"""
        # Fill board (If the board is full its Draw!)
        Tic_Tac_Toe.board[0][0] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[0][1] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[0][2] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[1][0] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[1][1] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[1][2] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[2][0] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[2][1] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[2][2] = Tic_Tac_Toe.CROSS
        self.assertEqual(Tic_Tac_Toe.is_draw(), True)

        # Clean 1 block
        Tic_Tac_Toe.board[1][2] = Tic_Tac_Toe.EMPTY
        self.assertNotEqual(Tic_Tac_Toe.is_draw(), True)
예제 #4
0
    def test_validate_3_in_diagonal(self):
        """This method testing a function that returns true if there is backward or forward diagonal of
        Cross or Circle, using the functions backward_diagonal and forward_diagonal"""
        # Clean board
        self.reset_board()

        # Make backward diagonal and check
        Tic_Tac_Toe.board[0][2] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[1][1] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[2][0] = Tic_Tac_Toe.CROSS
        self.assertTrue(Tic_Tac_Toe.validate_3_in_diagonal(Tic_Tac_Toe.CROSS))
        self.assertFalse(Tic_Tac_Toe.validate_3_in_diagonal(
            Tic_Tac_Toe.CIRCLE))

        # Cancel the middle one and check if false
        Tic_Tac_Toe.board[1][1] = Tic_Tac_Toe.EMPTY
        self.assertFalse(Tic_Tac_Toe.validate_3_in_diagonal(Tic_Tac_Toe.CROSS))

        # Check forward with circle
        self.reset_board()
        Tic_Tac_Toe.board[0][0] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[1][1] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[2][2] = Tic_Tac_Toe.CIRCLE
        self.assertTrue(Tic_Tac_Toe.validate_3_in_diagonal(Tic_Tac_Toe.CIRCLE))
        self.assertFalse(Tic_Tac_Toe.validate_3_in_diagonal(Tic_Tac_Toe.CROSS))

        # Cancel the middle one and check if false
        Tic_Tac_Toe.board[1][1] = Tic_Tac_Toe.EMPTY
        self.assertFalse(Tic_Tac_Toe.validate_3_in_diagonal(
            Tic_Tac_Toe.CIRCLE))
def clientthread(conn, addr):
    # sends a message to the client whose user object is conn
    conn.send("Welcome to this chatroom!")
    response_dict = dict()
    while True:
        try:
            data = conn.recv(2048)

            # Bucle de escucha. En él indicamos la forma de actuar al recibir las tramas del cliente
            input_dict = ast.literal_eval(data)
            if (type(input_dict) == "dict"):
                player = Tic_Tac_Toe.Player()
                message = input_dict["message"]
                if message == "init_player_data":
                    if gameData.player1 == None:
                        player.playerName = input_dict["player_name"]
                        player.playerShape = "X"
                        gameData.player1 = player
                    elif gameData.player2 == None:
                        player.playerName = input_dict["player_name"]
                        player.playerShape = "O"
                        gameData.player2 = player

                elif message == "new_move":
                    coord = input_dict["coord"]
                    nx = coord["x"]
                    ny = coord["y"]
                    playerShape = input_dict["player_shape"]
                    if playerShape == "X":
                        if (gameData.board.squares[nx][ny] == 0):
                            gameData.board.squares[nx][ny] = 1
                            playerMove = gameData.player1.newPlayerMove(coord)
                            gameData.turns.append(playerMove)
                            won_player = gameData.check_game_over()
                    elif playerShape == "O":
                        if (gameData.board.squares[nx][ny] == 0):
                            gameData.board.squares[nx][ny] = 2
                            playerMove = gameData.player1.newPlayerMove(coord)
                            gameData.turns.append(playerMove)
                            won_player = gameData.check_game_over()

                message_to_send = str(response_dict)
                broadcast(message_to_send, conn)

            else:
                """message may have no content if the connection 
                is broken, in this case we remove the connection"""
                remove(conn)

        except:
            continue
예제 #6
0
    def test_forward_diagonal(self):
        """This method testing a function that returns true if
        there is a backward diagonal of Cross or Circle
                for example:
                            || || O
                            || O ||
                            O || ||    """
        # Clean board
        self.reset_board()

        # Make a forward diagonal of circles
        Tic_Tac_Toe.board[0][0] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[1][1] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[2][2] = Tic_Tac_Toe.CIRCLE

        # Check function returns true
        self.assertTrue(Tic_Tac_Toe.forward_diagonal(Tic_Tac_Toe.CIRCLE))
        self.assertFalse(Tic_Tac_Toe.forward_diagonal(Tic_Tac_Toe.CROSS))

        # Cancel diagonal and check if false
        Tic_Tac_Toe.board[1][1] = Tic_Tac_Toe.EMPTY
        self.assertFalse(Tic_Tac_Toe.forward_diagonal(Tic_Tac_Toe.CIRCLE))
        self.assertFalse(Tic_Tac_Toe.forward_diagonal(Tic_Tac_Toe.CROSS))
예제 #7
0
def Spiel(spiele, wiederholungen):
    status = {
        "board": [0, 1, 2, 3, 4, 5, 6, 7, 8],
        "sieger": "none",
        "spieler": random.choice(("X", "O")),
        "WegX": [],
        "WegO": []
    }
    while game.check_end(status) == "none":
        if status["spieler"] == "X":
            entscheidung = False
            if random.randint(0, 30) == 1:
                while entscheidung == False:
                    Wahl = random.randint(0, 8)
                    if game.belegt(status, Wahl) == False:
                        status["WegX"].insert(0, [createState(status), Wahl])
                        status["board"][Wahl] = "X"
                        entscheidung = True
                        status["spieler"] = "O"
            else:
                while entscheidung == False:
                    Wahl = game.Agent(status)
                    #print(Wahl, "x; ")
                    if game.belegt(status, Wahl) == False:
                        status["WegX"].insert(0, [createState(status), Wahl])
                        status["board"][Wahl] = "X"
                        entscheidung = True
                        status["spieler"] = "O"

        else:
            entscheidung = False
            while entscheidung == False:
                #Wahl = random.randint(0,8)
                Wahl = game.Agent(status)
                if game.belegt(status, Wahl) == False:
                    status["WegO"].insert(0, [createState(status), Wahl])
                    status["board"][Wahl] = "O"
                    entscheidung = True
                    status["spieler"] = "X"
    status["sieger"] = game.check_end(status)
    anpassung(status, 0)
예제 #8
0
    def test_has_won(self):
        """This method testing a function that returns true or false if player has won
        player wining when ther is a full (3 in line) row, column or diagonal of his sign"""
        # Clean board
        self.reset_board()

        # Check Rows
        Tic_Tac_Toe.board[0][0] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[0][1] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[0][2] = Tic_Tac_Toe.CROSS
        self.assertTrue(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 0, 0))
        self.assertTrue(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 0, 1))
        self.assertTrue(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 0, 2))
        # Change 1 value and check
        Tic_Tac_Toe.board[0][0] = Tic_Tac_Toe.CIRCLE
        self.assertFalse(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 0, 0))
        self.assertFalse(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 0, 1))
        self.assertFalse(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 0, 2))

        # Check diagonal
        # Forward
        self.reset_board()
        Tic_Tac_Toe.board[0][2] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[1][1] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[2][0] = Tic_Tac_Toe.CROSS
        self.assertTrue(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 0, 0))
        self.assertTrue(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 0, 1))
        self.assertTrue(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 2, 0))
        # Backward
        self.reset_board()
        Tic_Tac_Toe.board[0][0] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[1][1] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[2][2] = Tic_Tac_Toe.CIRCLE
        self.assertTrue(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CIRCLE, 0, 0))
        self.assertTrue(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CIRCLE, 0, 1))
        self.assertTrue(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CIRCLE, 2, 0))

        # Check Columns
        self.reset_board()
        Tic_Tac_Toe.board[0][1] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[1][1] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[2][1] = Tic_Tac_Toe.CROSS
        self.assertTrue(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 0, 1))
        self.assertTrue(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 1, 1))
        self.assertTrue(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 2, 1))
        # Change 1 value and check
        Tic_Tac_Toe.board[2][1] = Tic_Tac_Toe.CIRCLE
        self.assertFalse(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 0, 1))
        self.assertFalse(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 1, 1))
        self.assertFalse(Tic_Tac_Toe.has_won(Tic_Tac_Toe.CROSS, 2, 1))
예제 #9
0
    def test_is_valid_move(self):
        """This function returns true when two first values we sent are
            between 0 to 2, and if this spot is not empty"""
        # Clean board
        self.reset_board()

        # Check valid values
        self.assertTrue(Tic_Tac_Toe.is_valid_move(0, 0, Tic_Tac_Toe.CROSS))
        self.assertTrue(Tic_Tac_Toe.is_valid_move(0, 1, Tic_Tac_Toe.CIRCLE))
        self.assertTrue(Tic_Tac_Toe.is_valid_move(0, 2, Tic_Tac_Toe.CROSS))
        self.assertTrue(Tic_Tac_Toe.is_valid_move(1, 0, Tic_Tac_Toe.CIRCLE))
        self.assertTrue(Tic_Tac_Toe.is_valid_move(1, 1, Tic_Tac_Toe.CROSS))
        self.assertTrue(Tic_Tac_Toe.is_valid_move(1, 2, Tic_Tac_Toe.CIRCLE))
        self.assertTrue(Tic_Tac_Toe.is_valid_move(2, 0, Tic_Tac_Toe.CROSS))
        self.assertTrue(Tic_Tac_Toe.is_valid_move(2, 1, Tic_Tac_Toe.CIRCLE))
        self.assertTrue(Tic_Tac_Toe.is_valid_move(2, 2, Tic_Tac_Toe.CROSS))

        # Check negative values
        self.assertFalse(Tic_Tac_Toe.is_valid_move(1, -1, Tic_Tac_Toe.CROSS))
        self.assertFalse(Tic_Tac_Toe.is_valid_move(-1, 1, Tic_Tac_Toe.CIRCLE))

        # Check value is bigger then 2
        self.assertFalse(Tic_Tac_Toe.is_valid_move(1, 3, Tic_Tac_Toe.CROSS))
        self.assertFalse(Tic_Tac_Toe.is_valid_move(3, 1, Tic_Tac_Toe.CIRCLE))

        # Check board-spot isn't empty
        Tic_Tac_Toe.board[1][1] = Tic_Tac_Toe.CROSS
        self.assertFalse(Tic_Tac_Toe.is_valid_move(1, 1, Tic_Tac_Toe.CROSS))
        Tic_Tac_Toe.board[0][1] = Tic_Tac_Toe.CROSS
        self.assertFalse(Tic_Tac_Toe.is_valid_move(0, 1, Tic_Tac_Toe.CIRCLE))
예제 #10
0
        Tic_Tac_Toe.board[0][0] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[0][1] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[0][2] = Tic_Tac_Toe.CIRCLE
        self.assertEqual(Tic_Tac_Toe.validate_3_in_row(2, Tic_Tac_Toe.CROSS),
                         Tic_Tac_Toe.validate_3_in_row(0, Tic_Tac_Toe.CIRCLE))

    def test_is_draw(self):
        """This method will check if 'is draw' function is working as we want"""
        # Fill board (If the board is full its Draw!)
        Tic_Tac_Toe.board[0][0] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[0][1] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[0][2] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[1][0] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[1][1] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[1][2] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[2][0] = Tic_Tac_Toe.CIRCLE
        Tic_Tac_Toe.board[2][1] = Tic_Tac_Toe.CROSS
        Tic_Tac_Toe.board[2][2] = Tic_Tac_Toe.CROSS
        self.assertEqual(Tic_Tac_Toe.is_draw(), True)

        # Clean 1 block
        Tic_Tac_Toe.board[1][2] = Tic_Tac_Toe.EMPTY
        self.assertNotEqual(Tic_Tac_Toe.is_draw(), True)


# Init the game so we can start make tests
Tic_Tac_Toe.init_game()

# Run tests commands
if __name__ == '__main__':
    unittest.main()
 def __init__(self):
     self.gameData = Tic_Tac_Toe.Game()
Port = int(sys.argv[2])
""" 
binds the server to an entered IP address and at the 
specified port number. 
The client must be aware of these parameters 
"""
server.bind((IP_address, Port))
""" 
listens for 100 active connections. This number can be 
increased as per convenience. 
"""
server.listen(100)

list_of_clients = []

gameData = Tic_Tac_Toe.Game()


def clientthread(conn, addr):
    # sends a message to the client whose user object is conn
    conn.send("Welcome to this chatroom!")
    response_dict = dict()
    while True:
        try:
            data = conn.recv(2048)

            # Bucle de escucha. En él indicamos la forma de actuar al recibir las tramas del cliente
            input_dict = ast.literal_eval(data)
            if (type(input_dict) == "dict"):
                player = Tic_Tac_Toe.Player()
                message = input_dict["message"]