473,473 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sudoku, just need help on generating random numbers... and then checking

2 New Member
okay, so i started on code for Sudoku on ready to program java ... very close to java. I was able to create a 2d array and display and change the variables. What i want to do now is to generate random numbers in the array. Numbers in between 1 and 9. and then i need to check for rows, columns and the boxes... i am new to arrays and i really need some help.

Expand|Select|Wrap|Line Numbers
  1. // The "Sudoku" class.
  2. import java.awt.*;
  3. import hsa.Console;
  4. public class Sudoku
  5. {
  6.     static Console c;           // The output console
  7.     public static void main (String[] args)
  8.     {
  9.         c = new Console ();
  10.  
  11.         final int ROWS = 9;
  12.         final int COLS = 9;
  13.         int row;
  14.         int col;
  15.         int newValue = 0;
  16.         /* row and col are what the user enters, to locate what place they want to change. New value is what 
  17. they want to change to*/
  18.  
  19.  
  20.  
  21.          int[] [] board = new int [ROWS] [COLS];
  22.        //creates the array
  23.         c.println("");
  24.         c.println("");
  25.         c.println("");
  26.         while (newValue != -1)   // entire loop prints ///////////////////////////////////all the numbers out
  27.         {
  28.             for (int i = 0 ; i < ROWS ; i++)
  29.             {
  30.  
  31.                 for (int j = 0 ; j < COLS ; j++)
  32.                 {
  33.  
  34.                     c.print ("    " + board [i] [j]);
  35.  
  36.                 }
  37.                 c.println ("");
  38.  
  39.  
  40.             }
  41.             c.print ("Row: ");
  42.             row = c.readInt () - 1;
  43.             c.print ("Column: ");
  44.             col = c.readInt () - 1;
  45.             c.print ("change to: ");
  46.             newValue = c.readInt ();//inputs 4 location
  47.             if (newValue < 1 || newValue > 9 || row <0 || row > 8 || col < 0 ||col>8)
  48.             {
  49.                 c.print ("your entry is invalid");
  50.                 c.clear();
  51.             }
  52.             else
  53.             {
  54.                 board [row] [col] = newValue;
  55.                 c.print (board [row] [col]);
  56.                 c.clear ();
  57.             }
  58.  
  59.  
  60.         }
  61.     }
  62. } // this is all it does, please help!
  63. // main method
  64. // Sudoku class
Jun 10 '10 #1
3 3914
jkmyoung
2,057 Recognized Expert Top Contributor
Since you have the two for loops printing the value of the cell, just before that, set that cell to a random value, eg

board[i][j] = rand()*9 + 1
c.print (" " + board [i] [j]);
Jun 10 '10 #2
diggity
2 New Member
the problem with doing that is that i also have to stay within the ruels of the game, the same number may appear twice in the same row/column or 3x3 cell
Jun 11 '10 #3
jkmyoung
2,057 Recognized Expert Top Contributor
How do you want to check for duplicates?
What do you want to do if there are no more possibilities (eg, the numbers generated so far lead to an invalid solution.)
Jun 14 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
2
by: Mike P | last post by:
I have a method that I am using to generate random numbers using Random.Next. However, every time I call the method I get the same random number. Is there a C# equivalent of Randomize is VB6? ...
10
by: Glenn Wilson | last post by:
I have a quick Question and I Hope some one can help or at least explain. What is happening is that I am trying to use random numbers in an application, as per the sample test code below. When I...
5
by: dav3 | last post by:
I have almost completed a monster assignment on sorting algorithms (quick, insertion and selection) using c++ but I am lost on one part of the assignment. I have to generate a random list of numbers...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
8
by: kiranchahar | last post by:
Hey all, How do I generate random numbers with Uniform distribution Uniform(a,b) using C-programming? I want to generate uniform random numbers which have mean following Uniform(p,q) and also...
0
SammyB
by: SammyB | last post by:
These are some "random" thoughts about generating random numbers in Visual Basic. Wikipedia will give a better introduction than I, see http://en.wikipedia.org/wiki/Random_number_generator. ...
2
by: enrique21 | last post by:
Im trying to generate random numbers using the statement: Random r = new Random(2); for (int i=1;i<=18;i++) but im having errors in my results here is my entire code so far: import...
12
by: 9966 | last post by:
Greetings, This is my second post till now. Thanks for all the advice given to me for the first post. Now I'm having problem with generating random numbers. I know if we want to generate a...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.