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:
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):
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?
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