<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Thevinu Senaratne</title>
    <description>The latest articles on DEV Community by Thevinu Senaratne (@thevinudan).</description>
    <link>https://dev.to/thevinudan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1498906%2F2ba9b35e-5a67-432f-b8c4-dc57b8644848.png</url>
      <title>DEV Community: Thevinu Senaratne</title>
      <link>https://dev.to/thevinudan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thevinudan"/>
    <language>en</language>
    <item>
      <title>Building a Tic-Tac-Toe Game in Python: A Step-by-Step Guide</title>
      <dc:creator>Thevinu Senaratne</dc:creator>
      <pubDate>Wed, 15 May 2024 16:02:19 +0000</pubDate>
      <link>https://dev.to/thevinudan/building-a-tic-tac-toe-game-in-python-a-step-by-step-guide-54ko</link>
      <guid>https://dev.to/thevinudan/building-a-tic-tac-toe-game-in-python-a-step-by-step-guide-54ko</guid>
      <description>&lt;p&gt;Tic Tac Toe, the classic game we've all played at some point, As a student still learning about python I have used all my beginner knowledge of the language to create this game. In this tutorial, we'll create a simple Tic Tac Toe game that runs in the terminal using Python. This project is beginner-friendly and will help you understand basic game logic and user input handling. Let's dive in!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Up the Game Board&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We'll start by initializing the game board, which in our case will be a list representing the Tic Tac Toe grid. Each cell of the grid will be represented by a number: 0 for empty, 1 for 'X', and 2 for 'O'. Here's how we initialize the board:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;board = [&lt;br&gt;
  0, 0, 0,&lt;br&gt;
  0, 0, 0,&lt;br&gt;
  0, 0, 0&lt;br&gt;
]&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Displaying the Board&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To make the game more user-friendly, we'll define a function to print the current state of the board. This function iterates through the board list and replaces the numeric values with 'X', 'O', or '-' for empty cells. Here's the &lt;code&gt;print_board()&lt;/code&gt; function:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def print_board():&lt;br&gt;
  display_position = ['-', '-', '-', '-', '-', '-', '-', '-', '-']&lt;br&gt;
  for i in range(9):&lt;br&gt;
    if board[i] == 1:&lt;br&gt;
      display_position[i] = 'X'&lt;br&gt;
    elif board[i] == 2:&lt;br&gt;
      display_position[i] = 'O'&lt;/code&gt;&lt;br&gt;
  &lt;code&gt;print('''&lt;/code&gt;&lt;br&gt;
&lt;code&gt;______________&lt;br&gt;
|  {} |  {} |  {} |&lt;br&gt;
|____|____|____|&lt;br&gt;
|  {} |  {} |  {} |&lt;br&gt;
|____|____|____|  &lt;br&gt;
|  {} |  {} |  {} |&lt;br&gt;
|____|____|____|&lt;/code&gt; &lt;code&gt;&lt;br&gt;
'''.format(*display_position))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This function will print out the current state of the board in a visually appealing manner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Checking for a Winner&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next, we need to implement a function to check if a player has won the game. We'll define a &lt;code&gt;check_win()&lt;/code&gt; function that examines the board state for any winning combinations. If a winning combination is found, the function returns &lt;code&gt;True&lt;/code&gt;; otherwise, it returns &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def check_win(num):&lt;br&gt;
    if board[0] == num and board[1] == num and board[2] == num:&lt;br&gt;
      return True&lt;br&gt;
    elif board[3] == num and board[4] == num and board[5] == num:&lt;br&gt;
      return True&lt;br&gt;
    # Other winning combinations...&lt;br&gt;
    else:&lt;br&gt;
      return False&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Main Game Loop&lt;br&gt;
Finally, we'll implement the main game loop. This loop controls the flow of the game, alternating between player turns until a winner is determined or the game ends in a draw. Players input their moves via the terminal.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;turn = 0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;while turn &amp;lt; 9:&lt;br&gt;
    # Player 1's turn.&lt;br&gt;
    while True:&lt;/code&gt;&lt;br&gt;
        &lt;code&gt;try:&lt;br&gt;
            p1_position = int(input("Player 1 enter position number (1 through 9): ")) - 1&lt;br&gt;
            if 0 &amp;lt;= p1_position &amp;lt;= 8 and board[p1_position] == 0:&lt;br&gt;
                board[p1_position] = 1&lt;br&gt;
                break&lt;br&gt;
            elif p1_position &amp;lt; 0 or p1_position &amp;gt; 8:&lt;br&gt;
                print('Invalid position number. Please enter a number between 1 and 9.')&lt;br&gt;
            else:&lt;br&gt;
                print('Position already taken. Please choose another position.')&lt;br&gt;
        except ValueError:&lt;br&gt;
            print('Invalid input. Please enter a number.')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# Increment turn counter and print the board.&lt;br&gt;
    turn += 1&lt;br&gt;
    print_board()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# Check if player 1 wins or if it's a draw.&lt;br&gt;
    if turn == 9:&lt;br&gt;
        print('Draw')&lt;br&gt;
        break&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;player_1_stat = check_win(1)&lt;br&gt;
    if player_1_stat:&lt;br&gt;
        print('Player 1 WINS')&lt;br&gt;
        break&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;code&gt;# Player 2's turn.&lt;br&gt;
    while True:&lt;br&gt;
        try:&lt;br&gt;
            p2_position = int(input("Player 2 enter position number (1 through 9): ")) - 1&lt;br&gt;
            if 0 &amp;lt;= p2_position &amp;lt;= 8 and board[p2_position] == 0:&lt;br&gt;
                board[p2_position] = 2&lt;br&gt;
                break&lt;br&gt;
            elif p2_position &amp;lt; 0 or p2_position &amp;gt; 8:&lt;br&gt;
                print('Invalid position number. Please enter a number between 1 and 9.')&lt;br&gt;
            else:&lt;br&gt;
                print('Position already taken. Please choose another position.')&lt;br&gt;
        except ValueError:&lt;br&gt;
            print('Invalid input. Please enter a number.')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# Increment turn counter and print the board.&lt;br&gt;
    turn += 1&lt;br&gt;
    print_board()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# Check if player 2 wins.&lt;br&gt;
    player_2_stat = check_win(2)&lt;br&gt;
    if player_2_stat:&lt;br&gt;
        print('Player 2 WINS')&lt;br&gt;
        break&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Congratulations! You've built a simple yet functional Tic Tac Toe game in Python. Feel free to expand upon this project by adding features like a replay option or implementing a graphical interface using libraries like Pygame or Tkinter. Happy coding!&lt;br&gt;
github link to my code : &lt;a href="https://github.com/ThevinuDan/Python-TIC-TAC-TOE.git"&gt;https://github.com/ThevinuDan/Python-TIC-TAC-TOE.git&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>python</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
