Well, I'm making a tic-tac-toe game in Python 2.7, and I need a code snippet to "make" the AI want to get a Tic-Tac-Toe, and make the game end if there's a draw. What can I do? This is the code as it is:

# Python Tic Tac Toe

import random
import time

# The Game Board

board = [0, 1, 2,
         3, 4, 5,
         6, 7, 8]

def show():
    print board[0], '|' ,board[1], '|' ,board[2]
    print '-----------'
    print board[3], '|' ,board[4], '|' ,board[5]
    print '-----------'
    print board[6], '|' ,board[7], '|' ,board[8]

def checkLine(char, spot1, spot2, spot3):
    if board[spot1] == char and board[spot2] == char and board[spot3] == char:
        return True

def checkAll(char):
    if checkLine(char, 0, 1, 2):
        return True
    if checkLine(char, 3, 4, 5):
        return True
    if checkLine(char, 6, 7, 8):
        return True
    if checkLine(char, 0, 3, 6):
        return True
    if checkLine(char, 1, 4, 7):
        return True
    if checkLine(char, 2, 5, 8):
        return True
    if checkLine(char, 0, 4, 8):
        return True
    if checkLine(char, 2, 4, 6):
        return True

while True:

    input = raw_input("Select a spot: ")
    input = int(input)

    if board[input] != 'x' and board[input] != 'o':
        board[input] = 'x'

        if checkAll('x') == True:
            print "~~~~~~~~~~~~"
            print "~~~X WINS~~~"
            show()
            print "~~~~~~~~~~~~"
            print "~~~~~~~~~~~~"
            time.sleep(2)
            break;

        while True:
            random.seed()
            opponent = random.randint(0,8)

            if board[opponent] != 'o'  and board[opponent] != 'x':
                board[opponent] = 'o'


                if checkAll('o') == True:
                    print "~~~~~~~~~~~~"
                    print "~~~O WINS~~~"
                    show()
                    print "~~~~~~~~~~~~"
                    print "~~~~~~~~~~~~"
                    time.sleep(2)
                    break;

                break;                

    else:
        print 'This spot is taken! Please try again.'

    show()

Recommended Answers

All 3 Replies

To end the game if there is a tie, set the last "while True" to some varialble instead that goes to False when there are no more empty spaces, like

empty_space=True
while empty space:
    input = raw_input("Select a spot: ")
    input = int(input)
    ## rest of code

    empty_space=test_for_empty(board)

To get the computer to try to win requires that you have a list of moves to counter each move of the opponent. Mostly, it is block any compbination of 2 in a row, and don't let the opponent get a corner where they can line up 2 possible wins. You can also simplify the code with lists.

def checkAll(char):
    wins_list = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [1, 4, 7], [2, 5, 8],
                  [0, 4, 8], [2, 4, 6]]
    for each_list in wins_list:
        if check_line(ch, win_list[0], win_list[1], win_list[2]):
            return True

##replaces
if checkLine(char, 0, 1, 2):
    return True
if checkLine(char, 3, 4, 5):
    return True
if checkLine(char, 6, 7, 8):
    return True
if checkLine(char, 0, 3, 6):
    return True
if checkLine(char, 1, 4, 7):
    return True
if checkLine(char, 2, 5, 8):
    return True
if checkLine(char, 0, 4, 8):
    return True
if checkLine(char, 2, 4, 6):
    return True

And similarly for printing the winner, both print the same thing except for "X" or "O" so send the x/o to a function that will print the winner so there is one block of code instead of two.

There was accidental mixup in variable names in woooee's answer.

def checkAll(char):
    wins_list = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [1, 4, 7], [2, 5, 8],
                  [0, 4, 8], [2, 4, 6]]
    for first, second, third in wins_list:
        if check_line(ch, first, second, third):
            return True
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.