Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. tic tac toe
  4. """
  5.  
  6. # imports
  7.  
  8. # development helpers
  9.  
  10. DEBUG = True
  11.  
  12. def debug(*messages):
  13. """
  14. honestly, I would not code this... rather, I would use the logging
  15. library, but that is an entirely new topic. rather, I just want to
  16. introduce you to the idea of using a debug function... this also
  17. makes it easier to turn debugging on or off when building the
  18. program
  19.  
  20. ....last, this block of triple-quoted text, when immediately placed
  21. after the define line (i.e., the triple quotes are the *first* bit
  22. of text at the start of the function) is a python doc-string... these
  23. are a convention used for documentation... if you do "help(DEBUG)"
  24. from the console (after loading the program), it will print this
  25. block of text. cool!
  26. """
  27. if DEBUG:
  28. print("DEBUG:", *messages)
  29.  
  30. # game functions
  31.  
  32. def play_game():
  33. """
  34. main game loop
  35. """
  36. debug("play_game: start")
  37. winner = None
  38. board = make_new_board()
  39. while not winner:
  40. show_board(board)
  41. debug("TODO: build play_game")
  42. winner = check_board(board) # TODO: delete this line
  43. game_over(winner)
  44. debug("play_game: done")
  45.  
  46. def make_new_board():
  47. debug("make_new_board called")
  48. return [
  49. [" ", " ", " "], # row 1
  50. [" ", " ", " "], # row 2
  51. [" ", " ", " "], # row 3
  52. ]
  53.  
  54. def show_board(board):
  55. debug("show_board: start")
  56. board_template = """
  57. A B C
  58. -------------
  59. 1 | {a1} | {b1} | {c1} |
  60. -------------
  61. 2 | {a2} | {b2} | {c2} |
  62. -------------
  63. 3 | {a3} | {b3} | {c3} |
  64. -------------
  65. """
  66. print(
  67. board_template.format(
  68. a1=board[0][0],
  69. b1=board[0][1],
  70. c1=board[0][2],
  71. a2=board[1][0],
  72. b2=board[1][1],
  73. c2=board[1][2],
  74. a3=board[2][0],
  75. b3=board[2][1],
  76. c3=board[2][2],
  77. )
  78. )
  79. debug("show_board: done")
  80.  
  81. def check_board(board):
  82. debug("check_board: start")
  83. winner = "TODO: change this to None"
  84. debug("TODO: build check_board")
  85. debug("check_board: done")
  86. return winner
  87.  
  88. def game_over(winner):
  89. debug("game_over: start")
  90. debug("TODO: build game_over")
  91. debug("game_over: done")
  92.  
  93. def instructions():
  94. debug("instructions: start")
  95. debug("TODO: build instructions")
  96. debug("instructions: done")
  97.  
  98. # run
  99.  
  100. if __name__=="__main__":
  101. play_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement