关于我写的井字游戏课程的问题

时间:2018-05-02 08:34:36

标签: python python-3.x random syntax-error tic-tac-toe

我正在python中制作一个tic-tac-toe程序。

我有两个问题:

  1. 当我们在游戏中创建对角线或线(xxx或OOO)时,如何创建终止move()的技巧。

  2. 在我的程序中发生了一些错误::第28行:( UnboundLocalError:赋值前引用的局部变量'stop')

  3. 我的代码是::

    import random
    
    board = {"top-l":" ","top-m":" ","top-r":" ","mid-l":" ","mid-m":" ","mid-r":" ","low-l":" ","low-m":" ","low-r":" "}
    
    def print_board(board):
        print( board["top-l"] + "|" + board["top-m"] + "|" + board["top-r"])
        print("--------")
        print( board["mid-l"] + "|" + board["mid-m"] + "|" + board["mid-r"])
        print("--------")
        print( board["low-l"] + "|" + board["low-m"] + "|" + board["low-r"])
        if random.randint(0,1) == 1:
            turn = "X"#user
        else:
            turn = "O"# computer
    
    
    def instructions():
        print("TYPE top FOR TOP ROW, mid FOR MIDDLE ROW AND low FOR LOWEST ROW")
        print(" ")
        print("TYPE -l FOR LEFT CORNER, -m FOR MIDDLE CORNER AND -r FOR RIGHT CORNER")
        print(" ")
        print("SO COMMAND FOR TOP RIGHT CORNER SHOULD BE top-r ")
        print("AN EMPTY BOARD LOOKS LIKE::")
        print_board(board)
    
    
    def move():
            for i in range(10):
    
                print_board(board)
                print("CHANCE NO. " + str(i))
                if turn == "O":
                    if i == 1:
                        print("COMPUTER WILL TAKE THE FIRST TURN(FOR " + turn + ")")
                    else:
                        print("IT'S COMPUTER TURN NOW")
                    y = random.randint(0,9)
                    move = str(board_list[y])
                elif turn == "x":
                    if i == 1:
                        print("USER WILL TAKE THE FIRST TURN(FOR " + turn + "). PLEASE ENTER YOUR MOVE")
                    else:
                        print("IT'S USERS TURN NOW. PLEASE ENTER YOUR MOVE")        
                    move = input()
                print("STEP TAKEN IS ::" + move)
                board["move"] = turn
                if turn == "x":
                    tu = 0
                    turn = "O"
                elif turn == "O":
                    tu = 1
                    turn = "X"
                if board["top-l"] == board["top-m"] == board["top-r"] or board["mid-l"] == board["mid-m"] == board["mid-r"] or board["low-l"] == board["low-m"] == board["low-r"] or board["mid-l"] == board["top-l"] == board["low-l"] or board["mid-m"] == board["top-m"] == board["low-m"] or board["mid-r"] == board["top-r"] == board["low-r"] or board["top-l"] == board["mid-m"] == board["low-r"] or board["top-r"] == board["mid-m"] == board["low-l"]:
                    stop = 1
                else:
                    stop = 0
    
    if __name__ ==  "__main__":
        board_list = list(board.keys())
    
        tu = int(0)# 0 for computer
                   # 1 for user
        stop = int(0)# 0 = continue
        print("PRESENTING YOU TIC-TAC-TOE GAME v1.0 BY DK SHARAMA")
        print("PLEASE ENTER YOUR NAME::")
        user = str(input())
        print("WELCOME " + user)
        instructions()
        print("TO PLAY PRESS 1 ELSE 0")
        play = int(input())
        if play == 1:
            move()
            if stop == 1:
                print("GAME OVER")
                if tu == 0:
                    print("COMPUTER WON")
                elif tu == 1:
                    print("USER WON")
            elif stop == 0:
                print("IT'S A TIE :: NO ONE WON")
    

0 个答案:

没有答案