josharsh / 100LinesOfCode

Let's build something productive in less than 100 Lines of Code.
GNU General Public License v3.0
649 stars 313 forks source link

Python TicTacToe #335

Open vivekkdagar opened 1 year ago

vivekkdagar commented 1 year ago

I've made a simple Tic Tac Toe Game in Python, can I push it here?

github-actions[bot] commented 1 year ago

Message that will be displayed on users first issue

vivekkdagar commented 1 year ago

What?

On Fri, 29 Sept, 2023, 01:15 github-actions[bot], @.***> wrote:

Message that will be displayed on users first issue

— Reply to this email directly, view it on GitHub https://github.com/josharsh/100LinesOfCode/issues/335#issuecomment-1739908869, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVPJ45JLMW2B7KOMZAL4UN3X4XHWVANCNFSM6AAAAAA5LO6HA4 . You are receiving this because you authored the thread.Message ID: @.***>

rayenkhayati commented 1 year ago

Sure! Here's an example of a Tic Tac Toe game implementation in Python:


# Tic Tac Toe

# Create the game board
board = [' ' for _ in range(9)]

# Function to display the board
def display_board():
    print('-------------')
    print('|', board[0], '|', board[1], '|', board[2], '|')
    print('-------------')
    print('|', board[3], '|', board[4], '|', board[5], '|')
    print('-------------')
    print('|', board[6], '|', board[7], '|', board[8], '|')
    print('-------------')

# Function to check for a win
def check_win():
    # Rows
    if board[0] == board[1] == board[2] != ' ':
        return True
    if board[3] == board[4] == board[5] != ' ':
        return True
    if board[6] == board[7] == board[8] != ' ':
        return True
    # Columns
    if board[0] == board[3] == board[6] != ' ':
        return True
    if board[1] == board[4] == board[7] != ' ':
        return True
    if board[2] == board[5] == board[8] != ' ':
        return True
    # Diagonals
    if board[0] == board[4] == board[8] != ' ':
        return True
    if board[2] == board[4] == board[6] != ' ':
        return True

    return False

# Function to check for a draw
def check_draw():
    if ' ' not in board:
        return True
    return False

# Function to play the game
def play_game():
    current_player = 'X'

    while True:
        display_board()
        position = int(input("Player " + current_player + ", choose a position (1-9): ")) - 1

        if board[position] == ' ':
            board[position] = current_player
        else:
            print("Invalid move. Try again.")
            continue

        if check_win():
            display_board()
            print("Player", current_player, "wins!")
            break

        if check_draw():
            display_board()
            print("It's a draw!")
            break

        # Switch player
        current_player = 'O' if current_player == 'X' else 'X'

# Start the game
play_game()

i hope you enjoy the game
vivekkdagar commented 1 year ago

No i was asking if i can put my own code in this repo

BishalPrasad05 commented 12 months ago

I can implement the game, can you assign me this issue?