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.
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:
I only looked at terrible code mistakes, but I think their are also a lot of logical mistakes...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.")
You should really re-study python
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks