diff --git a/.DS_Store b/.DS_Store index e7cb634..6aee1eb 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Gui.PNG b/Gui.PNG new file mode 100644 index 0000000..ddfa971 Binary files /dev/null and b/Gui.PNG differ diff --git a/README.md b/README.md index cdf1fac..41337e6 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,3 @@ -# Sudoku Game In Python +# Tic Tac Toe Game In Python - -### run: - -* python gui.py - -### Mac - -* python3 gui.py \ No newline at end of file + \ No newline at end of file diff --git a/Winning_message.PNG b/Winning_message.PNG new file mode 100644 index 0000000..9ebcf05 Binary files /dev/null and b/Winning_message.PNG differ diff --git a/chooseLevel.py b/chooseLevel.py deleted file mode 100644 index b86c16c..0000000 --- a/chooseLevel.py +++ /dev/null @@ -1,72 +0,0 @@ -import pygame - -BLACK = (0, 0, 0) -WHITE = (255, 255, 255) -GREEN = (0, 255, 0) -L_GREEN = (150, 255, 150) -RED = (255, 0, 0) -L_RED = (255, 204, 203) -GRAY = (80, 80, 80) -YELLOW = (255, 255, 0) -# -pygame.init() -X = 300 -Y = 200 -size = (X, Y) -window = pygame.display.set_mode(size) -font = pygame.font.Font('freesansbold.ttf', 25) - - - -def drawButton(left, top, color, textInButton): - rectSize = pygame.Rect(left, top, 60, 30) - pygame.draw.rect(window, color, rectSize) # left, top, width, height - pygame.draw.rect(window, BLACK, rectSize, 3) - fontButton = pygame.font.Font('freesansbold.ttf', 20) - textButton = fontButton.render(textInButton, True, BLACK, ) - textRectButton = textButton.get_rect() - textRectButton.center = (left + 30, top + 15) - window.blit(textButton, textRectButton) - - -def chooseLevel(): - level = 0 - text = font.render('choose difficulty level', True, BLACK, WHITE) - textRect = text.get_rect() - textRect.center = (X // 2, Y // 2 - 40) - - pygame.display.set_caption("Sudoku King") - - done = True - while done: - window.fill(WHITE) - window.blit(text, textRect) - drawButton(40, 100, GRAY, "1") - drawButton(120, 100, GRAY, "2") - drawButton(200, 100, GRAY, "3") - pos = pygame.mouse.get_pos() - - for event in pygame.event.get(): - if event.type == pygame.QUIT: - # deactivates the pygame library - pygame.quit() - # quit the program. - quit() - if event.type == pygame.MOUSEBUTTONDOWN: - # print("Click ", pos) - if (40 <= pos[0] <= 100) and (100 <= pos[1] <= 130): - level = 1 - if (120 <= pos[0] <= 180) and (100 <= pos[1] <= 130): - level = 2 - if (200 <= pos[0] <= 260) and (100 <= pos[1] <= 130): - level = 3 - if level != 0: - # print(level) - pygame.quit() - return level - - # Draws the surface object to the screen. - pygame.display.update() - - -# chooseLevel() diff --git a/draw_messge.PNG b/draw_messge.PNG new file mode 100644 index 0000000..08f6243 Binary files /dev/null and b/draw_messge.PNG differ diff --git a/generator.py b/generator.py deleted file mode 100644 index 3bcfa99..0000000 --- a/generator.py +++ /dev/null @@ -1,100 +0,0 @@ -import random -import copy - -# firstBoard = [ -# [0, 0, 0, 0, 0, 0, 0, 0, 0], -# [0, 0, 0, 0, 0, 0, 0, 0, 0], -# [0, 0, 0, 0, 0, 0, 0, 0, 0], -# [0, 0, 0, 0, 0, 0, 0, 0, 0], -# [0, 0, 0, 0, 0, 0, 0, 0, 0], -# [0, 0, 0, 0, 0, 0, 0, 0, 0], -# [0, 0, 0, 0, 0, 0, 0, 0, 0], -# [0, 0, 0, 0, 0, 0, 0, 0, 0], -# [0, 0, 0, 0, 0, 0, 0, 0, 0] -# ] - - -def printBoard(board): - for i in range(len(board)): - if i % 3 == 0 and i != 0: - print("- - - - - - - - - - - -") - for j in range(len(board[0])): - if j % 3 == 0 and j != 0: - print(" | ", end="") - if j == 8: # end of the row - print(board[i][j]) - else: - print(str(board[i][j]) + " ", end="") - - -def findEmpty(board): - for y in range(len(board)): - for x in range(len(board[0])): - if board[y][x] == 0: - return y, x # y = row , x = column - # if we got here it mean that we finish the sudoku, so return none - return None - - -def validCheck(board, number, coordinates): - # checking row - for x in range(len(board[0])): - if number == board[coordinates[0]][x] and coordinates[1] != x: # coordinates[0]= row - return False - - # checking column - for y in range(len(board)): - if number == board[y][coordinates[1]] and coordinates[0] != y: - return False - - # checking the box - box_x = coordinates[1] // 3 - box_y = coordinates[0] // 3 - - for y in range(box_y * 3, box_y * 3 + 3): - for x in range(box_x * 3, box_x * 3 + 3): - if number == board[y][x] and (y, x) != coordinates: - return False - - return True - - -def generateRandomBoard(board): - # end condition:- getting to the end of the board - the function findEmpty return NONE - find = findEmpty(board) - if find is None: # if find != False - return True - else: - row, col = find - for number in range(1, 10): - randomNumber = random.randint(1, 9) # TODO: need to work on the algorithm a bit more - - # TODO: to not rand the same number over and over again - if validCheck(board, randomNumber, (row, col)): - board[row][col] = randomNumber - if generateRandomBoard(board): - return True - - board[row][col] = 0 - return False - - -def deleteCells(firstBoard,number): - while number: - row = random.randint(0, 8) - col = random.randint(0, 8) - if firstBoard[row][col] != 0: - firstBoard[row][col] = 0 - number = number - 1 - - -def sudokuGenerate(firstBoard, level): - - # printBoard(firstBoard) - generateRandomBoard(firstBoard) - # printBoard(firstBoard) - if level == 1: - deleteCells(firstBoard,30) - if level == 2: - deleteCells(firstBoard,40) - if level == 3: - deleteCells(firstBoard,50) diff --git a/gifs/playing the game.gif b/gifs/playing the game.gif deleted file mode 100644 index 81f9aac..0000000 Binary files a/gifs/playing the game.gif and /dev/null differ diff --git a/gifs/start the game.gif b/gifs/start the game.gif deleted file mode 100644 index b21b4a9..0000000 Binary files a/gifs/start the game.gif and /dev/null differ diff --git a/gui.py b/gui.py deleted file mode 100644 index 295813f..0000000 --- a/gui.py +++ /dev/null @@ -1,199 +0,0 @@ -import pygame -from sudokuSolver import * -from chooseLevel import * -import time - -# Define some colors -BLACK = (0, 0, 0) -WHITE = (255, 255, 255) -GREEN = (0, 255, 0) -L_GREEN = (150, 255, 150) -RED = (255, 0, 0) -L_RED = (255, 204, 203) -GRAY = (60, 60, 60) -L_GRAY = (220, 220, 220) -YELLOW = (255, 255, 0) - -# This sets the WIDTH and HEIGHT of each grid location -WIDTH = HEIGHT = 50 - -# This sets the margin between each cell -MARGIN = 5 -numbers_1to9 = [pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4, pygame.K_5, pygame.K_6, pygame.K_7, pygame.K_8, - pygame.K_9] - -# Set the width and height of the screen [width, height] -size = (500, 500) -# screen = pygame.display.set_mode(size) -pygame.init() -font = pygame.font.Font('freesansbold.ttf', 32) - -# pygame.display.set_caption("Sudoku King") - -# Loop until the user clicks the close button. -done = False - - -def cheatingAllTheWay(): - for row in range(len(Board)): - for column in range(len(Board[row])): - Board[row][column] = solvedBoard[row][column] - addNumToBoard(Board[row][column], row, column, L_GREEN) - time.sleep(0.05) - pygame.display.flip() - finish() - - -def addNumToBoard(number, row, column, color): - addNewRect(row, column, WHITE, 5) - addNewRect(row, column, color, None) - font = pygame.font.Font('freesansbold.ttf', 32) - text = font.render(str(number), True, BLACK, ) - textRect = text.get_rect() # get_rect() -> Returns a new rectangle covering the entire surface. - textRect.center = ((MARGIN + WIDTH) * column + MARGIN + WIDTH / 2, (MARGIN + HEIGHT) * row + MARGIN + WIDTH / 2) - screen.blit(text, textRect) - drawTheBorder() - - -def finish(): - if solvedBoard == Board: - print("good") - else: - print("not good") - - -def addNewRect(row, col, color, width): - rectSize = pygame.Rect((MARGIN + WIDTH) * col + MARGIN, (MARGIN + HEIGHT) * row + MARGIN, WIDTH, - HEIGHT) - if width is not None: - pygame.draw.rect(screen, color, rectSize, width) # coloring only the border - else: - pygame.draw.rect(screen, color, rectSize) # coloring the whole rectangle - - -def flickering(timeFlickering, color): # flickering with color on-off - addNewRect(row, column, color, 5) - pygame.display.flip() - time.sleep(timeFlickering) - addNewRect(row, column, WHITE, 5) - pygame.display.flip() - time.sleep(timeFlickering) - addNewRect(row, column, color, 5) - pygame.display.flip() - time.sleep(timeFlickering) - addNewRect(row, column, WHITE, 5) - pygame.display.flip() - - -def drawTheBorder(): - dif = 500 // 9 - for i in range(10): - thick = 5 - pygame.draw.line(screen, GRAY, (0, i * dif + 2), (500, i * dif + 2), thick) - pygame.draw.line(screen, GRAY, (i * dif + 2, 0), (i * dif + 2, 500), thick) - for i in range(10): - if i % 3 == 0: - thick = 8 - pygame.draw.line(screen, BLACK, (0, i * dif), (500, i * dif), thick) - pygame.draw.line(screen, BLACK, (i * dif, 0), (i * dif, 500), thick) - - -def drawInitBoard(): - # printBoard(solvedBoard) - for row in range(len(Board)): - for column in range(len(Board[row])): - color = L_GRAY - if Board[row][column] == 0: # if we want to change to background of the empty cells - color = WHITE - # ----- drawing the rect ------ - pygame.draw.rect(screen, color, - [(MARGIN + WIDTH) * column + MARGIN, (MARGIN + HEIGHT) * row + MARGIN, WIDTH, HEIGHT]) - # show nothing if the number is 0 - font = pygame.font.Font('freesansbold.ttf', 32) - if Board[row][column] == 0: - text = font.render(" ", True, BLACK, ) # render(text, anti-alias[True], color, background=None) - else: - text = font.render(str(Board[row][column]), True, BLACK, ) - - textRect = text.get_rect() # get_rect() -> Returns a new rectangle covering the entire surface. - textRect.center = ( - (MARGIN + WIDTH) * column + MARGIN + WIDTH / 2, (MARGIN + HEIGHT) * row + MARGIN + WIDTH / 2) - screen.blit(text, textRect) - drawTheBorder() - - -# -------- Main Program Loop ----------- -if __name__ == "__main__": - flag1 = True - - while flag1: - level = chooseLevel() - if level == 1 or level == 2 or level == 3: - print(level) - flag1 = False - pygame.display.set_caption("Sudoku King1") - screen = pygame.display.set_mode(size) - - sol = mainSolver(level) # first at all the script solve the sudoku by itself - - print("solveBoard") - printBoard(sol) - - # ------ draw the board ------ - pygame.init() - screen.fill(BLACK) - drawInitBoard() - readyForInput = False - key = None - while not done: - # --- Main event loop - - for event in pygame.event.get(): - if event.type == pygame.QUIT: - done = True - if event.type == pygame.KEYDOWN: - if event.key in numbers_1to9: - key = chr(event.key) - if event.key == pygame.K_RETURN: - finish() - if event.key == pygame.K_c: - cheatingAllTheWay() - if event.type == pygame.MOUSEBUTTONDOWN: - # ------ if clicked on a cell get his row and column ------ - if readyForInput is True: - addNewRect(row, column, WHITE, None) - drawTheBorder() - readyForInput = False - - pos = pygame.mouse.get_pos() - column = pos[0] // (WIDTH + MARGIN) - row = pos[1] // (WIDTH + MARGIN) - # ------ checking if it is a empty (0 inside) ------ - if Board[row][column] == 0: - # ------ coloring the border of the clicked cell ----- #TODO YELLOW - - addNewRect(row, column, YELLOW, 5) - readyForInput = True - # ------ now only wait for input from the user ----- - - if readyForInput and key is not None: - # ------ checking if the key is good at it's place ------ - if int(key) == sol[row][column]: - Board[row][column] = key - flickering(0.1, GREEN) # flickering at a 0.2 seconds with the color green - addNumToBoard(key, row, column, L_GREEN) - else: - flickering(0.1, RED) # flickering at a 0.2 seconds with the color red - addNumToBoard(key, row, column, L_RED) - - # ----------------------------------------------- - drawTheBorder() - readyForInput = False - - key = None - pygame.display.flip() - pygame.display.update() - - -# Close the window and quit. -pygame.quit() diff --git a/images/choosing difficulty level.PNG b/images/choosing difficulty level.PNG deleted file mode 100644 index f6cbf34..0000000 Binary files a/images/choosing difficulty level.PNG and /dev/null differ diff --git a/images/gaming.PNG b/images/gaming.PNG deleted file mode 100644 index 5a457cd..0000000 Binary files a/images/gaming.PNG and /dev/null differ diff --git a/images/playing the game.PNG b/images/playing the game.PNG deleted file mode 100644 index a52e39e..0000000 Binary files a/images/playing the game.PNG and /dev/null differ diff --git a/resources/base.png b/resources/base.png new file mode 100644 index 0000000..95c69de Binary files /dev/null and b/resources/base.png differ diff --git a/sudokuSolver.py b/sudokuSolver.py deleted file mode 100644 index b0970e9..0000000 --- a/sudokuSolver.py +++ /dev/null @@ -1,45 +0,0 @@ -import copy -from generator import * - -# -------- Global board ---------------- - -Board = [ - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0] - ] - -solvedBoard = copy.deepcopy(Board) - -def solve(board): - # end condition:- getting to the end of the board - the function findEmpty return NONE - find = findEmpty(board) - if find is None: # if find != False - return True - else: - row, col = find - for number in range(1, 10): - if validCheck(board, number, (row, col)): - board[row][col] = number - # TODO: need to show it on the GUI - - if solve(board): - return True - - board[row][col] = 0 - # TODO: delete the number in the GUI - return False - - -def mainSolver(level): - sudokuGenerate(Board, level) - solvedBoard = copy.deepcopy(Board) - solve(solvedBoard) - return solvedBoard - \ No newline at end of file diff --git a/ttgame.py b/ttgame.py new file mode 100644 index 0000000..2aee41e --- /dev/null +++ b/ttgame.py @@ -0,0 +1,208 @@ +from tkinter import * +from tkinter import ttk +import tkinter.messagebox + +root=Tk() +root.title("Tic Tac Toe") +#add Buttons +bu1=ttk.Button(root,text=' ') +bu1.grid(row=0,column=0,sticky='snew',ipadx=40,ipady=40) +bu1.config(command=lambda: ButtonClick(1)) + +bu2=ttk.Button(root,text=' ') +bu2.grid(row=0,column=1,sticky='snew',ipadx=40,ipady=40) +bu2.config(command=lambda: ButtonClick(2)) + +bu3=ttk.Button(root,text=' ') +bu3.grid(row=0,column=2,sticky='snew',ipadx=40,ipady=40) +bu3.config(command=lambda: ButtonClick(3)) + +bu4=ttk.Button(root,text=' ') +bu4.grid(row=1,column=0,sticky='snew',ipadx=40,ipady=40) +bu4.config(command=lambda: ButtonClick(4)) + +bu5=ttk.Button(root,text=' ') +bu5.grid(row=1,column=1,sticky='snew',ipadx=40,ipady=40) +bu5.config(command=lambda: ButtonClick(5)) + +bu6=ttk.Button(root,text=' ') +bu6.grid(row=1,column=2,sticky='snew',ipadx=40,ipady=40) +bu6.config(command=lambda: ButtonClick(6)) + +bu7=ttk.Button(root,text=' ') +bu7.grid(row=2,column=0,sticky='snew',ipadx=40,ipady=40) +bu7.config(command=lambda: ButtonClick(7)) + +bu8=ttk.Button(root,text=' ') +bu8.grid(row=2,column=1,sticky='snew',ipadx=40,ipady=40) +bu8.config(command=lambda: ButtonClick(8)) + +bu9=ttk.Button(root,text=' ') +bu9.grid(row=2,column=2,sticky='snew',ipadx=40,ipady=40) +bu9.config(command=lambda: ButtonClick(9)) + +playerturn=ttk.Label(root,text=" Player 1 turn! ") +playerturn.grid(row=3,column=0,sticky='snew',ipadx=40,ipady=40) + +playerdetails=ttk.Label(root,text=" Player 1 is X\n\n Player 2 is O") +playerdetails.grid(row=3,column=2,sticky='snew',ipadx=40,ipady=40) + +res=ttk.Button(root,text='Restart') +res.grid(row=3,column=1,sticky='snew',ipadx=40,ipady=40) +res.config(command=lambda: restartbutton()) + +a=1 +b=0 +c=0 +def restartbutton(): + global a,b,c + a=1 + b=0 + c=0 + playerturn['text']=" Player 1 turn! " + bu1['text']=' ' + bu2['text']=' ' + bu3['text']=' ' + bu4['text']=' ' + bu5['text']=' ' + bu6['text']=' ' + bu7['text']=' ' + bu8['text']=' ' + bu9['text']=' ' + bu1.state(['!disabled']) + bu2.state(['!disabled']) + bu3.state(['!disabled']) + bu4.state(['!disabled']) + bu5.state(['!disabled']) + bu6.state(['!disabled']) + bu7.state(['!disabled']) + bu8.state(['!disabled']) + bu9.state(['!disabled']) + +#after getting result(win or loss or draw) disable button +def disableButton(): + bu1.state(['disabled']) + bu2.state(['disabled']) + bu3.state(['disabled']) + bu4.state(['disabled']) + bu5.state(['disabled']) + bu6.state(['disabled']) + bu7.state(['disabled']) + bu8.state(['disabled']) + bu9.state(['disabled']) + + +def ButtonClick(id): + global a,b,c + print("ID:{}".format(id)) + + #for player 1 turn + if id==1 and bu1['text']==' ' and a==1: + bu1['text']="X" + a=0 + b+=1 + if id==2 and bu2['text']==' ' and a==1: + bu2['text']="X" + a=0 + b+=1 + if id==3 and bu3['text']==' ' and a==1: + bu3['text']="X" + a=0 + b+=1 + if id==4 and bu4['text']==' ' and a==1: + bu4['text']="X" + a=0 + b+=1 + if id==5 and bu5['text']==' ' and a==1: + bu5['text']="X" + a=0 + b+=1 + if id==6 and bu6['text']==' ' and a==1: + bu6['text']="X" + a=0 + b+=1 + if id==7 and bu7['text']==' ' and a==1: + bu7['text']="X" + a=0 + b+=1 + if id==8 and bu8['text']==' ' and a==1: + bu8['text']="X" + a=0 + b+=1 + if id==9 and bu9['text']==' ' and a==1: + bu9['text']="X" + a=0 + b+=1 + #for player 2 turn + if id==1 and bu1['text']==' ' and a==0: + bu1['text']="O" + a=1 + b+=1 + if id==2 and bu2['text']==' ' and a==0: + bu2['text']="O" + a=1 + b+=1 + if id==3 and bu3['text']==' ' and a==0: + bu3['text']="O" + a=1 + b+=1 + if id==4 and bu4['text']==' ' and a==0: + bu4['text']="O" + a=1 + b+=1 + if id==5 and bu5['text']==' ' and a==0: + bu5['text']="O" + a=1 + b+=1 + if id==6 and bu6['text']==' ' and a==0: + bu6['text']="O" + a=1 + b+=1 + if id==7 and bu7['text']==' ' and a==0: + bu7['text']="O" + a=1 + b+=1 + if id==8 and bu8['text']==' ' and a==0: + bu8['text']="O" + a=1 + b+=1 + if id==9 and bu9['text']==' ' and a==0: + bu9['text']="O" + a=1 + b+=1 + + #checking for winner + if( bu1['text']=='X' and bu2['text']=='X' and bu3['text']=='X' or + bu4['text']=='X' and bu5['text']=='X' and bu6['text']=='X' or + bu7['text']=='X' and bu8['text']=='X' and bu9['text']=='X' or + bu1['text']=='X' and bu4['text']=='X' and bu7['text']=='X' or + bu2['text']=='X' and bu5['text']=='X' and bu8['text']=='X' or + bu3['text']=='X' and bu6['text']=='X' and bu9['text']=='X' or + bu1['text']=='X' and bu5['text']=='X' and bu9['text']=='X' or + bu3['text']=='X' and bu5['text']=='X' and bu7['text']=='X'): + disableButton() + c=1 + tkinter.messagebox.showinfo("Tic Tac Toe","Winner is player 1") + elif( bu1['text']=='O' and bu2['text']=='O' and bu3['text']=='O' or + bu4['text']=='O' and bu5['text']=='O' and bu6['text']=='O' or + bu7['text']=='O' and bu8['text']=='O' and bu9['text']=='O' or + bu1['text']=='O' and bu4['text']=='O' and bu7['text']=='O' or + bu2['text']=='O' and bu5['text']=='O' and bu8['text']=='O' or + bu3['text']=='O' and bu6['text']=='O' and bu9['text']=='O' or + bu1['text']=='O' and bu5['text']=='O' and bu9['text']=='O' or + bu3['text']=='O' and bu5['text']=='O' and bu7['text']=='O'): + disableButton() + c=1 + tkinter.messagebox.showinfo("Tic Tac Toe","Winner is player 2") + elif b==9: + disableButton() + c=1 + tkinter.messagebox.showinfo("Tic Tac Toe","Match is Draw.") + + if a==1 and c==0: + playerturn['text']=" Player 1 turn! " + elif a==0 and c==0: + playerturn['text']=" Player 2 turn! " + +root.mainloop() +