2017-08-07 6 views
-1

私はコーディングが新しく、最近pythonを学び始めました。私の最初の課題は、チックタックのつま先のゲームを構築することです。以下はゲームのコードですが、ゲームをやり直すか、ゲームを終了したいとき以外はうまくいっていますが、ループが壊れたり、ゲームが始まらない場合があります。ありがとうございました!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

字下げを修正して、見ているものを正確に表現できるようにしてください。現在のところ、これは間違いなく実行できません。また、これを小さくしてより小さなコードベースにすることはできますか?問題自体を削除せずに切り捨てることができるかなりのコードがあると思います。 – roganjosh

+0

あなたの2番目の 'while'ループは無限です... – Mangohero1

+0

ゲームの再起動を求められたら 'r'を入力すると入力を再度要求していますか? –

答えて

0

投稿されたコードを読むのは難しいです。しかし、それはあなたの真は無限のサイクルだと私には思われます。私はあなたが中断することを可能にする休憩を見ません

+1

あなたは正しいと思います。なぜこれがどういうものなのかを詳しく説明する必要があります。つまり、 'while'ループを入力すると' command'を更新することはできません。 – roganjosh

+0

whileループの途中で休憩していますが、まったく動作していないようですが、リプレイ機能も壊れてしまいました。 – Maomaozii

1

whileメインゲームのループは無限に実行されています。
これを解決するには、continue_gameフラグを使用できます。
簡単な例:ユーザー入力に基づいてapproriateとしてブール値を返すことができreplay

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

方法。

さらに、ゲームを再起動する必要がある場合は、空の値で行列を再初期化する必要があることに注意してください。

+0

ありがとう、これは問題だと思います。私はあなたが与えた提案に基づいてそれに取り組み、再びあなたに知らせます。 – Maomaozii

+0

私は行列を再初期化するいくつかの問題を抱えていますが、ボードをクリアする方法についていくつかのポインタを共有してもよろしいですか?ありがとうございました! – Maomaozii

+0

初期化したときに再初期化することができます。 'Matrix'はグローバルなので、メソッド' player1_input' - 'global Matrix'に行を追加し、'再生 '呼び出しの後に' Matrix = [['' 'を範囲(3)範囲内(3)] ' –