Results 1 to 9 of 9
Thread: Some truble with simple program
- 02-14-2013, 12:57 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 18
- Rep Power
- 0
Some truble with 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:
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):Java Code:package practice5; public class Terning { public int dice; public Terning() { roll(); } public int roll() { dice = (int)(Math.random()*6) +1; return dice; } }
And my main program, with "Turn-method = omGang()":Java Code:package practice5; import java.util.Scanner; public class Player { static String player; static 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 void 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(); } }
It would be awesome if some1 could help me out with this1 ;)Java 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(); spiller.Player(); omGang(); } } public static void omGang() { int ANTALL_SPILLERE = 2; boolean choice = true; String valg; int next_kast; int Sum = Player.points; // 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"); } }
- 02-14-2013, 01:26 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Some truble with simple program
Your Player class has static variables in there.
They should not be static, as that means there is a single name or points value for all Players.
Also I'm guessing the 'void Player()' is supposed to be a constructor, so remove the void, and you can skip the 'spiller.Player()' line later on.
Of course all that is on the assumption you are using the Player object called 'spiller', and there's no sign of that in omgang().Please do not ask for code as refusal often offends.
- 02-14-2013, 01:35 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 18
- Rep Power
- 0
Re: Some truble with simple program
Thanks for quick reply. When I changed variables in "Player class" to not be static, I've got an error in "omGang-method".
. I suppose that i can't call variable form another constructor?Java Code:int Sum = Player.points;
- 02-14-2013, 01:42 PM #4
Re: Some truble with simple program
Get rid of all the static stuff. Use an Array of Player in MainProgram. Iterate over the Array's elements and use the getter and setter methods for name and points.
Coding standards: variables and method names always start with lower case characters. Never name methods the same as the class name (player()).Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 02-14-2013, 04:23 PM #5
Re: Some truble with simple program
OPTYMISTA, please go through the Forum Rules, especially the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-14-2013, 04:29 PM #6
Member
- Join Date
- Feb 2013
- Posts
- 18
- Rep Power
- 0
Re: Some truble with simple program
Sorry, it won't happen again... ;)OPTYMISTA, please go through the Forum Rules, especially the third paragraph.
Now I've figured out how I refer each player object to different array-position. Such as
. 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.Java Code:return sum;
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.
- 02-14-2013, 04:32 PM #7
Re: Some truble with simple program
Uhm, a player object already encapsulates a name and a score (points). So you just have to add up the points of the current player.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 02-14-2013, 04:55 PM #8
Member
- Join Date
- Feb 2013
- Posts
- 18
- Rep Power
- 0
Re: Some truble with simple program
I think i didn't quite get what do you mean. I know that player object contains value "points", but I'm not sure how to use it.
Later I want to compare scores, so I can decide who wins. Shouldn't values of points for each player be saved in an new array (arrayPOINTS) then?
- 02-14-2013, 05:41 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Some truble with simple program
Why?
If you have the two Player objects then simply compare the two points values held by them.
(Note I have ignored draws there...)Java Code:if (player1.getPoints() > player2.getPoints()) { System.out.println("Player 1 wins!"); } else { System.out.println("Player 2 wins!"); }Please do not ask for code as refusal often offends.
Similar Threads
-
Help with simple program...
By blink16j in forum New To JavaReplies: 4Last Post: 01-15-2013, 05:59 PM -
Very Simple Program But Can't Get Right
By mike_ in forum New To JavaReplies: 3Last Post: 11-21-2011, 01:45 AM -
Simple program, simple problem
By taymilll in forum New To JavaReplies: 12Last Post: 06-20-2011, 05:12 AM -
Please help with simple program.. Very simple.
By jonytek in forum New To JavaReplies: 7Last Post: 02-14-2011, 12:44 AM -
simple program
By blastoff in forum New To JavaReplies: 5Last Post: 04-14-2010, 11:25 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks