pycharm 我写这段代码是为了在Python中制作一个没有GUI的井字游戏

xyhw6mcr  于 2023-04-20  发布在  PyCharm
关注(0)|答案(2)|浏览(159)

我做了这个代码井字棋在python和迭代后,它的工作正常,但在计算机之间停止输入.我的代码涉及到一个用户与计算机游戏.请帮助我,我知道我的代码不包含注解,但我希望你能理解,我是一个初学者,所以请帮助我!!!
在这段代码中,计算机首先让你选择OX,然后进行抛硬币,你输入1或0,并将其与一个在1和0之间的随机值进行比较。
对于计算机,我使用了randint()函数,以便它选择放置XO的行和列。在每次迭代中,代码都会检查计算机获得的随机行或列是否与已经填充的行或列不匹配。所以这就是所有关于此代码的内容!!

import random
row1=None
column1=None
row2 = None
column2 = None
data1=[]
data2=[]
data3=[]
data4=[]
count=0
count2=0
print("Welcome to TIC-TAC-TOE!!")
tic=input("Enter O or X as your choice --> ")
if tic=='O':
    tac='X'
else:
    tac='O'
block=[[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
i=0
choice=int(input("Enter 1 for Head and 0 for Tail --> "))
x=random.randint(0,1)
if choice==x:
    print("You won the toss you get the first chance : ")
    print("## Enter the row and column you want to put your choice in ##")
    row1=int(input("Row --> "))
    data1.append(row1)
    column1=int(input("Column --> "))
    data2.append(column1)
    block[row1-1][column1-1]=tic
    print(block[0][0],"|",block[0][1],"|",block[0][2])
    print(block[1][0],"|",block[1][1],"|",block[1][2])
    print(block[2][0],"|",block[2][1],"|",block[2][2])
    while True:
        i+=1
        if block[0][0]==block[0][1] and block[0][1]==block[0][2] and block[0][0]!=' ':
            if block[0][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[1][0]==block[1][1] and block[1][1]==block[1][2] and block[1][0]!=' ':
            if block[1][0]==tic:
                print("You Win!!")
            else:
                print('you Lose!!')
            break
        elif block[2][0]==block[2][1] and block[2][1]==block[2][2] and block[2][0]!=' ':
            if block[2][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][0]==block[1][0] and block[1][0]==block[2][0] and block[0][0]!=' ':
            if block[0][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][1]==block[1][1] and block[1][1]==block[2][1] and block[0][1]!=' ':
            if block[0][1]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][2]==block[1][2] and block[1][2]==block[2][2] and block[0][2]!=' ':
            if block[0][2]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][0]==block[1][1] and block[1][1]==block[2][2] and block[0][0]!=' ':
            if block[0][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][2]==block[1][1] and block[1][1]==block[2][0] and block[0][2]!=' ':
            if block[0][2]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif i%2!=0:
            print("## A.I ##")
            if row2!=None and column2!=None:
                row2=random.randint(1,3)
                data3.append(row2)
                index1+=1
                length1=len(data3)
                for j in data1:
                    if row2==j:
                        count+=1
                for k in range(0,index1):
                    if row2==data3[k]:
                        count2+=1
                while count>0 or count2>0:
                    row2=random.randint(1,3)
                    for j in data1:
                        if row2 == j:
                            count += 1
                    for k in range(0, index1):
                        if row2 == data3[k]:
                            count2 += 1
                data3[index1]=row2
                column2=random.randint(1,3)
                data4.append(column2)
                index2+=1
                length2=len(data4)
                count=0
                count2=0
                for j in data2:
                    if row2==j:
                        count+=1
                for k in range(0,index2):
                    if column2==data4[k]:
                        count2+=1
                while count>0 or count2>0:
                    column2=random.randint(1,3)
                    for j in data2:
                        if row2 == j:
                            count += 1
                    for k in range(0, index2):
                        if column2 == data4[k]:
                            count2 += 1
                data4[index2]=column2
                block[row2-1][column2-1] = tac
            else:
                row2=random.randint(1,3)
                data3.append(row2)
                index1=0
                for j in data1:
                    if row2==j:
                        count+=1
                while count>0:
                    count=0
                    row2=random.randint(1,3)
                    data3[0]=row2
                    for j in data1:
                        if row2 == j:
                            count += 1
                column2=random.randint(1,3)
                data4.append(column2)
                index2=0
                count = 0
                for j in data2:
                    if row2 == j:
                        count += 1
                while count>0:
                    count=0
                    column2=random.randint(1,3)
                    data4[0]=column2
                    for j in data2:
                        if row2 == j:
                            count += 1
                block[row2 - 1][column2 - 1] = tac
            #block[row2-1][column2-1]=tac
            print(block[0][0],"|",block[0][1],"|",block[0][2])
            print(block[1][0],"|",block[1][1],"|",block[1][2])
            print(block[2][0],"|",block[2][1],"|",block[2][2])
        elif i%2==0:
            print('## Your Turn ##')
            row1=int(input("Row --> "))
            data1.append(row1)
            column1=int(input("Column --> "))
            data2.append(column2)
            block[row1-1][column1-1]=tic
            print(block[0][0],"|",block[0][1],"|",block[0][2])
            print(block[1][0],"|",block[1][1],"|",block[1][2])
            print(block[2][0],"|",block[2][1],"|",block[2][2])
        else:
            print("Draw!!")
            break
else:
    print("## A.I won the toss ##")
    print("## A.I ##")
    row2=random.randint(1,3)
    data3.append(row2)
    index1=0
    column2=random.randint(1,3)
    data4.append(column2)
    index2=0
    block[row2-1][column2-1]=tac
    print(block[0][0],"|",block[0][1],"|",block[0][2])
    print(block[1][0],"|",block[1][1],"|",block[1][2])
    print(block[2][0],"|",block[2][1],"|",block[2][2])
    while True:
        i+=1
        if block[0][0]==block[0][1] and block[0][1]==block[0][2] and block[0][0]!=' ':
            if block[0][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[1][0]==block[1][1] and block[1][1]==block[1][2] and block[1][0]!=' ':
            if block[1][0]==tic:
                print("You Win!!")
            else:
                print('you Lose!!')
            break
        elif block[2][0]==block[2][1] and block[2][1]==block[2][2] and block[2][0]!=' ':
            if block[2][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][0]==block[1][0] and block[1][0]==block[2][0] and block[0][0]!=' ':
            if block[0][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][1]==block[1][1] and block[1][1]==block[2][1] and block[0][1]!=' ':
            if block[0][1]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][2]==block[1][2] and block[1][2]==block[2][2] and block[0][2]!=' ':
            if block[0][2]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][0]==block[1][1] and block[1][1]==block[2][2] and block[0][0]!=' ':
            if block[0][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][2]==block[1][1] and block[1][1]==block[2][0] and block[0][2]!=' ':
            if block[0][2]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif i%2==0:
            print("## A.I ##")
            row2=random.randint(1,3)
            data3.append(row2)
            index1+=1
            length1 = len(data3)
            for j in data1:
                if row2==j:
                    count+=1
            for k in range(0,index1):
                if row2==data3[k]:
                    count2+=1
            while count>0 or count2>0:
                count=0
                count2=0
                row2=random.randint(1,3)
                for j in data1:
                    if row2 == j:
                        count += 1
                for k in range(0,index1):
                    if row2==data3[k]:
                        count2+=1
            data3[index1]=row2
            count=0
            count2=0
            column2=random.randint(1,3)
            data4.append(column2)
            index2+=1
            length2 = len(data4)
            for j in data2:
                if column2==j:
                    count+=1
            for k in range(0,index2):
                if column2==data4[k]:
                    count2+=1
            while count>0 or count2>0:
                count=0
                count2=0
                column2=random.randint(1,3)
                for j in data2:
                    if column2 == j:
                        count += 1
                for k in range(0,index2):
                    if column2==data4[k]:
                        count2+=1
            data4[index2]=column2
            block[row2-1][column2-1]=tac
            print(block[0][0],"|",block[0][1],"|",block[0][2])
            print(block[1][0],"|",block[1][1],"|",block[1][2])
            print(block[2][0],"|",block[2][1],"|",block[2][2])
        elif i%2!=0:
            print("## Your Turn ##")
            row1=int(input("Row --> "))
            data1.append(row1)
            column1=int(input("Column --> "))
            data2.append(column2)
            block[row1-1][column1-1]=tic
            print(block[0][0],"|",block[0][1],"|",block[0][2])
            print(block[1][0],"|",block[1][1],"|",block[1][2])
            print(block[2][0],"|",block[2][1],"|",block[2][2])
        else:
            print("Draw!!")
            break
print("Thanks for playing!!")
fykwrbwg

fykwrbwg1#

我稍微修改了一下代码,去掉了while循环,代之以另一个while循环,它检查该行和列所在的位置是否白色,如果没有,那么列继续随机变化,直到有一个地方有空白。现在对于行,我只是检查整行是否被占用,如果是,那么行继续随机变化。这整个逻辑被改变了,只是轮到计算机。和猜猜我减少了代码从300行到188行一样50%
也改变了绘制的逻辑,它基本上是rekt,它只是现在检查是否所有的地方都不是白色,如果是,那么它给出 * 绘制 *

改进的代码

import random
row1=None
column1=None
row2 = None
column2 = None
print("Welcome to TIC-TAC-TOE!!")
tic=input("Enter O or X as your choice --> ")
if tic=='O':
    tac='X'
else:
    tac='O'
block=[[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
i=0
choice=int(input("Enter 1 for Head and 0 for Tail --> "))
x=random.randint(0,1)
if choice==x:
    print("You won the toss you get the first chance : ")
    print("## Enter the row and column you want to put your choice in ##")
    row1=int(input("Row --> "))
    column1=int(input("Column --> "))
    block[row1-1][column1-1]=tic
    print(block[0][0],"|",block[0][1],"|",block[0][2])
    print(block[1][0],"|",block[1][1],"|",block[1][2])
    print(block[2][0],"|",block[2][1],"|",block[2][2])
    index1=0
    index2=0
    while True:
        i+=1
        if block[0][0]==block[0][1] and block[0][1]==block[0][2] and block[0][0]!=' ':
            if block[0][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[1][0]==block[1][1] and block[1][1]==block[1][2] and block[1][0]!=' ':
            if block[1][0]==tic:
                print("You Win!!")
            else:
                print('you Lose!!')
            break
        elif block[2][0]==block[2][1] and block[2][1]==block[2][2] and block[2][0]!=' ':
            if block[2][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][0]==block[1][0] and block[1][0]==block[2][0] and block[0][0]!=' ':
            if block[0][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][1]==block[1][1] and block[1][1]==block[2][1] and block[0][1]!=' ':
            if block[0][1]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][2]==block[1][2] and block[1][2]==block[2][2] and block[0][2]!=' ':
            if block[0][2]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][0]==block[1][1] and block[1][1]==block[2][2] and block[0][0]!=' ':
            if block[0][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][2]==block[1][1] and block[1][1]==block[2][0] and block[0][2]!=' ':
            if block[0][2]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif i%2!=0:
            print("## A.I ##")
            if row2!=None and column2!=None:
                row2=random.randint(1,3)
                while block[row2-1][0]!=' ' and block[row2-1][1]!=' ' and block[row2-1][2]!=' ':
                    row2=random.randint(1,3)
                column2=random.randint(1,3)
                while block[row2-1][column2-1]!=' ':
                    column2=random.randint(1,3)
            else:
                row2=random.randint(1,3)
                column2=random.randint(1,3)
                while block[row2-1][column2-1]!=' ':
                    column2=random.randint(1,3)
            block[row2-1][column2-1]=tac
            print(block[0][0],"|",block[0][1],"|",block[0][2])
            print(block[1][0],"|",block[1][1],"|",block[1][2])
            print(block[2][0],"|",block[2][1],"|",block[2][2])
        elif i%2==0:
            print('## Your Turn ##')
            row1=int(input("Row --> "))
            column1=int(input("Column --> "))
            block[row1-1][column1-1]=tic
            print(block[0][0],"|",block[0][1],"|",block[0][2])
            print(block[1][0],"|",block[1][1],"|",block[1][2])
            print(block[2][0],"|",block[2][1],"|",block[2][2])
        if block[0][0]!=' ' and block[0][1]!=' ' and block[0][2]!=' ' and block[1][0]!=' ' and block[1][1]!=' ' and block[1][2]!=' 'and block[2][0]!=' ' and block[2][1]!=' 'and block[2][2]!=' ':
            print("Draw!!")
            break
else:
    print("## A.I won the toss ##")
    print("## A.I ##")
    row2=random.randint(1,3)
    column2=random.randint(1,3)
    block[row2-1][column2-1]=tac
    print(block[0][0],"|",block[0][1],"|",block[0][2])
    print(block[1][0],"|",block[1][1],"|",block[1][2])
    print(block[2][0],"|",block[2][1],"|",block[2][2])
    while True:
        i+=1
        if block[0][0]==block[0][1] and block[0][1]==block[0][2] and block[0][0]!=' ':
            if block[0][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[1][0]==block[1][1] and block[1][1]==block[1][2] and block[1][0]!=' ':
            if block[1][0]==tic:
                print("You Win!!")
            else:
                print('you Lose!!')
            break
        elif block[2][0]==block[2][1] and block[2][1]==block[2][2] and block[2][0]!=' ':
            if block[2][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][0]==block[1][0] and block[1][0]==block[2][0] and block[0][0]!=' ':
            if block[0][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][1]==block[1][1] and block[1][1]==block[2][1] and block[0][1]!=' ':
            if block[0][1]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][2]==block[1][2] and block[1][2]==block[2][2] and block[0][2]!=' ':
            if block[0][2]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][0]==block[1][1] and block[1][1]==block[2][2] and block[0][0]!=' ':
            if block[0][0]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif block[0][2]==block[1][1] and block[1][1]==block[2][0] and block[0][2]!=' ':
            if block[0][2]==tic:
                print("You Win!!")
            else:
                print("You Lose!!")
            break
        elif i%2==0:
            print("## A.I ##")
            row2=random.randint(1,3)
            while block[row2 - 1][0] != ' ' and block[row2 - 1][1] != ' ' and block[row2 - 1][2] != ' ':
                row2 = random.randint(1, 3)
            column2=random.randint(1,3)
            while block[row2-1][column2-1]!=' ':
                column2=random.randint(1,3)
            block[row2-1][column2-1]=tac
            print(block[0][0],"|",block[0][1],"|",block[0][2])
            print(block[1][0],"|",block[1][1],"|",block[1][2])
            print(block[2][0],"|",block[2][1],"|",block[2][2])
        elif i%2!=0:
            print("## Your Turn ##")
            row1=int(input("Row --> "))
            column1=int(input("Column --> "))
            block[row1-1][column1-1]=tic
            print(block[0][0],"|",block[0][1],"|",block[0][2])
            print(block[1][0],"|",block[1][1],"|",block[1][2])
            print(block[2][0],"|",block[2][1],"|",block[2][2])
        if block[0][0]!=' ' and block[0][1]!=' ' and block[0][2]!=' ' and block[1][0]!=' ' and block[1][1]!=' ' and block[1][2]!=' 'and block[2][0]!=' ' and block[2][1]!=' 'and block[2][2]!=' ':
            print("Draw!!")
            break
print("Thanks for playing!!")
chhqkbe1

chhqkbe12#

这是一个模块化版本,增加了在所有条件下正确运行的检查:

import random

def print_board(board):
    """Print the Tic-Tac-Toe board"""
    print(board[0][0], "|", board[0][1], "|", board[0][2])
    print("--+---+--")
    print(board[1][0], "|", board[1][1], "|", board[1][2])
    print("--+---+--")
    print(board[2][0], "|", board[2][1], "|", board[2][2])
    
def get_player_choice(board, player):
    """Get the row and column where the player wants to place their mark"""
    while True:
        try:
            row = int(input(f"Player {player}, enter the row number (1-3): "))
            column = int(input(f"Player {player}, enter the column number (1-3): "))
            if not (1 <= row <= 3 and 1 <= column <= 3):
                raise ValueError("Invalid row or column number")
            if board[row-1][column-1] != " ":
                raise ValueError("That cell is already occupied")
            return row-1, column-1
        except ValueError as e:
            print(e)

def has_won(board, player):
    """Check if the player has won the game"""
    for i in range(3):
        # Check rows
        if board[i][0] == board[i][1] == board[i][2] == player:
            return True
        # Check columns
        if board[0][i] == board[1][i] == board[2][i] == player:
            return True
    # Check diagonals
    if board[0][0] == board[1][1] == board[2][2] == player:
        return True
    if board[0][2] == board[1][1] == board[2][0] == player:
        return True
    return False

def play_game():
    """Play a game of Tic-Tac-Toe"""
    print("Welcome to Tic-Tac-Toe!")
    board = [[" "]*3 for _ in range(3)]
    players = ["X", "O"]
    random.shuffle(players)
    print(f"The first player is {players[0]}")
    while True:
        for player in players:
            print_board(board)
            row, column = get_player_choice(board, player)
            board[row][column] = player
            if has_won(board, player):
                print_board(board)
                print(f"Player {player} has won the game!")
                return
            if all(board[i][j] != " " for i in range(3) for j in range(3)):
                print_board(board)
                print("It's a tie!")
                return

play_game()

相关问题