#๐Ÿ”’ tic tac toe computer

35 messages ยท Page 1 of 1 (latest)

burnt mistBOT
#

@pallid birch

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

pallid birch
#

am i stupid or is that integer division right there returning a float?

burnt mistBOT
#

:warning: Your 3.13 eval job has completed with return code 0.

[No output]
pallid birch
#

!e print(3.0//3)

burnt mistBOT
pallid birch
#

huhhh

wind snow
#

hm thats weird

#

you might just have to do int(x / y)

pallid birch
#

!e print(3//3)

burnt mistBOT
pallid birch
#

wow

#

apparently float//int := float, but im like 90% certain it didnt get a float there

wind snow
#

i think it's because it turns both into floats

#

!e print(3 // 3.0)

burnt mistBOT
wind snow
#

yup

#

i dont get why it wouldnt just make an int

#

!e print(3.0 // 3.0)

burnt mistBOT
wind snow
#

python :pain:

#

@pallid birch just cast it to an int to be very sure

pallid birch
#

just did that...

#

what i was actually doing is tic tac toe

wind snow
wind snow
pallid birch
#

from copy import deepcopy as dc
def best_move(board,player):
    winner = is_won(board)
    if winner == 1:
        return 1
    elif winner == 0:
        return 0
    elif isfull(board):
        return 0.5

    tries = []
    for k,i in enumerate(board):
        for k_,i_ in enumerate(i):
            if i_ == None:
                tries.append(dc(board))
                tries[-1][k][k_] = player

    if player == 1:
        return max(best_move(i,0) for i in tries)
    else:
        return min(best_move(i,0) for i in tries)


def isfull(board):
    if not any(any(i == None for i in j) for j in board):
        return True
    return False


def is_won(board):
    if any(all(j == 1 for j in i) for i in board):
        return 1
    elif any(all(j == 0 for j in i) for i in board):
        return 0

    elif any(all(board[i][j] == 1 for i in range(3)) for j in range(3)):
        return 1
    elif any(all(board[i][j] == 0 for i in range(3)) for j in range(3)):
        return 0

    elif all(board[i][i] == 1 for i in range(3)):
        return 1
    elif all(board[i][i] == 0 for i in range(3)):
        return 0

    elif all(board[i][2-i] == 1 for i in range(3)):
        return 1
    elif all(board[i][2-1] == 0 for i in range(3)):
        return 0

    else: return None


def render(board):
    print(("\n"+"-"*11+"\n").join("|".join([[" X "," O "][j] if not j is None else "   " for j in i]) for i in board))


def game(board,player):
    if isfull(board):
        return -1

    if player == 0:
        move = int(input("Player 1 enter your move: "))
        board[int(move//3)][int(move%3)] = 1
        render(board)

    elif player == 1:
        print("computing best move")
        move = best_move(board,1)
        board[int(move//3)][int(move%3)] = 0
        render(board)

    winner = is_won(board)
    if winner:
        return winner

    return game(board, not player)

board = [[None]*3 for _ in range(3)]
game(board,0)
#

for some reason the computer can just override fields

#

just ignore everything but best_move(), thats where the mistake is

#

tic tac toe computer

wind snow
pallid birch
#

i know, i have them all memorized anyways

#

but its about the concept

#

i could throw that at connect 4 or chess in the future

burnt mistBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.