def minMax(situation,player,game,depth):
    name=game['name']
    if name == 'othello':
        import othello as Game
    elif name == 'tic_tac_toe':
        import tictactoe as Game
    elif name =='nim_game':
        import nim_game as Game
    if Player.name(player)==COMPUTER_NAME:
        coeff=1
    else:
        coeff=-1
    next_player=changePlayer(player,game)
    if Game.isFinished(situation) or depth==0:
        #décomenter pour vérifier le résultat de la fonction d'évaluation
        #print(Game.evalFunction(situation,player,game)*coeff) 
        return Game.evalFunction(situation,player,game)*coeff,situation
    else:
        next_situation=Game.nextSituations(game, situation, player)
        res=None
        if Player.name(player)==COMPUTER_NAME:
            for sit in next_situation:
                temp =minMax(sit,next_player,game,depth-1)
                if res==None or temp!=None and res[0]<temp[0]:
                   res=temp
        else:
            for sit in next_situation:
                temp =minMax(sit,next_player,game,depth-1)
                if res==None or temp!=None and res[0]>temp[0]:
                    res=temp
        try:
            return res[0],sit
        except:
            print (res)
def play(name):
    """
    """
    assert name in LIST_GAME, 'g must be in LIST_GAME'
    if name=='othello':
        import othello as Game
    elif name=='tic_tac_toe':
        import tictactoe as Game
    elif name=='nim_game':
        import nim_game as Game
    game = init_game(name)
    situation= Game.initSituation(game)
    turn_passed=0
    current_player=first_player(game)
    Game.displaySituation(situation)
    while not Game.isFinished(situation) and turn_passed<2:
        if Game.playerCanPlay(game, situation, current_player):
            turn_passed=0
            if Player.name(current_player)!='minMAX':
                situation = Game.humanPlayerPlays(game,current_player,situation)
            else:
                print("computer's playing")
                situation = minmax.main(game,current_player,situation)
            Game.displaySituation(situation)
        else:
            turn_passed +=1
        current_player = next_player(current_player,game)
    winner=Game.getWinner(game,situation,current_player)
    if winner == None:
        print("It's a draw")
    elif Player.name(winner)!="minMAX":
        print("Well done {:s} you win".format(Player.name(winner)))
    else:
        print("Sorry, you loose")
def _input_action(game, situation, player) :# on a good way
    """
    """
    print(Player.name(player)+" it's your turn to play")
    print('Enter a letter between a and h followed by a number between 1 and 8')
    action=input('Where do you want to play? ')
    try:
        coins=game['coins'][Player.coins(player)]
        assert len(action)==2
        column= ord(action[0])-ord('a')
        line = int(action[1])-1
        new_situation=[]
        for i in range (8):
            new_situation.append([])
            new_situation[i]= situation[i].copy()
            for direction in DIRECTIONS:
                _possible_action(column,line,DIRECTIONS[direction],player,new_situation,game)
        if new_situation!=situation:
            new_situation[column][line]=coins
            situation = new_situation
            return situation
    except AssertionError:
        pass
    except ValueError:
        pass
    print('unauthorised action')
    return _input_action(game, situation, player)
def minMaxFinal(situation,player,game):
    name=game['name']
    if name == 'othello':
        import othello as Game
    elif name == 'tic_tac_toe':
        import tictactoe as Game
    elif name =='nim_game':
        import nim_game as Game
    next_player=changePlayer(player,game)
    if Game.isFinished(situation):
        return (Game.evalFunction(situation,player),situation)
    else:
        next_situation=Game.nextSituations(game, situation, player)
        res=None
        if Player.name(player)==COMPUTER_NAME:
            for sit in next_situation:
                temp =minMaxFinal(sit,next_player,game)
                if res==None or temp!=None and res[0]<temp[0]:
                   res=temp
        else:
            for sit in next_situation:
                temp =minMaxFinal(sit,next_player,game)
                if res==None or temp!=None and res[0]>temp[0]:
                    res=temp
        try:
            return res[0],sit
        except:
            pass
Example #5
0
    def substitution(self):
        for widget in self.playing_roster_team1_frame.winfo_children():
            widget.destroy()

        for widget in self.playing_roster_team2_frame.winfo_children():
            widget.destroy()

        for player in self.team1_roster_list:
            if player not in self.team1_playing_list:
                self.team1_playing_list.append(player)

        for player in self.team2_roster_list:
            if player not in self.team2_playing_list:
                self.team2_playing_list.append(player)

        for player in self.team1_playing_list:
            Radiobutton(self.playing_roster_team1_frame,
                        text=player.name()).pack()

        for player in self.team2_playing_list:
            Radiobutton(self.playing_roster_team2_frame,
                        text=player.name()).pack()
def _input_pebbles(player, number_pebbles, number_message):
    """
    reads the number of pebbles taken by player
    :returns: *(int)* -- the number of taken pebbles
    """
    print(Player.name(player)+" it's your turn, you can take "+number_message+" pebbles.")
    nb_taken_pebbles = input("how many pebbles do you take ? ")
    try:
        if int(nb_taken_pebbles) in number_pebbles:
            return int(nb_taken_pebbles)
        # else : invalid input  : redo input
    except ValueError:
        # input is not an integer : redo input
        pass
    print("illegal play, you can only take "+number_message+" pebbles")
    return _input_pebbles(player,number_pebbles, number_message)
Example #7
0
 def display_list_of_players(self):
     for player in self.player_list:
         name_label = player.name()
         Button(self.list_of_players_window, text=name_label,
                command=lambda i = player:self.select_player(i)).pack()