TechBee
TechBee

Reputation: 31

Tic Tac Toe,Python, Index already filled problem

I have been trying to solve this code from the past few days where if I by mistake place my symbol("x" or "o") in the index already containing symbol, then it should display message "index already filled!", the problem is that it is showing that message even for the indexes which aren't filled. I am a beginner, please help me with this code and please answer my question with your code since i'm kind of bad at English.

import numpy

board=numpy.array([["-","-","-"],["-","-","-"],["-","-","-"]])

numpy.matrix(board)

def pos(row,col,symbol,altr):

    while(board[row][col]==symbol or board[row][col]==altr):
        print("index already filled")
        place(symbol,altr)
    board[row][col]=symbol
    return board

def place(symbol,altr):

    c=int(input("enter index: "))
    if(c==1):
         return pos(0,0,symbol,altr)
    elif(c==2):
         return pos(0,1,symbol,altr)
    elif(c==3):
         return pos(0,2,symbol,altr)
    elif(c==4):
         return pos(1,0,symbol,altr)
    elif(c==5):
         return pos(1,1,symbol,altr)
    elif(c==6):
         return pos(1,2,symbol,altr)
    elif(c==7):
        return pos(2,0,symbol,altr)
    elif(c==8):
         return pos(2,1,symbol,altr)
    elif(c==9):
         return pos(2,2,symbol,altr)
def won(symbol):

    return check_rows(symbol) or check_cols(symbol) or check_diagnols(symbol)
def check_rows(symbol):

    for i in range(3):
        count=0
        for j in range(3):
            if(board[i][j]==symbol):
                count+=1
        if(count==3):
            return True
    else:
        return False
def check_cols(symbol):

    for j in range(3):
        count=0
        for i in range(3):
            if(board[i][j]==symbol):
                count+=1
        if(count==3):
            return True
    else:
        return False
def check_diagnols(symbol):

    if(board[0][0]==board[1][1] and board[1][1]==board[2][2] and board[2][2]==symbol):
        return True
    if(board[0][2]==board[1][1] and board[1][1]==board[2][0] and board[2][0]==symbol):
        return True
    return False

def play():

    p1=input("player1,enter your name: ")
    p2=input("player2,enter your name: ")
    pl1="x"
    pl2="o"
    i=0
    while(i<9):
        if(i%2==0):
            print(board)
            print(p1,"your turn")
            place(pl1,pl2)
            if(won(pl1)):
                print(board)
                print(p1,"you won")
                break
        else:
            if(i%2==1):
                print(board)
                print(p2,"your turn")
                place(pl2,pl1)
                if(won(pl2)):
                    print(board)
                    print(p2,"you won")
                    break
        i+=1
    if not won(pl1) and not won(pl2):
        print(board)
        print("draw")

Upvotes: 3

Views: 286

Answers (2)

Jason Yang
Jason Yang

Reputation: 13061

I think the problem is place call pos, and pos call place, again and again. It's not necessary to return board in both functions.

def pos(row,col,symbol,altr):

    if board[row][col]==symbol or board[row][col]==altr:
        print("index already filled")
        return False
    else:
        board[row][col]=symbol
        return True

def place(symbol,altr):

    while True
        c=int(input("enter index: "))
        if 1 <= c <= 9:
            c = c-1
            if pos(c//3, c%3, symbol, alter):
                return

I explained this with pseudo code

def pos(row, column, player_symbol, another_player_sumbol):
    # Check (row, cell) if not occupied
    if selected_cell not with blank syambol:
        message('Cell occupied')
        return input_is_NG
    else:
        set cell occupied
        return input_is_OK

def place(symbol,altr):

    while True (until position_is_OK):
        input position of cell
        if position is from 1 to 9:
            position -= 1
            if pos(position//3, position%3, symbol, alter) is OK:
                return # exit from place, and next turn for another player
        user_input_is_NG, while loop again

Upvotes: 1

trincot
trincot

Reputation: 350147

The while is not appropriate: the condition's result will never change, no matter how many times you loop. So effectively it is an infinite loop: once you get in, you never get out again.

It should be an if. And secondly, when the if condition is true, you don't want the execution of board[row][col]=symbol with those values of row and col. You should let that assignment happen through the recursive call of place. That execution will come to this function with (hopefully) better values of row and col, and will take care of putting the value in the board. So, ... you need to put that assignment in an else block, so it only happens when the location is fine.

    if (board[row][col]==symbol or board[row][col]==altr):
        print("index already filled")
        place(symbol,altr)
    else:
        board[row][col]=symbol

Remark: Even though it works like this, I would not have chosen for the recursive solution. You may want to look at an alternative where you have no recursion, but a while loop in the place function.

Upvotes: 2

Related Questions