如何打印井字游戏板?

时间:2019-01-28 18:04:47

标签: python python-3.x tic-tac-toe

我正在尝试为井字游戏打印板。当我尝试运行它时,什么也没有发生,并且说语法无效。无效的语法表明它与我的printboard函数有关。

我看不到代码有什么问题。

如何使它印制电路板?

#Tic-Tac-Toe Game

import os
import time
import random


board = [" " for x in range(10)]

def printTitle():
    print"""

----------------     1 | 2 | 3
TIC - TAC - TOE      4 | 5 | 6
________________     7 | 8 | 9

TO PLAY TIC - TAC - TOE, YOU NEED TO GET THREE IN A ROW.
YOUR CHOICES ARE BETWEEN 1 TO 9.

"""

def printBoard():
    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 ("   |   |   ")

while True:
    os.system("clear")
    printTitle()
    printBoard()
    choice = input("Please choose an empty space for X. ").upper()
    choice = int(choice)
    if board[choice] == " ":
        board[choice] = "X"
    else:
        print "Sorry, that space is not empty!"
        time.sleep(1)

结果应为:

   |   |
   |   |
   |   |
-------------
   |   |
   |   |
   |   |
-------------
   |   |
   |   |
   |   |

错误消息(来自@Prune):

  File "so.py", line 20
    """
      ^
SyntaxError: invalid syntax

2 个答案:

答案 0 :(得分:1)

def printTitle():
print"""

----------------     1 | 2 | 3
TIC - TAC - TOE      4 | 5 | 6
________________     7 | 8 | 9

TO PLAY TIC - TAC - TOE, YOU NEED TO GET THREE IN A ROW.
YOUR CHOICES ARE BETWEEN 1 TO 9.

"""

尝试一下:

str = '''
        ----------------     1 | 2 | 3
        TIC - TAC - TOE      4 | 5 | 6
        ________________     7 | 8 | 9

        TO PLAY TIC - TAC - TOE, YOU NEED TO GET THREE IN A ROW.
        YOUR CHOICES ARE BETWEEN 1 TO 9.

        '''
 def printTitle(str):
    print(str)

此功能下您的打印语句有问题

答案 1 :(得分:0)

该错误是由于您在printTitle()方法中的打印语句中的注释引号引起的,而不是由于最后一个print语句中的字符串未放在括号中。您需要对print语句进行更改:

  1. 在方法printTitle()中,在每行末尾添加删除注释标记并添加方括号和反斜杠('\'),用于表示多行字符串。
  2. 在while循环中,在最后一个打印语句中添加括号。

更正后的代码在这里供您参考。

import os
import time
import random


board = [" " for x in range(10)]

def printTitle():
    print("\
\
----------------     1 | 2 | 3\
TIC - TAC - TOE      4 | 5 | 6\
________________     7 | 8 | 9\
\
TO PLAY TIC - TAC - TOE, YOU NEED TO GET THREE IN A ROW.\
YOUR CHOICES ARE BETWEEN 1 TO 9.")


def printBoard():
    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 ("   |   |   ")

while True:
    os.system("clear")
    printTitle()
    printBoard()
    choice = input("Please choose an empty space for X. ").upper()
    choice = int(choice)
    if board[choice] == " ":
        board[choice] = "X"
    else:
        print("Sorry, that space is not empty!")
        time.sleep(1)