dimanche 31 janvier 2016

Simple Tic Tac Toe AI in Python 2.7 won't work

I'm creating a Tic Tac Toe game and I do it only by my idea - the online courses show a bit too difficult code for me as I am a beginner. Everything works fine in my code but when it comes to computer move there is always a problem I cannot find and solve.
I made three functions that search for a free place in a line:

  • vertical
  • horizontal
  • diagonal

By the names of that functions you can guess how my functions search.
For your information:

  • pl_array is the array which contains moves and is displayed
  • player_choice and computer_choice are the signs 'X' and 'O'
  • choice1 and choice2 was supposed to be player's and computer's signs

I want to check if computer can win and if not, check if it can block the player - that's why I created functions with choice1 and choice2 arguments.

def vertical(pl_array, choice1, choice2):
    table = [0, 1, 2]
    for item in table:
        if pl_array[0][item] == pl_array[1][item] == choice1 and pl_array[2][item] != 'X' and pl_array[2][item] != 'O':
            pl_array[2][item] = choice2
            return pl_array
        elif pl_array[1][item] == pl_array[2][item] == choice1 and pl_array[0][item] != 'X' and pl_array[0][item] != 'O':
            pl_array[0][item] = choice2
            return pl_array
        elif pl_array[0][item] == pl_array[2][item] == choice1 and pl_array[1][item] != 'X' and pl_array[1][item] != 'O':
            pl_array[1][item] = choice2
            return pl_array
        else:
            return False

def horizontal(pl_array, choice1, choice2):
    table = [0, 1, 2]
    for item in table:
        if pl_array[item][0] == pl_array[item][1] == choice1 and pl_array[item][2] != 'X' and pl_array[item][2] != 'O':
            pl_array[item][2] = choice2
            return pl_array
        elif pl_array[item][0] == pl_array[item][2] == choice1 and pl_array[item][1] != 'X' and pl_array[item][1] != 'O':
            pl_array[item][1] = choice2
            return pl_array
        elif pl_array[item][1] == pl_array[item][2] == choice1 and pl_array[item][0] != 'X' and pl_array[item][0] != 'O':
            pl_array[item][0] = choice2
            return pl_array
        else:
            return False

def diagonal(pl_array, choice1, choice2):
    if pl_array[0][0] == pl_array[1][1] == choice1 and pl_array[2][2] != 'X' and pl_array[2][2] != 'O':
        pl_array[2][2] = choice2
        return pl_array
    elif pl_array[0][0] == pl_array[2][2] == choice1 and pl_array[1][1] != 'X' and pl_array[1][1] != 'O':
        pl_array[1][1] = choice2
        return pl_array
    elif pl_array[1][1] == pl_array[2][2] == choice1 and pl_array[0][0] != 'X' and pl_array[0][0] != 'X':
        pl_array[0][0] = choice2
        return pl_array
    # The code above search for an empty place in the first line
    # and the code below does the same for the second line
    elif pl_array[0][2] == pl_array[1][1] == choice1 and pl_array[2][0] != 'X' and pl_array[2][0] != 'O':
        pl_array[2][0] = choice2
        return pl_array
    elif pl_array[0][2] == pl_array[2][0] == choice1 and pl_array[1][1] != 'X' and pl_array[1][1] != 'O':
        pl_array[1][1] = choice2
        return pl_array
    elif pl_array[1][1] == pl_array[2][0] == choice1 and pl_array[0][2] != 'X' and pl_array[0][2] != 'O':
        pl_array[0][2] = choice2
        return pl_array
    else:
        return False

Those are my three mentioned functions and the code below presents the computer's turn. To find where i made the mistake I have added the print statements but it just confused me more.

def computer_move(pl_array, player_choice, computer_choice):
    print 'Poziom 0'
    if horizontal(pl_array, computer_choice, computer_choice) == False:
        print 'Poziom 1'
        if vertical(pl_array, computer_choice, computer_choice) == False:
            print 'Poziom 2'
            if diagonal(pl_array, computer_choice, computer_choice) == False:
                print 'Poziom 3'
                if horizontal(pl_array, player_choice, computer_choice) == False:
                    print 'Poziom 4'
                    if vertical(pl_array, player_choice, computer_choice) == False:
                        print 'Poziom 5'
                        if diagonal(pl_array, player_choice, computer_choice) == False:
                            print 'Poziom 6'
                            x = random.randint(0, 2)       # it makes moves randomly
                            y = random.randint(0, 2)       # in both cases
                            while pl_array[x][y] == "X" or pl_array[x][y] == "O":
                                x = random.randint(0, 2)       # it makes moves randomly
                                y = random.randint(0, 2)
                            else:
                                pl_array[x][y] = computer_choice
                                return pl_array

I know it's a lot of code but I asked another question about it - here's link to that.
Why won't it work?
Where have I made a mistake?
Do I call if statements properly in computer's turn?
Any help appreciated.

Aucun commentaire:

Enregistrer un commentaire