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 GUI #152

Open
Immanuel-Anthony opened this issue Oct 4, 2024 · 23 comments
Open

Python TicTacToe GUI #152

Immanuel-Anthony opened this issue Oct 4, 2024 · 23 comments
Assignees

Comments

@Immanuel-Anthony
Copy link
Contributor

Would like to add a python script for a GUI Tic Tac Toe Game

@uddipbisht
Copy link
Contributor

can you assign this

@uddipbisht
Copy link
Contributor

from tkinter import *
from tkinter import messagebox

Player1 = 'X'
stop_game = False

def clicked(r, c):
global Player1

if Player1 == "X" and states[r][c] == 0 and not stop_game:
    b[r][c].configure(text="X")
    states[r][c] = 'X'
    Player1 = 'O'

elif Player1 == 'O' and states[r][c] == 0 and not stop_game:
    b[r][c].configure(text='O')
    states[r][c] = 'O'
    Player1 = "X"

check_if_win()

def check_if_win():
global stop_game

for i in range(3):
    if states[i][0] == states[i][1] == states[i][2] != 0:
        stop_game = True
        messagebox.showinfo("Winner", states[i][0] + " Won!")
        return

    elif states[0][i] == states[1][i] == states[2][i] != 0:
        stop_game = True
        messagebox.showinfo("Winner", states[0][i] + " Won!")
        return

if states[0][0] == states[1][1] == states[2][2] != 0:
    stop_game = True
    messagebox.showinfo("Winner", states[0][0] + " Won!")
    return

if states[0][2] == states[1][1] == states[2][0] != 0:
    stop_game = True
    messagebox.showinfo("Winner", states[0][2] + " Won!")
    return

if all(states[i][j] != 0 for i in range(3) for j in range(3)):
    stop_game = True
    messagebox.showinfo("Tie", "It's a tie!")

root = Tk()
root.title("Tic Tac Toe")
root.resizable(0, 0)

b = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
states = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

for i in range(3):
for j in range(3):
b[i][j] = Button(height=4, width=8, font=("Helvetica", "20"), command=lambda r=i, c=j: clicked(r, c))
b[i][j].grid(row=i, column=j)

mainloop()

@trishan9
Copy link
Owner

trishan9 commented Oct 4, 2024

Would like to add a python script for a GUI Tic Tac Toe Game

Ok, you can add the project! I will assign this issue to you!

@trishan9
Copy link
Owner

trishan9 commented Oct 4, 2024

can you assign this

sure! you can add gui python projects!

@trishan9
Copy link
Owner

trishan9 commented Oct 4, 2024

from tkinter import * from tkinter import messagebox

Player1 = 'X' stop_game = False

def clicked(r, c): global Player1

if Player1 == "X" and states[r][c] == 0 and not stop_game:
    b[r][c].configure(text="X")
    states[r][c] = 'X'
    Player1 = 'O'

elif Player1 == 'O' and states[r][c] == 0 and not stop_game:
    b[r][c].configure(text='O')
    states[r][c] = 'O'
    Player1 = "X"

check_if_win()

def check_if_win(): global stop_game

for i in range(3):
    if states[i][0] == states[i][1] == states[i][2] != 0:
        stop_game = True
        messagebox.showinfo("Winner", states[i][0] + " Won!")
        return

    elif states[0][i] == states[1][i] == states[2][i] != 0:
        stop_game = True
        messagebox.showinfo("Winner", states[0][i] + " Won!")
        return

if states[0][0] == states[1][1] == states[2][2] != 0:
    stop_game = True
    messagebox.showinfo("Winner", states[0][0] + " Won!")
    return

if states[0][2] == states[1][1] == states[2][0] != 0:
    stop_game = True
    messagebox.showinfo("Winner", states[0][2] + " Won!")
    return

if all(states[i][j] != 0 for i in range(3) for j in range(3)):
    stop_game = True
    messagebox.showinfo("Tie", "It's a tie!")

root = Tk() root.title("Tic Tac Toe") root.resizable(0, 0)

b = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] states = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

for i in range(3): for j in range(3): b[i][j] = Button(height=4, width=8, font=("Helvetica", "20"), command=lambda r=i, c=j: clicked(r, c)) b[i][j].grid(row=i, column=j)

mainloop()

I will assign the issue to you, please contribute to the repo!

@trishan9
Copy link
Owner

trishan9 commented Oct 4, 2024

Issue has been assigned to you guys @Immanuel-Anthony @uddipbisht

@trishan9
Copy link
Owner

trishan9 commented Oct 4, 2024

Happy contribution!

@uddipbisht
Copy link
Contributor

i have made the tic tac toe game bettween two friends

@uddipbisht
Copy link
Contributor

Screenshot 2024-10-04 233419
import random
class TicTacToe:
def init(self):
self.board = []
def create_board(self):
for i in range(3):
row = []
for j in range(3):
row.append('-')
self.board.append(row)
def get_random_first_player(self):
return random.randint(0, 1)
def fix_spot(self, row, col, player):
self.board[row][col] = player
def is_player_win(self, player):
win = None
n = len(self.board)
for i in range(n):
win = True
for j in range(n):
if self.board[i][j] != player:
win = False
break
if win:
return win
for i in range(n):
win = True
for j in range(n):
if self.board[j][i] != player:
win = False
break
if win:
return win
win = True
for i in range(n):
if self.board[i][i] != player:
win = False
break
if win:
return win
win = True
for i in range(n):
if self.board[i][n - 1 - i] != player:
win = False
break
if win:
return win
return False
for row in self.board:
for item in row:
if item == '-':
return False
return True
def is_board_filled(self):
for row in self.board:
for item in row:
if item == '-':
return False
return True
def swap_player_turn(self, player):
return 'X' if player == 'O' else 'O'
def show_board(self):
for row in self.board:
for item in row:
print(item, end=" ")
print()
def start(self):
self.create_board()
player = 'X' if self.get_random_first_player() == 1 else 'O'
while True:
print(f"Player {player} turn")
self.show_board()
row, col = list(
map(int, input("Enter row and column numbers to fix spot: ").split()))
print()
self.fix_spot(row - 1, col - 1, player)
if self.is_player_win(player):
print(f"Player {player} wins the game!")
break
if self.is_board_filled():
print("Match Draw!")
break
player = self.swap_player_turn(player)
print()
self.show_board()
tic_tac_toe = TicTacToe()
tic_tac_toe.start()

@trishan9
Copy link
Owner

trishan9 commented Oct 5, 2024

You can contribute this to the repo! @uddipbisht

@jayendranar02
Copy link

I am interested for contributing on this issue, please assign

@jayendranar02
Copy link

i will try to make more interesting GUI .

@trishan9
Copy link
Owner

trishan9 commented Oct 6, 2024

I am interested for contributing on this issue, please assign

sure, we are open to any contribution!

@trishan9
Copy link
Owner

trishan9 commented Oct 6, 2024

Assigned to you @jayendranar02!

@trishan9
Copy link
Owner

trishan9 commented Oct 6, 2024

Happy contribution!

@jayendranar02
Copy link

import tkinter as tk
from tkinter import messagebox

class TicTacToe:
def init(self, master):
self.master = master
self.master.title("Tic Tac Toe")
self.current_player = "X"
self.board = [""] * 9
self.buttons = []
self.create_buttons()

def create_buttons(self):
    for i in range(9):
        button = tk.Button(self.master, text="", font="Arial 20", width=5, height=2,
                           command=lambda i=i: self.make_move(i))
        button.grid(row=i // 3, column=i % 3)
        self.buttons.append(button)

def make_move(self, index):
    if self.board[index] == "" and not self.check_winner():
        self.board[index] = self.current_player
        self.buttons[index].config(text=self.current_player)
        if self.check_winner():
            messagebox.showinfo("Game Over", f"Player {self.current_player} wins!")
        elif "" not in self.board:
            messagebox.showinfo("Game Over", "It's a draw!")
        else:
            self.current_player = "O" if self.current_player == "X" else "X"

def check_winner(self):
    winning_combinations = [(0, 1, 2), (3, 4, 5), (6, 7, 8), 
                            (0, 3, 6), (1, 4, 7), (2, 5, 8), 
                            (0, 4, 8), (2, 4, 6)]
    for combo in winning_combinations:
        if self.board[combo[0]] == self.board[combo[1]] == self.board[combo[2]] != "":
            return True
    return False

if name == "main":
root = tk.Tk()
game = TicTacToe(root)
root.mainloop()

@jayendranar02
Copy link

How the Code Works----->

Import Libraries:
We import tkinter for GUI and messagebox for displaying messages.

TicTacToe Class:
The main class to manage the game. It initializes the game board and the current player.
The create_buttons method creates a 3x3 grid of buttons for the Tic Tac Toe game.
The make_move method handles the logic for placing a mark on the board, checking for wins or draws, and switching players.
The check_winner method checks all possible winning combinations to see if a player has won.
Main Block:

The script initializes the tkinter main loop and starts the game.

@jayendranar02
Copy link

terminal---->
python tic_tac_toe.py

@jayendranar02
Copy link

jayendranar02 commented Oct 6, 2024

Features of the Game--->
1).Players alternate turns, placing their marks (X or O) on the board.
2).The game checks for a winner after each move and announces the winner in a message box.
3).If all squares are filled without a winner, the game declares a draw.

@jayendranar02
Copy link

Conclusion
This simple Tic Tac Toe GUI game provides a great starting point for understanding GUI programming in Python. You can further enhance the game by adding features like resetting the game, keeping score, or improving the visual design.

uddipbisht added a commit to uddipbisht/HelloWorld that referenced this issue Oct 6, 2024
Python TicTacToe GUI trishan9#152 it is a game between two friends in tictactoe
@uddipbisht uddipbisht removed their assignment Oct 6, 2024
@uddipbisht
Copy link
Contributor

no by mistake

@uddipbisht
Copy link
Contributor

check my

@trishan9
Copy link
Owner

trishan9 commented Oct 8, 2024

check my

you have not yet created PR in the repo!

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

No branches or pull requests

4 participants