944,198 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 4904
  • Python RSS
Jun 26th, 2005
0

my pride and joy

Expand Post »
I just finished my first program, other then a few test ones. it is tic-tac-toe. If you want to try it. here is the link
http://webpages.charter.net/lindbergfamily/game.py
It is not very advanced, just mainly a bunch of if then and while statments.
If any of you care to comment on my boring code, feel free, I would appreciate any suggestions.

python Syntax (Toggle Plain Text)
  1. #!/usr/bin/env python
  2. # this is the game tic-tac-toe
  3. # it was fully programmed by shane lindberg
  4.  
  5. # instructions.py
  6. #
  7. # this function gives instuctions to
  8. # the user for playing the game
  9.  
  10. def instructions():
  11.  
  12.  
  13. print """\n\tWelcome to the game of tic-tac-toe\n
  14. \t --a game of man against machine--\n
  15. you first need to choose to go first or second. If you
  16. choose to go first you will be X's. If you choose to
  17. go second you will be O's.\n
  18. you will choose your 'move' on the board by using the
  19. following key.
  20.  
  21. DO NOT FORGET THE ORDER OF THE NUMBERS IN THE KEY BELOW"""
  22.  
  23. instruc_list = [ '1','2','3','4','5','6','7','8','9' ]
  24.  
  25. print_board(instruc_list)
  26. print ' '
  27.  
  28. # print_board.py
  29. #
  30. # this function prints the board, it takes
  31. # its parameter in the form of a list, the
  32. # list must have at least 9 elments
  33.  
  34. def print_board(order):
  35. print " "
  36. print " "
  37. print " ",order[0], "|", order[1], "|", order[2]
  38. print " -----------"
  39. print " ",order[3], "|", order[4], "|", order[5]
  40. print " -----------"
  41. print " ",order[6], "|", order[7], "|", order[8]
  42.  
  43. # x_or_o.py
  44. #
  45. # this function asks the user if he or
  46. # she wants to go first, if they choose
  47. # to go first, they will be X's, if they
  48. # choose to go second, they will be O's
  49.  
  50. def x_or_o():
  51.  
  52. print "\nWould you like to go first or second?\n"
  53.  
  54. answer = ""
  55. choices = ('first', 'second')
  56. while answer not in choices:
  57. answer = raw_input("please enter 'first' or 'second'(enter here)")
  58. if answer == "first":
  59. print "\nyou chose to go first, so that will make you X's"
  60. if answer == "second":
  61. print "\nyou chose to go second, you must feel brave to give"
  62. print "the computer the advantage. You will be O's"
  63. return answer
  64.  
  65. # answer.py
  66. #
  67. # this function asks for a move and checks to see
  68. # if it is a legal place to choose.
  69.  
  70. def answer():
  71. guess = raw_input("please enter your move(enter here)")
  72. while guess not in ('1','2','3','4','5','6','7','8','9'):
  73. guess = raw_input("please enter a number chosen from 1-9(enter here)")
  74. guess = int(guess)
  75. while True:
  76. if position[guess -1] in ('X', 'O'):
  77. guess = raw_input("that space is already occupied, please make another move(enter here)")
  78. guess = int(guess)
  79. else:
  80. break
  81. return guess -1
  82.  
  83. # test comp_answer
  84.  
  85. def comp_answer():
  86.  
  87. raw_input("enter return to let the computer take its turn")
  88.  
  89. BEST_MOVES = ( 4,0,2,6,8,1,3,7,5 )
  90. # the most favorable move
  91. #
  92. # the following for statment determines if the
  93. # computer has a move that will win the game,
  94. # if so it returns that value as its move
  95.  
  96. for i in WINNER:
  97. winns = []
  98. if i[0] in comp_moves:
  99. winns.append(i[0])
  100. if i[1] in comp_moves:
  101. winns.append(i[1])
  102. if i[2] in comp_moves:
  103. winns.append(i[2])
  104. if len(winns) == 2:
  105. for k in i:
  106. if k not in winns and k not in moves and k not in comp_moves:
  107. return k
  108.  
  109. # the second most favorable move
  110. #
  111. # the following for statment determines if the
  112. # user has two in a row and needs to be blocked
  113. # if so the computer returns that value as its move
  114.  
  115. for i in WINNER:
  116. winns = []
  117. if i[0] in moves:
  118. winns.append(i[0])
  119. if i[1] in moves:
  120. winns.append(i[1])
  121. if i[2] in moves:
  122. winns.append(i[2])
  123. if len(winns) == 2:
  124. for k in i:
  125. if k not in winns and k not in moves and k not in comp_moves:
  126. return k
  127.  
  128. # the final part of the stratagie is to choose moves
  129. # in order of importance from the local tuple called
  130. # best moves. This tuple starts with the middle position
  131. # then the corners, then the 4 final sides
  132.  
  133. for i in BEST_MOVES:
  134. if i not in moves and i not in comp_moves:
  135. return i
  136.  
  137.  
  138. # update_moves.py
  139. #
  140. # this function takes user input from the function answer.py
  141. # and appends the list called position. This provides stdin
  142. # for the print_board and the winner function
  143.  
  144. def update_moves(answer):
  145. if who_first == 'first':
  146. position[answer] = 'X'
  147. else:
  148. position[answer] = 'O'
  149. moves.append(answer)
  150.  
  151. # comp_update_moves.py
  152. #
  153. # this function updates the computers
  154. # moves. it gets stdin from comp_answer
  155.  
  156. def comp_update_moves(comp_answer):
  157. if who_first == 'first':
  158. position[comp_answer] = 'O'
  159. else:
  160. position[comp_answer] = 'X'
  161. comp_moves.append(comp_answer)
  162.  
  163. # winner.py
  164. # this function checks to see if anyone has one the game
  165. # it takes its data from update_moves
  166.  
  167. def winner(moves,comp_moves):
  168.  
  169. won = ""
  170.  
  171. for i in WINNER:
  172. if i[0] in moves and i[1] in moves and i[2] in moves:
  173. won = 0
  174.  
  175. for i in WINNER:
  176. if i[0] in comp_moves and i[1] in comp_moves and i[2] in comp_moves:
  177. won = 0
  178. return won
  179.  
  180. # congrat_winner.py
  181. #
  182. # this function allows you to decide who won
  183. # so you are able to congrat the winner, or
  184. # make fun at the loser
  185.  
  186. def congrat(moves):
  187. WINNER = ( (0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7),
  188. (2,5,8), (0,4,8), (2,4,6) )
  189.  
  190. won = 1
  191. for i in WINNER:
  192. if i[0] in moves and i[1] in moves and i[2] in moves:
  193. won = 0
  194. return won
  195.  
  196. # here is where the actual program starts to run
  197.  
  198.  
  199. WINNER = ( (0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7),
  200. (2,5,8), (0,4,8), (2,4,6) )
  201. position = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
  202. moves = []
  203. comp_moves = []
  204. who_first = ""
  205. who_won = 1
  206.  
  207.  
  208. instructions()
  209. raw_input('press return to continue')
  210.  
  211. while winner(moves,comp_moves) != 0:
  212.  
  213. # if the game is a tie this break statement is used
  214. if len(moves) + len(comp_moves) == 9:
  215. who_won = 0
  216. break
  217.  
  218. # these first two 'if' statements only run
  219. # once during this while loop. their just used
  220. # to start either 'X's or 'O's. They also take
  221. # the first answer from the user or the computer
  222.  
  223. if who_first == "":
  224. who_first = x_or_o()
  225. if who_first == 'first':
  226. print_board(position)
  227. update_moves(answer())
  228. next_turn = 0
  229. else:
  230. print_board(position)
  231. comp_update_moves(comp_answer())
  232. next_turn = 1
  233.  
  234. # the following if-else statment alternate the computer and
  235. # users turns, while also collecting data and updating data
  236.  
  237. print_board(position)
  238. if next_turn == 0:
  239. comp_update_moves(comp_answer())
  240. next_turn = 1
  241. else:
  242. update_moves(answer())
  243. next_turn = 0
  244.  
  245. print_board(position)
  246.  
  247. if who_won == 0:
  248. print "\nit was a tie, maybe you will win next time"
  249. elif congrat(moves) == 0:
  250. print "\nyou beat the computer, man is still triumphant"
  251. else:
  252. print "\nthe computer beat you, you let mankind down :-("
Last edited by vegaseat; Oct 19th, 2008 at 10:54 am. Reason: changed nonfunctional php tags to newer code tag style
Similar Threads
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Jun 26th, 2005
0

Re: my pride and joy

Looks like you put a lot of thought and effort into this game. Something is lousing up the indentations in the php-codefield, did you mix spaces and tabs?

I would highly recommend to stick with spaces only, since tab settings can differ from one computer to the other. Looks like the php-codefield replaces all tabs with spaces, but not along your tab setting!

The code on your web site is fine and works great as long as I use IDLE and don't "detabbify"!

Looks like your tabs were set to 8 spaces. Used the IDLE option and got rid of the tabs. Now the php-codefield looks correct ...
python Syntax (Toggle Plain Text)
  1. #!/usr/bin/env python
  2. # this is the game tic-tac-toe
  3. # it was fully programmed by shane lindberg
  4.  
  5. # instructions.py
  6. #
  7. # this function gives instuctions to
  8. # the user for playing the game
  9.  
  10. def instructions():
  11.  
  12.  
  13. print """\n\tWelcome to the game of tic-tac-toe\n
  14. \t --a game of man against machine--\n
  15. you first need to choose to go first or second. If you
  16. choose to go first you will be X's. If you choose to
  17. go second you will be O's.\n
  18. you will choose your 'move' on the board by using the
  19. following key.
  20.  
  21. DO NOT FORGET THE ORDER OF THE NUMBERS IN THE KEY BELOW"""
  22.  
  23. instruc_list = [ '1','2','3','4','5','6','7','8','9' ]
  24.  
  25. print_board(instruc_list)
  26. print ' '
  27.  
  28. # print_board.py
  29. #
  30. # this function prints the board, it takes
  31. # its parameter in the form of a list, the
  32. # list must have at least 9 elments
  33.  
  34. def print_board(order):
  35. print " "
  36. print " "
  37. print " ",order[0], "|", order[1], "|", order[2]
  38. print " -----------"
  39. print " ",order[3], "|", order[4], "|", order[5]
  40. print " -----------"
  41. print " ",order[6], "|", order[7], "|", order[8]
  42.  
  43. # x_or_o.py
  44. #
  45. # this function asks the user if he or
  46. # she wants to go first, if they choose
  47. # to go first, they will be X's, if they
  48. # choose to go second, they will be O's
  49.  
  50. def x_or_o():
  51.  
  52. print "\nWould you like to go first or second?\n"
  53.  
  54. answer = ""
  55. choices = ('first', 'second')
  56. while answer not in choices:
  57. answer = raw_input("please enter 'first' or 'second'(enter here)")
  58. if answer == "first":
  59. print "\nyou chose to go first, so that will make you X's"
  60. if answer == "second":
  61. print "\nyou chose to go second, you must feel brave to give"
  62. print "the computer the advantage. You will be O's"
  63. return answer
  64.  
  65. # answer.py
  66. #
  67. # this function asks for a move and checks to see
  68. # if it is a legal place to choose.
  69.  
  70. def answer():
  71. guess = raw_input("please enter your move(enter here)")
  72. while guess not in ('1','2','3','4','5','6','7','8','9'):
  73. guess = raw_input("please enter a number chosen from 1-9(enter here)")
  74. guess = int(guess)
  75. while True:
  76. if position[guess -1] in ('X', 'O'):
  77. guess = raw_input("that space is already occupied, please make another move(enter here)")
  78. guess = int(guess)
  79. else:
  80. break
  81. return guess -1
  82.  
  83. # test comp_answer
  84.  
  85. def comp_answer():
  86.  
  87. raw_input("enter return to let the computer take its turn")
  88.  
  89. BEST_MOVES = ( 4,0,2,6,8,1,3,7,5 )
  90. # the most favorable move
  91. #
  92. # the following for statment determines if the
  93. # computer has a move that will win the game,
  94. # if so it returns that value as its move
  95.  
  96. for i in WINNER:
  97. winns = []
  98. if i[0] in comp_moves:
  99. winns.append(i[0])
  100. if i[1] in comp_moves:
  101. winns.append(i[1])
  102. if i[2] in comp_moves:
  103. winns.append(i[2])
  104. if len(winns) == 2:
  105. for k in i:
  106. if k not in winns and k not in moves and k not in comp_moves:
  107. return k
  108.  
  109. # the second most favorable move
  110. #
  111. # the following for statment determines if the
  112. # user has two in a row and needs to be blocked
  113. # if so the computer returns that value as its move
  114.  
  115. for i in WINNER:
  116. winns = []
  117. if i[0] in moves:
  118. winns.append(i[0])
  119. if i[1] in moves:
  120. winns.append(i[1])
  121. if i[2] in moves:
  122. winns.append(i[2])
  123. if len(winns) == 2:
  124. for k in i:
  125. if k not in winns and k not in moves and k not in comp_moves:
  126. return k
  127.  
  128. # the final part of the stratagie is to choose moves
  129. # in order of importance from the local tuple called
  130. # best moves. This tuple starts with the middle position
  131. # then the corners, then the 4 final sides
  132.  
  133. for i in BEST_MOVES:
  134. if i not in moves and i not in comp_moves:
  135. return i
  136.  
  137.  
  138. # update_moves.py
  139. #
  140. # this function takes user input from the function answer.py
  141. # and appends the list called position. This provides stdin
  142. # for the print_board and the winner function
  143.  
  144. def update_moves(answer):
  145. if who_first == 'first':
  146. position[answer] = 'X'
  147. else:
  148. position[answer] = 'O'
  149. moves.append(answer)
  150.  
  151. # comp_update_moves.py
  152. #
  153. # this function updates the computers
  154. # moves. it gets stdin from comp_answer
  155.  
  156. def comp_update_moves(comp_answer):
  157. if who_first == 'first':
  158. position[comp_answer] = 'O'
  159. else:
  160. position[comp_answer] = 'X'
  161. comp_moves.append(comp_answer)
  162.  
  163. # winner.py
  164. # this function checks to see if anyone has one the game
  165. # it takes its data from update_moves
  166.  
  167. def winner(moves,comp_moves):
  168.  
  169. won = ""
  170.  
  171. for i in WINNER:
  172. if i[0] in moves and i[1] in moves and i[2] in moves:
  173. won = 0
  174.  
  175. for i in WINNER:
  176. if i[0] in comp_moves and i[1] in comp_moves and i[2] in comp_moves:
  177. won = 0
  178. return won
  179.  
  180. # congrat_winner.py
  181. #
  182. # this function allows you to decide who won
  183. # so you are able to congrat the winner, or
  184. # make fun at the loser
  185.  
  186. def congrat(moves):
  187. WINNER = ( (0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7),
  188. (2,5,8), (0,4,8), (2,4,6) )
  189.  
  190. won = 1
  191. for i in WINNER:
  192. if i[0] in moves and i[1] in moves and i[2] in moves:
  193. won = 0
  194. return won
  195.  
  196. # here is where the actual program starts to run
  197.  
  198.  
  199. WINNER = ( (0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7),
  200. (2,5,8), (0,4,8), (2,4,6) )
  201. position = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
  202. moves = []
  203. comp_moves = []
  204. who_first = ""
  205. who_won = 1
  206.  
  207.  
  208. instructions()
  209. raw_input('press return to continue')
  210.  
  211. while winner(moves,comp_moves) != 0:
  212.  
  213. # if the game is a tie this break statement is used
  214. if len(moves) + len(comp_moves) == 9:
  215. who_won = 0
  216. break
  217.  
  218. # these first two 'if' statements only run
  219. # once during this while loop. their just used
  220. # to start either 'X's or 'O's. They also take
  221. # the first answer from the user or the computer
  222.  
  223. if who_first == "":
  224. who_first = x_or_o()
  225. if who_first == 'first':
  226. print_board(position)
  227. update_moves(answer())
  228. next_turn = 0
  229. else:
  230. print_board(position)
  231. comp_update_moves(comp_answer())
  232. next_turn = 1
  233.  
  234. # the following if-else statment alternate the computer and
  235. # users turns, while also collecting data and updating data
  236.  
  237. print_board(position)
  238. if next_turn == 0:
  239. comp_update_moves(comp_answer())
  240. next_turn = 1
  241. else:
  242. update_moves(answer())
  243. next_turn = 0
  244.  
  245. print_board(position)
  246.  
  247. if who_won == 0:
  248. print "\nit was a tie, maybe you will win next time"
  249. elif congrat(moves) == 0:
  250. print "\nyou beat the computer, man is still triumphant"
  251. else:
  252. print "\nthe computer beat you, you let mankind down :-("
Last edited by vegaseat; Oct 19th, 2008 at 10:56 am. Reason: changed from php code tags
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 26th, 2005
0

Re: my pride and joy

I will take your advice and just use spaces from now on. Thanks for your advice :-)
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Jun 27th, 2005
0

Re: my pride and joy

One suggestion I would make to improve the comfort for the player, is to show the number pattern before every move the player makes. Easy to do, just repeat the line you have already used in function instructions() in function answer(), and make instruc_list global (declare it outside the function).

python Syntax (Toggle Plain Text)
  1. #!/usr/bin/env python
  2. # this is the game tic-tac-toe
  3. # it was fully programmed by shane lindberg
  4.  
  5. # made list global so it could be used in answer()
  6. instruc_list = [ '1','2','3','4','5','6','7','8','9' ]
  7.  
  8. # instructions.py
  9. #
  10. # this function gives instuctions to
  11. # the user for playing the game
  12.  
  13. def instructions():
  14.  
  15.  
  16. print """\n\tWelcome to the game of tic-tac-toe\n
  17. \t --a game of man against machine--\n
  18. you first need to choose to go first or second. If you
  19. choose to go first you will be X's. If you choose to
  20. go second you will be O's.\n
  21. you will choose your 'move' on the board by using the
  22. following numeric key."""
  23.  
  24. #instruc_list = [ '1','2','3','4','5','6','7','8','9' ]
  25.  
  26. print_board(instruc_list)
  27. print ' '
  28.  
  29. # print_board.py
  30. #
  31. # this function prints the board, it takes
  32. # its parameter in the form of a list, the
  33. # list must have at least 9 elments
  34.  
  35. def print_board(order):
  36. #print " "
  37. print " "
  38. print " ",order[0], "|", order[1], "|", order[2]
  39. print " -----------"
  40. print " ",order[3], "|", order[4], "|", order[5]
  41. print " -----------"
  42. print " ",order[6], "|", order[7], "|", order[8]
  43.  
  44. # x_or_o.py
  45. #
  46. # this function asks the user if he or
  47. # she wants to go first, if they choose
  48. # to go first, they will be X's, if they
  49. # choose to go second, they will be O's
  50.  
  51. def x_or_o():
  52.  
  53. print "\nWould you like to go first or second?\n"
  54.  
  55. answer = ""
  56. choices = ('first', 'second')
  57. while answer not in choices:
  58. answer = raw_input("please enter 'first' or 'second'(enter here)")
  59. if answer == "first":
  60. print "\nyou chose to go first, so that will make you X's"
  61. if answer == "second":
  62. print "\nyou chose to go second, you must feel brave to give"
  63. print "the computer the advantage. You will be O's"
  64. return answer
  65.  
  66. # answer.py
  67. #
  68. # this function asks for a move and checks to see
  69. # if it is a legal place to choose.
  70.  
  71. def answer():
  72. print_board(instruc_list) # added this line to aid player
  73. guess = raw_input("please enter your move(enter here)")
  74. while guess not in ('1','2','3','4','5','6','7','8','9'):
  75. guess = raw_input("please enter a number chosen from 1-9(enter here)")
  76. guess = int(guess)
  77. while True:
  78. if position[guess -1] in ('X', 'O'):
  79. guess = raw_input("that space is already occupied, please make another move(enter here)")
  80. guess = int(guess)
  81. else:
  82. break
  83. return guess -1
  84.  
  85. # test comp_answer
  86.  
  87. def comp_answer():
  88.  
  89. raw_input("press enter to let the computer take its turn")
  90.  
  91. BEST_MOVES = ( 4,0,2,6,8,1,3,7,5 )
  92. # the most favorable move
  93. #
  94. # the following for statment determines if the
  95. # computer has a move that will win the game,
  96. # if so it returns that value as its move
  97.  
  98. for i in WINNER:
  99. winns = []
  100. if i[0] in comp_moves:
  101. winns.append(i[0])
  102. if i[1] in comp_moves:
  103. winns.append(i[1])
  104. if i[2] in comp_moves:
  105. winns.append(i[2])
  106. if len(winns) == 2:
  107. for k in i:
  108. if k not in winns and k not in moves and k not in comp_moves:
  109. return k
  110.  
  111. # the second most favorable move
  112. #
  113. # the following for statment determines if the
  114. # user has two in a row and needs to be blocked
  115. # if so the computer returns that value as its move
  116.  
  117. for i in WINNER:
  118. winns = []
  119. if i[0] in moves:
  120. winns.append(i[0])
  121. if i[1] in moves:
  122. winns.append(i[1])
  123. if i[2] in moves:
  124. winns.append(i[2])
  125. if len(winns) == 2:
  126. for k in i:
  127. if k not in winns and k not in moves and k not in comp_moves:
  128. return k
  129.  
  130. # the final part of the stratagie is to choose moves
  131. # in order of importance from the local tuple called
  132. # best moves. This tuple starts with the middle position
  133. # then the corners, then the 4 final sides
  134.  
  135. for i in BEST_MOVES:
  136. if i not in moves and i not in comp_moves:
  137. return i
  138.  
  139.  
  140. # update_moves.py
  141. #
  142. # this function takes user input from the function answer.py
  143. # and appends the list called position. This provides stdin
  144. # for the print_board and the winner function
  145.  
  146. def update_moves(answer):
  147. if who_first == 'first':
  148. position[answer] = 'X'
  149. else:
  150. position[answer] = 'O'
  151. moves.append(answer)
  152.  
  153. # comp_update_moves.py
  154. #
  155. # this function updates the computers
  156. # moves. it gets stdin from comp_answer
  157.  
  158. def comp_update_moves(comp_answer):
  159. if who_first == 'first':
  160. position[comp_answer] = 'O'
  161. else:
  162. position[comp_answer] = 'X'
  163. comp_moves.append(comp_answer)
  164.  
  165. # winner.py
  166. # this function checks to see if anyone has one the game
  167. # it takes its data from update_moves
  168.  
  169. def winner(moves,comp_moves):
  170.  
  171. won = ""
  172.  
  173. for i in WINNER:
  174. if i[0] in moves and i[1] in moves and i[2] in moves:
  175. won = 0
  176.  
  177. for i in WINNER:
  178. if i[0] in comp_moves and i[1] in comp_moves and i[2] in comp_moves:
  179. won = 0
  180. return won
  181.  
  182. # congrat_winner.py
  183. #
  184. # this function allows you to decide who won
  185. # so you are able to congrat the winner, or
  186. # make fun at the loser
  187.  
  188. def congrat(moves):
  189. WINNER = ( (0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7),
  190. (2,5,8), (0,4,8), (2,4,6) )
  191.  
  192. won = 1
  193. for i in WINNER:
  194. if i[0] in moves and i[1] in moves and i[2] in moves:
  195. won = 0
  196. return won
  197.  
  198. # here is where the actual program starts to run
  199.  
  200.  
  201. WINNER = ( (0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7),
  202. (2,5,8), (0,4,8), (2,4,6) )
  203. position = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
  204. moves = []
  205. comp_moves = []
  206. who_first = ""
  207. who_won = 1
  208.  
  209.  
  210. instructions()
  211. raw_input('press return to continue')
  212.  
  213. while winner(moves,comp_moves) != 0:
  214.  
  215. # if the game is a tie this break statement is used
  216. if len(moves) + len(comp_moves) == 9:
  217. who_won = 0
  218. break
  219.  
  220. # these first two 'if' statements only run
  221. # once during this while loop. their just used
  222. # to start either 'X's or 'O's. They also take
  223. # the first answer from the user or the computer
  224.  
  225. if who_first == "":
  226. who_first = x_or_o()
  227. if who_first == 'first':
  228. print_board(position)
  229. update_moves(answer())
  230. next_turn = 0
  231. else:
  232. print_board(position)
  233. comp_update_moves(comp_answer())
  234. next_turn = 1
  235.  
  236. # the following if-else statment alternate the computer and
  237. # users turns, while also collecting data and updating data
  238.  
  239. print_board(position)
  240. if next_turn == 0:
  241. comp_update_moves(comp_answer())
  242. next_turn = 1
  243. else:
  244. update_moves(answer())
  245. next_turn = 0
  246.  
  247. print_board(position)
  248.  
  249. if who_won == 0:
  250. print "\nit was a tie, maybe you will win next time"
  251. elif congrat(moves) == 0:
  252. print "\nyou beat the computer, man is still triumphant"
  253. else:
  254. print "\nthe computer beat you, you let mankind down :-("
Darn computer beats me most of the time!
Last edited by vegaseat; Oct 19th, 2008 at 10:58 am. Reason: php tags don't work any longer on DaniWeb, changed them
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 27th, 2005
0

Re: my pride and joy

I was thinking of doing something like that. For better yet usablilty, a gui, that allows a mouse pointer to choose would be better. Would something like pygame or TKinter be the way to go. Or do those need to be used together. I am currently using this book to learn python

http://www.amazon.com/exec/obidos/tg...books&n=507846

further into it, it gets into gui game making. I think the next chapter is in introduction to OOP.
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Jun 27th, 2005
0

Re: my pride and joy

Tkinter and PyGame are not the same. PyGame uses the Simple DirectMedia Layer (SDL) library, a little fancier for graphics, animation and sound than Tkinter. Tkinter is its own GUI library based on TCL, not quit so fancy, but also much simpler to use.

With Tkinter you can quickly create a pad of nine buttons using grid to position them, then go from there. You can assign a command to each button to put in a X or O when clicked, applying the logic you already worked out.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 28th, 2005
0

Re: my pride and joy

I have been using gedit(linux), as my editor. I found an option that fill tabs with spaces. So even if I use tabs and spaces together, the editer translates it all to spaces.
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Jun 29th, 2005
0

Re: my pride and joy

Quote originally posted by shanenin ...
I have been using gedit(linux), as my editor. I found an option that fill tabs with spaces. So even if I use tabs and spaces together, the editer translates it all to spaces.
Should work as long your tabs are set to the correct amount of spaces!

On the Windows platform I am still using a mix of editors/IDEs like ConTEXT, DrPython, IDLE, SPE and PythonWin. None of these are perfect, each has its strengths. DrPython allows for fast experimentation with code, but has no context sensitive help.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: internet form handling
Next Thread in Python Forum Timeline: ftplib and the retrbinary() command





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC