Saltar al contenido

minimax tic tac toe tamaño variable ejemplo de código de Python

Ejemplo: cómo codificar un juego de tic tac toe con ai python

#Tic Tac Toe game in python by techwithtim

board =[' 'for x inrange(10)]definsertLetter(letter, pos):
    board[pos]= letter

defspaceIsFree(pos):return board[pos]==' 'defprintBoard(board):print('   |   |')print(' '+ board[1]+' | '+ board[2]+' | '+ board[3])print('   |   |')print('-----------')print('   |   |')print(' '+ board[4]+' | '+ board[5]+' | '+ board[6])print('   |   |')print('-----------')print('   |   |')print(' '+ board[7]+' | '+ board[8]+' | '+ board[9])print('   |   |')defisWinner(bo, le):return(bo[7]== le and bo[8]== le and bo[9]== le)or(bo[4]== le and bo[5]== le and bo[6]== le)or(bo[1]== le and bo[2]== le and bo[3]== le)or(bo[1]== le and bo[4]== le and bo[7]== le)or(bo[2]== le and bo[5]== le and bo[8]== le)or(bo[3]== le and bo[6]== le and bo[9]== le)or(bo[1]== le and bo[5]== le and bo[9]== le)or(bo[3]== le and bo[5]== le and bo[7]== le)defplayerMove():
    run =Truewhile run:
        move =input('Please select a position to place an 'X' (1-9): ')try:
            move =int(move)if move >0and move <10:if spaceIsFree(move):
                    run =False
                    insertLetter('X', move)else:print('Sorry, this space is occupied!')else:print('Please type a number within the range!')except:print('Please type a number!')defcompMove():
    possibleMoves =[x for x, letter inenumerate(board)if letter ==' 'and x !=0]
    move =0for let in['O','X']:for i in possibleMoves:
            boardCopy = board[:]
            boardCopy[i]= let
            if isWinner(boardCopy, let):
                move = i
                return move

    cornersOpen =[]for i in possibleMoves:if i in[1,3,7,9]:
            cornersOpen.append(i)iflen(cornersOpen)>0:
        move = selectRandom(cornersOpen)return move

    if5in possibleMoves:
        move =5return move

    edgesOpen =[]for i in possibleMoves:if i in[2,4,6,8]:
            edgesOpen.append(i)iflen(edgesOpen)>0:
        move = selectRandom(edgesOpen)return move

defselectRandom(li):import random
    ln =len(li)
    r = random.randrange(0,ln)return li[r]defisBoardFull(board):if board.count(' ')>1:returnFalseelse:returnTruedefmain():print('Welcome to Tic Tac Toe!')
    printBoard(board)whilenot(isBoardFull(board)):ifnot(isWinner(board,'O')):
            playerMove()
            printBoard(board)else:print('Sorry, O's won this time!')breakifnot(isWinner(board,'X')):
            move = compMove()if move ==0:print('Tie Game!')else:
                insertLetter('O', move)print('Computer placed an 'O' in position', move ,':')
                printBoard(board)else:print('X's won this time! Good Job!')breakif isBoardFull(board):print('Tie Game!')whileTrue:
    answer =input('Do you want to play again? (Y/N)')if answer.lower()=='y'or answer.lower =='yes':
        board =[' 'for x inrange(10)]print('-----------------------------------')
        main()else:break

Si guardas alguna sospecha y disposición de refinar nuestro ensayo eres capaz de dejar una acotación y con deseo lo leeremos.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *