Help to solve this problem

Hello everyone, I am currently working on MINMAX alogorithm on TIC-TAC-TOE, before that I am trying to code out a small idea, that might help me during minmax, here’s the code.
The goal is to randomly select each element at a time from one array and append to other array. I want to improve this . Provide your feedback,suggestions.

import random

def getRandom():
    arr = ['random1','random2','random3','random4','random5','random6','random7','random8','random9','random10']
    selected = []
    selected.append(arr[9])
    print(selected)
    print(random.randint(0,len(arr)))
    while len(selected) < 10:
        
        if arr[random.randint(0,len(arr))] not in selected:
            
            selected.append(arr[random.randint(0,len(arr))])
        else :
            print('Its already there')

    print(selected)
        
        

getRandom()

random.randint() is a function that will return a random number every time you call it. So if you call it 3 times, you might get 3 different numbers → meaning if you want to work with the number, you need to save it in a variable.

Also in order to save time, you should remove the element from arr. Because if selected has 9 elements, your while-loop would on average take 10 runs to add the last element, despite the fact there is only one eligable element.

Also also, in oder to REALLY save time: did you check if the random-library already contains a method to create a random order?

Thank you, the point to use the random.randint() as a variable, was the key.

I have written the code for the minmax for tic-tac-toe , it’s still unfinished but I am unable to go forward. Any suggestions?

import random

player = 'X'
player2 = 'O'
winner = None
gameRunning = True
winstatus = False
board = ['-','-','-',
         '-','-','-',
         '-','-','-']
#first setup the board 
def board_(board):
    # board = ['-','-','-',
    #          '-','-','-',
    #          '-','-','-']
    #board[6] = '-'
    print(board[0] + ' | ' + board[1] + ' | ' + board[2])
    print(board[3] + ' | ' + board[4] + ' | ' + board[5])
    print(board[6] + ' | ' + board[7] + ' | ' + board[8])
    #board[6] = '-'
    return board

print(board_(board))

#take the input
def input_(board):
    inp = int(input("Enter the position : "))
    board = board_(board)
    if inp >= 1 and inp <= 9:
        print(inp)
        if board[inp-1] == '-':
            board[inp-1] = player
        #print(board)

    else:
        print('Please check the input value before playing the game')

input_(board)

#check winning situations for players
def checkHorizontal(board):
    global gameRunning
    if board[0] == board[1] == board[2] and board[0] != '-':
        print('YaY you won!!!')
        winstatus = True
        gameRunning = False
        #return True
    elif board[3] == board[4] == board[5] and board[3] != '-':
        print('yay')
        winstatus = True
        gameRunning = False
    elif board[6] == board[7] == board[8] and board[6] != '-':
        print('yay')
        winstatus = True
        gameRunning = False

def checkVertical(board):
    global gameRunning
    if board[0] == board[3] == board[6] and board[0] != '-':
        print('yay') 
        winstatus = True
        gameRunning = False
    elif board[1] == board[4] == board[7] and board[1] != '-':
        print('yay')
        winstatus = True
        gameRunning = False
    elif board[2] == board[5] == board[8] and board[2] != '-':
        print('yay')
        winstatus = True
        gameRunning = False

def checkDiagonal(board):
    global gameRunning
    if board[0] == board[4] == board[8] and board[0] != '-':
        print('yay')
        winstatus = True
        gameRunning = False

    elif board[2] == board[4] == board[6] and board[2] != '-':
        print('yay')
        winstatus = True
        gameRunning = False


# def checkTie(board):
#     global gameRunning
#     for i in range(0,9):
#         if board[i] != '-':
#             print('Its a TIE')
#             gameRunning = False

# def switchPlayer(board):
#     for i in range(0,9):
#         if player == 'X' and board[i] != '-':
#             player = 'O'
    
score = 0   
def score_():
    global score
    if checkDiagonal(board) or checkHorizontal(board) or checkVertical(board):
        #global score
        score = 10
    # elif checkTie(board):
        
    #     score = 0
    else:
        score = -10

def minmax(board):
    scores  =[]
    moves = []
    rand_val = random.randint(0,len(board)) - 1
    if board[rand_val] == '-' and board[rand_val] != player:
        board[rand_val] = player2
        moves.append(rand_val)
    

while gameRunning:
    input_(board)
    checkHorizontal(board)
    checkVertical(board)
    checkDiagonal(board)
    #checkTie(board)
    #switchPlayer(board)
    score_()
    #board_(board)
    minmax(board)


print(score)

What’s the problem? Please elaborate what issue you are encountering.
That’s like 50-100 lines of code and you didn’t encorporate a couple of suggestions I gave you to shorten it.

So, what do you expect us to do? Read it and give suggestions… on what?

Also consider uploading the code on something like Replit, so we could try it out.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.