Skip to content

Instantly share code, notes, and snippets.

@ssssssssk
Created February 20, 2021 01:10
Show Gist options
  • Select an option

  • Save ssssssssk/7a4def209321c63ec61a164b12514b81 to your computer and use it in GitHub Desktop.

Select an option

Save ssssssssk/7a4def209321c63ec61a164b12514b81 to your computer and use it in GitHub Desktop.

Revisions

  1. ssssssssk created this gist Feb 20, 2021.
    73 changes: 73 additions & 0 deletions tictac.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    import os

    # Create a dictionary to store moves in.
    spots = {'a1' : " ", 'b1' : " ", 'c1' : " ", 'a2' : " ", 'b2' : " ", 'c2' : " ", 'a3' : " ", 'b3' : " ", 'c3' : " "}

    # list of moves that have already been made.
    illegal_moves = []

    # Don't let the game go on for ever.
    turns = 10
    message = "Let's play Tic Tac Toe."

    while turns != 0:
    # Draw board
    row_1 = ' {0} | {1} | {2}'.format(spots.get('a1'), spots.get('b1'), spots.get('c1'))
    row_2 = ' {0} | {1} | {2}'.format(spots.get('a2'), spots.get('b2'), spots.get('c2'))
    row_3 = ' {0} | {1} | {2}'.format(spots.get('a3'), spots.get('b3'), spots.get('c3'))

    os.system('clear')
    print(message)
    print(" ")
    print(" a b c ")
    print(" ")
    print("1 ", row_1, end="\n")
    print(" ---|---|---", end="\n")
    print("2 ", row_2, end="\n")
    print(" ---|---|---", end="\n")
    print("3 ", row_3, end="\n")
    print(" ")

    # Select player, starts with X.
    if turns % 2 == 0:
    player = "X"
    else:
    player = "O"

    if turns != 1:

    choice = input(f"Enter the column and row of your move, {player}: ")
    print(" ")

    # make sure the move is possible (ie in the spots dictionary)
    while choice not in spots:
    choice = input(f"Enter a valid move, {player}: ")
    else:
    pass

    # Make sure another player hasn't already made that move.
    while choice in illegal_moves:
    choice = input(f"Please make a choice that hasn't already been made, {player}: ")
    else:
    pass

    # write to the board if the move is ok
    spots[choice] = player
    # make it such that the move can't be made again.
    illegal_moves.append(choice)

    # Check if at the end of this round a player has already won
    # Surely, there's a better way of doing this??
    if spots['a1'] == spots['a2'] == spots['a3'] == player or spots['b1'] == spots['b2'] == spots['b3'] == player or spots['c1'] == spots['c2'] == spots['c3'] == player or spots['a1'] == spots['b1'] == spots['c1'] == player or spots['a2'] == spots['b2'] == spots['c2'] == player or spots['a3'] == spots['b3'] == spots['c3'] == player or spots['a1'] == spots['b2'] == spots['c3'] == player or spots['a3'] == spots['b2'] == spots['c1'] == player:

    message = (f"{player} wins!")
    turns = 2

    turns = turns - 1

    print(" ")
    print("Game Over!")

    # Things to clean up: printing could be done with one single line, would be cool to write the dictionary so that it used input to produce the grid for extra large ttt action.

    # Other things to try: Curses? Colors?