예제 #1
0
def nextSituations(game, situation, player): # Could be ok
    """
    returns the list of situations that can be reached from given situation by the player in the game

    :param game: the game
    :type game: a two players game
    :param situation: the current situation
    :type situation: a game situation
    :param player: the current player
    :type player: player
    :returns: *(list<situtation>)* -- the list of situations that can be reached from given situation when player plays one round in the game
    """
    next_situation=[]
    coins= game['coins'][Player.coins(player)]
    for column in range (8):
        for line in range(8):
            if situation[column][line]==None:
                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
                    next_situation.append(new_situation)
    return next_situation
예제 #2
0
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)
예제 #3
0
def evalFunction(situation, player,game):
    """
    the evaluation function for the min-max algorithm. It evaluates the given situation, the evaluation function increases with the quality of the situation for the player
         
    :param situation: the current situation
    :type situation: a game situation
    :param player: the current player
    :type player: player
    :returns: *(number)* -- the score of the given situation for the given player.
        The better the situation for the minmax player, the higher the score. The opposite for human player.
    """
    win= 1000
    loose = -1000
    turn_pass=-200
    coins=game['coins'][Player.coins(player)]
    coeff= _square_coeff()
    value = 0
    if isFinished(situation):
        if getWinner(game,situation,player)== player:
            value=win
        else:
            value=loose
    elif not playerCanPlay(game,situation,player):
        value= turn_pass
    else:
        for column in range(8):
            for line in range(8):
                if situation[column][line]==coins:
                    value+=coeff[column][line]
                elif situation[column][line]!=None:
                    value-=coeff[column][line]
    return value
예제 #4
0
def _possible_action(colum,line,direction,player,situation,game): #OK
    """
    give the coordinate of the square in which you can play in the given direction is this square exist, otherwise, returns None
    """
    coins = game['coins'][Player.coins(player)]
    try :
        assert colum>=0 and colum<=7
        assert line>=0 and line<=7
        next_colum1,next_line1=_next_square(colum,line,direction)
        assert next_colum1>=0 and next_colum1<=7
        assert next_line1>=0 and next_colum1<=7
        if (situation[next_colum1][next_line1] != coins) and (situation[next_colum1][next_line1] != None):
            next_colum2,next_line2=_next_square(next_colum1,next_line1,direction)
            assert next_colum2>=0 and next_colum2<=7
            assert next_line2>=0 and next_line2<=7
            if situation[next_colum2][next_line2] == coins:
                situation[next_colum1][next_line1]=coins
                situation[colum][line]=coins
                return (colum,line)
            elif situation[next_colum2][next_line2]!=coins:
                res=_possible_action(next_colum1,next_line1,direction,player,situation,game)
                if res!=None:
                    situation[next_colum1][next_line1]=coins
                    situation[colum][line]=coins
                return res
    except IndexError:
        pass
    except AssertionError:
        pass
    return None
def getWinner(game, situation, player):
    """
    Gives the winner of the game that end in situation

    :param game: the game 
    :type game: game
    :param situation: the situation which is a final game situation
    :type situation: a game situation
    :param player: the player who should have played if situation was not final (then other player plays previous turn)
    :type player: player
    :returns: *(player)* -- the winner player or None in case of tie game

    :CU: situation is a final situation
    """
    spec = Player.coins(player)
    for i in range(len(situation)):
        if situation[i][0] == situation[i][1] == situation[i][2] == spec :
            return player
        if i == 0:
            for j in range(3):
                if situation[i][j] == situation[i+1][j] == situation[i+2][j] == spec :
                    return player
                if j == 0 or j == 2 :
                    if situation[i][j] == situation[i+1][abs(j-1)] == situation[i+2][abs(j-2)] == spec :
                        return player
    return None
def onClick(x, y, b, sit, pone, ptwo, event):
    """
    """
    global current
    coin = Player.coins(current)
    if tictactoe.onboard(x, y) and sit[x][y] == " ":
        sit[x][y] = coin
    if current == pone:
        current = ptwo
    else:
        current = pone
    __redraw(sit, b)
def first_player(game):
    """
    get the first player to play
    """
    name=game['name']
    if name == 'othello':
        color= Player.coins(game['player1'])
        if color == 'black':
            return game['player1']
        else:
            return game['player2']
    else:
        return game['player1']
예제 #8
0
def getWinner(game, situation, player):#should be ok
    """
    Gives the winner of the game that end in situation

    :param game: the game 
    :type game: game
    :param situation: the situation which is a final game situation
    :type situation: a game situation
    :param player: the player who should have played if situation was not final (then other player plays previous turn)
    :type player: player
    :returns: *(player)* -- the winner player or None in case of tie game

    :CU: situation is a final situation
    """
    white=0
    black=0
    winner=None
    for column in range(8):
        for line in range (8):
            if situation[column][line]==game['coins']['white']:
                white+=1
            elif situation[column][line]==game['coins']['black']:
                black+=1
    if white>black:
        if Player.coins(player)=='white':
            winner=player
        elif player==game['player1']:
            winner=game['player2']
        else:
            winner=game['player1']
    elif black>white:
        if Player.coins(player)=='black':
            winner=player
        elif player==game['player1']:
            winner=game['player2']
        else:
            winner=game['player1']
    return winner
def humanPlayerPlays(game, player, situation):
    """
    makes the human player plays for given situation in the game

    :param game: the game 
    :type game: game
    :param player: the human player
    :type player: player
    :param situation: the current situation
    :type situation: a game situation
    :returns: *(game situtation)* -- the game situation reached afte the human player play
    """
    x, y = inputcoords(situation, player)
    coin = Player.coins(player)
    situation[x][y] = coin
    return situation
예제 #10
0
def nextSituations(game, situation, player):
    """
    returns the list of situations that can be reached from given situation by the player in the game

    :param game: the game
    :type game: a two players game
    :param situation: the current situation
    :type situation: a game situation
    :param player: the current player
    :type player: player
    :returns: *(list<situtation>)* -- the list of situations that can be reached from given situation when player plays one round in the game
    """
    spec = Player.coins(player)
    sits = []
    for i in range(3):
        for j in range(3):
            if situation[i][j] == '':
                sit = initSituation([])
                for x in range(3):
                    for y in range(3):
                        sit[x][y]= situation[x][y]
                sit[i][j] = spec
                sits.append((sit,(i,j)))
    return sits