Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- tic tac toe
- """
- # imports
- # development helpers
- DEBUG = True
- def debug(*messages):
- """
- honestly, I would not code this... rather, I would use the logging
- library, but that is an entirely new topic. rather, I just want to
- introduce you to the idea of using a debug function... this also
- makes it easier to turn debugging on or off when building the
- program
- ....last, this block of triple-quoted text, when immediately placed
- after the define line (i.e., the triple quotes are the *first* bit
- of text at the start of the function) is a python doc-string... these
- are a convention used for documentation... if you do "help(DEBUG)"
- from the console (after loading the program), it will print this
- block of text. cool!
- """
- if DEBUG:
- print("DEBUG:", *messages)
- # game functions
- def play_game():
- """
- main game loop
- """
- debug("play_game: start")
- winner = None
- board = make_new_board()
- while not winner:
- show_board(board)
- debug("TODO: build play_game")
- winner = check_board(board) # TODO: delete this line
- game_over(winner)
- debug("play_game: done")
- def make_new_board():
- debug("make_new_board called")
- return [
- [" ", " ", " "], # row 1
- [" ", " ", " "], # row 2
- [" ", " ", " "], # row 3
- ]
- def show_board(board):
- debug("show_board: start")
- board_template = """
- A B C
- -------------
- 1 | {a1} | {b1} | {c1} |
- -------------
- 2 | {a2} | {b2} | {c2} |
- -------------
- 3 | {a3} | {b3} | {c3} |
- -------------
- """
- print(
- board_template.format(
- a1=board[0][0],
- b1=board[0][1],
- c1=board[0][2],
- a2=board[1][0],
- b2=board[1][1],
- c2=board[1][2],
- a3=board[2][0],
- b3=board[2][1],
- c3=board[2][2],
- )
- )
- debug("show_board: done")
- def check_board(board):
- debug("check_board: start")
- winner = "TODO: change this to None"
- debug("TODO: build check_board")
- debug("check_board: done")
- return winner
- def game_over(winner):
- debug("game_over: start")
- debug("TODO: build game_over")
- debug("game_over: done")
- def instructions():
- debug("instructions: start")
- debug("TODO: build instructions")
- debug("instructions: done")
- # run
- if __name__=="__main__":
- play_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement