Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python TicTacToe #335

Open
vivekkdagar opened this issue Sep 28, 2023 · 5 comments
Open

Python TicTacToe #335

vivekkdagar opened this issue Sep 28, 2023 · 5 comments

Comments

@vivekkdagar
Copy link

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

@github-actions
Copy link

Message that will be displayed on users first issue

@vivekkdagar
Copy link
Author

vivekkdagar commented Sep 29, 2023 via email

@rayenkhayati
Copy link

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
Copy link
Author

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

@BishalPrasad05
Copy link

BishalPrasad05 commented Oct 11, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants