2017-01-12 3 views
0

Ich habe ein Multiplayer-Spiel von Noughts and Crosses mit Python 3 erstellt. Ich habe alle meinen Code zu arbeiten, mit Ausnahme der Check-Schleife.PYTHON: Nullen und Kreuze

Die Prüfschleife wird jedes Mal neu eingegeben, wenn es sich um einen Gewinn handelt oder wenn die Karte voll ist. Momentan endet mein Code nicht, nachdem ein Spieler 3 Symbole hintereinander eingegeben hat oder die Karte voll wird.

Dies ist mein Code so weit:

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

ich das geändert haben, aber es verlassen das Programm nicht nach jemand gewinnt. Es fordert den Benutzer weiterhin auf, mehr Rasterfelder einzugeben. –

+0

Ich habe den while-Zähler zu dem geändert, was Sie gesagt haben, und ich bekomme immer noch das gleiche Ergebnis. –

+0

Wenn ich das entferne, kommt das Gitter nach der ersten Eingabe nicht wieder auf. –

Antwort

0

es wegen der zwei Dinge. Zuerst setzen Sie count_move Wert in der Schleife zurück, folglich ist es immer Null und geht in eine Endlosschleife. Zweitens ändern Sie die symbol vor der Überprüfung, ob es ein Gewinn ist, überprüft check_win Routine für das nächste Symbol nicht das aktuelle Symbol des Players. Hier ist der Arbeitscode:

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

Die Unterroutine change_symbol ist hier:

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

Im Rahmen bewährten Verfahrens der Codierung vermeiden globale Variablen innerhalb von Subroutinen verwenden, es wird nur die Verwirrung addiert.

0

Ich habe mein Bestes gegeben, um zu kommentieren, warum ich Dinge gemacht habe und warum ich Dinge entfernt habe. Ich hoffe, dieses Beispiel hilft Ihnen zu verstehen, warum es nicht besser ist, Klassen zu verwenden, wenn Sie mit Python arbeiten. Ich habe einige andere Funktionen, die ich denke, kann Ihnen helfen, wie Sie

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) 

Ausgabe Programmierung weiter lernen:

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 
Verwandte Themen