3 Replies - 5229 Views - Last Post: 09 April 2007 - 07:22 PM Rate Topic: -----

#1 rocketdive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-April 07

Print char on random position in grid

Posted 09 April 2007 - 06:08 PM

I'm trying to make a program that randomly places one of 5 letters in a random place on a 20x20 grid. So the output should look something like this:

--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
----R---------------
--------------------
--------------------
--------------------


With the 'R' being the random letter (actually representing a color, in this case Red) and the random position being (5,17). I can generate the random letter, but I've no clue how to put it in a random spot on the grid. I'm thinking I have to do something along the lines of
if something
	print the letter
else
	print "-"
but i'm not sure what


My code for the program so far is...
import javax.swing.JOptionPane;

public class Homework5
{
	public static void main (String[]args)
	{
		int carColor = 0, color = 0;
		char colorChar = 0;
		
		System.out.println("Car Stats:");
				
		color = 1 + (int) (Math.random() * 5);		
		paint(color);
						
		int posHorizontal = 0, posVertical = 0;
		posHorizontal = 1 + (int) (Math.random() * 20);
		posVertical   = 1 + (int) (Math.random() * 20);
		System.out.print("\n Location: " + posHorizontal + ", " + posVertical);
		
		
		
System.out.println();		
for (int i = 0; i < 20; i++)
{
	for (int j = 0; j < 20; j++)
	{
		System.out.print("-");
	}
	System.out.println();
}	


	}		
		
	public static int paint(int color)
	{			
		char colorChar = 0;
		System.out.print(" Color:	");
		
		switch (color) {
			case 1:	System.out.print("Red");
				colorChar = 'R';
				break;
			case 2:	System.out.print("Green");
				colorChar = 'G';
				break;
			case 3:	System.out.print("Blue");
				colorChar = 'B';
				break;
			case 4:	System.out.print("White");
				colorChar = 'W';
				break;
			case 5:	System.out.print("Silver");
				colorChar = 'S';
				break;
		}
		System.out.println();
		return color;
	}
		
}


Another problem I've got is assigning the color for the initial readout ("Color: Red") /and/ the char to be placed in the grid ('R'). I don't know if I've done it right since I can't figure out how to put the char on the grid in the first place.
Also I know my nested for loop is most likely in the wrong place. Should it be somewhere in one of the methods I know I need to create, for vertical or horizontal position of the letter?
Thanks for any help! and sorry for anything that may not make sense in my question..

Is This A Good Question/Topic? 0
  • +

Replies To: Print char on random position in grid

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Print char on random position in grid

Posted 09 April 2007 - 06:47 PM

why not use a 2D array, ex: char grid[][] = new char[20][20];
then you can create random numbers between 0 and 19 twice, to find the position in the grid to place the letter. Initialize the array with all '-' chars, and display the array.
Was This Post Helpful? 0
  • +
  • -

#3 rocketdive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-April 07

Re: Print char on random position in grid

Posted 09 April 2007 - 07:04 PM

Ah! Sounds like an easy way to do it, but I haven't learned arrays quite yet. And this is for a class, so I don't think I should be jumping the gun..
Is there possibly another way I could do this, just using methods and all that easier stuff instead?
Thanks for your suggestion though-
Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Print char on random position in grid

Posted 09 April 2007 - 07:22 PM

no worries, a more basic approach could be, generate your 2 random numbers as int row, col; then:
for (int i = 0; i < 20; i++)
{
	for (int j = 0; j < 20; j++)
	{
		if(i==row&&j==col){
			 System.out.print(colorChar);
		}else{
			 System.out.print("-");
		}
	}
	System.out.println();
}


this should print out the - char in all positions except the position which matches both random row and column, which will be the value of colorChar.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1