Questions about a tic-tac-toe program I am writing

有些话、适合烂在心里 提交于 2019-12-23 03:17:51

问题


I am making a tic-tac-toe program in python.

I have two questions:

  1. How to create a trick to terminate the move() when we have created a diagonal or a line(xxx or OOO) in the game.

  2. In my program some error is occurring::in line 28::(UnboundLocalError: local variable 'stop' referenced before assignment)

My code is::

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")

来源:https://stackoverflow.com/questions/50130343/questions-about-a-tic-tac-toe-program-i-am-writing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!