2017-01-12 28 views
0

我使用Python 3創建了一個多人遊戲Noughts and Crosses。 除了check loop以外,我已經獲得了所有的代碼。PYTHON:Noughts and Crosses

如果每次輸入一個新的符號,或者如果棋盤已滿,則循環將每次輸入一個新符號。 目前,我的代碼沒有完成後,一個玩家連續輸入3個符號或板變滿。

這是我到目前爲止的代碼:

import random 

def start_player(): 

    startplayer = random.randint(1,2) 
    if startplayer == 1: 
     turn = 'X' 
     print("Player One (X) will start the game.") 
    else: 
     startplayer == 2 
     turn = 'O' 
     print("Player Two (O) will start the game.") 
    return turn 

def getmove(): 

    row = int(input("Please enter a number between 0 and 2: ")) 
    column = int(input("Please enter a number between 0 and 2: ")) 
    while grid[row][column] != "": 
     print("Invalid move.") 
     row = int(input("Please enter a number between 0 and 2: ")) 
     column = int(input("Please enter a number between 0 and 2: ")) 
    return row, column 

def mainturn(row, column): 

    global countmove 
    countmove = countmove + 1 
    global symbol 
    grid[row][column] = symbol 

    for y in range(0,(len(grid))): 
     print(grid[y]) 
    if symbol == 'X': 
     symbol = 'O' 
    elif symbol == 'O': 
     symbol = 'X' 
    return countmove 

def check_win(row, column, symbol):  

    if (grid[0][0] and grid[0][1] and grid[0][2] == symbol) or (grid[1][0] and grid[1][1] and grid[1][2] == symbol) or (grid[2][0] and grid[2][1] and grid[2][2] == symbol) or (grid[0][0] and grid[1][0] and grid[2][0] == symbol) or (grid[0][1] and grid[1][1] and grid[2][1] == symbol)or (grid[0][2] and grid[1][2] and grid[2][2] == symbol)or (grid[0][0] and grid[1][1] and grid[2][2] == symbol) or (grid[2][0] and grid[1][1] and grid[0][2] == symbol): 
     print("Well done!",symbol," won the game.") 
     return true 
    elif countmove == 9: 
     print("Board Full. Game over.") 


#main program 
grid = [["","",""],["","",""],["","",""]] 

countmove = 0 
win = 'false' 

for y in range(0,(len(grid))): 

    print(grid[y]) 

symbol = start_player() 

while countmove != 9 or win == 'false': 

    countmove = 0 
    row, column = getmove() 
    mainturn(row,column) 
    win = check_win(row,column, symbol) 
+0

我已經改變了,但有人獲勝之後,在沒有退出程序。它繼續提示用戶輸入更多的網格空間。 –

+0

我已經改變了你說的話,而我仍然得到相同的結果。 –

+0

如果我刪除它,在第一次輸入後,網格不會再次出現。 –

回答

0

這是因爲兩件事情。首先,您要重置循環中的count_move值,因此它始終爲零,並且會進入無限循環。其次,在檢查是否是勝利前,您正在更改symbolcheck_win例程正在檢查下一個符號而不是玩家的當前符號。這裏是工作代碼:

while countmove != 9 or win == 'false': 
    row, column = getmove() 
    mainturn(row,column) 
    win = check_win(row,column, symbol) 
    symbol = change_symbol(symbol) 

的子程序change_symbol是在這裏:

def change_symbol(symbol): 
    if symbol == 'X': 
     symbol = 'O' 
    elif symbol == 'O': 
     symbol = 'X' 
return symbol 

由於良好的編碼習慣的一部分,避免使用內部子程序的全局變量,它只是增加了混亂。

0

我盡我所能評論我爲什麼做了事情,爲什麼我刪除了東西。我希望這個例子可以幫助你明白爲什麼在使用python時不能使用類。我補充說,我認爲可以幫助你一些其他的功能,當你繼續學習編程

import random 

# Lets put all these functions into a class 
class Game: 

    # Lets set up the game 
    def __init__(self, player_one="X", player_two="O"): 

     self.player_one = player_one 
     self.player_two = player_two 

     # Where the game board is stored 
     # Using periods instead of spaces so the players can see the possible moves 
     self.game_board = [ 
          [".", ".", "."], 
          [".", ".", "."], 
          [".", ".", "."] 
          ] 


     # This value is to check if the game is still going 
     self.running = True 

     # Whos turn is it? 
     self.active_player = "" 

     # The tasks we HAVE to do to make the game work 
     self.start_player() 
     self.run_game() 

    # The function is part of the Game class so we have to pass self into it. 
    def start_player(self): 

     # Randomly Choose a starting player 
     startplayer = random.randint(1,2) 

     if startplayer == 1: 
      # We declared the string values in the __init__ function 
      player = self.player_one 
      print("Player One ({}) will start the game.".format(player)) 
     else: 
      startplayer == 2 
      player = self.player_two 
      print("Player Two ({}) will start the game.".format(player)) 

     # Set the initial player 
     self.active_player = player 

    def get_move(self): 
     # Seems silly to have them enter the rows and columns one by one 
     #row = int(input("Please enter a number between 0 and 2: ")) 
     #column = int(input("Please enter a number between 0 and 2: ")) 

     # Show the player whos turn it is 
     input_data = input("Player ({}) please choose a Column and a Row: ".format(self.active_player)) 

     # Default values that aren't in the game, if they arent changed they will be caught 
     row = -1 
     column = -1 

     # Users entry all types of funky data, lets make sure its right 
     try:  
      r, c = input_data.split(" ") 

      r = int(r) 
      c = int(c) 

      if r >= 0 and r <= 3: 
       row = int(r) 

      if c >= 0 and c <= 3: 
       column = int(c) 
     except: 
      print("Enter only two numbers (0, 1, or 2) seperated by a space") 

     return row, column 

     # This check for the grid should be its own function 
     #while grid[row][column] != "": 
     # print("Invalid move.") 
     # row = int(input("Please enter a number between 0 and 2: ")) 
     # column = int(input("Please enter a number between 0 and 2: ")) 

    def check_move(self, row, column): 

     if row == -1 or column == -1: 
      return False 

     # If the space is blank return True 
     if self.game_board[row][column] == ".": 
      return True 
     print("{} {} is an invalid move, try again".format(row, column)) 
     return False 

    # Add another function to print out the board for us 
    def show_board(self): 
     for row in self.game_board: 
      row_string = "" 
      for cell in row: 
       row_string = "{} {} ".format(row_string, cell) 
      print(row_string) 


    #def mainturn(row, column): 

    # Try to avoid using globals. We'll store these in our class 

    #global countmove 
    #countmove = countmove + 1 
    #global symbol 
    #grid[row][column] = symbol 

    #for y in range(0,(len(grid))): 
    # print(grid[y]) 
    #if symbol == 'X': 
    # symbol = 'O' 
    #elif symbol == 'O': 
    # symbol = 'X' 
    #return countmove 

    # This is one heck of an if statement. Lets turn it into a function 
    # if (grid[0][0] and grid[0][1] and grid[0][2] == symbol) or (grid[1][0] and grid[1][1] and grid[1][2] == symbol) or (grid[2][0] and grid[2][1] and grid[2][2] == symbol) or (grid[0][0] and grid[1][0] and grid[2][0] == symbol) or (grid[0][1] and grid[1][1] and grid[2][1] == symbol)or (grid[0][2] and grid[1][2] and grid[2][2] == symbol)or (grid[0][0] and grid[1][1] and grid[2][2] == symbol) or (grid[2][0] and grid[1][1] and grid[0][2] == symbol): 

    def check_win(self, symbol): 
     combinations = [ 
      # horizontal 
      [(0,0), (1,0), (2,0)], 
      [(0,1), (1,1), (2,1)], 
      [(0,2), (1,2), (2,2)], 
      # vertical 
      [(0,0), (0,1), (0,2)], 
      [(1,0), (1,1), (1,2)], 
      [(2,0), (2,1), (2,2)], 
      # crossed 
      [(0,0), (1,1), (2,2)], 
      [(2,0), (1,1), (0,2)] 
      ] 

     for coordinates in combinations: 
      letters = [self.game_board[x][y] for x, y in coordinates] 

      # If all the letters match 
      if "." not in letters: 
       if len(set(letters)) <= 1: 
        # returns corresponding letter for winner (X/O) 
        print("Well done {}! You won the game!".format(symbol)) 
        self.running = False 
        return True 

     return False 

    # Lets try another method of checking if the board is full 
    #elif countmove == 9: 
    # print("Board Full. Game over.") 
    #main program 
    def board_full(self): 
     for row in self.game_board: 
      if "." in row: 
       return False 
     print("The game is a draw :(") 

     # Stop the game 
     self.running = False 

     return True 

    def run_game(self): 
     # While the game is not over 
     while self.running != False: 

      # Show the player the board 
      self.show_board() 

      row, column = self.get_move() 

      # Is the move valid? 
      if self.check_move(row, column): 
       self.game_board[row][column] = self.active_player 

       # Did they win? 
       self.check_win(self.active_player) 

       # Change Players 
       if self.active_player == self.player_one: 
        self.active_player = self.player_two 
       else: 
        self.active_player = self.player_one 
     # Print the winning game board 
     self.show_board() 

g = Game("X", "O") 
    # Handled this in the class 
    #grid = [["","",""],["","",""],["","",""]] 

    #countmove = 0 

    #win = 'false' 

    # Turned this code into the show_board function 
    #for y in range(0,(len(grid))): 

    #print(grid[y]) 
    #symbol = start_player() 

    #while countmove != 9 or win == 'false': 

     # Shouldnt reset the countmove inside of the loop thats checking the countmove 
     #countmove = 0 

     #row, column = getmove() 

     #mainturn(row,column) 

     #win = check_win(row,column, symbol) 

輸出示例:

Player One (X) will start the game. 
. . . 
. . . 
. . . 
Player (X) please choose a Column and a Row: 0 1 
. X . 
. . . 
. . . 
Player (O) please choose a Column and a Row: 2 2 
. X . 
. . . 
. . O 
Player (X) please choose a Column and a Row: 0 0 
X X . 
. . . 
. . O 
Player (O) please choose a Column and a Row: 2 1 
X X . 
. . . 
. O O 
Player (X) please choose a Column and a Row: 0 2 
Well done X! You won the game! 
X X X 
. . . 
. O O