Reputation: 13
I'm trying to draw some dice next to each other, but I'm haven't been successful. I think that there is a better way of doing this than with IF
. So if anyone knows how that would be possible I'll appreciate your response.
public static void main(String[] args) {
int dice1 = 0;
int dice2 = 0;
int dice3 = 0;
dice1 = (int) (Math.random() * 6 + 1);
dice2 = (int) (Math.random() * 6 + 1);
dice3 = (int) (Math.random() * 6 + 1);
if (dice1 == 1) {
System.out.println("* * * * *");
System.out.println("* *");
System.out.println("* # *");
System.out.println("* *");
System.out.println("* * * * *");
}
if (dice1 == 2) {
System.out.println("* * * * *");
System.out.println("* # *");
System.out.println("* *");
System.out.println("* # *");
System.out.println("* * * * *");
}
if (dice1 == 3) {
System.out.println("* * * * *");
System.out.println("* # *");
System.out.println("* # *");
System.out.println("* # *");
System.out.println("* * * * *");
}
if (dice1 == 4) {
System.out.println("* * * * *");
System.out.println("* # # *");
System.out.println("* *");
System.out.println("* # # *");
System.out.println("* * * * *");
}
if (dice1 == 5) {
System.out.println("* * * * *");
System.out.println("* # # *");
System.out.println("* # *");
System.out.println("* # # *");
System.out.println("* * * * *");
}
if (dice1 == 6) {
System.out.println("* * * * *");
System.out.println("* # # *");
System.out.println("* # # *");
System.out.println("* # # *");
System.out.println("* * * * *");
}
if (dice2 == 1) {
System.out.println("* * * * *");
System.out.println("* *");
System.out.println("* # *");
System.out.println("* *");
System.out.println("* * * * *");
}
if (dice2 == 2) {
System.out.println("* * * * *");
System.out.println("* # *");
System.out.println("* *");
System.out.println("* # *");
System.out.println("* * * * *");
}
if (dice2 == 3) {
System.out.println("* * * * *");
System.out.println("* # *");
System.out.println("* # *");
System.out.println("* # *");
System.out.println("* * * * *");
}
if (dice2 == 4) {
System.out.println("* * * * *");
System.out.println("* # # *");
System.out.println("* *");
System.out.println("* # # *");
System.out.println("* * * * *");
}
if (dice2 == 5) {
System.out.println("* * * * *");
System.out.println("* # # *");
System.out.println("* # *");
System.out.println("* # # *");
System.out.println("* * * * *");
}
if (dice2 == 6) {
System.out.println("* * * * *");
System.out.println("* # # *");
System.out.println("* # # *");
System.out.println("* # # *");
System.out.println("* * * * *");
}
if (dice3 == 1) {
System.out.println("* * * * *");
System.out.println("* *");
System.out.println("* # *");
System.out.println("* *");
System.out.println("* * * * *");
}
if (dice3 == 2) {
System.out.println("* * * * *");
System.out.println("* # *");
System.out.println("* *");
System.out.println("* # *");
System.out.println("* * * * *");
}
if (dice3 == 3) {
System.out.println("* * * * *");
System.out.println("* # *");
System.out.println("* # *");
System.out.println("* # *");
System.out.println("* * * * *");
}
if (dice3 == 4) {
System.out.println("* * * * *");
System.out.println("* # # *");
System.out.println("* *");
System.out.println("* # # *");
System.out.println("* * * * *");
}
if (dice3 == 5) {
System.out.println("* * * * *");
System.out.println("* # # *");
System.out.println("* # *");
System.out.println("* # # *");
System.out.println("* * * * *");
}
if (dice3 == 6) {
System.out.println("* * * * *");
System.out.println("* # # *");
System.out.println("* # # *");
System.out.println("* # # *");
System.out.println("* * * * *");
}
}
Upvotes: 0
Views: 866
Reputation: 652
I can't for the life of me understand, why you would want to draw a set of dice in the console, instead of just writing it with a number:
System.out.println("Dice1: " + dice1);
But I'll bite. Make a new class file named Die, and copy the following code into the new file.
public class Die{
public int value;
Random r = new Random();
public Die(){
roll();
}
public void roll(){
value = r.nextInt(6) + 1;
}
public String retrieveOneLineOfDrawing(int linenumber){
switch(value){
case 1:
switch(linenumber){
case 1: return "* * * * *";
case 2: return "* *";
case 3: return "* # *";
case 4: return "* *";
case 5: return "* * * * *";
default: return "* * * * *";
}
case 2:
switch(linenumber){
case 1: return "* * * * *";
case 2: return "* # *";
case 3: return "* *";
case 4: return "* # *";
case 5: return "* * * * *";
default: return "* * * * *";
}
case 3:
switch(linenumber){
case 1: return "* * * * *";
case 2: return "* # *";
case 3: return "* # *";
case 4: return "* # *";
case 5: return "* * * * *";
default: return "* * * * *";
}
case 4:
switch(linenumber){
case 1: return "* * * * *";
case 2: return "* # # *";
case 3: return "* *";
case 4: return "* # # *";
case 5: return "* * * * *";
default: return "* * * * *";
}
case 5:
switch(linenumber){
case 1: return "* * * * *";
case 2: return "* # # *";
case 3: return "* # *";
case 4: return "* # # *";
case 5: return "* * * * *";
default: return "* * * * *";
}
case 6:
switch(linenumber){
case 1: return "* * * * *";
case 2: return "* # # *";
case 3: return "* # # *";
case 4: return "* # # *";
case 5: return "* * * * *";
default: return "* * * * *";
}
default: return "* * * * *";
}
}
}
STOP! Now, remove everything from your "public static void main(String[] args) {", and use this instead.
public static void main(String[] args) {
Die[] dice = new Die[3];
dice[0] = new Die();
dice[1] = new Die();
dice[2] = new Die();
int numberOfLinesInDrawings = 5;
String s = "";
for(int i = 1; i < numberOfLinesInDrawings+1; i++){
for(int j = 0; j < dice.length; j++){
s += dice[j].retrieveOneLineOfDrawing(i);
if(j < dice.length - 1) s += "\t\t";
}
if(i < numberOfLinesInDrawings) s += "\n";
}
System.out.println(s);
}
This gives you the opportunity to increase the amount of lines there are in your drawings, so you can make them bigger. They all have to have an equal number of lines, though. There is no real error-handling in this code. If your numberOfLinesInDrawings number doesn't match all the 6 drawings, you may get an exception.
If you can use StringBuilder, then do this instead:
public static void main(String[] args) {
Die[] dice = new Die[3];
dice[0] = new Die();
dice[1] = new Die();
dice[2] = new Die();
int numberOfLinesInDrawings = 5;
StringBuilder sb = new StringBuilder();
for(int i = 1; i < numberOfLinesInDrawings+1; i++){
for(int j = 0; j < dice.length; j++){
sb.append(dice[j].retrieveOneLineOfDrawing(i));
if(j < dice.length - 1) sb.append("\t\t");
}
if(i < numberOfLinesInDrawings) sb.append("\n");
}
System.out.println(sb);
}
StringBuilder is much more efficient than normal String-concatenation (+=).
EDIT: I'm sorry for the solution not working to begin with. I had no way of testing when I wrote it, and unfortunately, there were a few errors. They have been fixed now.
Upvotes: 0
Reputation: 13922
There's a million ways you could go, but but I would personally put each of the dice in a multidimensional array. First, put each die in an array
String[] die1 = new String[]{
"* * * * *",
"* *",
"* # *",
"* *",
"* * * * *"
}
For each line, you can print each die side by side like this
System.out.println(die1[0] + " " + die2[0]); // and so on for each of the 5 lines.
But instead of die1, die2, etc I'd put each of those arrays into a big dice array so you can easily reference each die by index (die[0][0], die[1][0], etc).
String[][] dice = new String[][]{
{ "* * * * *", // Die 1 -> this line is dice[0][0]
"* *",
"* # *",
"* *",
"* * * * *"
},
{ "* * * * *", // Die 2
"* # *",
"* *",
"* # *",
"* * * * *"
}
}
Now, printing your lines side by side is as easy as:
int roll1 = 2; // Two made
int roll2 = 5; // up numbers
/* Note that the index will be one off from the reflected number.
You may want to subtract 1 from your random number or put a blank die at index 0
*/
for(int x=0; x<dice[roll1].length; x++){
System.out.println(dice[roll1][x] + " " + dice[roll2][x]);
}
This way, you only "draw" each die once instead of dice*numberOfRolls; you don't need a ton of if-elses; and it allows you to print the dice any way you want by concatenating strings.
Upvotes: 3
Reputation: 4319
I did it within a test, don't wonder.
You want something like that, right?
* * * * * * * * * * * * * * *
* # # * * # * * # *
* * * * * # *
* # # * * # * * # *
* * * * * * * * * * * * * * *
The following will work, it begins with the method "rollTheDice()" - have fun:
private final static String BORDER = "* * * * *";
private final static String LEFT = "* ";
private final static String RIGHT = " *";
private final static String NEWLINE = "\n";
private final static String NEXT_DIE = " ";
@Test
public void doit() {
this.rollTheDice();
}
private void rollTheDice() {
int[] dice = { (int) (Math.random() * 6 + 1), (int) (Math.random() * 6 + 1), (int) (Math.random() * 6 + 1) };
StringBuilder sb = new StringBuilder();
sb.append(BORDER).append(NEXT_DIE).append(BORDER).append(NEXT_DIE).append(BORDER).append(NEWLINE);
sb.append(this.createAllRows(dice, Position.TOP)).append(NEWLINE);
sb.append(this.createAllRows(dice, Position.MIDDLE)).append(NEWLINE);
sb.append(this.createAllRows(dice, Position.BOTTOM)).append(NEWLINE);
sb.append(BORDER).append(NEXT_DIE).append(BORDER).append(NEXT_DIE).append(BORDER).append(NEWLINE);
System.out.println(sb.toString());
}
private enum Position {
TOP, MIDDLE, BOTTOM
};
private String createAllRows(int[] dice, Position pos) {
String rows = "";
for (int die : dice) {
rows += this.createSingleRow(die, pos) + NEXT_DIE;
}
return rows;
}
private String createSingleRow(int die, Position pos) {
String row = LEFT;
switch (pos) {
case TOP: {
row += handleTop(die);
break;
}
case MIDDLE: {
row += handleMiddle(die);
break;
}
case BOTTOM:
default: {
row += handleBottom(die);
}
}
return row + RIGHT;
}
private String handleTop(int die) {
switch (die) {
case 1:
return " ";
case 2:
return " #";
case 3:
return " #";
case 4:
return "# #";
case 5:
return "# #";
case 6:
default:
return "# #";
}
}
private String handleMiddle(int die) {
switch (die) {
case 1:
return " # ";
case 2:
return " ";
case 3:
return " # ";
case 4:
return " ";
case 5:
return " # ";
case 6:
default:
return "# #";
}
}
private String handleBottom(int die) {
switch (die) {
case 1:
return " ";
case 2:
return "# ";
case 3:
return "# ";
case 4:
return "# #";
case 5:
return "# #";
case 6:
default:
return "# #";
}
}
Upvotes: 0
Reputation: 181
You could create a String variable for each line of the dice. you could then add to the string variables depending on what the dice roll was and print the variables at the end, to show the dice next to each other.
for example:
String ln1="";
String ln2="";
String ln3="";
String ln4="";
String ln5="";
if (dice1 == 2) {
ln1+="* * * * *";
ln2+="* # *";
ln3+="* *";
ln4+="* # *";
ln5+="* * * * *";
}
else if (dice1 == 3) {
ln1+="* * * * *";
ln2+="* # *";
ln3+="* # *";
ln4+="* # *";
ln5+="* * * * *";
}
...
System.out.println(ln1);
System.out.println(ln2);
System.out.println(ln3);
System.out.println(ln4);
System.out.println(ln5);
Hope this helps.
Upvotes: 1