Sudoku help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 5
Reputation: harshadap is an unknown quantity at this point 
Solved Threads: 0
harshadap harshadap is offline Offline
Newbie Poster

Hiiiiiiii all i m new to dis community. plz help me its urgent

 
0
  #1
Sep 2nd, 2009
I m doing Program for sudoku. The blank grid displayed but how to pass random integer values to that grid. here is my code:

  1. import javax.swing.BorderFactory;
  2. import javax.swing.JComponent;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import java.awt.*;
  6. import javax.swing.JFormattedTextField;
  7. import javax.swing.text.DefaultFormatterFactory;
  8. import javax.swing.text.NumberFormatter;
  9.  
  10. @SuppressWarnings("empty-statement")
  11. public class Board
  12. {
  13.  
  14. private static final Dimension FIELD_SIZE = new Dimension(60, 60);
  15. private static final int EMPTY = 0;
  16. private JPanel grid = new JPanel();
  17. private JPanel[][] subgrid = new JPanel[3][3];
  18. private JFormattedTextField[][] cell = new JFormattedTextField[9][9];
  19. private static int num=0,col=9,row=9;
  20. private int Value=0;
  21. private String strValue;
  22.  
  23. private int GridBoard[][] = {{0, 6, 0, 1, 0, 4, 0, 5, 0},
  24. {0, 0, 8, 3, 0, 5, 6, 0, 0},
  25. {2, 0, 0, 0, 0, 0, 0, 0, 1},
  26. {0, 0, 6, 0, 0, 0, 3, 0, 0},
  27. {7, 0, 0, 9, 0, 1, 0, 0, 4},
  28. {5, 0, 0, 0, 0, 0, 0, 0, 2},
  29. {0, 0, 7, 2, 0, 6, 9, 0, 0},
  30. {0, 4, 0, 5, 0, 8, 0, 7, 0}};
  31. int GridArray[][] = null;
  32.  
  33. public Board()
  34. {
  35. grid.setBorder(BorderFactory.createLineBorder(Color.black, 3));
  36. grid.setLayout(new GridLayout(3, 3));
  37.  
  38. for (int i = 0; i < subgrid.length; i++)
  39. {
  40. for (int j = 0; j < subgrid[i].length; j++)
  41. {
  42. subgrid[i][j] = new JPanel(new GridLayout(3, 3));
  43. subgrid[i][j].setBorder(BorderFactory.createLineBorder(Color.black, 1));
  44. grid.add(subgrid[i][j]);
  45. }
  46. }
  47.  
  48. for (int i = 0; i < cell.length; i++)
  49. {
  50. for (int j = 0; j < cell[i].length; j++)
  51. {
  52. cell[i][j] = new JFormattedTextField(new DefaultFormatterFactory(new NumberFormatter()));
  53. cell[i][j].setValue(new Integer(Value));
  54.  
  55. cell[i][j].setPreferredSize(FIELD_SIZE);
  56. cell[i][j].setFont( new Font("sans-serif", Font.BOLD, 20));
  57. cell[i][j].setHorizontalAlignment(JFormattedTextField.CENTER);
  58. subgrid[i/3][j/3].add(cell[i][j]);
  59. }
  60. }
  61. }
  62.  
  63. Board(int[][] result)
  64. {
  65. this.grid =grid;
  66. }
  67. public JComponent getPanel() //return type of getPanel() is JComponent
  68. {
  69. return grid;
  70. }
  71.  
  72. public void setCellValue(int num, int row, int col)
  73. {
  74. num=0;
  75. for (int r = 1;r<=9 ;r++ )
  76. {
  77. for (int c = 1;c<=9 ; c++)
  78. {
  79. cell[r][c] = new JFormattedTextField();
  80. strValue=GridBoard.toString().replace("0", " ");
  81.  
  82. String text = (num == EMPTY) ? " " : String.valueOf(num);
  83. cell [r][c].setText(text);
  84.  
  85. }
  86. }
  87. }
  88.  
  89. public int getCellValue(int row, int col)
  90. {
  91. int cellval=0;
  92. try
  93. {
  94. for (int r = 0;r<cell.length ;r++ )
  95. {
  96. for (int c = 0;c<cell[r].length ; c++)
  97. {
  98. cellval = Integer.parseInt(cell[r][c].getText());
  99. }
  100. }
  101. }
  102. catch (NumberFormatException e)
  103. {
  104. cellval = EMPTY;
  105. }
  106. return cellval;
  107. }
  108.  
  109. public void clearBoard()
  110. {
  111. for (int r = 0; r < 9 ; r++ )
  112. {
  113. for (int c = 0; c < 9 ; c++)
  114. {
  115. cell[r][c] = new JFormattedTextField();
  116. }
  117. }
  118. }
  119.  
  120. public static boolean isValid(boolean[] choices){
  121. for (int i = 1; i <= 9; i++)
  122. {
  123. if (choices[i]) return true;
  124. }
  125. return false;
  126. }
  127.  
  128.  
  129. protected boolean checkRow( int num )
  130. {
  131. // loop through row and check values
  132. for (int i = 0; i < cell[row-1].length; i++)
  133. {
  134. if(num == Integer.parseInt(cell[row-1][i].getText()));
  135. {
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141.  
  142. protected boolean checkCol( int num )
  143. {
  144. // loop through row and check values
  145. for (int i = 0; i < cell[col-1].length; i++)
  146. {
  147. if(num == Integer.parseInt(cell[col-1][i].getText()))
  148. {
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154.  
  155. protected boolean inBox(int number)
  156. {
  157. for(int i = 0; i < subgrid.length; i++)
  158. {
  159. for(int j = 0 ; j < subgrid[i].length; j++)
  160. {
  161. if(number == Integer.parseInt(cell[i][j].getText()))
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167.  
  168. private static void createAndShowGUI()
  169. {
  170. JFrame frame = new JFrame("Sudoku");
  171. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  172. frame.getContentPane().add(new Board().getPanel());
  173. frame.pack();
  174. frame.setLocationRelativeTo(null);
  175. frame.setVisible(true);
  176. }
  177.  
  178.  
  179. public static void main(String[] args)
  180. {
  181. Board board= new Board();
  182. javax.swing.SwingUtilities.invokeLater(new Runnable()
  183. {
  184. public void run()
  185. {
  186. createAndShowGUI();
  187. }
  188. });
  189. }
  190. }
Last edited by Ezzaral; Sep 2nd, 2009 at 3:27 pm. Reason: Added [code] [/code] tags. Please use them to format any code that you post.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 147
Reputation: GDICommander is an unknown quantity at this point 
Solved Threads: 19
GDICommander's Avatar
GDICommander GDICommander is offline Offline
Junior Poster

Re: Sudoku help

 
0
  #2
Sep 2nd, 2009
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 5
Reputation: harshadap is an unknown quantity at this point 
Solved Threads: 0
harshadap harshadap is offline Offline
Newbie Poster

Re: Sudoku help

 
0
  #3
Sep 4th, 2009
Thanks for suggestion. I done that but random number does not displayed in the grid. can you give source code for that.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 5
Reputation: harshadap is an unknown quantity at this point 
Solved Threads: 0
harshadap harshadap is offline Offline
Newbie Poster

Re: Sudoku help

 
0
  #4
Sep 6th, 2009
hello
can anybody give me idea of how to validate numbers in sudoku and how to apply color for that particular no and cell
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC