Week 2

During week 2, I want to finish setting my blog page in the CSP folder of my site so I can make blogs for my plans, hacks, and games.

  • I want to be able to make a tic tac toe game using the skills we learned on Monday.
  • I want to finish configuring my CSP blog page so i can start to create posts about my plans, ahcks, and games.

Here’s my “Tic Tac Toe” game I amde in python.

Code

def print_board(board): print(“TICTACTOE”) for row in board: print(“ | “.join(row)) print(“———”)

def check_winner(board, player): for row in board: if all(cell == player for cell in row): return True for col in range(3): if all(board[row][col] == player for row in range(3)): return True if all(board[i][i] == player for i in range(3)) or all(board[i][2 - i] == player for i in range(3)): return True return False

def is_board_full(board): return all(cell != “ “ for row in board for cell in row)

def play_game(): board = [[” “ for _ in range(3)] for _ in range(3)] current_player = “X”

while True: print_board(board) print(f”Player {current_player}’s turn”)

col = int(input("Enter column (0, 1, 2): "))
while True:  
  if col in (0, 1, 2):
    break
  else: 
    col = int(input("Enter column PROPERLY (0, 1, 2): "))
    

row = int(input("Enter row (0, 1, 2): "))
while True:  
  if row in (0, 1, 2):
    break
  else: 
    row = int(input("Enter row PROPERLY (0, 1, 2): "))
    
if board[row][col] == " ":
  board[row][col] = current_player
  if check_winner(board, current_player):
    print_board(board)
    print(f"Player {current_player} wins!")
    break
  elif is_board_full(board):
    print_board(board)
    print("It's a draw!")
    break
  else:
    current_player = "O" if current_player == "X" else "X"
else:
  print("That cell is already occupied. Try again.")

play_game()

Issues

One of the biggest issues with this game was that you could select a number that was not 0,1, or 2, and it would crash. So to avoid this, I created an loop using true/false to stop a user from putting in numbers that are not 0,1, and 2, as well as keep the game from crashing.