├── cgu-logo.png ├── README.md └── sudoku.py /cgu-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sudhaanshuu/Sudoku/HEAD/cgu-logo.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sudoku 2 | # Sudoku Game - Python and Pygame 3 | 4 | ## Overview 5 | This Sudoku game is a project developed by Sudhanshu Kumar, a computer science student. It provides an interactive platform for playing and solving Sudoku puzzles. The game is built using Python and Pygame, making it accessible and enjoyable for Sudoku enthusiasts. 6 | 7 | ## Features 8 | - Sudoku puzzle generator: The game offers a Sudoku puzzle generator that creates unique puzzles with varying difficulty levels. 9 | - User-friendly interface: The graphical user interface (GUI) is designed for ease of use, providing an engaging and interactive Sudoku experience. 10 | - Real-time validation: The game checks user inputs in real-time, highlighting errors and preventing invalid moves. 11 | - Timer and scoring: Players can track their progress with a timer, making it ideal for both casual and competitive play. 12 | - Save and load: The game allows players to save their progress and load previous games to continue at any time. 13 | - Multiple difficulty levels: Sudoku puzzles are available in different difficulty levels, catering to both beginners and advanced players. 14 | 15 | ## How to Play 16 | 1. Run the Python script to launch the game. 17 | 2. Use the mouse to select cells and input numbers. 18 | 3. The game will validate your inputs in real-time. 19 | 4. Solve the puzzle by filling in all cells correctly. 20 | 5. Track your time and progress while playing. 21 | 22 | ## Installation 23 | Ensure you have Python and Pygame installed on your system: 24 | - Python: [Download Python](https://www.python.org/downloads/) 25 | - Pygame: Install using pip with the command `pip install pygame` 26 | 27 | Clone the repository or download the game files and run the main script: 28 | 29 | ## Author 30 | - Sudhanshu Kumar 31 | - Computer Science Engineering Student 32 | - [GitHub Profile](https://github.com/sudhanshu-kumar) 33 | 34 | ## Feedback and Contributions 35 | Feedback and contributions to this project are welcome. If you encounter any issues, have suggestions, or want to contribute to the development of this Sudoku game, please feel free to contact the author. 36 | 37 | ## License 38 | This Sudoku game is open-source and distributed under the [MIT License](LICENSE). You are free to use, modify, and distribute the code as per the terms of the license. 39 | 40 | -------------------------------------------------------------------------------- /sudoku.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.font.init() 4 | 5 | screen = pygame.display.set_mode((500, 600)) 6 | 7 | pygame.display.set_caption("Sudoku Solver By Sudhanshu") 8 | img = pygame.image.load('cgu-logo.png') 9 | pygame.display.set_icon(img) 10 | 11 | x = 0 12 | y = 0 13 | dif = 500 / 9 14 | val = 0 15 | grid =[ 16 | [7, 8, 0, 4, 0, 0, 1, 2, 0], 17 | [6, 0, 0, 0, 7, 5, 0, 0, 9], 18 | [0, 0, 0, 6, 0, 1, 0, 7, 8], 19 | [0, 0, 7, 0, 4, 0, 2, 6, 0], 20 | [0, 0, 1, 0, 5, 0, 9, 3, 0], 21 | [9, 0, 4, 0, 6, 0, 0, 0, 5], 22 | [0, 7, 0, 3, 0, 0, 0, 1, 2], 23 | [1, 2, 0, 0, 0, 7, 4, 0, 0], 24 | [0, 4, 9, 2, 0, 6, 0, 0, 7] 25 | ] 26 | 27 | font1 = pygame.font.SysFont("times", 20) 28 | font2 = pygame.font.SysFont("times", 20) 29 | def get_cord(pos): 30 | global x 31 | x = pos[0]//dif 32 | global y 33 | y = pos[1]//dif 34 | 35 | def draw_box(): 36 | for i in range(2): 37 | pygame.draw.line(screen, (255, 0, 0), (x * dif-3, (y + i)*dif), (x * dif + dif + 3, (y + i)*dif), 7) 38 | pygame.draw.line(screen, (255, 0, 0), ( (x + i)* dif, y * dif ), ((x + i) * dif, y * dif + dif), 7) 39 | 40 | def draw(): 41 | 42 | for i in range (9): 43 | for j in range (9): 44 | if grid[i][j]!= 0: 45 | 46 | pygame.draw.rect(screen, (0, 153, 153), (i * dif, j * dif, dif + 1, dif + 1)) 47 | 48 | text1 = font1.render(str(grid[i][j]), 1, (0, 0, 0)) 49 | screen.blit(text1, (i * dif + 15, j * dif + 15)) 50 | for i in range(10): 51 | if i % 3 == 0 : 52 | thick = 7 53 | else: 54 | thick = 1 55 | pygame.draw.line(screen, (0, 0, 0), (0, i * dif), (500, i * dif), thick) 56 | pygame.draw.line(screen, (0, 0, 0), (i * dif, 0), (i * dif, 500), thick) 57 | 58 | def draw_val(val): 59 | text1 = font1.render(str(val), 1, (0, 0, 0)) 60 | screen.blit(text1, (x * dif + 15, y * dif + 15)) 61 | 62 | def raise_error1(): 63 | text1 = font1.render("WRONG !!!", 1, (0, 0, 0)) 64 | screen.blit(text1, (20, 570)) 65 | def raise_error2(): 66 | text1 = font1.render("Wrong !!! Not a valid Key", 1, (0, 0, 0)) 67 | screen.blit(text1, (20, 570)) 68 | 69 | def valid(m, i, j, val): 70 | for it in range(9): 71 | if m[i][it]== val: 72 | return False 73 | if m[it][j]== val: 74 | return False 75 | it = i//3 76 | jt = j//3 77 | for i in range(it * 3, it * 3 + 3): 78 | for j in range (jt * 3, jt * 3 + 3): 79 | if m[i][j]== val: 80 | return False 81 | return True 82 | 83 | def solve(grid, i, j): 84 | 85 | while grid[i][j]!= 0: 86 | if i<8: 87 | i+= 1 88 | elif i == 8 and j<8: 89 | i = 0 90 | j+= 1 91 | elif i == 8 and j == 8: 92 | return True 93 | pygame.event.pump() 94 | for it in range(1, 10): 95 | if valid(grid, i, j, it)== True: 96 | grid[i][j]= it 97 | global x, y 98 | x = i 99 | y = j 100 | screen.fill((255, 255, 255)) 101 | draw() 102 | draw_box() 103 | pygame.display.update() 104 | pygame.time.delay(20) 105 | if solve(grid, i, j)== 1: 106 | return True 107 | else: 108 | grid[i][j]= 0 109 | screen.fill((255, 255, 255)) 110 | 111 | draw() 112 | draw_box() 113 | pygame.display.update() 114 | pygame.time.delay(50) 115 | return False 116 | 117 | def instruction(): 118 | text1 = font2.render("PRESS D TO RESET TO DEFAULT / R TO EMPTY", 1, (0, 0, 0)) 119 | text2 = font2.render("ENTER VALUES AND PRESS ENTER TO VISUALIZE", 1, (0, 0, 0)) 120 | screen.blit(text1, (20, 520)) 121 | screen.blit(text2, (20, 540)) 122 | 123 | def result(): 124 | text1 = font1.render("FINISHED PRESS R or D", 1, (0, 0, 0)) 125 | screen.blit(text1, (20, 570)) 126 | run = True 127 | flag1 = 0 128 | flag2 = 0 129 | rs = 0 130 | error = 0 131 | while run: 132 | 133 | screen.fill((255, 255, 255)) 134 | for event in pygame.event.get(): 135 | if event.type == pygame.QUIT: 136 | run = False 137 | if event.type == pygame.MOUSEBUTTONDOWN: 138 | flag1 = 1 139 | pos = pygame.mouse.get_pos() 140 | get_cord(pos) 141 | if event.type == pygame.KEYDOWN: 142 | if event.key == pygame.K_LEFT: 143 | x-= 1 144 | flag1 = 1 145 | if event.key == pygame.K_RIGHT: 146 | x+= 1 147 | flag1 = 1 148 | if event.key == pygame.K_UP: 149 | y-= 1 150 | flag1 = 1 151 | if event.key == pygame.K_DOWN: 152 | y+= 1 153 | flag1 = 1 154 | if event.key == pygame.K_1: 155 | val = 1 156 | if event.key == pygame.K_2: 157 | val = 2 158 | if event.key == pygame.K_3: 159 | val = 3 160 | if event.key == pygame.K_4: 161 | val = 4 162 | if event.key == pygame.K_5: 163 | val = 5 164 | if event.key == pygame.K_6: 165 | val = 6 166 | if event.key == pygame.K_7: 167 | val = 7 168 | if event.key == pygame.K_8: 169 | val = 8 170 | if event.key == pygame.K_9: 171 | val = 9 172 | if event.key == pygame.K_RETURN: 173 | flag2 = 1 174 | if event.key == pygame.K_r: 175 | rs = 0 176 | error = 0 177 | flag2 = 0 178 | grid =[ 179 | [0, 0, 0, 0, 0, 0, 0, 0, 0], 180 | [0, 0, 0, 0, 0, 0, 0, 0, 0], 181 | [0, 0, 0, 0, 0, 0, 0, 0, 0], 182 | [0, 0, 0, 0, 0, 0, 0, 0, 0], 183 | [0, 0, 0, 0, 0, 0, 0, 0, 0], 184 | [0, 0, 0, 0, 0, 0, 0, 0, 0], 185 | [0, 0, 0, 0, 0, 0, 0, 0, 0], 186 | [0, 0, 0, 0, 0, 0, 0, 0, 0], 187 | [0, 0, 0, 0, 0, 0, 0, 0, 0] 188 | ] 189 | 190 | if event.key == pygame.K_d: 191 | rs = 0 192 | error = 0 193 | flag2 = 0 194 | grid =[ 195 | [7, 8, 0, 4, 0, 0, 1, 2, 0], 196 | [6, 0, 0, 0, 7, 5, 0, 0, 9], 197 | [0, 0, 0, 6, 0, 1, 0, 7, 8], 198 | [0, 0, 7, 0, 4, 0, 2, 6, 0], 199 | [0, 0, 1, 0, 5, 0, 9, 3, 0], 200 | [9, 0, 4, 0, 6, 0, 0, 0, 5], 201 | [0, 7, 0, 3, 0, 0, 0, 1, 2], 202 | [1, 2, 0, 0, 0, 7, 4, 0, 0], 203 | [0, 4, 9, 2, 0, 6, 0, 0, 7] 204 | ] 205 | if flag2 == 1: 206 | if solve(grid, 0, 0)== False: 207 | error = 1 208 | else: 209 | rs = 1 210 | flag2 = 0 211 | if val != 0: 212 | draw_val(val) 213 | if valid(grid, int(x), int(y), val)== True: 214 | grid[int(x)][int(y)]= val 215 | flag1 = 0 216 | else: 217 | grid[int(x)][int(y)]= 0 218 | raise_error2() 219 | val = 0 220 | 221 | if error == 1: 222 | raise_error1() 223 | if rs == 1: 224 | result() 225 | draw() 226 | if flag1 == 1: 227 | draw_box() 228 | instruction() 229 | pygame.display.update() 230 | pygame.quit() 231 | 232 | 233 | --------------------------------------------------------------------------------