# Create Tic Tac Toe with Cats and Dogs instead of X’s and O’s

Tic Tac Toe is a classic game that is played by millions of people around the world. The objective of the game is to align three identical elements in a row, either vertically, horizontally, or diagonally. Typically, the game is played with X’s and O’s, but why not make it more fun and interesting by using cats and dogs instead? In this tutorial, we’ll walk you through the process of creating a Tic Tac Toe game with cats and dogs using Python.

## Getting Started with Tic Tac Toe

Before we dive into the coding, let’s take a look at the rules of the game. Tic Tac Toe is a two-player game. One player uses cats, and the other player uses dogs. The game is played on a 3×3 grid. The first player to get three cats or three dogs in a row wins the game. If all nine squares are filled and there is no winner, the game is declared a draw.

### Setting Up the Game Board

To create the Tic Tac Toe game board, we will be using a two-dimensional array in Python. A two-dimensional array is simply a list of lists. Each element is accessed by specifying its row and column indices. Here’s the code to set up the game board:

``````board = [
["_", "_", "_"],
["_", "_", "_"],
["_", "_", "_"]
]
``````

In this code, we initialize the board to contain underscores in each of the nine squares. The underscores will serve as placeholders for the cats and dogs that the players will place on the board.

### Displaying the Game Board

Now that we have set up the game board, let’s create a function that will display the board on the screen. We will use ASCII art to represent the cats and dogs on the board. Here’s the code:

``````def display_board(board):
for row in board:
print("|", end="")
for square in row:
print(square + "|", end="")
print()
``````

In this code, we use a nested loop to iterate over each element in the board and print it to the screen. The end parameter ensures that each row is printed on a separate line.

### Placing Cats and Dogs on the Board

Now that we can display the board on the screen, let’s create a function that allows the players to place their cats and dogs on the board. Here’s the code:

``````def place_piece(board, row, col, piece):
if board[row][col] == "_":
board[row][col] = piece
return True
else:
return False
``````

In this code, we use a conditional statement to check if the square on the board is empty. If the square is empty, we place the cat or dog on the board. If the square is not empty, we return False to indicate that the move is invalid.

### Determining the Winner

Finally, let’s create a function that determines the winner of the game. To win the game, a player must align three of their cats or dogs in a row, either vertically, horizontally, or diagonally. Here’s the code:

``````def check_winner(board):
for i in range(3):
if board[i][0] == board[i][1] == board[i][2] != "_":
return board[i][0]
if board[0][i] == board[1][i] == board[2][i] != "_":
return board[0][i]
if board[0][0] == board[1][1] == board[2][2] != "_":
return board[0][0]
if board[0][2] == board[1][1] == board[2][0] != "_":
return board[0][2]
return None
``````

In this code, we use a series of conditional statements to check if any of the players have aligned three of their cats or dogs in a row. If a player has won, we return their piece. If there is no winner, we return None to indicate that the game is still in progress.

## Putting It All Together

Now that we have all the individual functions in place, let’s put them together to create the complete Tic Tac Toe game with cats and dogs. Here’s the code:

``````board = [
["_", "_", "_"],
["_", "_", "_"],
["_", "_", "_"]
]

def display_board(board):
for row in board:
print("|", end="")
for square in row:
print(square + "|", end="")
print()

def place_piece(board, row, col, piece):
if board[row][col] == "_":
board[row][col] = piece
return True
else:
return False

def check_winner(board):
for i in range(3):
if board[i][0] == board[i][1] == board[i][2] != "_":
return board[i][0]
if board[0][i] == board[1][i] == board[2][i] != "_":
return board[0][i]
if board[0][0] == board[1][1] == board[2][2] != "_":
return board[0][0]
if board[0][2] == board[1][1] == board[2][0] != "_":
return board[0][2]
return None

def main():
turn = "cat"
game_over = False

while not game_over:
display_board(board)
row = int(input("Enter row number: "))
col = int(input("Enter column number: "))

if turn == "cat":
if place_piece(board, row, col, "cat"):
turn = "dog"
else:
if place_piece(board, row, col, "dog"):
turn = "cat"

winner = check_winner(board)
if winner:
display_board(board)
print("The " + winner + "s have won!")
game_over = True

if "_" not in [square for row in board for square in row]:
display_board(board)
print("It's a draw!")
game_over = True

main()
``````

In this code, we initialize the game by setting the turn to “cat” and the game_over flag to False. We then display the game board, prompt the user to enter their move, and place their cat or dog on the board. After each move, we check if there is a winner or if the game has ended in a draw. If there is a winner, we display the game board and announce the winner. If the game ends in a draw, we display the game board and announce that the game has ended in a draw.

## Conclusion

In this tutorial, we walked you through the process of creating a Tic Tac Toe game with cats and dogs using Python. We used a two-dimensional array to set up the game board, ASCII art to represent the cats and dogs, and a series of conditional statements to check if a player had won or if the game had ended in a draw. By the end of the tutorial, you should have a better understanding of how to use arrays, loops, conditional statements, and functions in Python to create a simple game. Have fun playing Tic Tac Toe with your furry friends!

Scroll to Top