Python Forum

Full Version: TicTacToe PC vs PC
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi fellow forum users.

I have written following code where both users are computer. Only input required is to select the first user ( between PC1 and PC2). I have added logic rules also to defeat the opponent user also. I would thus expect that each game to draw. However I see that some of the times one of the users wins. I am not able to figure out why. Request some help .

Basic flow:

1. select first playing user
2. draw initial box condition
3. user plays - (first user always blocks central position to start the game, to user's advantage Smile )
this is where it
a) first checks the board for playing user's winning next step
b) if not, then it checks for opponet user's winning position and blocks it
c) else select place randomly from available positions

4. result is checked -
if win - say who won
else move on to next move
else say its a draw
----------------------------------------------

import os, sys, time, random

list = [
    [1, 'open'],
    [2, 'open'],
    [3, 'open'],
    [4, 'open'],
    [5, 'open'],
    [6, 'open'],
    [7, 'open'],
    [8, 'open'],
    [9, 'open']
]
movesctr = 0
rulez = 'none'
inpt = 0
pcloc1 = 0
# print("Len =", len(list))
# i=0
# j=0
# k=0
result = False
opener = 'user'


def welcome():
    print("Welcome to TicTacToe game...this is PC vs PC ")
    print("PC1 uses X, PC2 uses O")


def whoplaysfirst():
    global opener
    print("Who starts the game?")
    userinpt1 = input("Enter 1(PC1) or 2 (PC2)")
    if userinpt1 == '1':

        print("\nPC1 will start the game now...")
        time.sleep(5)
        print("\nORIGINAL BOX POSITION")
        prinbox()
        opener = 'PC1'
    elif userinpt1 == '2':
        print("\nPC2 will start the game now...")
        time.sleep(5)
        print("\nORIGINAL BOX POSITION")
        prinbox()
        # getinput_usr()
        opener = 'PC2'

    else:
        print("\nInvalid entry...try again")
        whoplaysfirst()


def prinbox():
    global list
    i = 0
    j = 0
    k = 0
    print("\n------------------")
    while i < len(list):
        if k == 3:
            print("\n\t")
            k = 0
            i = i - 1
        else:
            print(list[i][0], end="\t")
            k += 1
        i += 1
    print("\n------------------")

def checkresult():
    global movesctr
    if (list[0][0] == "X" and list[1][0] == "X" and list[2][0] == "X") or (
            list[3][0] == "X" and list[4][0] == "X" and list[5][0] == "X") or (
            list[6][0] == "X" and list[7][0] == "X" and list[8][0] == "X"):
        print("PC1 Wins")
        sys.exit()
    elif (list[0][0] == "X" and list[3][0] == "X" and list[6][0] == "X") or (
            list[1][0] == "X" and list[4][0] == "X" and list[7][0] == "X") or (
            list[2][0] == "X" and list[5][0] == "X" and list[8][0] == "X"):
        print("PC1 Wins")
        sys.exit()
    elif (list[0][0] == "X" and list[4][0] == "X" and list[8][0] == "X") or (
            list[2][0] == "X" and list[4][0] == "X" and list[6][0] == "X"):
        print("PC1 Wins")
        sys.exit()
    elif (list[0][0] == "O" and list[1][0] == "O" and list[2][0] == "O") or (
            list[3][0] == "O" and list[4][0] == "O" and list[5][0] == "O") or (
            list[6][0] == "O" and list[7][0] == "O" and list[8][0] == "O"):
        print("PC2 Wins")
        sys.exit()
    elif (list[0][0] == "O" and list[3][0] == "O" and list[6][0] == "O") or (
            list[1][0] == "O" and list[4][0] == "O" and list[7][0] == "O") or (
            list[2][0] == "O" and list[5][0] == "O" and list[8][0] == "O"):
        print("PC2 Wins")
        sys.exit()
    elif (list[0][0] == "O" and list[4][0] == "O" and list[8][0] == "O") or (
            list[2][0] == "O" and list[4][0] == "O" and list[6][0] == "O"):
        print("PC2 Wins")
        sys.exit()
    elif movesctr==8 or movesctr==9:
        print("It's a DRAW")
        sys.exit()



def generateandvalidateinput_PC1():
    global pcloc1, rulez

    if list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'X'
        pcloc1 = 5
        rulez='center rule'
    # ---------------24 rules to find winning position for playing user
    elif list[0][0] == 'X' and list[1][0] == 'X' and list[2][1] != 'taken':
        list[2][1] = 'taken'
        list[2][0] = 'X'
        pcloc1 = 3


    elif list[3][0] == 'X' and list[4][0] == 'X' and list[5][1] != 'taken':
        list[5][1] = 'taken'
        list[5][0] = 'X'
        pcloc1 = 6


    elif list[6][0] == 'X' and list[7][0] == 'X' and list[8][1] != 'taken':
        list[8][1] = 'taken'
        list[8][0] = 'X'
        pcloc1 = 9


    elif list[1][0] == 'X' and list[2][0] == 'X' and list[0][1] != 'taken':
        list[0][1] = 'taken'
        list[0][0] = 'X'
        pcloc1 = 0

    elif list[4][0] == 'X' and list[5][0] == 'X' and list[3][1] != 'taken':
        list[3][1] = 'taken'
        list[3][0] = 'X'
        pcloc1 = 4

    elif list[7][0] == 'X' and list[8][0] == 'X' and list[6][1] != 'taken':
        list[6][1] = 'taken'
        list[6][0] = 'X'
        pcloc1 = 7


    elif list[0][0] == 'X' and list[2][0] == 'X' and list[1][1] != 'taken':
        list[1][1] = 'taken'
        list[1][0] = 'X'
        pcloc1 = 2

    elif list[3][0] == 'X' and list[5][0] == 'X' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'X'
        pcloc1 = 5

    elif list[6][0] == 'X' and list[8][0] == 'X' and list[7][1] != 'taken':
        list[7][1] = 'taken'
        list[7][0] = 'X'
        pcloc1 = 8


    elif list[0][0] == 'X' and list[4][0] == 'X' and list[8][1] != 'taken':
        list[8][1] = 'taken'
        list[8][0] = 'X'
        pcloc1 = 9

    elif list[4][0] == 'X' and list[8][0] == 'X' and list[0][1] != 'taken':
        list[0][1] = 'taken'
        list[0][0] = 'X'
        pcloc1 = 1

    elif list[0][0] == 'X' and list[8][0] == 'X' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'X'
        pcloc1 = 5

    elif list[2][0] == 'X' and list[4][0] == 'X' and list[6][1] != 'taken':
        list[6][1] = 'taken'
        list[6][0] = 'X'
        pcloc1 = 7

    elif list[4][0] == 'X' and list[6][0] == 'X' and list[2][1] != 'taken':
        list[2][1] = 'taken'
        list[2][0] = 'X'
        pcloc1 = 3


    elif list[2][0] == 'X' and list[6][0] == 'X' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'X'
        pcloc1 = 5


    elif list[0][0] == 'X' and list[3][0] == 'X' and list[6][1] != 'taken':
        list[6][1] = 'taken'
        list[6][0] = 'X'
        pcloc1 = 7


    elif list[3][0] == 'X' and list[6][0] == 'X' and list[0][1] != 'taken':
        list[0][1] = 'taken'
        list[0][0] = 'X'
        pcloc1 = 1

    elif list[0][0] == 'X' and list[6][0] == 'X' and list[3][1] != 'taken':
        list[3][1] = 'taken'
        list[3][0] = 'X'
        pcloc1 = 4

    elif list[1][0] == 'X' and list[4][0] == 'X' and list[7][1] != 'taken':
        list[7][1] = 'taken'
        list[7][0] = 'X'
        pcloc1 = 8

    elif list[4][0] == 'X' and list[7][0] == 'X' and list[1][1] != 'taken':
        list[1][1] = 'taken'
        list[1][0] = 'X'
        pcloc1 = 2

    elif list[1][0] == 'X' and list[7][0] == 'X' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'X'
        pcloc1 = 5

    elif list[2][0] == 'X' and list[5][0] == 'X' and list[8][1] != 'taken':
        list[8][1] = 'taken'
        list[8][0] = 'X'
        pcloc1 = 9

    elif list[5][0] == 'X' and list[8][0] == 'X' and list[2][1] != 'taken':
        list[2][1] = 'taken'
        list[2][0] = 'X'
        pcloc1 = 3

    elif list[2][0] == 'X' and list[8][0] == 'X' and list[5][1] != 'taken':
        list[5][1] = 'taken'
        list[5][0] = 'X'
        pcloc1 = 6
    # ---------------24 rules to find winning position for opponent user and block it
    elif list[0][0] == 'O' and list[1][0] == 'O' and list[2][1] != 'taken':
        list[2][1] = 'taken'
        list[2][0] = 'X'
        pcloc1 = 3

    elif list[3][0] == 'O' and list[4][0] == 'O' and list[5][1] != 'taken':
        list[5][1] = 'taken'
        list[5][0] = 'X'
        pcloc1 = 6

    elif list[6][0] == 'O' and list[7][0] == 'O' and list[8][1] != 'taken':
        list[8][1] = 'taken'
        list[8][0] = 'X'
        pcloc1 = 9

    elif list[1][0] == 'O' and list[2][0] == 'O' and list[0][1] != 'taken':
        list[0][1] = 'taken'
        list[0][0] = 'X'
        pcloc1 = 1

    elif list[4][0] == 'O' and list[5][0] == 'O' and list[3][1] != 'taken':
        list[3][1] = 'taken'
        list[3][0] = 'X'
        pcloc1 = 4

    elif list[7][0] == 'O' and list[8][0] == 'O' and list[6][1] != 'taken':
        list[6][1] = 'taken'
        list[6][0] = 'X'
        pcloc1 = 7

    elif list[0][0] == 'O' and list[2][0] == 'O' and list[1][1] != 'taken':
        list[1][1] = 'taken'
        list[1][0] = 'X'
        pcloc1 = 2

    elif list[3][0] == 'O' and list[5][0] == 'O' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'X'
        pcloc1 = 5

    elif list[6][0] == 'O' and list[8][0] == 'O' and list[7][1] != 'taken':
        list[7][1] = 'taken'
        list[7][0] = 'X'
        pcloc1 = 8



    elif list[0][0] == 'O' and list[4][0] == 'O' and list[8][1] != 'taken':
        list[8][1] = 'taken'
        list[8][0] = 'X'
        pcloc1 = 9

    elif list[4][0] == 'O' and list[8][0] == 'O' and list[0][1] != 'taken':
        list[0][1] = 'taken'
        list[0][0] = 'X'
        pcloc1 = 1

    elif list[0][0] == 'O' and list[8][0] == 'O' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'X'
        pcloc1 = 5

    elif list[2][0] == 'O' and list[4][0] == 'O' and list[6][1] != 'taken':
        list[6][1] = 'taken'
        list[6][0] = 'X'
        pcloc1 = 7

    elif list[4][0] == 'O' and list[6][0] == 'O' and list[2][1] != 'taken':
        list[2][1] = 'taken'
        list[2][0] = 'X'
        pcloc1 = 3

    elif list[2][0] == 'O' and list[6][0] == 'O' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'X'
        pcloc1 = 5


    elif list[0][0] == 'O' and list[3][0] == 'O' and list[6][1] != 'taken':
        list[6][1] = 'taken'
        list[6][0] = 'X'
        pcloc1 = 7

    elif list[3][0] == 'O' and list[6][0] == 'O' and list[0][1] != 'taken':
        list[0][1] = 'taken'
        list[0][0] = 'X'
        pcloc1 = 1

    elif list[0][0] == 'O' and list[6][0] == 'O' and list[3][1] != 'taken':
        list[3][1] = 'taken'
        list[3][0] = 'X'
        pcloc1 = 4

    elif list[1][0] == 'O' and list[4][0] == 'O' and list[7][1] != 'taken':
        list[7][1] = 'taken'
        list[7][0] = 'X'
        pcloc1 = 8

    elif list[4][0] == 'O' and list[7][0] == 'O' and list[1][1] != 'taken':
        list[1][1] = 'taken'
        list[1][0] = 'X'
        pcloc1 = 2

    elif list[1][0] == 'O' and list[7][0] == 'O' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'X'
        pcloc1 = 5


    elif list[2][0] == 'O' and list[5][0] == 'O' and list[8][1] != 'taken':
        list[8][1] = 'taken'
        list[8][0] = 'X'
        pcloc1 = 9

    elif list[5][0] == 'O' and list[8][0] == 'O' and list[2][1] != 'taken':
        list[2][1] = 'taken'
        list[2][0] = 'X'
        pcloc1 = 3

    elif list[2][0] == 'O' and list[8][0] == 'O' and list[5][1] != 'taken':
        list[5][1] = 'taken'
        list[5][0] = 'X'
        pcloc1 = 6
    # ---------------else use random available position
    else:
        pcloc1 = random.randint(0, 8)
        if list[pcloc1][1] == 'taken':
            generateandvalidateinput_PC1()
        else:
            list[pcloc1][0] = "X"
            list[pcloc1][1] = 'taken'
            pcloc1 = pcloc1 + 1

def generateandvalidateinput_PC2():
    global pcloc2

    if list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'O'
        pcloc2 = 5


    elif list[0][0] == 'O' and list[1][0] == 'O' and list[2][1] != 'taken':
        list[2][1] = 'taken'
        # list[2][0] = 'O'
        pcloc2 = 3

    elif list[3][0] == 'O' and list[4][0] == 'O' and list[5][1] != 'taken':
        list[5][1] = 'taken'
        list[5][0] = 'O'
        pcloc2 = 6

    elif list[6][0] == 'O' and list[7][0] == 'O' and list[8][1] != 'taken':
        list[8][1] = 'taken'
        list[8][0] = 'O'
        pcloc2 = 9

    elif list[1][0] == 'O' and list[2][0] == 'O' and list[0][1] != 'taken':
        list[0][1] = 'taken'
        list[0][0] = 'O'
        pcloc2 = 0

    elif list[4][0] == 'O' and list[5][0] == 'O' and list[3][1] != 'taken':
        list[3][1] = 'taken'
        list[3][0] = 'O'
        pcloc2 = 4

    elif list[7][0] == 'O' and list[8][0] == 'O' and list[6][1] != 'taken':
        list[6][1] = 'taken'
        list[6][0] = 'O'
        pcloc2 = 7

    elif list[0][0] == 'O' and list[2][0] == 'O' and list[1][1] != 'taken':
        list[1][1] = 'taken'
        list[1][0] = 'O'
        pcloc2 = 2

    elif list[3][0] == 'O' and list[5][0] == 'O' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'O'
        pcloc2 = 5

    elif list[6][0] == 'O' and list[8][0] == 'O' and list[7][1] != 'taken':
        list[7][1] = 'taken'
        list[7][0] = 'O'
        pcloc2 = 8

    elif list[0][0] == 'O' and list[4][0] == 'O' and list[8][1] != 'taken':
        list[8][1] = 'taken'
        list[8][0] = 'O'
        pcloc2 = 9

    elif list[4][0] == 'O' and list[8][0] == 'O' and list[0][1] != 'taken':
        list[0][1] = 'taken'
        list[0][0] = 'O'
        pcloc2 = 1

    elif list[0][0] == 'O' and list[8][0] == 'O' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'O'
        pcloc2 = 5

    elif list[2][0] == 'O' and list[4][0] == 'O' and list[6][1] != 'taken':
        list[6][1] = 'taken'
        list[6][0] = 'O'
        pcloc2 = 7

    elif list[4][0] == 'O' and list[6][0] == 'O' and list[2][1] != 'taken':
        list[2][1] = 'taken'
        list[2][0] = 'O'
        pcloc2 = 3

    elif list[2][0] == 'O' and list[6][0] == 'O' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'O'
        pcloc2 = 5


    elif list[0][0] == 'O' and list[3][0] == 'O' and list[6][1] != 'taken':
        list[6][1] = 'taken'
        list[6][0] = 'O'
        pcloc2 = 7


    elif list[3][0] == 'O' and list[6][0] == 'O' and list[0][1] != 'taken':
        list[0][1] = 'taken'
        list[0][0] = 'O'
        pcloc2 = 1

    elif list[0][0] == 'O' and list[0][0] == 'O' and list[3][1] != 'taken':
        list[3][1] = 'taken'
        list[3][0] = 'O'
        pcloc2 = 4

    elif list[1][0] == 'O' and list[4][0] == 'O' and list[7][1] != 'taken':
        list[7][1] = 'taken'
        list[7][0] = 'O'
        pcloc2 = 8

    elif list[4][0] == 'O' and list[7][0] == 'O' and list[1][1] != 'taken':
        list[1][1] = 'taken'
        list[1][0] = 'O'
        pcloc2 = 2

    elif list[1][0] == 'O' and list[7][0] == 'O' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'O'
        pcloc2 = 5

    elif list[2][0] == 'O' and list[5][0] == 'O' and list[8][1] != 'taken':
        list[8][1] = 'taken'
        list[8][0] = 'O'
        pcloc2 = 9

    elif list[5][0] == 'O' and list[8][0] == 'O' and list[2][1] != 'taken':
        list[2][1] = 'taken'
        list[2][0] = 'O'
        pcloc2 = 3

    elif list[2][0] == 'O' and list[8][0] == 'O' and list[5][1] != 'taken':
        list[5][1] = 'taken'
        list[5][0] = 'O'
        pcloc2 = 6
    # ------------------------------------------------------------------------
    elif list[0][0] == 'X' and list[1][0] == 'X' and list[2][1] != 'taken':
        list[2][1] = 'taken'
        list[2][0] = 'O'
        pcloc2 = 3

    elif list[3][0] == 'X' and list[4][0] == 'X' and list[5][1] != 'taken':
        list[5][1] = 'taken'
        list[5][0] = 'O'
        pcloc2 = 6

    elif list[6][0] == 'X' and list[7][0] == 'X' and list[8][1] != 'taken':
        list[8][1] = 'taken'
        list[8][0] = 'O'
        pcloc2 = 9

    elif list[1][0] == 'X' and list[2][0] == 'X' and list[0][1] != 'taken':
        list[0][1] = 'taken'
        list[0][0] = 'O'
        pcloc2 = 1

    elif list[4][0] == 'X' and list[5][0] == 'X' and list[3][1] != 'taken':
        list[3][1] = 'taken'
        list[3][0] = 'O'
        pcloc2 = 4

    elif list[7][0] == 'X' and list[8][0] == 'X' and list[6][1] != 'taken':
        list[6][1] = 'taken'
        list[6][0] = 'O'
        pcloc2 = 7

    elif list[0][0] == 'X' and list[2][0] == 'X' and list[1][1] != 'taken':
        list[1][1] = 'taken'
        list[1][0] = 'O'
        pcloc2 = 2

    elif list[3][0] == 'X' and list[5][0] == 'X' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'O'
        pcloc2 = 5

    elif list[6][0] == 'X' and list[8][0] == 'X' and list[7][1] != 'taken':
        list[7][1] = 'taken'
        list[7][0] = 'O'
        pcloc2 = 8



    elif list[0][0] == 'X' and list[4][0] == 'X' and list[8][1] != 'taken':
        list[8][1] = 'taken'
        list[8][0] = 'O'
        pcloc2 = 9

    elif list[4][0] == 'X' and list[8][0] == 'X' and list[0][1] != 'taken':
        list[0][1] = 'taken'
        list[0][0] = 'O'
        pcloc2 = 1

    elif list[0][0] == 'X' and list[8][0] == 'X' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'O'
        pcloc2 = 5

    elif list[2][0] == 'X' and list[4][0] == 'X' and list[6][1] != 'taken':
        list[6][1] = 'taken'
        list[6][0] = 'O'
        pcloc2 = 7

    elif list[4][0] == 'X' and list[6][0] == 'X' and list[2][1] != 'taken':
        list[2][1] = 'taken'
        list[2][0] = 'O'
        pcloc2 = 3

    elif list[2][0] == 'X' and list[6][0] == 'X' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'O'
        pcloc2 = 5


    elif list[0][0] == 'X' and list[3][0] == 'X' and list[6][1] != 'taken':
        list[6][1] = 'taken'
        list[6][0] = 'O'
        pcloc2 = 7

    elif list[3][0] == 'X' and list[6][0] == 'X' and list[0][1] != 'taken':
        list[0][1] = 'taken'
        list[0][0] = 'O'
        pcloc2 = 1

    elif list[0][0] == 'X' and list[0][0] == 'X' and list[3][1] != 'taken':
        list[3][1] = 'taken'
        list[3][0] = 'O'
        pcloc2 = 4

    elif list[1][0] == 'X' and list[4][0] == 'X' and list[7][1] != 'taken':
        list[7][1] = 'taken'
        list[7][0] = 'O'
        pcloc2 = 8

    elif list[4][0] == 'X' and list[7][0] == 'X' and list[1][1] != 'taken':
        list[1][1] = 'taken'
        list[1][0] = 'O'
        pcloc2 = 2

    elif list[1][0] == 'X' and list[7][0] == 'X' and list[4][1] != 'taken':
        list[4][1] = 'taken'
        list[4][0] = 'O'
        pcloc2 = 5


    elif list[2][0] == 'X' and list[5][0] == 'X' and list[8][1] != 'taken':
        list[8][1] = 'taken'
        list[8][0] = 'O'
        pcloc2 = 9

    elif list[5][0] == 'X' and list[8][0] == 'X' and list[2][1] != 'taken':
        list[2][1] = 'taken'
        list[2][0] = 'O'
        pcloc2 = 3

    elif list[2][0] == 'X' and list[8][0] == 'X' and list[5][1] != 'taken':
        list[5][1] = 'taken'
        list[5][0] = 'O'
        pcloc2 = 6
    # ------------------------------------------------------------------------
    else:
        pcloc2 = random.randint(0, 8)
        if list[pcloc2][1] == 'taken':
            generateandvalidateinput_PC2()
        else:
            list[pcloc2][0] = "O"
            list[pcloc2][1] = 'taken'
            pcloc2 = pcloc2 + 1


def pc1_move():
    global movesctr
    generateandvalidateinput_PC1()
    print(" \n\nPC1 will play now...")
    time.sleep(5)

    prinbox()
    print("                         (PC1 played 'X' at : ", pcloc1, ")")
    movesctr += 1
    print("Total Moves = ", movesctr)
    checkresult()
    pc2_move()


def pc2_move():
    global movesctr
    generateandvalidateinput_PC2()
    print(" \n\nPC2 will play now...")
    time.sleep(5)

    prinbox()
    print("                         (PC2 played 'O' at : ", pcloc2, ")")
    movesctr+=1
    print("Total Moves = ", movesctr)
    checkresult()
    pc1_move()



os.system('cls')
welcome()
prinbox()
whoplaysfirst()
if opener == 'PC1':
    pc1_move()
    #movesctr+=1
    #print("Total Moves = ",movesctr)

elif opener == 'PC2':
    pc2_move()




# checkresult()
Sorry, but you cannot expect anyone to look into several hundred lines of if/elif/else blocks to check your logic. Not to mention the use of global variables and other issues in your code...
You need to at least try to narrow down where the problem is. If you don't know how to debug, adding extra prints to show the values of your variables and which branches of if and else will help. Since you're also generating random values (e.g. line 634), you might want to make that deterministic, if it helps. To do that, use the random.seed function with a constant value (since you're not seeding the generator, the current time is used, so you'll get different values each time you run the program).
I have figured it out Cool