Tic Tac Toe Python
Tic-tac-toe is a simple paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game. It's also known as Noughts and Crosses or Xs and Os.
Tic-tac-toe is a classic game that has been enjoyed by people of all ages for many years. It is a simple game with a small set of rules and can be easily implemented in many programming languages. In this article, we will be discussing how to create a Tic-tac-toe game using Python.
Tic-tac-toe is a simple game that can be implemented in Python using a 2D list to represent the game board and basic control flow statements to determine the winner or if the game is a draw. A simple version of the game could be implemented using functions to handle player moves, check for a winner, and display the game board. More advanced versions could include additional features such as AI players or a graphical user interface.
Tic-tac-toe, also known as noughts and crosses or Xs and Os, is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3x3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner. The game ends in a draw if all of the spaces on the grid are filled, and neither player has won. It's a very simple yet addictive game that can be played on paper or using a computer or mobile application.
To start, we will first create a 3x3 grid using a two-dimensional list. We will initialize the grid with empty spaces, represented by the character ' '.
grid = [[' ',' ',' '],
[' ',' ',' '],
[' ',' ',' ']]
Next, we will create a function to display the grid on the screen. This function will simply iterate through the grid and print each element in the correct position.
def display_grid(grid):
for row in grid:
print(row)
We can then call this function to display the grid at any point in the game.
Now, we need to create a function for handling player moves. This function will take the player's input, which will be the coordinates of the grid where they want to place their mark. The function will then check if the space is empty, and if it is, it will place the player's mark in the space. If the space is not empty, the function will ask the player to choose another space.
def player_move(grid, player):
while True:
x, y = input("Player " + player + ", enter coordinates (x y): ").split()
x, y = int(x), int(y)
if grid[x][y] == ' ':
grid[x][y] = player
break
else:
print("Space is already occupied, try again.")
In this function, we are using the input function to get the player's coordinates, and then splitting the input string into two variables, x and y. We are also using the int function to convert the string input into integers. The function then checks if the space is empty and if it is, it places the player's mark in the space. If the space is not empty, the function will ask the player to choose another space.
Now we will check for the winning conditions for the game, for that, we will create a function 'check_win' which will check the rows, columns and diagonals of the grid to see if any player has won.
def check_win(grid):
# check rows
for i in range(3):
if grid[i][0] == grid[i][1] == grid[i][2] and grid[i][0] != ' ':
return grid[i][0]
To Check Columns and diagonals:
# check columns
for i in range(3):
if grid[0][i] == grid[1][i] == grid[2][i] and grid[0][i] != ' ':
return grid[0][i]
# check diagonals
if grid[0][0] == grid[1][1] == grid[2][2] and grid[0][0] != ' ':
return grid[0][0]
if grid[0][2] == grid[1][1] == grid[2][0] and grid[0][2] != ' ':
return grid[0][2]
return None
Here is an example of a simple Tic-Tac-Toe game implemented in Python:
Sample code:
board = [[' ',' ',' '],
[' ',' ',' '],
[' ',' ',' ']]
def print_board(board):
for row in board:
print(row)
def check_win(board):
# check rows
for row in board:
if row[0] == row[1] == row[2]:
return row[0]
# check columns
for col in range(3):
if board[0][col] == board[1][col] == board[2][col]:
return board[0][col]
# check diagonals
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
player = 'X'
while True:
print_board(board)
print(f"Player {player}, choose a row (0-2):")
row = int(input())
print(f"Player {player}, choose a column (0-2):")
col = int(input())
if board[row][col] == ' ':
board[row][col] = player
else:
print("That spot is already taken.")
continue
winner = check_win(board)
if winner:
print_board(board)
print(f"Player {winner} wins!")
break
player = 'O' if player == 'X' else 'X'
Output: