Esempio n. 1
0
class Interface(BoxLayout):
    def __init__(self):
        super(Interface, self).__init__()

        self.IP = '212.47.247.190'
        self.PORT = 4444
        self.BOARDWIDTH = 7
        self.BOARDHEIGHT = 6
        self.ROND = "images/rond.png"
        self.CROIX = "images/croix.png"
        self.EGAL = "images/egal.png"
        self.board = []
        self.boutons = []
        self.ids.message.text = ""
        self.qui_commence = randint(0, 1)
        self.jeu_en_cours = False
        self.a_qui_le_tour = ""
        self.push_boutns = []
        self.user = None
        self.room = None
        self.stream = None

        # Initialisation
        self.initialisation()

        event = Clock.schedule_interval(self.joue_machine, 2)

    def initialisation(self):
        self.qui_commence = randint(0, 1)
        self.jeu_en_cours = False
        self.a_qui_le_tour = ""
        self.ids.message.text = ""
        self.board = []
        self.boutons = []
        self.push_boutns = []
        # Reinitialisation de la board
        # We construct our table with empty cases
        for x in range(self.BOARDWIDTH):
            self.board.append([' '] * self.BOARDHEIGHT)
        for x in range(self.BOARDWIDTH):
            self.boutons.append([0] * self.BOARDHEIGHT)
        for x in range(self.BOARDWIDTH):
            self.push_boutns.append(0)

        # self.boutons[colonne][ligne]
        self.boutons[0][0] = self.ids.bouton_1_1
        self.boutons[1][0] = self.ids.bouton_1_2
        self.boutons[2][0] = self.ids.bouton_1_3
        self.boutons[3][0] = self.ids.bouton_1_4
        self.boutons[4][0] = self.ids.bouton_1_5
        self.boutons[5][0] = self.ids.bouton_1_6
        self.boutons[6][0] = self.ids.bouton_1_7

        self.boutons[0][1] = self.ids.bouton_2_1
        self.boutons[1][1] = self.ids.bouton_2_2
        self.boutons[2][1] = self.ids.bouton_2_3
        self.boutons[3][1] = self.ids.bouton_2_4
        self.boutons[4][1] = self.ids.bouton_2_5
        self.boutons[5][1] = self.ids.bouton_2_6
        self.boutons[6][1] = self.ids.bouton_2_7

        self.boutons[0][2] = self.ids.bouton_3_1
        self.boutons[1][2] = self.ids.bouton_3_2
        self.boutons[2][2] = self.ids.bouton_3_3
        self.boutons[3][2] = self.ids.bouton_3_4
        self.boutons[4][2] = self.ids.bouton_3_5
        self.boutons[5][2] = self.ids.bouton_3_6
        self.boutons[6][2] = self.ids.bouton_3_7

        self.boutons[0][3] = self.ids.bouton_4_1
        self.boutons[1][3] = self.ids.bouton_4_2
        self.boutons[2][3] = self.ids.bouton_4_3
        self.boutons[3][3] = self.ids.bouton_4_4
        self.boutons[4][3] = self.ids.bouton_4_5
        self.boutons[5][3] = self.ids.bouton_4_6
        self.boutons[6][3] = self.ids.bouton_4_7

        self.boutons[0][4] = self.ids.bouton_5_1
        self.boutons[1][4] = self.ids.bouton_5_2
        self.boutons[2][4] = self.ids.bouton_5_3
        self.boutons[3][4] = self.ids.bouton_5_4
        self.boutons[4][4] = self.ids.bouton_5_5
        self.boutons[5][4] = self.ids.bouton_5_6
        self.boutons[6][4] = self.ids.bouton_5_7

        self.boutons[0][5] = self.ids.bouton_6_1
        self.boutons[1][5] = self.ids.bouton_6_2
        self.boutons[2][5] = self.ids.bouton_6_3
        self.boutons[3][5] = self.ids.bouton_6_4
        self.boutons[4][5] = self.ids.bouton_6_5
        self.boutons[5][5] = self.ids.bouton_6_6
        self.boutons[6][5] = self.ids.bouton_6_7

        self.push_boutns[0] = self.ids.bouton_7_1
        self.push_boutns[1] = self.ids.bouton_7_2
        self.push_boutns[2] = self.ids.bouton_7_3
        self.push_boutns[3] = self.ids.bouton_7_4
        self.push_boutns[4] = self.ids.bouton_7_5
        self.push_boutns[5] = self.ids.bouton_7_6
        self.push_boutns[6] = self.ids.bouton_7_7
        ## Mise en place du background-image  pour les boutons push
        for item in self.push_boutns:
            item.background_normal = "images/3.jpeg"

    def start_game(self):

        self.initialisation()
        #self.board = [[' ', ' ', ' '] for k in range(4)]
        for item in self.boutons:
            for b in item:
                b.background_normal = "images/orange.png"
        self.qui_commence = randint(0, 1)
        self.jeu_en_cours = True
        # Premier coup
        if self.qui_commence == 0:
            # la machine commence
            self.a_qui_le_tour = "rond"
            self.joue_machine(0)
            self.ids.message.text = "C'est ton tour ..."
            self.a_qui_le_tour = "croix"
        else:
            # l'utilisateur commence
            self.a_qui_le_tour = "croix"
            self.ids.message.text = "C'est toi qui commence ..."

    def authentication(self):
        self.user = User(self.ids.login.text, self.ids.password.text)
        self.stream = Stream(self.IP, self.PORT, self.user)
        self.stream.connect_to_server()
        self.stream.negociate_version('1.0')
        self.stream.login()
        if self.stream.isAuthentified:
            print("yes")
            self.stream.create_room()
            self.stream.join_room(self.stream.user.roomId)
            self.ids.message.text = "Authentification reussie"
            self.ids.password.text = ""
            self.ids.login.text = ""
            self.stream.getSock().close()
        '''
        if "SUCCESS:AUTHENTICATED" in str(self.stream.login()):
            prin("yes")
            self.stream.create_room()
            self.stream.join_room(self.stream.user.roomId)
            self.ids.message.text = "Authentification reussie"
            self.ids.password.text = ""
            self.ids.login.text = ""
            self.stream.getSock().close()
            '''

    def joue_machine(self, t):
        # Joue au hasard
        if self.a_qui_le_tour == "rond" and self.jeu_en_cours:
            fini = False
            while not fini:
                ligne = randint(0, 5)
                colonne = randint(0, 6)
                if self.board[colonne][ligne] == " ":
                    self.board[colonne][ligne] = "rond"
                    self.boutons[colonne][
                        ligne].background_normal = "images/rond.png"
                    fini = True
            if not self.verifie_gagne():
                self.a_qui_le_tour = "croix"
                self.ids.message.text = "C'est ton tour ..."

    def joue(self, wid, move):
        if self.jeu_en_cours and self.a_qui_le_tour == "croix":

            if self.isValidMove(self.board, move):
                self.makeMove(self.board, self.boutons, "croix",
                              "images/croix.png", move)
                self.a_qui_le_tour = "rond"
                self.verifie_gagne()

    def makeMove(self, board, boutons, player, player_flag, column):
        # this function parse column x choosen by user and put it at the first empty column, the loop parse from the head to the bottom
        for y in range(self.BOARDHEIGHT - 1, -1, -1):
            if board[column][y] == ' ':
                board[column][y] = player
                boutons[column][y].background_normal = player_flag
                return

    def isValidMove(self, board, move):
        # This function verify if the move input is in the interval [0-width] and the head of column table is not emty else return false
        if move < 0 or move >= (self.BOARDWIDTH):
            return False

        if board[move][0] != ' ':
            return False
        return True

    def verifie_gagne(self):
        gagne = False
        gagnant = None
        # Verifications horizontales
        if self.board[0][0] == self.board[0][1] and self.board[0][
                0] == self.board[0][2] and self.board[0][0] != " ":
            gagnant = self.board[0][0]
            gagne = True
        elif self.board[1][0] == self.board[1][1] and self.board[1][
                0] == self.board[1][2] and self.board[1][0] != " ":
            gagnant = self.board[1][0]
            gagne = True
        elif self.board[2][0] == self.board[2][1] and self.board[2][
                0] == self.board[2][2] and self.board[2][0] != " ":
            gagnant = self.board[2][0]
            gagne = True
        # Verifications verticales
        elif self.board[0][0] == self.board[1][0] and self.board[0][
                0] == self.board[2][0] and self.board[0][0] != " ":
            gagnant = self.board[0][0]
            gagne = True
        elif self.board[0][1] == self.board[1][1] and self.board[0][
                1] == self.board[2][1] and self.board[0][1] != " ":
            gagnant = self.board[0][1]
            gagne = True
        elif self.board[0][2] == self.board[1][2] and self.board[0][
                2] == self.board[2][2] and self.board[0][2] != " ":
            gagnant = self.board[0][2]
            gagne = True
        # Verifications diagonales
        elif self.board[0][0] == self.board[1][1] and self.board[0][
                0] == self.board[2][2] and self.board[0][0] != " ":
            gagnant = self.board[0][0]
            gagne = True
        elif self.board[0][2] == self.board[1][1] and self.board[0][
                2] == self.board[2][0] and self.board[0][2] != " ":
            gagnant = self.board[0][2]
            gagne = True
        if gagne:
            self.jeu_en_cours = False
            if gagnant == "rond":
                self.ids.message.text = "Je gagne !!!"
            else:
                self.ids.message.text = "Bravo ! Tu gagnes !!!"
        return gagne
Esempio n. 2
0
class Interface(BoxLayout):
    def __init__(self):
        super(Interface, self).__init__()

        self.IP = '212.47.247.190'
        self.PORT = 4444
        self.BOARDWIDTH = 5
        self.BOARDHEIGHT = 6
        self.ROND = "images/rond.png"
        self.CROIX = "images/croix.png"
        self.EGAL = "images/egal.png"
        self.DEFAULT_BACK = "images/orange.png"
        self.SERV_FLAG = ""
        self.serv_tile = "S"
        self.client_tile = ""
        self.CLIENT_FLAG = ""
        self.board = []
        self.boutons = []
        self.ids.message.text = ""
        self.qui_commence = randint(0, 1)
        self.jeu_en_cours = False
        self.a_qui_le_tour = ""
        self.push_boutns = []
        self.user = None
        self.room = None
        self.stream = None

        # Initialisation
        self.initialisation()
        event = Clock.schedule_interval(self.joue_machine, 2)

    def initialisation(self):
        self.a_qui_le_tour = ""
        self.ids.message.text = ""
        self.board = []
        self.boutons = []
        self.push_boutns = []
        # Reinitialisation de la board
        # We construct our table with empty cases
        for x in range(self.BOARDWIDTH):
            self.board.append([' '] * self.BOARDHEIGHT)
        for x in range(self.BOARDWIDTH):
            self.boutons.append([0] * self.BOARDHEIGHT)
        for x in range(self.BOARDWIDTH):
            self.push_boutns.append(0)

        # self.boutons[colonne][ligne]
        self.boutons[0][0] = self.ids.bouton_1_1
        self.boutons[1][0] = self.ids.bouton_1_2
        self.boutons[2][0] = self.ids.bouton_1_3
        self.boutons[3][0] = self.ids.bouton_1_4
        self.boutons[4][0] = self.ids.bouton_1_5

        self.boutons[0][1] = self.ids.bouton_2_1
        self.boutons[1][1] = self.ids.bouton_2_2
        self.boutons[2][1] = self.ids.bouton_2_3
        self.boutons[3][1] = self.ids.bouton_2_4
        self.boutons[4][1] = self.ids.bouton_2_5

        self.boutons[0][2] = self.ids.bouton_3_1
        self.boutons[1][2] = self.ids.bouton_3_2
        self.boutons[2][2] = self.ids.bouton_3_3
        self.boutons[3][2] = self.ids.bouton_3_4
        self.boutons[4][2] = self.ids.bouton_3_5

        self.boutons[0][3] = self.ids.bouton_4_1
        self.boutons[1][3] = self.ids.bouton_4_2
        self.boutons[2][3] = self.ids.bouton_4_3
        self.boutons[3][3] = self.ids.bouton_4_4
        self.boutons[4][3] = self.ids.bouton_4_5

        self.boutons[0][4] = self.ids.bouton_5_1
        self.boutons[1][4] = self.ids.bouton_5_2
        self.boutons[2][4] = self.ids.bouton_5_3
        self.boutons[3][4] = self.ids.bouton_5_4
        self.boutons[4][4] = self.ids.bouton_5_5

        self.boutons[0][5] = self.ids.bouton_6_1
        self.boutons[1][5] = self.ids.bouton_6_2
        self.boutons[2][5] = self.ids.bouton_6_3
        self.boutons[3][5] = self.ids.bouton_6_4
        self.boutons[4][5] = self.ids.bouton_6_5

        self.push_boutns[0] = self.ids.bouton_7_1
        self.push_boutns[1] = self.ids.bouton_7_2
        self.push_boutns[2] = self.ids.bouton_7_3
        self.push_boutns[3] = self.ids.bouton_7_4
        self.push_boutns[4] = self.ids.bouton_7_5
        ## Mise en place du background-image  pour les boutons push
        for item in self.push_boutns:
            item.background_normal = "images/3.jpeg"

    def start_game(self):

        self.initialisation()
        #self.board = [[' ', ' ', ' '] for k in range(4)]
        for item in self.boutons:
            for b in item:
                b.background_normal = self.DEFAULT_BACK

        # quel jouer joue en premier en mets ici que c'est le client en statique (voir après)
        self.qui_commence = 1
        # si c'est la machine qui joue en premier
        if self.qui_commence == 0:
            # la machine commence
            self.a_qui_le_tour = "serveur"
            self.joue_machine(0)
            self.ids.message.text = "Le serveur joue"
            self.a_qui_le_tour = "client"
        else:
            # l'utilisateur commence
            self.a_qui_le_tour = "client"
            self.ids.message.text = "Le client commence la partie"

    def define_user_flag(self):
        if self.user.color == 'X':
            self.CLIENT_FLAG = self.CROIX
            self.client_tile = "x"
            self.SERV_FLAG = self.ROND
        elif self.user.color == 'O':
            self.CLIENT_FLAG = self.ROND
            self.client_tile = "o"
            self.SERV_FLAG = self.CROIX
        elif self.user.color == '=':
            self.CLIENT_FLAG = self.EGAL
            self.client_tile = "="
            self.SERV_FLAG = self.ROND

    def get_flags(self, tile):
        if tile == self.client_tile:
            return self.CLIENT_FLAG
        elif tile == ' ':
            return self.DEFAULT_BACK
        else:
            self.serv_tile = tile
            return self.SERV_FLAG

    def authentication(self):
        if self.jeu_en_cours:
            self.ids.message.text = "Une partie est déja en cours "
            return
        if self.ids.login.text == "" or self.ids.password.text == "":
            self.ids.message.text = "Veuillez remplir les champs"
        else:
            self.ids.message.text = ""
            self.user = User(self.ids.login.text, self.ids.password.text)
            self.stream = Stream(self.IP, self.PORT, self.user)
            self.stream.connect_to_server()
            self.stream.negociate_version('1.0')
            self.stream.login()
            if self.stream.isAuthentified:
                self.stream.create_room()
                self.stream.join_room(self.stream.user.roomId)
                # récuper la couleur
                self.stream.get_color()
                self.define_user_flag()
                self.ids.message.text = "Authentification réussie"
                self.ids.password.text = ""
                self.ids.login.text = ""
                self.jeu_en_cours = True
                self.start_game()
            self.stream.getSock().close()

    def drawBoard(self, board):
        for x in range(self.BOARDWIDTH):
            for y in range(self.BOARDHEIGHT):
                self.boutons[x][y].background_normal = self.get_flags(
                    board[x][y])

    def joue_machine(self, t):
        # Joue au hasard
        if self.a_qui_le_tour == "serveur" and self.jeu_en_cours:
            fini = False
            while not fini:
                ## normalement on récupère le board duu serveur apres qu'ila jouer
                new_board = self.stream.get_boad()
                ##Vérifier que labord récupéré est de la même taille que la notre
                if len(new_board) == len(self.board) and len(
                        new_board[0]) == len(self.board[0]):
                    self.board = new_board
                    fini = True
                    self.drawBaoard(self.board)
            #if not self.verifie_gagne():
            if not self.isWinner(self.board, self.serv_tile):
                self.a_qui_le_tour = "client"
                self.ids.message.text = "à vous de jouer "
            else:
                self.ids.message.text = "Le serveur gagne !!!"
                self.jeu_en_cours = False

    def automatic_play(self):
        if self.jeu_en_cours and self.a_qui_le_tour == "client":
            # recupérer un move à partir de l'IA
            move, score = FourinarowAI.getBestMove(self.board,
                                                   self.client_tile,
                                                   self.serv_tile, 5)
            ##envoyer le move
            self.stream.move(move)
            self.makeMove(self.board, self.boutons, self.client_tile,
                          self.CLIENT_FLAG, move)
            if not self.isWinner(self.board, self.client_tile):
                self.a_qui_le_tour = "serveur"
                self.ids.message.text = "C'est le tour du serveur "
            else:
                self.ids.message.text = "Je gagne !!!"
                self.jeu_en_cours = False

    def joue(self, wid, move):
        if self.jeu_en_cours and self.a_qui_le_tour == "client":
            if self.isValidMove(self.board, move - 1):
                ##envoyer le move
                self.stream.move(move)
                self.makeMove(self.board, self.boutons, self.client_tile,
                              self.CLIENT_FLAG, move - 1)
                if not self.isWinner(self.board, self.client_tile):
                    self.a_qui_le_tour = "serveur"
                    self.ids.message.text = "C'est le tour du serveur "
                else:
                    self.ids.message.text = "Je gagne !!!"
                    self.jeu_en_cours = False

    def makeMove(self, board, boutons, player, player_flag, column):
        # this function parse column x choosen by user and put it at the first empty column, the loop parse from the head to the bottom
        for y in range(self.BOARDHEIGHT - 1, -1, -1):
            if board[column][y] == ' ':
                board[column][y] = player
                boutons[column][y].background_normal = player_flag
                return

    def isValidMove(self, board, move):
        # This function verify if the move input is in the interval [0-width] and the head of column table is not emty else return false
        if move < 0 or move >= (self.BOARDWIDTH):
            return False

        if board[move][0] != ' ':
            return False
        return True

    def isBoardFull(self, board):
        for x in range(self.BOARDWIDTH):
            for y in range(self.BOARDHEIGHT):
                if board[x][y] == ' ':
                    return False
        return True

    # check if the player is winner (diagonal , horizontal vertical ) 4 respectively tile
    def isWinner(self, board, tile):
        # check horizontal spaces
        for y in range(self.BOARDHEIGHT):
            # check if 4 tile are respectively egal a tile in horizontal space
            for x in range(self.BOARDWIDTH - 3):
                if board[x][y] == tile and board[x + 1][y] == tile and board[
                        x + 2][y] == tile and board[x + 3][y] == tile:
                    return True

    # check vertical spaces
        for x in range(self.BOARDWIDTH):
            # check if 4 tile are respectively egal a tile in vertical space
            for y in range(self.BOARDHEIGHT - 3):
                if board[x][y] == tile and board[x][y + 1] == tile and board[
                        x][y + 2] == tile and board[x][y + 3] == tile:
                    return True

        # check / diagonal spaces
        for x in range(self.BOARDWIDTH - 3):
            for y in range(3, self.BOARDHEIGHT):
                if board[x][y] == tile and board[x + 1][
                        y - 1] == tile and board[x + 2][
                            y - 2] == tile and board[x + 3][y - 3] == tile:
                    return True

        # check \ diagonal spaces
        for x in range(self.BOARDWIDTH - 3):
            for y in range(self.BOARDHEIGHT - 3):
                if board[x][y] == tile and board[x + 1][
                        y + 1] == tile and board[x + 2][
                            y + 2] == tile and board[x + 3][y + 3] == tile:
                    return True

        return False