├── Code ├── Assets │ ├── choose.png │ ├── intro.png │ ├── riam_img.png │ └── sudoku_img.png ├── Changelog.txt ├── Rat in a Maze │ ├── Assets │ │ ├── arrowkeys.png │ │ ├── intro.png │ │ ├── maze_ui.png │ │ ├── rat.png │ │ └── user_entry.png │ ├── Changelog.txt │ ├── Rat_in_a_maze (Without UI).py │ └── Rat_in_a_maze_UI.py ├── Sudoku │ ├── Assets │ │ ├── arrowkeys.png │ │ ├── intro.png │ │ ├── numpad.png │ │ ├── sudoku_UI.png │ │ └── user_entry.png │ ├── Changelog.txt │ ├── __pycache__ │ │ └── solve_sudoku_UI.cpython-310.pyc │ ├── solve_sudoku (Without UI).py │ └── solve_sudoku_UI.py └── main.py ├── Final Review └── Final Review Presentation.pdf ├── README.md ├── Review 1 └── Team 10 Presentation Review 1.pdf ├── Review 2 └── Team 10 Presentation Review 2.pdf └── SnapShots ├── Screenshot (17).png ├── Screenshot (18).png ├── Screenshot (19).png ├── Screenshot (20).png ├── Screenshot (21).png ├── Screenshot (22).png ├── Screenshot (23).png ├── Screenshot (24).png ├── Screenshot (25).png ├── Screenshot (26).png ├── Screenshot (27).png ├── Screenshot (28).png ├── Screenshot (29).png ├── Screenshot (30).png ├── Screenshot (31).png ├── Screenshot (32).png ├── Screenshot (33).png └── Screenshot (34).png /Code/Assets/choose.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Assets/choose.png -------------------------------------------------------------------------------- /Code/Assets/intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Assets/intro.png -------------------------------------------------------------------------------- /Code/Assets/riam_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Assets/riam_img.png -------------------------------------------------------------------------------- /Code/Assets/sudoku_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Assets/sudoku_img.png -------------------------------------------------------------------------------- /Code/Changelog.txt: -------------------------------------------------------------------------------- 1 | Project Name: Main 2 | Last Updated: 02/03/2022 18:45 3 | Last Updated by: Sarthak S Kumar 4 | Changelog: 5 | 2/03/2022 18:45 Sarthak S Kumar 6 | # Code Optimisation 7 | 8 | 1/02/2022 10:13 Sarthak S Kumar 9 | # Updated Window Title 10 | # Updated Background 11 | # Added Exit Button 12 | # Comments and refining 13 | 14 | 28/02/2022 20:29 Sarthak S Kumar 15 | # To link buttons with functions 16 | # Importing RIAM and Sudoku Codes 17 | 18 | ` 27/02/2022 20:25 Sarthak S Kumar 19 | # Added Comments 20 | # Created Intro Frame with Background 21 | # Added Buttons for Sudoku and Rat in a Maze 22 | # Added choose screen with buttons 23 | Pending: 24 | # Scaling issues 25 | ''' -------------------------------------------------------------------------------- /Code/Rat in a Maze/Assets/arrowkeys.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Rat in a Maze/Assets/arrowkeys.png -------------------------------------------------------------------------------- /Code/Rat in a Maze/Assets/intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Rat in a Maze/Assets/intro.png -------------------------------------------------------------------------------- /Code/Rat in a Maze/Assets/maze_ui.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Rat in a Maze/Assets/maze_ui.png -------------------------------------------------------------------------------- /Code/Rat in a Maze/Assets/rat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Rat in a Maze/Assets/rat.png -------------------------------------------------------------------------------- /Code/Rat in a Maze/Assets/user_entry.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Rat in a Maze/Assets/user_entry.png -------------------------------------------------------------------------------- /Code/Rat in a Maze/Changelog.txt: -------------------------------------------------------------------------------- 1 | Project Name: Rat in a maze 2 | Last Updated: 04/03/2022 21:43 3 | Last Updated by: Sarthak S Kumar 4 | Changelog: 5 | 04/03/2022 21:43 Sarthak S Kumar 6 | # Changed the rat object color 7 | # UI Changes 8 | 9 | 1/03/2022 13:40 Sarthak S Kumar 10 | # Bug Fixes 11 | # Added new maze sizes 12 | # Code Optimisation and rearrangement 13 | 14 | 28/02/2022 20:29 Sarthak S Kumar 15 | #Fixed Error while trying to execute from main.py 16 | 17 | ` 27/02/2022 02:17 Sarthak S Kumar 18 | # Fixed Overlapping Text when user solves the maze 19 | # Widget Scaling Corrections 20 | # Fixed Background Images descaling and moving out of frame 21 | 22 | 27/02/2022 22:59 Varun Chandrashekar 23 | # Function to manage maze box sizes and other fixes 24 | # Fixed obstacle boxes appearing after backtracking solution 25 | 26 | 24/02/2022 15:05 Sarthak S Kumar 27 | # Success and Failure Messages 28 | 29 | 24/02/2022 15:10 Varun Chandrashekar 30 | # Scaling Issues resolved 31 | 32 | 23/02/2022 23:12 Sarthak S Kumar 33 | # Functionality to change the color of squares visited previously 34 | # Canvas size updation in maze_ui 35 | # Code Refinement and comments added 36 | 37 | 23/02/2022 20:54 Sarthak S Kumar 38 | # Added Functionality to check whether the user solution is correct or not 39 | # UI updation when user solves the maze, or gives up 40 | # Added Timer functionality 41 | # Code Refinement and Comments 42 | 43 | 22/02/2022 21:47 Sarthak S Kumar 44 | # Added Functionality to let the user solve the maze manually 45 | # Comments and Decluttering 46 | 47 | 21/02/2022 11:40 Sarthak S Kumar 48 | # Added the new try, prompt window (Exit Screen) 49 | # Fixed Username not displaying while using main() 50 | 51 | 08/02/2022 20:40 Sarthak S Kumar 52 | # Added Welcome Screen, User Entry Screen, and Maze UI Screen 53 | # Show the randomly generated maze 54 | # Show the solution for the randomly generated maze 55 | 56 | 08/02/2022 9:10 Vishal M Godi 57 | # Algorithm to solve Rat in a Maze using Backtracking 58 | Pending: 59 | --Cleared -------------------------------------------------------------------------------- /Code/Rat in a Maze/Rat_in_a_maze (Without UI).py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | import time 4 | import copy 5 | print("Welcome!!!\n") 6 | time.sleep(1) 7 | sol_maze = [] 8 | N = int(input("Enter size of maze: ")) 9 | print("\n") 10 | while True: 11 | maze = np.random.randint(2, size=(N, N)) 12 | 13 | def MAZE(maze): 14 | global sol 15 | sol = copy.deepcopy(maze) 16 | 17 | if BACKTRACKING_ALGORITHM(maze, 0, 0, sol) == False: 18 | return False 19 | 20 | return True 21 | 22 | def BACKTRACKING_ALGORITHM(maze, x, y, sol): 23 | 24 | if x == N-1 and y == N-1: 25 | sol[x][y] = 5 26 | return True 27 | 28 | if check(maze, x, y) == True: 29 | sol[x][y] = 5 30 | 31 | if BACKTRACKING_ALGORITHM(maze, x+1, y, sol) == True: 32 | return True 33 | 34 | if BACKTRACKING_ALGORITHM(maze, x, y+1, sol) == True: 35 | return True 36 | 37 | sol[x][y] = 0 38 | return False 39 | 40 | def check(maze, x, y): 41 | if x >= 0 and x < N and y >= 0 and y < N and maze[x][y] == 1: 42 | return True 43 | 44 | if MAZE(maze) == False or maze[N-1][N-1] == 0 or maze[0][0] == 0: 45 | continue 46 | 47 | time.sleep(1) 48 | for i in maze: 49 | for j in i: 50 | print(str(j), end=" ") 51 | print() 52 | time.sleep(1) 53 | print("\nThis is the maze to be solved") 54 | break 55 | 56 | 57 | time.sleep(1) 58 | ch = input(("\nEnter y to get solution: ")) 59 | print('\n') 60 | if ch == 'y' or ch == 'Y': 61 | for i in sol: 62 | for j in i: 63 | print(str(j), end=" ") 64 | print() 65 | print("\nThis is the solution maze") 66 | -------------------------------------------------------------------------------- /Code/Rat in a Maze/Rat_in_a_maze_UI.py: -------------------------------------------------------------------------------- 1 | """ 2 | Project Name: Rat in a maze 3 | Last Updated: 04/03/2022 21:43 4 | Last Updated by: Sarthak S Kumar 5 | """ 6 | # Modules 7 | import numpy as np 8 | import random 9 | import time 10 | import copy 11 | import datetime 12 | from tkinter import * 13 | from tkinter import ttk 14 | from PIL import Image 15 | from PIL import ImageTk 16 | from ctypes import windll 17 | 18 | # Variables 19 | first_puzzle = True 20 | white = "#ffffff" 21 | black = "#000000" 22 | purple = "#4d1354" 23 | font_face = "HK Grotesk" 24 | 25 | # Functions 26 | windll.shcore.SetProcessDpiAwareness(1) # To Enable High Resolution Scaling in Windows OS 27 | 28 | 29 | def manage_box_size(height, width, N): # Set the size of boxes according to the resolution and maze size 30 | squaresize = int((((height*width)/(5))/(N**2))**0.5) 31 | return squaresize 32 | 33 | 34 | def algo_main(): # Backtracking Algorithm for Rat in a Maze 35 | sol_maze = [] 36 | while True: 37 | global maze 38 | maze = np.random.randint(2, size=(N, N)) 39 | 40 | def MAZE(maze): 41 | global sol 42 | sol = copy.deepcopy(maze) 43 | 44 | if BACKTRACKING_ALGORITHM(maze, 0, 0, sol) == False: 45 | return False 46 | 47 | return True 48 | 49 | def BACKTRACKING_ALGORITHM(maze, x, y, sol): 50 | 51 | if x == N - 1 and y == N - 1: 52 | sol[x][y] = 5 53 | return True 54 | 55 | if check(maze, x, y) == True: 56 | sol[x][y] = 5 57 | 58 | if BACKTRACKING_ALGORITHM(maze, x + 1, y, sol) == True: 59 | return True 60 | 61 | if BACKTRACKING_ALGORITHM(maze, x, y + 1, sol) == True: 62 | return True 63 | 64 | sol[x][y] = 1 65 | return False 66 | 67 | def check(maze, x, y): 68 | if x >= 0 and x < N and y >= 0 and y < N and maze[x][y] == 1: 69 | return True 70 | 71 | if MAZE(maze) == False or maze[N - 1][N - 1] == 0 or maze[0][0] == 0: 72 | continue 73 | break 74 | 75 | 76 | def riam_main(): # Program execution begins from here. 77 | """Tkinter Window Initialisation""" 78 | 79 | master = Toplevel() # master = Tk() if main window 80 | master.title("Rat in a Maze") 81 | # master.attributes('-fullscreen', True) #To enable fullscreen mode 82 | 83 | # Get the resolution of working monitor 84 | height = master.winfo_screenheight() 85 | width = master.winfo_screenwidth() 86 | resolution = str(width) + "x" + str(height) 87 | 88 | master.geometry(resolution) 89 | master.configure(bg=white) 90 | 91 | if first_puzzle: 92 | """ Welcome Screen """ 93 | intro = Frame(master) 94 | intro.place(anchor="nw") 95 | 96 | bg = PhotoImage(file=r"Code\Rat in a Maze\Assets\intro.png") 97 | canvas1 = Canvas(intro, height=height, width=width) 98 | canvas1.pack() 99 | canvas1.create_image(0, 0, image=bg, anchor="nw") 100 | 101 | intro.after(3000, intro.destroy) 102 | intro.wait_window(intro) 103 | 104 | """User Entry Screen""" 105 | user_entry = Frame(master, background=white) 106 | user_entry.pack() 107 | 108 | bg = PhotoImage(file=r"Code\Rat in a Maze\Assets\user_entry.png") 109 | canvas1 = Canvas(user_entry, height=height, width=width) 110 | canvas1.pack() 111 | canvas1.create_image(0, 0, image=bg, anchor="nw") 112 | 113 | Label(user_entry, text="Welcome", font=(font_face, 50), fg=black, bg=white).place(anchor='center', x=(960/1920)*width + (2*width)/10, y=(275/1080)*height) 114 | Label(user_entry, text="Your Name: ", font=(font_face, 25), fg=black, bg=white).place(anchor='e', x=(800/1920)*width + (2*width)/10, y=(450/1080)*height) 115 | Label(user_entry, text="Maze Size: ", font=(font_face, 25), fg=black, bg=white).place(anchor='e', x=(800/1920)*width + (2*width)/10, y=(540/1080)*height) 116 | 117 | sizebox = Entry(user_entry, font=(font_face, 20), fg=black, bg=white) 118 | sizebox.place(anchor='w', x=(960/1920)*width + (2*width)/10, y=(450/1080)*height) 119 | 120 | clicked = StringVar() 121 | clicked.set("Select one") 122 | 123 | gridsizes = ["5 x 5", "10 x 10", "15 x 15", "20 x 20", "25 x 25", "30 x 30"] 124 | drop = OptionMenu(user_entry, clicked, *gridsizes) 125 | drop.place(anchor='w', x=(960/1920)*width + (2*width)/10, y=(540/1080)*height) 126 | drop.config(font=(font_face, (20)), bg=white, fg=black) 127 | 128 | def movenext(): 129 | global N, username, x 130 | username = sizebox.get() # Maze Size Variable 131 | x = clicked.get().split() 132 | N = int(x[0]) 133 | user_entry.after(1000, user_entry.destroy) 134 | 135 | submit = Button(user_entry, text="Submit", command=movenext, bg=purple, font=(font_face, (25)), fg="white").place(anchor='center', x=(960/1920)*width + (2*width)/10, y=(800/1920)*width) 136 | 137 | user_entry.wait_window(user_entry) 138 | 139 | algo_main() # Algorithm Begins 140 | 141 | """Maze UI""" 142 | squaresize = manage_box_size(height, width, N) # Dynamically changes with the maze size 143 | tile_color = " " 144 | rectbox_coordinates = [0, 0, squaresize, squaresize] 145 | forbidden = [] # To store co-ordinates of obstacle boxes in maze grid 146 | 147 | maze_UI = Frame(master, background=white) 148 | maze_UI.pack() 149 | 150 | bg2 = PhotoImage(file=r"Code\Rat in a Maze\Assets\maze_ui.png") # ./Assets/maze_ui.png (Mac) 151 | canvas2 = Canvas(maze_UI, height=height, width=width) 152 | canvas2.pack() 153 | canvas2.create_image(0, 0, image=bg2, anchor="nw") 154 | 155 | headline = Label(maze_UI, text=f"Hello {username}\n Your {tile_color.join(x)} maze is here. Solve it", font=(font_face, 30), fg=black, bg=white) 156 | headline.place(anchor='center', x=(1390/1920)*width, y=(375/1080)*height) 157 | 158 | # Canvas to display Maze to be solved 159 | question_canvas = Canvas(maze_UI, height=(N * squaresize)-squaresize/10, width=(N * squaresize)-squaresize/10, bg=white) 160 | question_canvas.place(anchor='center', x=(500/1920)*width, y=(512/1080)*height) 161 | 162 | # Drawing the maze to be solved in the question canvas 163 | for i in maze: 164 | for j in i: 165 | if j == 1: 166 | color = "white" 167 | else: 168 | color = "black" 169 | forbidden.append(tuple([rectbox_coordinates[i] for i in range(4)])) 170 | 171 | square = question_canvas.create_rectangle(rectbox_coordinates[0], rectbox_coordinates[1], rectbox_coordinates[2], rectbox_coordinates[3], fill=color, width=0) 172 | rectbox_coordinates[0] += squaresize 173 | rectbox_coordinates[2] += squaresize 174 | 175 | rectbox_coordinates[0], rectbox_coordinates[2] = 0, squaresize 176 | rectbox_coordinates[1] += squaresize 177 | rectbox_coordinates[3] += squaresize 178 | 179 | # Rat Object to Traverse through the maze 180 | rat = question_canvas.create_oval(0, 0, squaresize, squaresize, fill="#bd174e", width=0) 181 | 182 | endpoint = question_canvas.create_rectangle(squaresize*(N-1), squaresize*(N-1), squaresize*N, squaresize * N, fill="#660033", outline="#660033") 183 | 184 | use = Label(maze_UI, text="Use", font=(font_face, 20), fg=black, bg=white) 185 | use.place(anchor='e', x=((height+width)/2 - (width/7)), y=(540/1080)*height) 186 | 187 | img = Image.open(r"Code\Rat in a Maze\Assets\arrowkeys.png") # ./Assets/arrowkeys.png (Mac) 188 | img = img.resize((int((height*width)/(4*(height+width))), int((height*width)/(4*(height+width)))), Image.ANTIALIAS) 189 | photoImg = ImageTk.PhotoImage(img) 190 | 191 | arrow = Canvas(maze_UI, width=(height*width)/(4*(height+width)), height=(height*width)/(4*(height+width)), bg="white", bd=0, highlightthickness=0, relief='ridge') 192 | arrow.place(x=(height+width)/2 - (width/22), y=(540/1080)*height, anchor='e') 193 | arrow.create_image(0, 0, image=photoImg, anchor='nw') 194 | 195 | labe = Label(maze_UI, text="to move the rat", font=(font_face, 20), fg=black, bg=white) 196 | labe.place(anchor='w', x=(height+width)/2 - (width/25), y=(540/1080)*height) 197 | 198 | visited_squares = [] # List to stores the squares in which the rat has been before 199 | 200 | def pathsquare(coord): # To change color of the path traversed accordingly 201 | split_coords = [coord[0], coord[1], coord[2], coord[3]] 202 | if split_coords not in visited_squares: 203 | visited_squares.append(split_coords) 204 | question_canvas.create_rectangle(coord[0], coord[1], coord[2], coord[3], fill="purple", width=0) 205 | question_canvas.tag_raise(rat) 206 | else: 207 | question_canvas.create_rectangle(coord[0], coord[1], coord[2], coord[3], fill="orange", width=0) 208 | question_canvas.tag_raise(rat) 209 | 210 | def nextstep(): # When Next button is clicked 211 | Next = Tk() 212 | Next.geometry(str(int(1500/1920*width)) + "x" + str(int(300/1080*height))) 213 | Next.title("Solving the Maze") 214 | Next.configure(bg=white, border=1) 215 | 216 | message = Label(Next, text=f"Wanna solve another maze?", font=( 217 | font_face, 30), fg=black, bg=white) 218 | message.place(anchor='center', x=(750/1080)*height, y=(100/1080)*height) 219 | 220 | def restart(): # Restart the program 221 | global first_puzzle 222 | Next.destroy() 223 | master.destroy() 224 | first_puzzle = False 225 | riam_main() 226 | 227 | yes = Button(Next, text="Yeah", command=restart, bg=purple, font=(font_face, (20)), fg="white") 228 | yes.place(anchor='center', x=(550/1080)*height, y=(200/1080)*height) 229 | no = Button(Next, text="Nah", command=exit, bg=purple, font=(font_face, (20)), fg="white") 230 | no.place(anchor='center', x=(950/1080)*height, y=(200/1080)*height) 231 | 232 | def user_solved(): # When user manages to solve the maze 233 | 234 | use.destroy() 235 | arrow.destroy() 236 | labe.destroy() 237 | headline.destroy() 238 | comp_solve.destroy() 239 | 240 | # To check the number of seconds elapsed after game started 241 | endtime = datetime.datetime.now() 242 | td = endtime - starttime 243 | elapsed = td.total_seconds() 244 | successmessages = ["Yay! You made it!", "Good one fella!", "That was quite easy!", "Wonderful!", "You aced it!"] 245 | Label(maze_UI, text="🎉 Congratulations 🎉", font=(font_face, 40), fg=black, bg=white).place(anchor='center', x=(1390/1920)*width, y=(375/1080)*height) 246 | Label(maze_UI, text=random.choice(successmessages), font=(font_face, 30), fg=black, bg=white).place(anchor='center', x=(1390/1920)*width, y=(455/1080)*height) 247 | Label(maze_UI, text=f"You solved the maze in {round(elapsed, 2)} seconds", font=(font_face, 20), fg=black, bg=white).place(anchor='center', x=(1390/1920)*width, y=(520/1080)*height) 248 | 249 | # Displays Confirmation Window on clicking next 250 | nextb = Button(maze_UI, text="Next", command=nextstep, bg=purple, font=(font_face, (20)), fg="white") 251 | nextb.place(anchor='center', x=(1390/1920)*width, y=(600/1080)*height) 252 | 253 | # Co-ordinates of end point in the maze 254 | end_coordinates = [squaresize*(N-1), squaresize*(N-1), squaresize*N, squaresize * N] 255 | 256 | # Keybind Events 257 | def left(event): 258 | rat_xy = question_canvas.coords(rat) 259 | if rat_xy[0] > 0: 260 | x = -squaresize 261 | y = 0 262 | if (rat_xy[0] + x, rat_xy[1], rat_xy[2] + x, rat_xy[3]) not in forbidden: 263 | question_canvas.move(rat, x, y) 264 | pathsquare(rat_xy) 265 | if question_canvas.coords(rat) == end_coordinates: 266 | user_solved() 267 | 268 | def right(event): 269 | rat_xy = question_canvas.coords(rat) 270 | if rat_xy[2] <= N * squaresize - 1: 271 | x = squaresize 272 | y = 0 273 | if (rat_xy[0] + x, rat_xy[1], rat_xy[2] + x, rat_xy[3]) not in forbidden: 274 | question_canvas.move(rat, x, y) 275 | pathsquare(rat_xy) 276 | if question_canvas.coords(rat) == end_coordinates: 277 | user_solved() 278 | 279 | def up(event): 280 | rat_xy = question_canvas.coords(rat) 281 | if rat_xy[1] > 0: 282 | x = 0 283 | y = -squaresize 284 | if (rat_xy[0], rat_xy[1] + y, rat_xy[2], rat_xy[3] + y) not in forbidden: 285 | question_canvas.move(rat, x, y) 286 | pathsquare(rat_xy) 287 | if question_canvas.coords(rat) == end_coordinates: 288 | user_solved() 289 | 290 | def down(event): 291 | rat_xy = question_canvas.coords(rat) 292 | if rat_xy[3] <= N * squaresize - 1: 293 | x = 0 294 | y = squaresize 295 | if (rat_xy[0], rat_xy[1] + y, rat_xy[2], rat_xy[3] + y) not in forbidden: 296 | question_canvas.move(rat, x, y) 297 | pathsquare(rat_xy) 298 | if question_canvas.coords(rat) == end_coordinates: 299 | user_solved() 300 | 301 | master.bind("", left) 302 | master.bind("", right) 303 | master.bind("", up) 304 | master.bind("", down) 305 | 306 | global starttime, endtime 307 | starttime = datetime.datetime.now() 308 | 309 | # When user gives up on solving the maze 310 | def solve(): 311 | question_canvas.destroy() 312 | 313 | # To display the maze solution 314 | solution_canvas = Canvas(maze_UI, height=(N * squaresize)-squaresize/10, width=(N * squaresize)-squaresize/10, bg=white) 315 | solution_canvas.place(anchor='center', x=(500/1920)*width, y=(512/1080)*height) 316 | 317 | rectbox_coordinates = [0, 0, squaresize, squaresize] 318 | for i in sol: 319 | for j in i: 320 | if j == 5: 321 | color = "green" 322 | elif j == 1: 323 | color = white 324 | else: 325 | color = black 326 | square = solution_canvas.create_rectangle(rectbox_coordinates[0], rectbox_coordinates[1], rectbox_coordinates[2], rectbox_coordinates[3], fill=color, width=0) 327 | rectbox_coordinates[0] += squaresize 328 | rectbox_coordinates[2] += squaresize 329 | 330 | rectbox_coordinates[0], rectbox_coordinates[2] = 0, squaresize 331 | rectbox_coordinates[1] += squaresize 332 | rectbox_coordinates[3] += squaresize 333 | 334 | use.destroy() 335 | arrow.destroy() 336 | labe.destroy() 337 | headline.destroy() 338 | comp_solve.destroy() 339 | 340 | failmessages = [r"❌ You gave up so quick!", r"🧠 Was it really tough?", r"😂 Help yourself!", "👎 You couldn't make it!"] 341 | Label(maze_UI, text=random.choice(failmessages), font=(font_face, 40), fg=black, bg=white).place(anchor='center', x=(1390/1920)*width, y=(375/1080)*height) 342 | Label(maze_UI, text=f"Better luck next time!", font=(font_face, 20), fg=black, bg=white).place(anchor='center', x=(1390/1920)*width, y=(475/1080)*height) 343 | 344 | # Displays Confirmation Window on clicking next 345 | nextb = Button(maze_UI, text="Next", command=nextstep, 346 | bg=purple, font=(font_face, (20)), fg="white") 347 | nextb.place(anchor='center', x=(1390/1920)*width, y=(600/1080)*height) 348 | 349 | # Button to let computer display the solution 350 | comp_solve = Button(maze_UI, text="Give Up!", command=solve, 351 | bg=purple, font=(font_face, (20)), fg="white") 352 | comp_solve.place(anchor='center', x=(1390/1920)*width, y=(800/1080)*height) 353 | 354 | mainloop() 355 | 356 | 357 | # For Standalone 358 | '''if __name__ == "__main__": 359 | riam_main()''' 360 | -------------------------------------------------------------------------------- /Code/Sudoku/Assets/arrowkeys.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Sudoku/Assets/arrowkeys.png -------------------------------------------------------------------------------- /Code/Sudoku/Assets/intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Sudoku/Assets/intro.png -------------------------------------------------------------------------------- /Code/Sudoku/Assets/numpad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Sudoku/Assets/numpad.png -------------------------------------------------------------------------------- /Code/Sudoku/Assets/sudoku_UI.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Sudoku/Assets/sudoku_UI.png -------------------------------------------------------------------------------- /Code/Sudoku/Assets/user_entry.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Sudoku/Assets/user_entry.png -------------------------------------------------------------------------------- /Code/Sudoku/Changelog.txt: -------------------------------------------------------------------------------- 1 | Project Name: solve_sudoku 2 | Last Updated: 13/03/2022 10:10 3 | Last Updated by: Sarthak S Kumar 4 | 5 | Changelog: 6 | 13/03/2022 10:10 Sarthak S Kumar 7 | # Show solution in different color 8 | # Code Optimisation 9 | 10 | 04/03/2022 19:42 Sarthak S Kumar 11 | # Added Difficulty Levels 12 | # UI changes 13 | 14 | 03/03/2022 15:37 Sarthak S Kumar 15 | # Removed generate_random.py file 16 | # Minor changes 17 | 18 | 02/03/2022 18:33 Sarthak S Kumar 19 | # Changed Window Title 20 | # Code Optimisation 21 | 22 | 28/02/2022 20:29 Sarthak S Kumar 23 | #Fixed Error while trying to execute from main.py 24 | 25 | 27/02/2022 15:29 Sarthak S Kumar 26 | # Fixed 'Nah' Button Not working in next_UI 27 | # Fixed Numbers overlaying above existing numbers in Sudoku_UI 28 | # Added play info in sudoku_UI screen 29 | # Added chances available functionality 30 | 31 | 26/02/2022 20:25 Sarthak S Kumar 32 | # Success and Failure Messages 33 | # Solved text overlapping during user entry in question_canvas 34 | # Added key bindings 35 | # Resolved 'Nah' Button not working in next_UI 36 | # Code Refinement and Comments 37 | 38 | 26/02/2022 13:17 Varun Chandrashekar 39 | # Resolved Copy bug, while trying to store and access puzzle question 40 | 41 | 25/02/2022 21:19 Sarthak S Kumar 42 | # Added the new try, prompt window (Exit Screen) 43 | # Resolved Username not displaying while using main() 44 | # Added Functionality to let the user solve the puzzle manually 45 | # UI updation when user solves the puzzle, or gives up 46 | # Canvas size updation in sudoku_ui 47 | 48 | 24/02/2022 22:59 Sarthak S Kumar 49 | # Added Welcome Screen, User Entry Screen, and Sudoku UI Screen 50 | # Show the randomly generated sudoku puzzle 51 | # Added Navigator/Selector functionality 52 | 53 | 24/02/2022 18:47 Varun Chandrashekar 54 | # Algorithm to solve sudoku puzzles using Backtracking 55 | # Command line running of the program with output 56 | Pending: 57 | # Scaling Issues -------------------------------------------------------------------------------- /Code/Sudoku/__pycache__/solve_sudoku_UI.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Code/Sudoku/__pycache__/solve_sudoku_UI.cpython-310.pyc -------------------------------------------------------------------------------- /Code/Sudoku/solve_sudoku (Without UI).py: -------------------------------------------------------------------------------- 1 | import math 2 | from generate_random import generate 3 | 4 | # Split the 9x9 puzzle into the 3x3 squares 5 | 6 | 7 | def make_small_boxes(current_board): 8 | 9 | small_boxes = { 10 | "11": [], 11 | "12": [], 12 | "13": [], 13 | "21": [], 14 | "22": [], 15 | "23": [], 16 | "31": [], 17 | "32": [], 18 | "33": [], 19 | } 20 | 21 | for row in range(1, 10): 22 | arg1 = math.ceil(row/3) 23 | 24 | for column in range(1, 10): 25 | 26 | arg2 = math.ceil(column/3) 27 | args = str(arg1) + str(arg2) 28 | small_boxes[args].append(current_board[row-1][column-1]) 29 | 30 | return small_boxes 31 | 32 | # Get empty position in the puzzle 33 | 34 | 35 | def empty(board): 36 | for row in range(len(board)): 37 | for column in range(len(board[row])): 38 | if(board[row][column] == 0): 39 | return(row, column) 40 | 41 | return None 42 | 43 | # Check whether board is valid if number is inserted in given position 44 | 45 | 46 | def is_position_valid(board, small_boxes, number, position): 47 | 48 | for i in range(9): 49 | if((i != position[1]) and (number == board[position[0]][i])): # Check in the same row 50 | return "r" 51 | elif((i != position[0]) and (number == board[i][position[1]])): # Check in the same column 52 | return "c" 53 | 54 | args = str(math.floor(position[0]/3)+1)+str(math.floor(position[1]/3)+1) 55 | 56 | if(number in small_boxes[args]): # Check in the small 3x3 boxes 57 | return "b" 58 | 59 | return "" 60 | 61 | # Solving the 62 | 63 | 64 | def solve(puzzle, small_boxes): 65 | 66 | print("/"*100) 67 | display_board(puzzle) 68 | empty_position = empty(puzzle) 69 | 70 | if not empty_position: 71 | return True # Return if there is no empty position 72 | else: 73 | row, column = empty_position 74 | 75 | for i in range(1, 10): 76 | 77 | if(not is_position_valid(puzzle, small_boxes, i, (row, column))): # If a valid number is found put it in the puzzle 78 | puzzle[row][column] = i 79 | small_boxes = make_small_boxes(puzzle) 80 | 81 | if solve(puzzle, small_boxes): 82 | return True 83 | 84 | puzzle[row][column] = 0 # Backtrack 85 | 86 | return False 87 | 88 | 89 | def start(puzzle, original): 90 | 91 | display_board(puzzle) 92 | print("\n\nPress 'y' to solve the puzzle, or try to solve it yourself!\n\nPress any key to start solving\n") 93 | 94 | 95 | def display_board(board): 96 | for row in board: 97 | print("—"*38) 98 | for element in row: 99 | if element == 0: 100 | element = "◼" 101 | print(" | " + str(element), end="") 102 | print(" |") 103 | 104 | 105 | if __name__ == '__main__': 106 | 107 | problem = generate(1, make_small_boxes) 108 | print(problem) 109 | '''solve(problem, make_small_boxes(problem)) 110 | display_board(problem)''' 111 | -------------------------------------------------------------------------------- /Code/Sudoku/solve_sudoku_UI.py: -------------------------------------------------------------------------------- 1 | """ 2 | Project Name: solve_sudoku 3 | Last Updated: 13/03/2022 10:10 4 | Last Updated by: Sarthak S Kumar 5 | """ 6 | # Modules 7 | import copy 8 | import math 9 | import random 10 | import datetime 11 | from tkinter import * 12 | from tkinter import ttk 13 | from PIL import Image 14 | from PIL import ImageTk 15 | from ctypes import windll 16 | 17 | # Variables 18 | first_puzzle = True 19 | white = "#ffffff" 20 | black = "#000000" 21 | purple = "#4d1354" 22 | font_face = "HK Grotesk" 23 | 24 | # Functions 25 | windll.shcore.SetProcessDpiAwareness(1) # To Enable High Resolution Scaling in Windows OS 26 | 27 | 28 | # Sudoku Global Functions 29 | def generate(difficulty, make_small_boxes): 30 | 31 | initial = [ 32 | [9, 5, 7, 6, 1, 3, 2, 8, 4], 33 | [4, 8, 3, 2, 5, 7, 1, 9, 6], 34 | [6, 1, 2, 8, 4, 9, 5, 3, 7], 35 | [1, 7, 8, 3, 6, 4, 9, 5, 2], 36 | [5, 2, 4, 9, 7, 1, 3, 6, 8], 37 | [3, 6, 9, 5, 2, 8, 7, 4, 1], 38 | [8, 4, 5, 7, 9, 2, 6, 1, 3], 39 | [2, 9, 1, 4, 3, 6, 8, 7, 5], 40 | [7, 3, 6, 1, 8, 5, 4, 2, 9] 41 | ] 42 | 43 | shuffles = random.randint(5, 11) 44 | 45 | # shuffling columns and rows 46 | for i in range(shuffles): 47 | 48 | indexs = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] 49 | 50 | for index in indexs: 51 | indices = random.sample(index, 2) 52 | 53 | for row in initial: 54 | row[indices[0]], row[indices[1] 55 | ] = row[indices[1]], row[indices[0]] 56 | initial[indices[0]], initial[indices[1] 57 | ] = initial[indices[1]], initial[indices[0]] 58 | 59 | if(difficulty == 1): 60 | probability = (0.6, 0.4) 61 | elif(difficulty == 2): 62 | probability = (0.5, 0.5) 63 | elif(difficulty == 3): 64 | probability = (0.4, 0.6) 65 | 66 | for row in initial: 67 | for pos in range(0, 9): 68 | if random.choices([True, False], weights=probability)[0]: 69 | row[pos] = 0 70 | 71 | return initial 72 | 73 | 74 | def make_small_boxes(current_board): 75 | 76 | small_boxes = {"11": [], "12": [], "13": [], "21": [], "22": [], "23": [], "31": [], "32": [], "33": []} 77 | 78 | for row in range(1, 10): 79 | arg1 = math.ceil(row/3) 80 | 81 | for column in range(1, 10): 82 | 83 | arg2 = math.ceil(column/3) 84 | args = str(arg1) + str(arg2) 85 | small_boxes[args].append(current_board[row-1][column-1]) 86 | 87 | return small_boxes 88 | 89 | 90 | def empty(board): # Get empty position in the puzzle 91 | for row in range(len(board)): 92 | for column in range(len(board[row])): 93 | if(board[row][column] == 0): 94 | return(row, column) 95 | 96 | return None 97 | 98 | 99 | def is_position_valid(board, small_boxes, number, position): # Check whether board is valid if number is inserted in given position 100 | 101 | for i in range(9): 102 | if((i != position[1]) and (number == board[position[0]][i])): # Check in the same row 103 | return "r" 104 | elif((i != position[0]) and (number == board[i][position[1]])): # Check in the same column 105 | return "c" 106 | 107 | args = str(math.floor(position[0]/3)+1)+str(math.floor(position[1]/3)+1) 108 | 109 | if(number in small_boxes[args]): # Check in the small 3x3 boxes 110 | return "b" 111 | 112 | return "" 113 | 114 | 115 | def csolve(puzzle, small_boxes): # Solving the puzzle 116 | 117 | empty_position = empty(puzzle) 118 | 119 | if not empty_position: 120 | return True # Return if there is no empty position 121 | else: 122 | row, column = empty_position 123 | 124 | for i in range(1, 10): 125 | 126 | if(not is_position_valid(puzzle, small_boxes, i, (row, column))): # If a valid number is found put it in the puzzle 127 | puzzle[row][column] = i 128 | small_boxes = make_small_boxes(puzzle) 129 | 130 | if csolve(puzzle, small_boxes): 131 | return True 132 | 133 | puzzle[row][column] = 0 # Backtrack 134 | 135 | return False 136 | 137 | 138 | first_puzzle = True 139 | 140 | 141 | def sudoku_main(): # Program execution begins from here. 142 | """Tkinter Window Initialisation""" 143 | 144 | master = Toplevel() # master = Tk() if main window 145 | master.title("Solve Sudoku") 146 | # master.attributes('-fullscreen', True) #To enable fullscreen mode 147 | 148 | master.geometry("1920x1080") 149 | master.configure(bg=purple) 150 | 151 | if first_puzzle: 152 | """ Welcome Screen """ 153 | intro = Frame(master) 154 | intro.place(anchor="nw") 155 | 156 | bg = PhotoImage(file=r"Code\Sudoku\Assets\intro.png") 157 | canvas1 = Canvas(intro, height=1080, width=2000) 158 | canvas1.pack() 159 | canvas1.create_image(0, 0, image=bg, anchor="nw") 160 | 161 | intro.after(3000, intro.destroy) 162 | intro.wait_window(intro) 163 | 164 | """User Entry Screen""" 165 | user_entry = Frame(master, background=purple) 166 | user_entry.pack() 167 | 168 | bg = PhotoImage(file=r"Code\Sudoku\Assets\user_entry.png") 169 | canvas1 = Canvas(user_entry, height=1080, width=2000) 170 | canvas1.pack() 171 | canvas1.create_image(0, 0, image=bg, anchor="nw") 172 | 173 | Label(user_entry, text="Welcome", font=(font_face, 50), fg=white, bg=purple).place(anchor='center', x=960, y=275) 174 | 175 | Label(user_entry, text="Your Name: ", font=(font_face, 25), fg=white, bg=purple).place(anchor='e', x=800, y=450) 176 | Label(user_entry, text="Difficulty: ", font=(font_face, 25), fg=white, bg=purple).place(anchor='e', x=800, y=540) 177 | 178 | sizebox = Entry(user_entry, font=(font_face, 20), fg=white, bg=purple) 179 | sizebox.place(anchor='w', x=960, y=450) 180 | 181 | clicked = StringVar() 182 | clicked.set("Select one") 183 | 184 | difficulties = ["Easy", "Intermediate", "Advanced"] 185 | drop = OptionMenu(user_entry, clicked, *difficulties) 186 | drop.place(anchor='w', x=960, y=540) 187 | drop.config(font=(font_face, (20)), bg=purple, fg=white) 188 | 189 | def movenext(): 190 | global username, x 191 | 192 | x = clicked.get() 193 | if x == "Easy": 194 | x = 3 195 | elif x == "Intermediate": 196 | x = 2 197 | elif x == "Advanced": 198 | x = 1 199 | else: 200 | raise Exception("Error in getting difficulty level") 201 | username = sizebox.get() 202 | user_entry.after(1000, user_entry.destroy) 203 | 204 | submit = Button(user_entry, text="Submit", command=movenext, bg=purple, font=(font_face, (25)), fg=white, highlightcolor=white, highlightbackground=purple).place(anchor="center", x=960, y=800) 205 | user_entry.wait_window(user_entry) 206 | 207 | """Sudoku UI""" 208 | sudoku_UI = Frame(master, background=purple) 209 | sudoku_UI.pack() 210 | 211 | bg2 = PhotoImage(file=r"Code\Sudoku\Assets\sudoku_UI.png") 212 | canvas2 = Canvas(sudoku_UI, height=1080, width=2000) 213 | canvas2.pack() 214 | canvas2.create_image(0, 0, image=bg2, anchor="nw") 215 | 216 | headline = Label(sudoku_UI, text=f"Hello {username}\n Your sudoku is here. Go ahead, Solve it", font=(font_face, 30), fg=white, bg=purple) 217 | headline.place(anchor='center', x=1390, y=375) 218 | 219 | use = Label(sudoku_UI, text="Use" + " "*16 + "to enter numbers", font=(font_face, 20), fg=white, bg=purple) 220 | use.place(anchor='center', x=1395, y=540) 221 | 222 | img = Image.open(r"Code\Sudoku\Assets\numpad.png") # ./Assets/arrowkeys.png (Mac) 223 | img = img.resize((100, 100), Image.ANTIALIAS) 224 | photoImg = ImageTk.PhotoImage(img) 225 | 226 | numpad = Canvas(sudoku_UI, width=100, height=100, bg=purple, bd=0, highlightthickness=0, relief='ridge') 227 | numpad.place(x=1295, y=540, anchor='center') 228 | numpad.create_image(0, 0, image=photoImg, anchor='nw') 229 | 230 | use2 = Label(sudoku_UI, text="and" + " "*16 + "to move the selector", font=(font_face, 20), fg=white, bg=purple) 231 | use2.place(anchor='center', x=1390, y=640) 232 | 233 | img2 = Image.open(r"Code\Sudoku\Assets\arrowkeys.png") # ./Assets/arrowkeys.png (Mac) 234 | img2 = img2.resize((100, 100), Image.ANTIALIAS) 235 | photoImg2 = ImageTk.PhotoImage(img2) 236 | 237 | arrow = Canvas(sudoku_UI, width=100, height=100, bg=purple, bd=0, highlightthickness=0, relief='ridge') 238 | arrow.place(x=1270, y=640, anchor='center') 239 | arrow.create_image(0, 0, image=photoImg2, anchor='nw') 240 | 241 | # Canvas to display sudoku puzzle 242 | question_canvas = Canvas(sudoku_UI, height=720, width=720, bg=purple, bd=0, highlightthickness=0, relief='ridge') 243 | question_canvas.place(anchor='center', x=500, y=512) 244 | 245 | for i in (0, 3, 6, 9): # To draw lines 246 | question_canvas.create_line(0, 80*i, 730, 80*i, width=5, fill=white) 247 | question_canvas.create_line(80*i, 0, 80*i, 730, width=5, fill=white) 248 | 249 | for i in range(11): 250 | question_canvas.create_line(80*i, 0, 80*i, 730, width=2, fill=white) 251 | question_canvas.create_line(0, 80*i, 730, 80*i, width=2, fill=white) 252 | 253 | problem = generate(x, make_small_boxes) # Generates random valid sudoku puzzle 254 | puzzle = copy.deepcopy(problem) 255 | 256 | y_coord, x_coord, p, q = 40, 40, 0, 0 257 | allowed_squares = [] 258 | for i in problem: # adding numbers from question puzzle 259 | for j in i: 260 | if j == 0: 261 | j = "" 262 | allowed_squares.append((x_coord-40, y_coord-40, x_coord+40, y_coord+40)) 263 | question_canvas.create_text(x_coord, y_coord, font=(font_face, 30), fill=white, text=j) 264 | x_coord += 80 265 | p += 1 266 | p = 0 267 | x_coord = 40 268 | y_coord += 80 269 | q += 1 270 | 271 | pos = allowed_squares[1] 272 | selector = question_canvas.create_rectangle(pos[0], pos[1], pos[2], pos[3], fill=purple, width=3, outline="#35668f") 273 | 274 | # KeyBind Events 275 | def left(event): 276 | selector_xy = question_canvas.coords(selector) 277 | if selector_xy[0] > 0: 278 | x = -80 279 | y = 0 280 | question_canvas.move(selector, x, y) 281 | if (selector_xy[0] + x, selector_xy[1], selector_xy[2] + x, selector_xy[3]) in allowed_squares: 282 | question_canvas.tag_raise(selector) 283 | else: 284 | question_canvas.tag_lower(selector) 285 | 286 | def right(event): 287 | selector_xy = question_canvas.coords(selector) 288 | if selector_xy[2] <= 719: 289 | x = 80 290 | y = 0 291 | question_canvas.move(selector, x, y) 292 | if (selector_xy[0] + x, selector_xy[1], selector_xy[2] + x, selector_xy[3]) in allowed_squares: 293 | question_canvas.tag_raise(selector) 294 | else: 295 | question_canvas.tag_lower(selector) 296 | 297 | def up(event): 298 | selector_xy = question_canvas.coords(selector) 299 | if selector_xy[1] > 0: 300 | x = 0 301 | y = -80 302 | question_canvas.move(selector, x, y) 303 | if (selector_xy[0], selector_xy[1] + y, selector_xy[2], selector_xy[3] + y) in allowed_squares: 304 | question_canvas.tag_raise(selector) 305 | else: 306 | question_canvas.tag_lower(selector) 307 | 308 | def down(event): 309 | selector_xy = question_canvas.coords(selector) 310 | if selector_xy[3] <= 719: 311 | x = 0 312 | y = 80 313 | question_canvas.move(selector, x, y) 314 | if (selector_xy[0], selector_xy[1] + y, selector_xy[2], selector_xy[3] + y) in allowed_squares: 315 | question_canvas.tag_raise(selector) 316 | else: 317 | question_canvas.tag_lower(selector) 318 | 319 | usersol = copy.deepcopy(problem) # To store a copy of the question puzzle 320 | 321 | csolve(problem, make_small_boxes(problem)) # Solves the sudoku puzzle 322 | sudoku_sol = problem 323 | 324 | def addnum(event): # To add numbers entered by user in the puzzle 325 | selector_xy = question_canvas.coords(selector) 326 | if tuple(selector_xy) in allowed_squares: 327 | y = question_canvas.create_rectangle(selector_xy[0], selector_xy[1], selector_xy[2], selector_xy[3], fill=purple, outline=white, width=2) 328 | x = question_canvas.create_text(selector_xy[0]+40, selector_xy[1]+40, font=(font_face, 30), fill="#f2ea52", text=event) 329 | usersol[int((selector_xy[3]/80))-1][int((selector_xy[2]/80))-1] = int(event) 330 | 331 | # Keybindings 332 | master.bind("", left) 333 | master.bind("", right) 334 | master.bind("", up) 335 | master.bind("", down) 336 | 337 | master.bind("1", lambda event: addnum("1")) 338 | master.bind("2", lambda event: addnum("2")) 339 | master.bind("3", lambda event: addnum("3")) 340 | master.bind("4", lambda event: addnum("4")) 341 | master.bind("5", lambda event: addnum("5")) 342 | master.bind("6", lambda event: addnum("6")) 343 | master.bind("7", lambda event: addnum("7")) 344 | master.bind("8", lambda event: addnum("8")) 345 | master.bind("9", lambda event: addnum("9")) 346 | 347 | global starttime, endtime # To start the timer 348 | starttime = datetime.datetime.now() 349 | 350 | def nextstep(): # When Next button is clicked 351 | Next = Tk() 352 | Next.geometry("1500x300") 353 | Next.title("Solving Sudoku") 354 | Next.configure(bg=purple, border=1) 355 | message = Label(Next, text=f"Wanna solve another maze?", font=(font_face, 30), fg=white, bg=purple) 356 | message.place(anchor='center', x=750, y=100) 357 | 358 | def restart(): # Restart the program 359 | Next.destroy() 360 | master.destroy() 361 | sudoku_main() 362 | 363 | yes = Button(Next, text="Yeah", command=restart, bg=white, font=(font_face, (20)), fg=purple) 364 | yes.place(anchor='center', x=550, y=200) 365 | no = Button(Next, text="Nah", command=exit, bg=white, font=(font_face, (20)), fg=purple) 366 | no.place(anchor='center', x=950, y=200) 367 | 368 | def user_solved(): # When user manages to solve the maze 369 | comp_solve.destroy() 370 | check_sol.destroy() 371 | arrow.destroy() 372 | numpad.destroy() 373 | use.destroy() 374 | use2.destroy() 375 | 376 | # To check the number of seconds elapsed after game started 377 | endtime = datetime.datetime.now() 378 | td = endtime - starttime 379 | elapsed = td.total_seconds() 380 | 381 | successmessages = ["Yay! You made it!", "Good one fella!", "That was quite easy!", "Wonderful!", "You aced it!"] 382 | headline.configure(text=r"🎉 Congratulations 🎉") 383 | Label(sudoku_UI, text=random.choice(successmessages), font=(font_face, 20), fg=white, bg=purple).place(anchor='center', x=1390, y=445) 384 | Label(sudoku_UI, text=f"You solved the maze in {round(elapsed, 2)} seconds", font=(font_face, 15), fg=white, bg=purple).place(anchor='center', x=1390, y=525) 385 | 386 | nextb = Button(sudoku_UI, text="Next", command=nextstep, bg=white, font=(font_face, (20)), fg=purple) 387 | nextb.place(anchor='center', x=1390, y=600) 388 | 389 | def solve(): # When user gives up on solving the puzzle 390 | question_canvas.destroy() 391 | check_sol.destroy() 392 | arrow.destroy() 393 | numpad.destroy() 394 | use.destroy() 395 | use2.destroy() 396 | # To display the maze solution 397 | solution_canvas = Canvas(sudoku_UI, height=720, width=720, bg=purple, bd=0, highlightthickness=0, relief='ridge') 398 | solution_canvas.place(anchor='center', x=500, y=512) 399 | 400 | for i in (0, 3, 6, 9): # To draw lines 401 | solution_canvas.create_line(0, 80*i, 730, 80*i, width=5, fill=white) 402 | solution_canvas.create_line(80*i, 0, 80*i, 730, width=5, fill=white) 403 | 404 | for i in range(11): 405 | solution_canvas.create_line(80*i, 0, 80*i, 730, width=2, fill=white) 406 | solution_canvas.create_line(0, 80*i, 730, 80*i, width=2, fill=white) 407 | 408 | y_coord, x_coord = 40, 40 409 | for i in sudoku_sol: # Displays the puzzle answer 410 | for j in i: 411 | solution_canvas.create_text(x_coord, y_coord, font=(font_face, 30), fill="#0AF174", text=j) 412 | x_coord += 80 413 | x_coord = 40 414 | y_coord += 80 415 | 416 | y_coord, x_coord = 40, 40 417 | for i in puzzle: # Displays the puzzle answer 418 | for j in i: 419 | if j != 0: 420 | solution_canvas.create_text(x_coord, y_coord, font=(font_face, 30), fill="white", text=j) 421 | x_coord += 80 422 | x_coord = 40 423 | y_coord += 80 424 | 425 | comp_solve.destroy() 426 | 427 | failmessages = [r"❌ You gave up so quick!", r"🧠 Was it really tough?", r"😂 Help yourself!", "👎 You couldn't make it!"] 428 | headline.configure(text=random.choice(failmessages)) 429 | Label(sudoku_UI, text="Better luck next time", font=(font_face, 20), fg=white, bg=purple).place(anchor='center', x=1390, y=445) 430 | Label(sudoku_UI, text=f"With Experience comes Expertise!", font=(font_face, 15), fg=white, bg=purple).place(anchor='center', x=1390, y=525) 431 | 432 | # Displays Confirmation Window on clicking next 433 | nextb = Button(sudoku_UI, text="Next", command=nextstep, bg=white, font=(font_face, (20)), fg=purple) 434 | nextb.place(anchor='center', x=1390, y=600) 435 | 436 | chances = 0 437 | 438 | def check(): # Runs when user checks the solution entered 439 | nonlocal chances 440 | if usersol == sudoku_sol: 441 | user_solved() 442 | else: 443 | if chances == 0: 444 | use2.destroy() 445 | arrow.destroy() 446 | use.destroy() 447 | numpad.destroy() 448 | # Error Messages to display on not getting the right solution 449 | if chances < 9: 450 | chances += 1 451 | error_messages = ["You didn't get that right!", "No, something isn't right!", "Nah, its not finished yet!", "No relief mate!"] 452 | headline.configure(text=random.choice(error_messages)) 453 | Label(sudoku_UI, text="Check it again!", font=(font_face, 20), fg=white, bg=purple).place(anchor='center', x=1390, y=445) 454 | Label(sudoku_UI, text=f"Chances Remaining: {10-chances}", font=(font_face, 15), fg=white, bg=purple).place(anchor='center', x=1390, y=525) 455 | else: 456 | solve() 457 | 458 | # Button to let computer solve the puzzle 459 | comp_solve = Button(sudoku_UI, text="Solve Sudoku", command=solve, 460 | bg=white, font=(font_face, (20)), fg=purple) 461 | comp_solve.place(anchor='center', x=1640, y=800) 462 | 463 | # Button to check the user solution 464 | check_sol = Button(sudoku_UI, text="Check Solution", command=check, bg=white, font=(font_face, (20)), fg=purple) 465 | check_sol.place(anchor='center', x=1140, y=800) 466 | 467 | mainloop() 468 | 469 | 470 | # For Standalone 471 | """if __name__ == "__main__": 472 | sudoku_main() 473 | """ 474 | -------------------------------------------------------------------------------- /Code/main.py: -------------------------------------------------------------------------------- 1 | """ 2 | #Project Name: Main 3 | #Last Updated: 27/02/2022 20:25 4 | #Last Updated by: Sarthak S Kumar 5 | """ 6 | 7 | # Modules 8 | from tkinter import * 9 | import sys # noqa: E402 10 | sys.path.append('Code\Sudoku') # noqa: E402 11 | sys.path.append('Code\Rat in a Maze') # noqa: E402 12 | 13 | # Importing Rat in a Maze and Sudoku Codes 14 | import Rat_in_a_maze_UI # noqa: E402 15 | import solve_sudoku_UI # noqa: E402 16 | 17 | from ctypes import windll 18 | windll.shcore.SetProcessDpiAwareness(1) # To Enable High Resolution Scaling in Windows OS 19 | 20 | """Tkinter Window Initialisation""" 21 | 22 | master = Tk() 23 | master.title("Solving Constraint Satisfaction Problems using RAT") 24 | master.geometry("1920x1080") 25 | 26 | # Intro Screen 27 | intro = Frame(master) 28 | intro.place(anchor="nw") 29 | 30 | bg = PhotoImage(file=r"Code\Assets\intro.png") 31 | canvas1 = Canvas(intro, height=1080, width=1920) 32 | canvas1.pack() 33 | canvas1.create_image(0, -40, image=bg, anchor="nw") 34 | 35 | intro.after(3000, intro.destroy) 36 | intro.wait_window(intro) 37 | 38 | # choose screen 39 | choose = Frame(master) 40 | choose.place(anchor="nw") 41 | 42 | bg = PhotoImage(file=r"Code\Assets\choose.png") 43 | canvas2 = Canvas(choose, height=1080, width=1920) 44 | canvas2.pack() 45 | canvas2.create_image(0, -40, image=bg, anchor="nw") 46 | 47 | # Images for Buttons 48 | click_sudoku = PhotoImage(file=r'Code\Assets\sudoku_img.png') 49 | click_riam = PhotoImage(file=r'Code\Assets\riam_img.png') 50 | 51 | img_label = Label(image=click_sudoku, bg="#ffffff") 52 | img_label = Label(image=click_riam, bg="#ffffff") 53 | 54 | # Buttons for choosing project 55 | Button(choose, image=click_sudoku, command=solve_sudoku_UI.sudoku_main, borderwidth=0, bg="#ffffff", highlightcolor="#ffffff").place(x=600, y=540, anchor='center') 56 | Button(choose, image=click_riam, command=Rat_in_a_maze_UI.riam_main, borderwidth=0, bg="#ffffff", highlightcolor="#ffffff").place(x=1350, y=540, anchor='center') 57 | 58 | Button(choose, text="Exit", command=exit, bg="#4d1354", font=(r'HK Grotesk', (20)), fg="white").place(anchor='c', x=1650, y=950) 59 | 60 | mainloop() 61 | -------------------------------------------------------------------------------- /Final Review/Final Review Presentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Final Review/Final Review Presentation.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Solving Constraint Satisfaction Problems using RAT 2 |

3 | 4 |

5 | 6 | ## ✨ Project Description 7 |

✦ The project hinges on the method of backtracking to find the most feasible solution to the above problems.
8 | ✦ It also has a UI, which enables the user to interact with the program more effectively.
9 | ✦ It includes a facility to let the user solve these problems not only by a manual approach, but also lets him allow the computer to solve it too.


10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
※ Sudoku※ Rat in a maze
21 | 22 | ## 🤝 Team Members 23 |

Sarthak S Kumar
24 | ✦ Varun C
25 | ✦ Vishal M Godi

26 | 27 | 28 | ## 📤 Contributing to Solving-CSP-using-RAT 29 | We are so glad that you are interested in contributing to the this project. 30 | We only have a couple of rules and we hope you enjoy the process :) 31 | 32 | ### Contributing Rules 33 | 1. Don't move or delete any files. Only modify them. 34 | 2. Put all project files in the respective folders. 35 | 36 | ### Contributing Process 37 | 1. Fork the repository 38 | 2. Clone your forked repository to your computer 39 | 3. Head to the issues tab and look for an issue that you like. 40 | 4. Once you have decided what issue to work on, give it a shot! 41 | 5. Once done, push the code to your forked repository. 42 | 6. Head to the Pull Requests tab and click on "Create New Pull Request" 43 | 7. On the left of the arrow should be this repo and on the right should be yours. 44 | 8. Add a small description to the Pull Request describing what you've done. 45 | 9. Mention what Issue you have worked on. If the issue number is #3, you can mention "Closes #3" in the Pull Request description. 46 | 10. Submit Pull Request 47 | 48 | It's that easy! We hope you enjoy contributing to our repository. Don't hesitate to contact any of the maintainers about any problems! 49 | 50 | ## CSE Project SEM - I 2022. 51 | -------------------------------------------------------------------------------- /Review 1/Team 10 Presentation Review 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Review 1/Team 10 Presentation Review 1.pdf -------------------------------------------------------------------------------- /Review 2/Team 10 Presentation Review 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/Review 2/Team 10 Presentation Review 2.pdf -------------------------------------------------------------------------------- /SnapShots/Screenshot (17).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (17).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (18).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (18).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (19).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (19).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (20).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (20).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (21).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (21).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (22).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (22).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (23).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (23).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (24).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (24).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (25).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (25).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (26).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (26).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (27).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (27).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (28).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (28).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (29).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (29).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (30).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (30).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (31).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (31).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (32).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (32).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (33).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (33).png -------------------------------------------------------------------------------- /SnapShots/Screenshot (34).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SarthakSKumar/Solving-CSP-using-RAT/5cd4ad45c88b394af9e85e2713da75cbda2a4574/SnapShots/Screenshot (34).png --------------------------------------------------------------------------------