Results 1 to 9 of 9
Like Tree3Likes
  • 1 Post By Tolls
  • 2 Post By PhHein

Thread: Some truble with simple program

  1. #1
    OPTYMISTA is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Question 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:

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

    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();
    		
    	}
    }
    And my main program, with "Turn-method = omGang()":

    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");
    	}
    }
    It would be awesome if some1 could help me out with this1 ;)

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default 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().
    OPTYMISTA likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    OPTYMISTA is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default 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".
    Java Code:
    int Sum = Player.points;
    . I suppose that i can't call variable form another constructor?

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default 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()).
    Tolls and OPTYMISTA like this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Some truble with simple program

    OPTYMISTA, please go through the Forum Rules, especially the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    OPTYMISTA is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Re: Some truble with simple program

    OPTYMISTA, please go through the Forum Rules, especially the third paragraph.
    Sorry, it won't happen again... ;)

    Now I've figured out how I refer each player object to different array-position. Such as Some truble with simple program-array.png. Now I want to do the same thing with points. I've written an extra statement in "omGang()" that says:
    Java Code:
     return sum;
    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.

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default 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

  8. #8
    OPTYMISTA is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default 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?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: Some truble with simple program

    Why?
    If you have the two Player objects then simply compare the two points values held by them.
    Java Code:
    if (player1.getPoints() > player2.getPoints()) {
       System.out.println("Player 1 wins!");
    } else {
       System.out.println("Player 2 wins!");
    }
    (Note I have ignored draws there...)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Help with simple program...
    By blink16j in forum New To Java
    Replies: 4
    Last Post: 01-15-2013, 06:59 PM
  2. Very Simple Program But Can't Get Right
    By mike_ in forum New To Java
    Replies: 3
    Last Post: 11-21-2011, 02:45 AM
  3. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 06:12 AM
  4. Please help with simple program.. Very simple.
    By jonytek in forum New To Java
    Replies: 7
    Last Post: 02-14-2011, 01:44 AM
  5. simple program
    By blastoff in forum New To Java
    Replies: 5
    Last Post: 04-15-2010, 12:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •