473,774 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Arrays and Random

6 New Member
Hi, I am a beginner in JAVA language...
I am making a simple sudoku game (in notepad -I have to use only notepad).
I have my sudoku tables stored in 2d arrays e.g sudoku1[9][9].
Lets say I have 5 2d-arrays, sudoku1....sudo ku5, and I want a random one to be chosen... how do I do that.
I know I have to use class Random, but how do I make it in this case (i.e 2d arrays)??

also
this is how u put values in a 2d array...
int [][] example2d=
{
{0, 3, 2},
{0, 5, 8},
};
how can u put values in a 3d array??
int [][][] example3d=
???????
May 3 '07 #1
10 2016
Ganon11
3,652 Recognized Expert Specialist
Are the different sudoku values in several different files, or all in the same file?

To input into a 3d array...
Expand|Select|Wrap|Line Numbers
  1. int my3DArray = { { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} },
  2.                   { {10, 11, 12}, {13, 14, 15}, {16, 17, 18} },
  3.                   { {19, 20, 21}, {22, 23, 24}, {25, 26, 27} } }
May 3 '07 #2
Sheza7
6 New Member
I am a beginner, I am not sure about what you mean by filing them (I mean as a java programmer)... I have them stored like this...
Expand|Select|Wrap|Line Numbers
  1.   int [][] sudok1= 
  2.   {
  3.    {3, 5, 9, 8, 7, 1, 6, 4, 2},
  4.    {1, 2, 4, 3, 6, 9, 7, 5, 8},
  5.    {8, 6, 7, 2, 5, 4, 9, 1, 3},
  6.    {5, 8, 3, 1, 4, 7, 2, 9, 6},
  7.    {2, 9, 1, 6, 3, 8, 5, 7, 4},
  8.    {7, 4, 6, 5, 9, 2, 8, 3, 1},
  9.    {6, 7, 5, 4, 8, 3, 1, 2, 9},
  10.    {4, 1, 8, 9, 2, 5, 3, 6, 7},
  11.    {9, 3, 2, 7, 1, 6, 4, 8, 5},
  12.   };
  13.   int [][] sudok2= 
  14.   {
  15.    {3, 5, 9, 8, 7, 1, 6, 4, 2},
  16.    {1, 2, 4, 3, 6, 9, 7, 5, 8},
  17.    {8, 6, 7, 2, 5, 4, 9, 1, 3},
  18.    {5, 8, 3, 1, 4, 7, 2, 9, 6},
  19.    {2, 9, 1, 6, 3, 8, 5, 7, 4},
  20.    {7, 4, 6, 5, 9, 2, 8, 3, 1},
  21.    {6, 7, 5, 4, 8, 3, 1, 2, 9},
  22.    {4, 1, 8, 9, 2, 5, 3, 6, 7},
  23.    {9, 3, 2, 7, 1, 6, 4, 8, 5},
  24.   };
Is there a way of putting them into a file and extracting them?

I get this error for the 3d one you posted...
Expand|Select|Wrap|Line Numbers
  1. P1.java:17: illegal initializer for int
  2.  int my3DArray = { { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} },
For the randoming I came with the idea of putting all 2d arrays (sudoku1 & sudoku2 in this case) in a 3d one. Thus using
Expand|Select|Wrap|Line Numbers
  1.   Random ran = new Random();
  2.   sudok[ran.nextInt(2)];
...I will be able to extract a random 2d array.
Is that good, or is there a better way to do it?
May 3 '07 #3
Ganon11
3,652 Recognized Expert Specialist
Ahh...sorry, I had read your question wrong and thought you were storing the sudoku puzzles in a text file. Go ahead and ignore that first line :)

Yes, creating a 3d array like that should work...I wonder if you can initialize it like this:

Expand|Select|Wrap|Line Numbers
  1. int[][] sudoku1 = { /* blah blah blah here... */ };
  2. int[][] sudoku2 = { /* blah blah blah here too... */ };
  3. // ...and so on...
  4. int[][][] sudokus = {sudoku1, sudoku2, /* blah blah blah... */ };
May 4 '07 #4
Sheza7
6 New Member
P.S.:
I got the 3d array working (...forgot about [][][])
May 4 '07 #5
Sheza7
6 New Member
Expand|Select|Wrap|Line Numbers
  1. int[][] sudoku1 = { /* blah blah blah here... */ };
  2. int[][] sudoku2 = { /* blah blah blah here too... */ };
  3. // ...and so on...
  4. int[][][] sudokus = {sudoku1, sudoku2, /* blah blah blah... */ };
Can I do something like this...
Expand|Select|Wrap|Line Numbers
  1. int [][][] sudokus = { { {sudok1} },
  2.                               { {sudok2} } };
... I mean do I have to retype the whole 2d arrays again, isn't there a way to reference to them.
May 4 '07 #6
Ganon11
3,652 Recognized Expert Specialist
Well, if sudok1 and sudok2 are already 2d arrays, then you can't set them to the third 'array' of sudokus. You wouldn't have to retype the 2d arrays - you said you already had them declared in your program, yes? Then just initialize sudokus using those variables.
May 4 '07 #7
Sheza7
6 New Member
ok I guess I can do that...
Would it be possible (and also more efficient) to store&retrieve sudokus in a text file??

P.S: THANKS for all the help!
May 4 '07 #8
Ganon11
3,652 Recognized Expert Specialist
It definitely would be possible to store the sudokus in a text file. It would also allow you to (relatively) easily add more puzzles to be used in your program. Whether it's more efficient, though, is something I'm not sure of. I know it would take longer to open the file and read the numbers into your arrays. Whether that extra flexibility in adding puzzles is worth the added loading time is up to you.
May 4 '07 #9
Sheza7
6 New Member
But if I put them in a text file will I be able to retrieve a random table (pull randomly a whole 2d array).
I mean now that I have put them in a 3d one I can choose a random one.
Expand|Select|Wrap|Line Numbers
  1. my3DArray[ran.nextInt(3)][][]
I guess I have to first learn how the text file retrieving works.

also
how can the "user" store (meaning to store while the program is runned)?
Expand|Select|Wrap|Line Numbers
  1.  n=sc.nextInt()
How can that value stay permanently?
May 4 '07 #10

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

Similar topics

10
2506
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone can help it would be greatly appreaciated, I have tried many of
20
2687
by: titi | last post by:
Question The road and traffic authority for a small country currently uses a system to store information about all 'currently' licensed drivers. A licensed driver has the following info stored about them in a record; 1. Given name(s) of the license holder.(not more than 128 char (may have spaces)). 2. Surname of the license holder.(Not more then 128 same like above).
3
1926
by: bb91915 | last post by:
I am taking a beginners class in C and am attempting to write code for extra problems my instructor gave us. These are not for grade and are only used solely for practice. I have a hard time taking pseudocode and turning it into code. Since I am in a beginners class, we have only learned so much to date, so what I am presenting will be really rough, so please pardon my ignorance. Here are my instructions, with my code to follow: ...
1
4152
by: Michael Fitzpatrick | last post by:
Transferring arrays from C DLL's to VB.Net I have a DLL written in C. This DLL reads a text file and creates a several very large arrays, 500,000 points and even larger. I would like the get the data in VB.Net so that I can plot it. Presently I am creating an equally sized array in VB and copying the data from the DLL's array into the VB array. There must be a better way. I looked into using a SAFEARRAY but it looks to me that VB.Net...
6
3670
by: MC | last post by:
Good Morning Anyone know any good lessons online about Arrays in vb.net. I am a hobbiest and stuggling to understand Arrays. Let me explain briefly what I want to do. I am writing a Poker Solutions Program purely for my oen fun and want to shuffle a deck of cards. now the easiest way, i think, is to make an Array that looks like this:
5
1843
by: aaragon | last post by:
Hello everybody, I appreciate your taking the time to take a look at this example. I need some help to start the design of an application. To that purpose I'm using policy-based design. The idea is to have a Class that stores elements of Class2 in different ways (arrays in stack memory, arrays in heap memory, using the std::vector and so on). I would like the user to customize the creation of a class with Class2 and StoragePolicy like...
36
1896
Bigj0410
by: Bigj0410 | last post by:
i've been trying to figure out these questions and i can't seem to understand them...can anyone help? 1)Write a full program in C++ that creates the two-dimensional randomArray array with 10 rows and 10 columns, and fills it with random numbers respectively: the first row with random number in range [1, 100), the second row with random number in range [100, 200), …, the 10th row with random number in range [900, 1000), The program then...
16
2040
by: wokeup2sleep | last post by:
im a beginner at comp science, and my prof is using python, which is totally new tom me i had a few questions with the program im writing, im having a few problems. brief summary i have to do a project where there are nine boxes filled with number 1-8, and 1 space is empty, the game needs to be scrambled, also all the moves need to be saved. the game can be paused anytime during thegame. the object is to move the numbers into the black spot...
4
7332
by: Leroymetzker | last post by:
//My card class import java.util.Arrays; import java.util.Random; public class Card { //Attributes private int suit; private int value;
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9914
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8939
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2852
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.