Problems writing code for my project

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 4
Reputation: xaeubanks is an unknown quantity at this point 
Solved Threads: 0
xaeubanks xaeubanks is offline Offline
Newbie Poster

Problems writing code for my project

 
0
  #1
Oct 19th, 2009
I'm writing code in python for a tic tac toe game. I have ran into a problem. can any one help me out. thanks

  1. # TIC TAC TOE
  2. # Started: 10/19/09
  3. # Ended: still in progress
  4.  
  5. #GLOBAL CONSTANTS
  6. X = "X"
  7. O = "O"
  8. Empty = " "
  9. TIE = "TIE"
  10. NUM_SQUARES = 9
  11.  
  12. def display_instruct():
  13. """Display game instructions."""
  14. print \
  15. """
  16. Welcome to the intellectual challenge of all time: Tic-Tac-Toe.
  17. You will make your move know by entering a number, 0 - 8. The number
  18. will correspond to the board position as illustrated:
  19.  
  20. 0 | 1 | 2
  21. _________
  22.  
  23. 3 | 4 | 5
  24. _________
  25.  
  26. 6 | 7 | 8
  27.  
  28. Prepare yourself. The ultimate battle is about to begin. \n
  29. """
  30.  
  31. def ask_yes_no(question):
  32. """Ask a yes or no question."""
  33. response = None
  34. while response not in ("y", "n"):
  35. response = raw_input(question).lower()
  36. return response
  37.  
  38. def ask_number(question, low, high):
  39. """Ask for a number within a range."""
  40. response = None
  41. while response not in range(low, high):
  42. response = int(raw_input(question))
  43. return response
  44.  
  45. def pieces():
  46. """Determine if Player 1 or Computer goes first."""
  47. go_first = ask_yes_no("Do you require the first move? (y/n):")
  48. if go_first == "y":
  49. print "\nThen take the first move."
  50. player1 = X
  51. computer = O
  52. else:
  53. print "\nComputer will go first."
  54. player2 = X
  55. computer = O
  56. return computer, player1
  57.  
  58. def new_board():
  59. """Create new game board."""
  60. board = []
  61. for square in range(NUM_SQUARES):
  62. board.append(EMPTY)
  63. return board
  64.  
  65. def display_board(board):
  66. """Display game board on screen."""
  67. print "\n\t", board[0], "|", board[1], "|", board[2]
  68. print "\t", "------"
  69. print "\t", board[3], "|", board[4], "|", board[5]
  70. print "\t", "------"
  71. print "\t", board[6], "|", board[7], "|", board[8], "\n"
  72.  
  73. def legal_moves(board):
  74. """Create list of legal moves."""
  75. moves =[]
  76. for squares in range(NUM_SQUARES):
  77. if board[square] == EMPTY:
  78.  
  79. moves.append(square)
  80. return moves
  81.  
  82. def winner(board):
  83. """Determine the game winner."""
  84. WAYS_TO_WIN = ((0, 1, 2),
  85. (3, 4, 5),
  86. (6, 7, 8),
  87. (0, 3, 6),
  88. (1, 4, 7),
  89. (2, 5, 8),
  90. (0, 4, 8),
  91. (2, 4, 6))
  92.  
  93. for row in WAYS_TO_WIN:
  94. if board[row[0]] == board[row[1]] == board[row[2]] != EMPTY:
  95. winner = board[row[0]]
  96. return winner
  97.  
  98. if EMPTY not in board:
  99. return TIE
  100.  
  101. return None
  102.  
  103. def player1_move(board, player1):
  104. """Get Player 1 move."""
  105. legal = legal_moves(board)
  106. move = None
  107. while move not in legal:
  108. move = ask_number("Where will you move? (0 - 8): ", 0, NUM_SQUARES)
  109. if move not in legal:
  110. print "\nThat square is already occupied. Choose another.\n"
  111. print "Fine..."
  112. return move
  113.  
  114. def computer_move(board, computer, player1):
  115. """ Make computer move."""
  116. # make a copy to work with since function will be changing list
  117. board = board[:]
  118.  
  119. # the best positions to have, in order
  120. BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7)
  121. print "I shall take square number",
  122.  
  123.  
  124. # if computer can win, block that move
  125. for move in legal_moves(board):
  126. board[move] = computer
  127. if winner(board) == computer:
  128. print move
  129. return move
  130. # done checking this move, undo it
  131. board[move] = EMPTY
  132.  
  133. # if player1 can win, block that move
  134. for move in legal_moves(board):
  135. board[move] = player1
  136. if winner(board) == player1:
  137. print move
  138. return move
  139. # done checking this move, undo it
  140. board[move] = EMPTY
  141.  
  142. # since no one can win on next move, pick open square
  143. for move in BEST_MOVES:
  144. if move in legal_moves(board):
  145. print move
  146. return move
  147. #done checking this move, undo it
  148. board[move] = EMPTY
  149.  
  150. def next_turn(turn):
  151. """Switch turns."""
  152. if turn == X:
  153. return O
  154. else:
  155. return X
  156.  
  157. def congrat_winner(The_winner, computer, player1):
  158. """Congratulate the winner."""
  159. if the_winner != TIE:
  160. print the_winner, "won\n"
  161. else:
  162. print "It's a tie!\n"
  163. if the_winner == computer:
  164. print " Computer is great.\n"
  165. elif the_winner == player1:
  166. print "Player 1 is great.\n"
  167. elif the_winner == TIE:
  168. print "You two try again.\n"
  169.  
  170. def main():
  171. display_instruct()
  172. computer, player1 = pieses()
  173. turn = X
  174. board = new_board()
  175. display_board(board)
  176.  
  177. while not winner(board):
  178. if turn == player1:
  179. move = player1_move(board, player1)
  180. board[move] = player1
  181.  
  182. else:
  183. turn == computer
  184. move = computer_move(board, player2)
  185. board[move] = computer
  186.  
  187. display_board(board)
  188. turn = next_turn(turn)
  189. the_winner = winner(board)
  190. congrat_winner(the_winner , computer, player1)
  191.  
  192. # start the program
  193. main()
  194. raw_input("\n\nPress the enter key to quit.")

Editor's note:
If you have an error please state the error message.

Also, use code tags with your code to preserve the indentations. Otherwise the code is very difficult to read and not too many folks will help.

[code=python]
your Python code here
[/code]
Last edited by vegaseat; Oct 20th, 2009 at 10:36 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 183
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 53
snippsat's Avatar
snippsat snippsat is offline Offline
Junior Poster
 
0
  #2
Oct 19th, 2009
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: xaeubanks is an unknown quantity at this point 
Solved Threads: 0
xaeubanks xaeubanks is offline Offline
Newbie Poster
 
-1
  #3
Oct 19th, 2009
for move in legal_moves(board):
board[move] = computer
if winner(board) == computer:
print move
return move # done checking this move, undo it
board[move] = EMPTY
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 243
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 60
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #4
Oct 20th, 2009
soooooo... what's your question ?
and Yes pls use code tags next time.... so much easier to read!
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,151
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 952
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #5
Oct 20th, 2009
Just some obvious stuff:

Lines 124 to 140 have lost their indentations as a code block. They belong to function computer_move().

pieces is misspelled in line 172

You also need to do something about your globals (check case).

squares should be square in line 76

You will also discover some runtime problems.

Good luck with your project!
Last edited by vegaseat; Oct 20th, 2009 at 11:23 am.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: xaeubanks is an unknown quantity at this point 
Solved Threads: 0
xaeubanks xaeubanks is offline Offline
Newbie Poster
 
0
  #6
Oct 20th, 2009
There's an error in your program:
***'return' outside function (line 129)

This code is straight out of the book. I was trying this before I changed it for a two player game. The book is "Python Programming, for the absolute beginner second edition"
Last edited by xaeubanks; Oct 20th, 2009 at 4:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 183
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 53
snippsat's Avatar
snippsat snippsat is offline Offline
Junior Poster
 
0
  #7
Oct 20th, 2009
There's an error in your program:
***'return' outside function (line 129)
You can not use return outside a function.

  1. IDLE 2.6.2
  2. >>> def a(x, y):
  3. return x + y
  4.  
  5. >>> a(5, 8)
  6. 13
  7. >>> x = 5
  8. >>> y = 8
  9. >>> return x + y #this you can not do because outside a function
  10. SyntaxError: 'return' outside function
  11. >>>
If you gone make change a code like this you have to understad the basic of python.
Make small changes and test it out.
Return outside a function will always give a error.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso
 
0
  #8
Oct 20th, 2009
Did you download the code or retype it manually yourself?
There are some errors due to indentations and typos.

The return outside function error is really indentation error!
Last edited by bumsfeld; Oct 20th, 2009 at 7:08 pm.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: xaeubanks is an unknown quantity at this point 
Solved Threads: 0
xaeubanks xaeubanks is offline Offline
Newbie Poster
 
0
  #9
Oct 20th, 2009
I retyped it out of the book. making sure that this one would work before I tried to make a two player game. I have already fixed my typos, but still get return outside function line 129
Last edited by xaeubanks; Oct 20th, 2009 at 8:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso
 
0
  #10
Oct 20th, 2009
Read vegaseat's comment:
Lines 124 to 140 have lost their indentations as a code block. They belong to function computer_move().
You need to move all those lines to the right by 4 spaces.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 317 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC