python tic tac toe问题

时间:2017-08-07 20:26:34

标签: python if-statement while-loop tic-tac-toe

我是编码新手,最近开始学习python。我的第一个挑战是建立一个tic tac toe游戏。下面是我的游戏代码,一切正常,但是当我想重新开始游戏或结束游戏时,循环不会中断或游戏无法启动。任何人都可以知道我的代码有什么问题。谢谢!

Matrix = [[' ' for i in range(3)]for j in range(3)]
for i in Matrix:
    print(i)  #this is the 3x3 board

def check_done(value): #this is to check if the player has won the game
    for a in range(0,3):
       if Matrix[0][a]==Matrix[1][a]==Matrix[2][a]==value\
       or Matrix[a][0]==Matrix[a][1]==Matrix[a][2]==value:
            print ('won')
            return True
        #when the vertical column or horizontal row is equal, the     
        player won the game

       if Matrix[0][0]==Matrix[1][1]==Matrix[2][2]==value\
       or Matrix[2][0]==Matrix[1][1]==Matrix[0][2]==value:
          print('won')
          return True
       #when the diagonal rows are equal, the player won the game

       if ' ' not in Matrix[0] and ' ' not in Matrix[1] and ' ' not in     
       Matrix[2]:
            print('draw')
            return True
        #when every grid is filled in the board and no win criteria is  
         fulfilled, it is a draw
    return False

 def replay():  #this is to determine if the player wants to restart or   
                 end the game
     command =input('Enter r to restart, or e to end game: ')
     while True: 
     if command == 'r':
        player1_input()

     if command == 'e':
            return
            break

     else:
       print('Invalid command.')


def player1_input(): #this is an input function to allow players to         
                      position their next move
    print('Player 1 insert your name here')
    name1=input()
    print('player 2 insert your name here')
    name2=input()
    while True:
        inputValid = False
        while inputValid == False:
        print(name1,'please place x coordinates')
        xinput=int(input())
        print(name1,'please place y coordinates')
        yinput=int(input())
        if yinput >= 0 & yinput <= 2 & xinput >=0 & xinput <= 2: 
            if Matrix[yinput][xinput] == ' ':
                Matrix[yinput][xinput] = 'X'
                for i in Matrix:
                    print(i)
                if check_done('X'):
                    print(name1,'won')
                    replay()
                inputValid = True

    inputValid = False
    while inputValid == False:
        print(name2,'please place x coordinates')
        xinput=int(input())
        print(name2,'please place y coordinates')
        yinput=int(input())
        if yinput >= 0 & yinput <= 2 & xinput >=0 & xinput <= 2:
            if Matrix[yinput][xinput] == ' ':
                Matrix[yinput][xinput] = 'O'
                for i in Matrix:
                    print(i)
                if check_done('O'):
                    print(name2,'won')
                    replay()
                inputValid = True

 return True

2 个答案:

答案 0 :(得分:1)

主要游戏玩法的while循环无限运行 要解决此问题,您可以使用continue_game标志 一个简单的例子:

def player1_input():
    # rest of the code

    continue_game = True
    while continue_game:
        # logic for the game

        continue_game = replay()

def replay():
    # Returns true or false on user input.

    while True:
        command = raw_input('...')
        if command == 'r':
            return True

        elif command == 'e':
            return False

        else:
            print ('Invalid command')

方法replay可以根据用户输入返回布尔值作为approriate。

此外,请注意,如果游戏应该重新启动,则必须使用空值重新初始化矩阵。

答案 1 :(得分:0)

在发布时难以阅读代码。但在我看来,你的真实是一个无限循环。我没有看到任何允许你退出的休息时间