Closed Thread
Results 1 to 3 of 3

Thread: Need help with a code

  1. #1
    Valentine811 is offline Newbie
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Need help with a code

    Hi...I'm new at writing python and I'm trying to write a a tic tac toe game....something is wrong here but I cant' figure out what that something is. The bord gets printed out and it asks u if u wanna start, but that's about it. Please can someone help me figure out what the problem is? I've been trying for a loooong time now....



    Code:
    import random
    
    
    
    def asking(question):                            
        ("Would you like to go first? (y/n): ")
        response = None
        while response not in ("y", "n"):                                      
        response = raw_input("Would you like to go first? (y/n): ").lower()
        return response
    
    def number(question, low, high):               
               ("Enter a number between 0-8: ")
               answer = None
               while answer not in right(low, high):
       answer = int(raw_input(question))
                return answer                                             
       
    def pieces():
        global player, computer
        first = asking("Would you like to go first? (y/n): ")
        if first == "y":
            number = input('\nEnter a number: ')
            player = X
            computer = O
        else:
            print "\nThen I will go first. "
            computer = X
            player = O  
       return computer, player
    #game = raw_input("Choose yor position: ")
    #print_game
    def possible_moves(board):                                       
        ["0","1","2","3","4","5","6","7","8"]
        moves = []
        for square in range(NUM_SQUARES):
            if board[square] == EMPTY:            
             moves.append(square)                               
        return moves                                         
    
    def winner(board):
       Winning_Row = ((0,1,2),(3,4,5),(6,7,8),               (0,3,6),(1,4,7),(2,5,8),   
                    (0,4,8),(2,4,6))          
       for w in Winning_Row:
          if board[w[0]] == board[w[1]] == board[w[2]] != EMPTY:            winner = board[w[0]]
             return winner            
          if EMPTY not in board:  
             return TIE   
          return None
    
    def player_move(board, player):
       possible = possible_moves(board)
       move = None
       while move not in possible:                           
               move = raw_input("Make your move, 0-8: ")
       return move
    
    def computer_move(board, computer, player):
       board = board[:]
       Best_moves = (4, 2, 8, 6, 0, 7, 1, 3)                           
       for move in possible_moves():
          board[move] = computer
          if winner(board) == computer:
             print move
             return move
          board[move] = EMPTY
    
    def next_turn(turn):
       if turn == X:
          return O
       else:
          return X
          
    
    
    board = ["_","_","_","_","_","_","_","_","_"]                        
    print_board("".join(board))
    pieces()
    #winner = is_game_over()                                       
    
    def winner_now(winner, computer, player):
       if winner == player:
          print("You win!")
       elif winner == computer:
             print("Computer win!")
       elif winner == TIE:   
          print("Remi")
    
    def main():                                                   
          display_instructions()
          computer, player = pieces()
          turn = X                                             
          board = ["_","_","_","_","_","_","_","_","_"]
          print_board("".join(board))
          pieces()
          while not winner(board):
                if turn == player:
                   move = player_move(board, player)
                   board[move] = player
                else:
                   move == computer_moves(board, computer, player)
                   board[move] = computer
                print_board(board)
                turn = next_turn(turn)
                winner = winner(board)
                winner_now(winner, computer, player)
          main()                        
          raw_input("\n\nPress enter to quit.")
    Last edited by Valentine811; 04-15-2010 at 02:03 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Kruptein is offline Newbie
    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Re: Need help with a code

    It might be very disappointing, but your code really is a mess
    1. intendations are all wrong,
    2. functions are full with unsupported things, I will everytiime add a # with info after a wrong statement:
    Code:
    import random
    
    EMPTY = "_"                     
    X = "X"
    O = "O"
    TIE = "TIE"
    NUM_SQUARES = 9                     
    player = X
    computer = O
    
    def display_instructions():
        print \
       
    def print_board(board):                                                        
        board = board.replace("_"," ")
        print "." + "---." * 3
        for bound in [0,3,6]:
            print "|",
            for sym in board[bound:bound+3]:
                print sym, "|",
            if bound < 6:
                print "\n|" + "---|" * 3
        print "\n'" + "---'" * 3
    
    def asking(question):                            
        ("Would you like to go first? (y/n): ")  # wth is this?
        response = None
        while response not in ("y", "n"):                                      
            response = raw_input("Would you like to go first? (y/n): ").lower() # should be: respons=raw_input(question)
        return response
    
    def number(question, low, high):               
               ("Enter a number between 0-8: ")  # aah! all wrong, should go away
               answer = None
               while answer not in right(low, high):
                   answer = int(raw_input(question))
               return answer                                             
       
    def pieces():
        global player, computer
        first = asking("Would you like to go first? (y/n): ")
        if first == "y":
            number = input('\nEnter a number: ')
            player = X
            computer = O
        else:
            print "\nThen I will go first. "
            computer = X
            player = O  
       return computer, player
    #game = raw_input("Choose your position: ")
    #print_game
    def possible_moves(board):                                       
        ["0","1","2","3","4","5","6","7","8"]  # :f what is this supposed todo? I think you meant: moves = ["0","1","..."]
        moves = [] # change it with the above solution
        for square in range(NUM_SQUARES):
            if board[square] == EMPTY:            
                moves.append(square)                               
        return moves                                         
    
    def winner(board):
       Winning_Row = ((0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6))          
       for w in Winning_Row:
          if board[w[0]] == board[w[1]] == board[w[2]] != EMPTY:
              winner = board[w[0]]
          return winner            
          if EMPTY not in board:  
             return TIE   
          return None
    
    def player_move(board, player):
       possible = possible_moves(board)
       move = None
       while move not in possible:                           
               move = raw_input("Make your move, 0-8: ")
       return move
    
    def computer_move(board, computer, player):
       board = board[:]
       Best_moves = (4, 2, 8, 6, 0, 7, 1, 3)                           
       for move in possible_moves():
          board[move] = computer
          if winner(board) == computer:
             print move
             return move
          board[move] = EMPTY
    
    def next_turn(turn):
       if turn == X:
          return O
       else:
          return X
          
    
    
    board = ["_","_","_","_","_","_","_","_","_"]                        
    print_board("".join(board))
    pieces()
    #winner = is_game_over()                                       
    
    def winner_now(winner, computer, player):
       if winner == player:
          print("You win!")
       elif winner == computer:
             print("Computer win!")
       elif winner == TIE:   
          print("Remi")
    
    def main():                                                   
          display_instructions()
          computer, player = pieces()
          turn = X                                             
          board = ["_","_","_","_","_","_","_","_","_"]
          print_board("".join(board))
          pieces()
          while not winner(board):
                if turn == player:
                   move = player_move(board, player)
                   board[move] = player
                else:
                   move == computer_moves(board, computer, player)
                   board[move] = computer
                print_board(board)
                turn = next_turn(turn)
                winner = winner(board)
                winner_now(winner, computer, player)
          main()                        
          raw_input("\n\nPress enter to quit.")
    I only looked at terrible code mistakes, but I think their are also a lot of logical mistakes...
    You should really re-study python

  4. #3
    pedro3005 is offline Newbie
    Join Date
    Mar 2010
    Location
    Rio de Janeiro, Brazil
    Posts
    14
    Rep Power
    0

    Re: Need help with a code

    O.O
    This is really crazy!
    I don't think you really need all those functions, but then again, you've got more to worry about than that.
    As it has been said before, study python more. I recommend the python docs tutorial.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Problem in a href location from php code to html code
    By newphpcoder in forum PHP Development
    Replies: 5
    Last Post: 05-13-2011, 02:03 PM
  2. how to make a bar code reader program in VB 2008 please send me the code
    By tontonskie in forum Visual Basic Programming
    Replies: 1
    Last Post: 11-15-2010, 12:58 AM
  3. add code for font size under php echo code
    By newphpcoder in forum PHP Development
    Replies: 2
    Last Post: 11-10-2010, 11:03 PM
  4. Code: Capture Code from USB Camera
    By MrNobody in forum Visual Basic Tutorials
    Replies: 71
    Last Post: 08-14-2009, 08:59 AM
  5. Code: Capture Code from USB Camera
    By MrNobody in forum Tutorials
    Replies: 5
    Last Post: 09-08-2007, 06:00 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts