Advertisement
Guest User

Untitled

a guest
May 4th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 KB | None | 0 0
  1. /*********************************************************/
  2. /* */
  3. /* An applet to demonstrate recursion and backtracking */
  4. /* =================================================== */
  5. /* */
  6. /* V0.3 18-MAR-2007 P. Tellenbach www.heimetli.ch */
  7. /* */
  8. /*********************************************************/
  9. import java.applet.* ;
  10. import java.awt.* ;
  11.  
  12. /**
  13. * Solves a sudoku puzzle by recursion and backtracking
  14. */
  15. public class SimplifiedSudoku2 extends Applet implements Runnable
  16. {
  17. /** The model */
  18. protected int model[][] ;
  19.  
  20. /** The view */
  21. protected Button view[][] ;
  22.  
  23. /** Creates the model and sets up the initial situation */
  24. protected void createModel()
  25. {
  26. model = new int[16][16] ;
  27.  
  28. // Clear all cells
  29. for( int row = 0; row < 16; row++ )
  30. for( int col = 0; col < 16; col++ )
  31. model[row][col] = 0 ;
  32.  
  33. // Create the initial situation
  34. model[0][0] = 11 ;
  35. model[0][2] = 5 ;
  36. model[0][5] = 12 ;
  37. model[0][8] = 13 ;
  38. model[0][11] = 10;
  39.  
  40. model[1][1] = 13 ;
  41. model[1][6] = 5 ;
  42. model[1][8] = 1 ;
  43. model[1][10] = 14 ;
  44. model[1][12] = 2 ;
  45.  
  46. model[2][3] = 9 ;
  47. model[2][7] = 2 ;
  48. model[2][9] = 5 ;
  49. model[2][12] = 14 ;
  50. model[2][14] = 7 ;
  51.  
  52. model[3][0] = 4 ;
  53. model[3][2] = 12 ;
  54. model[3][4] = 8 ;
  55. model[3][11] = 16 ;
  56. model[3][13] = 10 ;
  57.  
  58. model[4][1] = 9 ;
  59. model[4][3] = 11 ;
  60. model[4][6] = 4 ;
  61. model[4][8] = 3 ;
  62. model[4][9] = 6 ;
  63. model[4][15] = 7 ;
  64.  
  65. model[5][0] = 6 ;
  66. model[5][2] = 8;
  67. model[5][4] = 10 ;
  68. model[5][5] = 13 ;
  69. model[5][7] = 16 ;
  70. model[5][10] = 11 ;
  71. model[5][12] = 15 ;
  72. model[5][14] = 9 ;
  73.  
  74. model[6][1] = 3 ;
  75. model[6][6] = 2 ;
  76. model[6][8] = 15 ;
  77. model[6][9] = 16;
  78. model[6][13] = 13 ;
  79. model[6][15] = 1 ;
  80.  
  81. model[7][0] = 16 ;
  82. model[7][3] = 2 ;
  83. model[7][4] = 3;
  84. model[7][5] = 1 ;
  85. model[7][7] = 5 ;
  86. model[7][9] = 14 ;
  87. model[7][11] = 8 ;
  88.  
  89. model[8][2] = 16;
  90. model[8][4] = 13 ;
  91. model[8][5] = 4 ;
  92. model[8][14] = 12 ;
  93.  
  94. model[9][2] = 10 ;
  95. model[9][3] = 3 ;
  96. model[9][5] = 7 ;
  97. model[9][8] = 5 ;
  98. model[9][10] = 16 ;
  99. model[9][12] = 9 ;
  100. model[9][15] = 15 ;
  101.  
  102. model[10][0] = 15 ;
  103. model[10][2] = 11 ;
  104. model[10][4] = 1 ;
  105. model[10][7] = 9 ;
  106. model[10][9] = 2 ;
  107. model[10][11] = 3 ;
  108. model[10][13] = 4 ;
  109.  
  110. model[11][6] = 8 ;
  111. model[11][10] = 4 ;
  112. model[11][12] = 10 ;
  113.  
  114. model[12][0] = 2 ;
  115. model[12][2] = 4 ;
  116. model[12][11] = 12 ;
  117. model[12][14] = 5 ;
  118.  
  119. model[13][1] = 12 ;
  120. model[13][3] = 13 ;
  121. model[13][7] = 1 ;
  122. model[13][10] = 10 ;
  123. model[13][13] = 3 ;
  124. model[13][15] = 8 ;
  125.  
  126. model[14][0] = 8 ;
  127. model[14][2] = 9 ;
  128. model[14][4] = 4 ;
  129. model[14][6] = 3 ;
  130. model[14][9] = 7 ;
  131. model[14][12] = 11 ;
  132. model[14][14] = 13 ;
  133.  
  134. model[15][1] = 5 ;
  135. model[15][3] = 14 ;
  136. model[15][7] = 11 ;
  137. model[15][10] = 1 ;
  138. model[15][11] = 6 ;
  139. model[15][15] = 10 ;
  140.  
  141. }
  142.  
  143. /** Creates an empty view */
  144. protected void createView()
  145. {
  146. setLayout( new GridLayout(16,16) ) ;
  147.  
  148. view = new Button[16][16] ;
  149.  
  150. // Create an empty view
  151. for( int row = 0; row < 16; row++ )
  152. for( int col = 0; col < 16; col++ )
  153. {
  154. view[row][col] = new Button() ;
  155. add( view[row][col] ) ;
  156. }
  157. }
  158.  
  159. /** Updates the view from the model */
  160. protected void updateView()
  161. {
  162. for( int row = 0; row < 16; row++ )
  163. for( int col = 0; col < 16; col++ )
  164. if( model[row][col] != 0 )
  165. view[row][col].setLabel( String.valueOf(model[row][col]) ) ;
  166. else
  167. view[row][col].setLabel( "" ) ;
  168. }
  169.  
  170. /** This method is called by the browser when the applet is loaded */
  171. public void init()
  172. {
  173. createModel() ;
  174. createView() ;
  175. updateView() ;
  176. }
  177.  
  178. /** Checks if num is an acceptable value for the given row */
  179. protected boolean checkRow( int row, int num )
  180. {
  181. for( int col = 0; col < 16; col++ )
  182. if( model[row][col] == num )
  183. return false ;
  184.  
  185. return true ;
  186. }
  187.  
  188. /** Checks if num is an acceptable value for the given column */
  189. protected boolean checkCol( int col, int num )
  190. {
  191. for( int row = 0; row < 16; row++ )
  192. if( model[row][col] == num )
  193. return false ;
  194.  
  195. return true ;
  196. }
  197.  
  198. /** Checks if num is an acceptable value for the box around row and col */
  199. protected boolean checkBox( int row, int col, int num )
  200. {
  201. row = (row / 4) * 4 ;
  202. col = (col / 4) * 4 ;
  203.  
  204. for( int r = 0; r < 4; r++ )
  205. for( int c = 0; c < 4; c++ )
  206. if( model[row+r][col+c] == num )
  207. return false ;
  208.  
  209. return true ;
  210. }
  211.  
  212. /** This method is called by the browser to start the applet */
  213. public void start()
  214. {
  215. // This statement will start the method 'run' to in a new thread
  216. (new Thread(this)).start() ;
  217. }
  218.  
  219. /** The active part begins here */
  220. public void run()
  221. {
  222. try
  223. {
  224. // Let the observers see the initial position
  225. //Thread.sleep( 100 ) ;
  226.  
  227. for(int i=0;i<16;i++)
  228. {
  229. for(int j=0;j<16;j++)
  230. {
  231. for(int k=1;k<17;k++)
  232. {
  233. if(model[i][j] == k)
  234. view[i][j].setBackground(changeColor(k));
  235. }
  236. }
  237. }
  238.  
  239. // Start to solve the puzzle in the left upper corner
  240. solve( 0, 0 ) ;
  241. }
  242. catch( Exception e )
  243. {
  244. }
  245. }
  246.  
  247. /** Recursive function to find a valid number for one single cell */
  248. public void solve( int col, int row ) throws Exception
  249. {
  250. // Throw an exception to stop the process if the puzzle is solved
  251. if( col > 16 )
  252. throw new Exception( "Solution found" ) ;
  253.  
  254. // If the cell is not empty, continue with the next cell
  255. if( model[row][col] != 0 )
  256. next( col, row ) ;
  257. else
  258. {
  259. // Find a valid number for the empty cell
  260. for( int num = 1; num < 17; num++ )
  261. {
  262. if( checkRow(row,num) && checkCol(col,num) && checkBox(row,col,num) )
  263. {
  264. model[row][col] = num ;
  265. // view[row][col].setBackground(changeColor(num));
  266. updateView() ;
  267.  
  268. // Let the observer see it
  269. //Thread.sleep( 100 ) ;
  270.  
  271. // Delegate work on the next cell to a recursive call
  272. next( col, row ) ;
  273. }
  274. }
  275.  
  276. // No valid number was found, clean up and return to caller
  277. model[row][col] = 0 ;
  278. //view[row][col].setBackground((new Button().getBackground()));
  279. updateView() ;
  280. }
  281. }
  282.  
  283. /** Calls solve for the next cell */
  284. public void next( int row, int col ) throws Exception
  285. {
  286. if( col < 15 )
  287. solve( row, col + 1 ) ;
  288. else
  289. solve( row + 1, 0 ) ;
  290. }
  291.  
  292. public Color changeColor(int num)
  293. {
  294. Color myColor = new Color(0,0,0);
  295. switch(num)
  296. {
  297. case 1:
  298. return myColor = new Color(176,23,31);
  299.  
  300. case 2:
  301. return myColor = new Color(199,21,133);
  302.  
  303. case 3:
  304. return myColor = new Color(153,50,205);
  305.  
  306. case 4:
  307. return myColor = new Color(0,0,205);
  308.  
  309. case 5:
  310. return myColor = new Color(198,226,255);
  311.  
  312. case 6:
  313. return myColor = new Color(0,205,102);
  314.  
  315. case 7:
  316. return myColor = new Color(0,139,69);
  317.  
  318. case 8:
  319. return myColor = new Color(255,255,0);
  320.  
  321. case 9:
  322. return myColor = new Color(255,215,0);
  323.  
  324. case 10:
  325. return myColor = new Color(205,173,0);
  326.  
  327. case 11:
  328. return myColor = new Color(255,127,0);
  329.  
  330. case 12:
  331. return myColor = new Color(224,255,255);
  332.  
  333. case 13:
  334. return myColor = new Color(159,182,205);
  335.  
  336. case 14:
  337. return myColor = new Color(255,182,193);
  338.  
  339. case 15:
  340. return myColor = new Color(255,240,245);
  341.  
  342. case 16:
  343. return myColor = new Color(193,255,193);
  344.  
  345. }
  346. return myColor;
  347. }
  348.  
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement