|
| 1 | +importpygame |
| 2 | +fromsudokuSolverimport* |
| 3 | +fromchooseLevelimport* |
| 4 | +importtime |
| 5 | + |
| 6 | +# Define some colors |
| 7 | +BLACK= (0,0,0) |
| 8 | +WHITE= (255,255,255) |
| 9 | +GREEN= (0,255,0) |
| 10 | +L_GREEN= (150,255,150) |
| 11 | +RED= (255,0,0) |
| 12 | +L_RED= (255,204,203) |
| 13 | +GRAY= (60,60,60) |
| 14 | +L_GRAY= (220,220,220) |
| 15 | +YELLOW= (255,255,0) |
| 16 | + |
| 17 | +# This sets the WIDTH and HEIGHT of each grid location |
| 18 | +WIDTH=HEIGHT=50 |
| 19 | + |
| 20 | +# This sets the margin between each cell |
| 21 | +MARGIN=5 |
| 22 | +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, |
| 23 | +pygame.K_9] |
| 24 | + |
| 25 | +# Set the width and height of the screen [width, height] |
| 26 | +size= (500,500) |
| 27 | +# screen = pygame.display.set_mode(size) |
| 28 | +pygame.init() |
| 29 | +font=pygame.font.Font('freesansbold.ttf',32) |
| 30 | + |
| 31 | +# pygame.display.set_caption("Sudoku King") |
| 32 | + |
| 33 | +# Loop until the user clicks the close button. |
| 34 | +done=False |
| 35 | + |
| 36 | + |
| 37 | +defcheatingAllTheWay(): |
| 38 | +forrowinrange(len(Board)): |
| 39 | +forcolumninrange(len(Board[row])): |
| 40 | +Board[row][column]=solvedBoard[row][column] |
| 41 | +addNumToBoard(Board[row][column],row,column,L_GREEN) |
| 42 | +time.sleep(0.05) |
| 43 | +pygame.display.flip() |
| 44 | +finish() |
| 45 | + |
| 46 | + |
| 47 | +defaddNumToBoard(number,row,column,color): |
| 48 | +addNewRect(row,column,WHITE,5) |
| 49 | +addNewRect(row,column,color,None) |
| 50 | +font=pygame.font.Font('freesansbold.ttf',32) |
| 51 | +text=font.render(str(number),True,BLACK, ) |
| 52 | +textRect=text.get_rect()# get_rect() -> Returns a new rectangle covering the entire surface. |
| 53 | +textRect.center= ((MARGIN+WIDTH)*column+MARGIN+WIDTH/2, (MARGIN+HEIGHT)*row+MARGIN+WIDTH/2) |
| 54 | +screen.blit(text,textRect) |
| 55 | +drawTheBorder() |
| 56 | + |
| 57 | + |
| 58 | +deffinish(): |
| 59 | +ifsolvedBoard==Board: |
| 60 | +print("good") |
| 61 | +else: |
| 62 | +print("not good") |
| 63 | + |
| 64 | + |
| 65 | +defaddNewRect(row,col,color,width): |
| 66 | +rectSize=pygame.Rect((MARGIN+WIDTH)*col+MARGIN, (MARGIN+HEIGHT)*row+MARGIN,WIDTH, |
| 67 | +HEIGHT) |
| 68 | +ifwidthisnotNone: |
| 69 | +pygame.draw.rect(screen,color,rectSize,width)# coloring only the border |
| 70 | +else: |
| 71 | +pygame.draw.rect(screen,color,rectSize)# coloring the whole rectangle |
| 72 | + |
| 73 | + |
| 74 | +defflickering(timeFlickering,color):# flickering with color on-off |
| 75 | +addNewRect(row,column,color,5) |
| 76 | +pygame.display.flip() |
| 77 | +time.sleep(timeFlickering) |
| 78 | +addNewRect(row,column,WHITE,5) |
| 79 | +pygame.display.flip() |
| 80 | +time.sleep(timeFlickering) |
| 81 | +addNewRect(row,column,color,5) |
| 82 | +pygame.display.flip() |
| 83 | +time.sleep(timeFlickering) |
| 84 | +addNewRect(row,column,WHITE,5) |
| 85 | +pygame.display.flip() |
| 86 | + |
| 87 | + |
| 88 | +defdrawTheBorder(): |
| 89 | +dif=500//9 |
| 90 | +foriinrange(10): |
| 91 | +thick=5 |
| 92 | +pygame.draw.line(screen,GRAY, (0,i*dif+2), (500,i*dif+2),thick) |
| 93 | +pygame.draw.line(screen,GRAY, (i*dif+2,0), (i*dif+2,500),thick) |
| 94 | +foriinrange(10): |
| 95 | +ifi%3==0: |
| 96 | +thick=8 |
| 97 | +pygame.draw.line(screen,BLACK, (0,i*dif), (500,i*dif),thick) |
| 98 | +pygame.draw.line(screen,BLACK, (i*dif,0), (i*dif,500),thick) |
| 99 | + |
| 100 | + |
| 101 | +defdrawInitBoard(): |
| 102 | +# printBoard(solvedBoard) |
| 103 | +forrowinrange(len(Board)): |
| 104 | +forcolumninrange(len(Board[row])): |
| 105 | +color=L_GRAY |
| 106 | +ifBoard[row][column]==0:# if we want to change to background of the empty cells |
| 107 | +color=WHITE |
| 108 | +# ----- drawing the rect ------ |
| 109 | +pygame.draw.rect(screen,color, |
| 110 | + [(MARGIN+WIDTH)*column+MARGIN, (MARGIN+HEIGHT)*row+MARGIN,WIDTH,HEIGHT]) |
| 111 | +# show nothing if the number is 0 |
| 112 | +font=pygame.font.Font('freesansbold.ttf',32) |
| 113 | +ifBoard[row][column]==0: |
| 114 | +text=font.render(" ",True,BLACK, )# render(text, anti-alias[True], color, background=None) |
| 115 | +else: |
| 116 | +text=font.render(str(Board[row][column]),True,BLACK, ) |
| 117 | + |
| 118 | +textRect=text.get_rect()# get_rect() -> Returns a new rectangle covering the entire surface. |
| 119 | +textRect.center= ( |
| 120 | + (MARGIN+WIDTH)*column+MARGIN+WIDTH/2, (MARGIN+HEIGHT)*row+MARGIN+WIDTH/2) |
| 121 | +screen.blit(text,textRect) |
| 122 | +drawTheBorder() |
| 123 | + |
| 124 | + |
| 125 | +# -------- Main Program Loop ----------- |
| 126 | +if__name__=="__main__": |
| 127 | +flag1=True |
| 128 | + |
| 129 | +whileflag1: |
| 130 | +level=chooseLevel() |
| 131 | +iflevel==1orlevel==2orlevel==3: |
| 132 | +print(level) |
| 133 | +flag1=False |
| 134 | +pygame.display.set_caption("Sudoku King1") |
| 135 | +screen=pygame.display.set_mode(size) |
| 136 | + |
| 137 | +sol=mainSolver(level)# first at all the script solve the sudoku by itself |
| 138 | + |
| 139 | +print("solveBoard") |
| 140 | +printBoard(sol) |
| 141 | + |
| 142 | +# ------ draw the board ------ |
| 143 | +pygame.init() |
| 144 | +screen.fill(BLACK) |
| 145 | +drawInitBoard() |
| 146 | +readyForInput=False |
| 147 | +key=None |
| 148 | +whilenotdone: |
| 149 | +# --- Main event loop |
| 150 | + |
| 151 | +foreventinpygame.event.get(): |
| 152 | +ifevent.type==pygame.QUIT: |
| 153 | +done=True |
| 154 | +ifevent.type==pygame.KEYDOWN: |
| 155 | +ifevent.keyinnumbers_1to9: |
| 156 | +key=chr(event.key) |
| 157 | +ifevent.key==pygame.K_RETURN: |
| 158 | +finish() |
| 159 | +ifevent.key==pygame.K_c: |
| 160 | +cheatingAllTheWay() |
| 161 | +ifevent.type==pygame.MOUSEBUTTONDOWN: |
| 162 | +# ------ if clicked on a cell get his row and column ------ |
| 163 | +ifreadyForInputisTrue: |
| 164 | +addNewRect(row,column,WHITE,None) |
| 165 | +drawTheBorder() |
| 166 | +readyForInput=False |
| 167 | + |
| 168 | +pos=pygame.mouse.get_pos() |
| 169 | +column=pos[0]// (WIDTH+MARGIN) |
| 170 | +row=pos[1]// (WIDTH+MARGIN) |
| 171 | +# ------ checking if it is a empty (0 inside) ------ |
| 172 | +ifBoard[row][column]==0: |
| 173 | +# ------ coloring the border of the clicked cell ----- #TODO YELLOW |
| 174 | + |
| 175 | +addNewRect(row,column,YELLOW,5) |
| 176 | +readyForInput=True |
| 177 | +# ------ now only wait for input from the user ----- |
| 178 | + |
| 179 | +ifreadyForInputandkeyisnotNone: |
| 180 | +# ------ checking if the key is good at it's place ------ |
| 181 | +ifint(key)==sol[row][column]: |
| 182 | +Board[row][column]=key |
| 183 | +flickering(0.1,GREEN)# flickering at a 0.2 seconds with the color green |
| 184 | +addNumToBoard(key,row,column,L_GREEN) |
| 185 | +else: |
| 186 | +flickering(0.1,RED)# flickering at a 0.2 seconds with the color red |
| 187 | +addNumToBoard(key,row,column,L_RED) |
| 188 | + |
| 189 | +# ----------------------------------------------- |
| 190 | +drawTheBorder() |
| 191 | +readyForInput=False |
| 192 | + |
| 193 | +key=None |
| 194 | +pygame.display.flip() |
| 195 | +pygame.display.update() |
| 196 | + |
| 197 | + |
| 198 | +# Close the window and quit. |
| 199 | +pygame.quit() |