Some trubles with a simple program
I have to write a simple "Black Jack" game with dices. Game can have random number of players.
At the beginning of the game each player roll 3 dices. Program have to add values of the dices and show the result.
Afterwards asks program if you want to continue, also "if you want to roll next dice"...until player answer "no".
Then it should save the value of points this player gained, and go to the next player. When i debugged program it turned out that instead of saving each
"value of points" in arrayPOINTS, it jumpes of whole "for" and value in arrayPOITS is deleted (=0). I don't know how to refer players to an array either.
I'm not so good in "arrays", so I have some though time writin' this program.
Here's my "roll-dice" class:
Code :
package practice5;
public class Terning {
public int dice;
public Terning() {
roll();
}
public int roll() {
dice = (int)(Math.random()*6) +1;
return dice;
}
}
Here's my player class (It should at least save name and value of poits gained) and player method (Ask about name, roll 3 dices and assume values):
Code :
package practice5;
import java.util.Scanner;
public class Player {
String player;
int points;
public void setPoints(int points) {
this.points = points;
}
public String getPlayer() {
return player;
}
public void setPlayer(String player) {
this.player = player;
}
public int getPoints() {
return points;
}
public Player() {
int kast1;
int kast2;
int kast3;
Scanner inn = new Scanner(System.in);
System.out.print("Name: ");
player = inn.next();
Terning terning = new Terning();
kast1 = terning.roll();
kast2 = terning.roll();
kast3 = terning.roll();
points = kast1 + kast2 + kast3;
System.out.print(points);
System.out.println();
}
}
And my main program, with "Turn-method = omGang()":
In addition: How can I call variable in omGang-method from Player-constructor?
Code :
package practice5;
import java.util.Scanner;
public class MainProgram {
public static void main(String[] args) {
int ANTALL_SPILLERE = 2;
// Hopper ut av while, så blir i=0 igjen.
for(int j=0;j<ANTALL_SPILLERE;j++) {
Player spiller = new Player();
omGang();
}
}
public static void omGang() {
int ANTALL_SPILLERE = 2;
boolean choice = true;
String valg;
int next_kast;
int Sum = Player.points; // (How can I call variable in omGang-method from Player constructor?)
// int ANTALL_SPILLERE = 2;
String[] Spillere;
Spillere = new String[ANTALL_SPILLERE];
int[] Poeng;
Poeng = new int[ANTALL_SPILLERE];
Terning terning = new Terning();
Scanner ny = new Scanner(System.in);
// hopper ut av løkka etter første omgang, hvorfor lagrer den ikke verdiene?
for(int i=0;i<ANTALL_SPILLERE+1;i++) {
while(choice == true) {
System.out.print("Vil du fortsette?(j/n): ");
valg = ny.next();
if(valg.equalsIgnoreCase("n")){
choice = false;
break;
}
next_kast = terning.roll();
Sum = Sum + next_kast;
System.out.println(Sum);
Poeng[i] = Sum;
if(Sum>21) {
System.out.print(Spillere[i]);
System.out.print(" tapte. ");
choice = false;
break;
}
if(Sum==21) {
System.out.print(Spillere[i]);
System.out.print(" vunnet. ");
choice = false;
break;
}
}
}
System.out.print("TEST");
}
}
It would be awesome if some1 could help me out with this1 ;)
Re: Some trubles with a simple program
Quote:
How can I call variable in omGang-method from Player-constructor
Variables defined in methods can not be referenced by other code. They are local to the method.
The method could return the value of a variable.
Re: Some trubles with a simple program
Quote:
an extra line in "Player-constructor"
Constructors do NOT return the value of a variable.
The omGang() method can return a value.
1 Attachment(s)
Re: Some trubles with a simple program
Now I've figured out how I refer each player object to different array-position. Such as Attachment 1779. Now I want to do the same thing with points. I've written an extra statement in "omGang()" that says: there sum is the assumed value of points after rolling dice.
I'm beginner and I want to understand what I'm doing, step by step. No matter if I sound like an idiot ;)) So thank YOU in advance for your attention to this matter.
Re: Some trubles with a simple program
Are you getting errors now? Copy the full text of the error messages and post them.
Does the code do what you want now? If not please explain.
Re: Some trubles with a simple program
Quote:
have to add up the points of the current player. But how can i do that?
Where are the points? In this array: arrayPOINTS
A way to add up the contents of an array is to use a loop. Initialize the sum to 0 before the loop, then add each element to the sum in the loop.
Re: Some trubles with a simple program
I have assumed the value of array "arrayPOINTS" in omGang-method. Here:
Code :
next_kast = terning.roll();
sum = sum + next_kast;
System.out.println(sum);
Poeng[i] = sum;
But this array is local and only for omGang-method, so I use it only to assume the value of points. What I want now is that this value can be saved in my array, which I call in main.
Re: Some trubles with a simple program
Are you asking how to return a value from a method to the code where the method is called?
What value(s) do you want returned from the method?
Another idea would be to pass a reference to the array to the method so that the method could update the contents of the array: