QNA > H > How To Create A Sudoku Game Website Using Python

How to create a Sudoku game website using Python

Hi!

I'm assuming you don't want graphical interface but if you do you will need the pyGame module/Framework and it takes plenty of time,consider the code without Pygame down below

It does need the numpy module here

So I think you are better off doing it without gui,just my opinion though and since I can't explain every line of code here,I assume you understand python

OR simply copy the code

Source: http://code.activestate.com/recipes/578250-sudoku-game-generator/

Edit:I may have made some mistakes while copying the code or something so I suggest you copy it from the link directly

Thanks!

  1. #!/usr/bin/env python 
  2. # -*- coding: utf-8 -*- 
  3.  
  4. """ 
  5. Sudoku game maker 
  6.  
  7. """ 
  8.  
  9. __author__ = 'Ripley6811' 
  10. __contact__ = 'python at boun.cr' 
  11. __copyright__ = '' 
  12. __license__ = '' 
  13. __date__ = 'Thu Aug 30 10:09:06 2012' 
  14. __version__ = '0.1' 
  15.  
  16. #=============================================================================== 
  17. # IMPORT STATEMENTS 
  18. #=============================================================================== 
  19. from numpy import *  
  20.  
  21. #=============================================================================== 
  22. # METHODS 
  23. #=============================================================================== 
  24.  
  25. def new_block(): 
  26. return random.permutation(arange(1,10)).reshape(3,3) 
  27.  
  28. def test_rowcol(S): 
  29. retval = True 
  30. for row in S: 
  31. if len(set(row).difference([0])) < count_nonzero(row): 
  32. retval = False 
  33. break 
  34. for col in S.T: 
  35. if len(set(col).difference([0]It's)) < count_nonzero(col): 
  36. retval = False 
  37. break 
  38. return retval 
  39.  
  40.  
  41.  
  42. def generate_grid(S=None, verbose=False): 
  43. #PART 1: SET FIRST THREE ROWS AND FIRST THREE COLUMNS 
  44. available = set(range(1,10)) 
  45. if S == None: 
  46. S = new_block() 
  47. if verbose: print S 
  48. while True: 
  49. Srow = append(append(S,new_block(),1),new_block(),1) 
  50. if test_rowcol(Srow): 
  51. if verbose: print Srow 
  52. break 
  53. while True: 
  54. Scol = append(append(S,new_block(),0),new_block(),0) 
  55. if test_rowcol(Scol): 
  56. Scol = append(Scol[3:],zeros((6,6),int),1) 
  57. if verbose: print Scol 
  58. break 
  59. S = append(Srow,Scol,0) 
  60. #PART 2: FILL IN THE REST OF GRID FROM PART 1. [3:,3:] 
  61. if verbose: print '.', 
  62. while True: 
  63. S[3:6,3:6] = new_block() 
  64. if test_rowcol(S[:6,:6]): 
  65. break 
  66. while True: 
  67. S[6:,6:] = new_block() 
  68. if test_rowcol(S): 
  69. break 
  70. for i in range(3,9): 
  71. for j in range(3,9): 
  72. if S[i,j] == 0: 
  73. subset = available.difference( set(S[i]) ).difference( set(S[:,j]) ) 
  74. if len(subset) == 1: 
  75. S[i,j] = subset.pop() 
  76. else: 
  77. S[3:,3:] = 0 
  78. return generate_grid(S, verbose) 
  79. if verbose: print '\n', S 
  80. return S 
  81.  
  82. def reduce_options(board, Pcube): 
  83.  
  84. row,col = where(board == 0) 
  85. playoption = [] 
  86. for i in range(9): 
  87. for j in range(9): 
  88. if board[i,j] != 0: 
  89. Pcube[i,j,Pcube[i,j]!=board[i,j]] *= 0 
  90.  
  91. for i,j in zip(row,col): 
  92. exclude = set(board[i]) 
  93. exclude = exclude.union(board[:,j]) 
  94. exclude = exclude.union(board[i/3*3:i/3*3+3,j/3*3:j/3*3+3].flat) 
  95. for each in exclude: 
  96. Pcube[i,j,Pcube[i,j]==each] = 0 
  97.  
  98. for layer in Pcube.T: # probable layers 1 through 9 
  99. for i in range(9): 
  100. rowsfilled = sum(layer[i,:3])>0, sum(layer[i,3:6])>0, sum(layer[i,6:])>0 
  101. if sum(rowsfilled) == 1: 
  102. rowsfilled = repeat(rowsfilled,3) 
  103. layer[i/3*3+(i+1)%3,rowsfilled] *= 0 
  104. layer[i/3*3+(i+2)%3,rowsfilled] *= 0 
  105. layer = layer.T 
  106. for i in range(9): 
  107. rowsfilled = sum(layer[i,:3])>0, sum(layer[i,3:6])>0, sum(layer[i,6:])>0 
  108. if sum(rowsfilled) == 1: 
  109. rowsfilled = repeat(rowsfilled,3) 
  110. layer[i/3*3+(i+1)%3,rowsfilled] *= 0 
  111. layer[i/3*3+(i+2)%3,rowsfilled] *= 0 
  112.  
  113.  
  114. # print str(Pcube.T).replace('0','~') 
  115.  
  116. for i,j in zip(row,col): 
  117. if count_nonzero(Pcube[i,j]) == 1: 
  118. playoption.append( (i,j,sum(Pcube[i,j])) ) 
  119. return playoption 
  120.  
  121.  
  122. def generate_game(S, verbose=False): 
  123. gametest = S.copy() 
  124.  
  125. for each in range(200): 
  126. i = random.randint(81) 
  127. temp = gametest.flat[i] 
  128. gametest.flat[i] = 0 
  129.  
  130. if not isSolvable(gametest): 
  131. gametest.flat[i] = temp 
  132. return gametest 
  133.  
  134.  
  135. def isSolvable(testgame): 
  136. board = testgame.copy() 
  137. P = ones((9,9,9),int) 
  138. for i in arange(9): 
  139. P[:,:,i] *= i+1 
  140. print 'GAME\n', str(board).replace('0','_') 
  141. playorder = [] 
  142. laststate = sum(P) 
  143. while sum(board == 0) > 0: 
  144. #REDUCE OPTIONS FOR EACH HOLE 
  145. playoptions = reduce_options(board, P) 
  146. print playoptions 
  147. # print str(board).replace('0','_') 
  148. for i,j,v in playoptions: 
  149. board[i,j] = v 
  150. thisstate = sum(P) 
  151. if thisstate == laststate: 
  152. break 
  153. else: 
  154. laststate = thisstate 
  155. return True if sum(board == 0) == 0 else False 
  156.  
  157.  
  158.  
  159.  
  160. def main(): 
  161. """Description of main()""" 
  162. solution = generate_grid(verbose=True) 
  163. sudoku = generate_game(solution, verbose=True) 
  164. print 'Solution\n', solution 
  165. print 'Sudoku\n', str(sudoku).replace('0','_') 
  166. print sum(sudoku == 0), 'blanks (', int(sum(sudoku == 0)/.81), '%)' 
  167.  
  168.  
  169. if __name__ == '__main__': 
  170. main() 

Di Piotr Methun

How do we create graphics in Python? :: Quale telefono è meglio, Mi4i o Meizu M2 nota?
Link utili