Tic-Tac-Toe
Game in Python:
Steps Involved:
1. Set up the board: Create a 3x3 grid
to represent the Tic-Tac-Toe board using a list of lists. Initialize it with
empty spaces to signify unmarked positions.
2. Display the board: Write a function
to display the Tic-Tac-Toe board with numbered positions, so kids can input
their moves accordingly.
3. Take user input: Create a function
that allows each player to input their move. Validate the input to ensure it is
within the valid range (1 to 9) and that the selected position is not already
taken.
4. Check for a win or draw: After each
move, check if the game has been won by either player or if it has resulted in
a draw (all positions filled, and no winner). To do this, check all possible
winning combinations of three in a row.
5. Switch players: Implement logic to
alternate between player X and player O for each turn.
6. Repeat the game: Ask the players if
they want to play again after the game ends (win or draw). If yes, reset the
board and start a new game. If not, end the program.
Codes:
|
```python def
print_board(board): print("Tic-Tac-Toe Board:") for row in board: print("|".join(row)) print("-" * 5) def
check_winner(board, player): for i in range(3): if all(board[i][j] == player for j in
range(3)): return True if all(board[j][i] == player for j 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_draw(board): return all(board[i][j] != " "
for i in range(3) for j in range(3)) def
take_input(): while True: try: move = int(input("Enter your
move (1-9): ")) if 1 <= move <= 9: return move else: print("Invalid input!
Please enter a number between 1 and 9.") except ValueError: print("Invalid input! Please
enter a number between 1 and 9.") def
play_game(): board = [[" " for _ in
range(3)] for _ in range(3)] players = ["X", "O"] player_idx = 0 while True: print_board(board) player = players[player_idx] move = take_input() row = (move - 1) // 3 col = (move - 1) % 3 if board[row][col] == " ": board[row][col] = player if check_winner(board, player): print_board(board) print(f"Player {player}
wins!") break elif is_draw(board): print_board(board) print("It's a
draw!") break player_idx = 1 - player_idx else: print("That position is
already taken. Try again.") if
__name__ == "__main__": print("Welcome to
Tic-Tac-Toe!") while True: play_game() play_again = input("Do you want
to play again? (yes/no): ") if play_again.lower() !=
"yes": break print("Thank you for playing
Tic-Tac-Toe!") ``` |
Explanation:
1. The program sets up the 3x3
Tic-Tac-Toe board as a list of lists, where each element represents a position
on the board.
2. The `print_board()` function
displays the current state of the board after each move.
3. The `check_winner()` function
checks if a player has won by examining all possible winning combinations (rows,
columns, and diagonals).
4. The `is_draw()` function checks if
the board is full and no player has won, resulting in a draw.
5. The `take_input()` function allows
each player to input their move, validates the input, and handles any errors.
6. The `play_game()` function is the
core of the program, where the game is played by alternating turns between
players, checking for a winner or draw after each move.
7. The program continues asking if the
players want to play again after the game ends.
This simple Python project offers kids
an opportunity to learn the basics of programming and have fun with a classic
game like Tic-Tac-Toe. They can experiment and make improvements to the game,
adding features like better user interfaces or smarter computer opponents. The
code's readability and simplicity make it an ideal starting point for kids to
explore the world of programming.
Contact at:
https://twitter.com/eatabook1987
https://www.facebook.com/sheraliesru/about



0 Comments