Jump to content

Need help with a code

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Valentine811

Valentine811

    Newbie

  • Members
  • Pip
  • 1 posts
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....




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.")


Edited by Valentine811, 15 April 2010 - 01:03 AM.


#2
Kruptein

Kruptein

    Newbie

  • Members
  • PipPip
  • 12 posts
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:
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

#3
pedro3005

pedro3005

    Newbie

  • Members
  • PipPip
  • 14 posts
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.