ChoOrHan points fail

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 271 Views - Last Post: 06 September 2019 - 03:48 PM Rate Topic: -----

#1 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 22-October 18

ChoOrHan points fail

Posted 06 September 2019 - 08:45 AM

hey guys i nedd some help....im working on choORhan game and im finished no errors or something like that...but i have a problem with points and taht is that points for some reasnos doesent increse i always have 0 ponts even if i score the result..you know even if result is han and i also have a han in the result...my points is always 0 (for both players couse i have a 2 player) so after 5 round for player 1 and player 2 score is always 0

class die
public class die {
    
     private int sides;
    private int values;
    
    public die(int s)
    {
        sides=s;
        roll();
    }
    
    public void roll()
    {
        Random r=new Random();
        values=r.nextInt(6)+1;
        
    }
    
    public int getSides()
    {
        return sides;
    }
    public int getValues()
    {
        return values;
    }
    
    
}




class dealer:
public class dealer {
    
    public int die1Value;
    public int die2Value;
    
    public dealer()
    {
        die1Value=0;
        die2Value=0;
    }
    
    public void rollDice()
    {
         int sides=6;
         die die1=new die(sides);
         die die2=new die(sides);
         die1Value=die1.getValues();
         die2Value=die2.getValues();
         
         
        
    }
    
    public String getChoHan()
    {
        int sum=die1Value+die2Value;
        String result;
        
        if(sum%2==0)
            result="cho(even) ";
        else
            result="han(odd) ";
        
        return result;
            
        
    }
    public int getDIe1Value()
    {
        return die1Value;
    }
    public int getDie2Value()
    {
        return die2Value;
    }
}



class player :
import java.util.Random;


public class player {
   private String name,guess;
   private int points;
    
    public player(String pName)
    {
        name=pName;
        guess=" ";
        points=0;
    }
    
    public void makeGEuss()
    {
        Random r=new Random();
        int guessnumber=r.nextInt(2);
        if(guessnumber==0)
            guess="cho(even)";
        else
            guess="han(odd)";
        
    }
    public  void addPoints(int newpoint)
    {
        points+=newpoint;
    }
    
    public String Getname()
    {
        return name;
    }
    public String getGuess()
    {
        return guess;
    }
    public int getPoint()
    {
        return points;
    }
}




main program with 3 function :
public class JavaP25ChoHan {

    
    public static void main(String[] args) {
        int max=5;
        String p1Name;
        String p2Name;
        Scanner input=new Scanner(System.in);
        System.out.print("enter name for the first player: ");
        p1Name=input.nextLine();
        System.out.print("enter name for the second player: ");
        p2Name=input.nextLine();
        
        dealer d=new dealer();
        player p1=new player(p1Name);
        player p2=new player(p2Name);
        
        for(int i=0;i<max;i++)
        {
            System.out.println("--------------------------");
            System.out.printf("now playing round %d\n",i+1);
            d.rollDice();
            p1.makeGEuss();
            p2.makeGEuss();
            RoundResult(d,p1,p2);
            displayWinner(p1,p2);
            
        }
        
    }
    
    public static void RoundResult(dealer d,player p1,player p2)
           
    {
        System.out.printf("the dealer rolled %s and %s.\n",d.getDIe1Value(),d.getDie2Value());
        System.out.printf("Reslut %s\n",d.getChoHan());
        checkguess(p1,d);
        checkguess(p2,d);
        
        
        
        
    }
    
    public static void checkguess(player p,dealer d)
    {
        int points_to_add=1;
        String guess=p.getGuess();
        String result=d.getChoHan();
        System.out.printf("the player %s guessed %s\n",p.Getname(),p.getGuess());
        
        if(guess.equalsIgnoreCase(result))
        {
            p.addPoints(points_to_add);
            System.out.printf("awarding %d point(s) to %s\n",points_to_add,p.Getname());
        }
        
            
        
        
    }
    
    public static void displayWinner(player p1,player p2)
    {
        System.out.println("------------------------\n");
        System.out.print("Game over here are the result");
        System.out.printf("%s: %d points\n",p1.Getname(),p1.getPoint());
        System.out.printf("%s: %d points\n",p2.Getname(),p2.getPoint());
        
        if(p1.getPoint()>p2.getPoint())
            System.out.println(p1.Getname()+" is the grand winner");
        else if(p2.getPoint()>p1.getPoint())
            System.out.println(p2.Getname()+" is the grand winner");
        else
            System.out.print("result are tied");
        
    }
    
}



Is This A Good Question/Topic? 0
  • +

Replies To: ChoOrHan points fail

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,903
  • Joined: 25-December 13

Re: ChoOrHan points fail

Posted 06 September 2019 - 08:49 AM

What variables should hold the points that are always 0?
Where are the values in those variables changed?
Is the code that changes the values in the variables being executed?
If it is not executed then the values will stay at their initial values.

This post has been edited by NormR: 06 September 2019 - 08:49 AM

Was This Post Helpful? 0
  • +
  • -

#3 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 22-October 18

Re: ChoOrHan points fail

Posted 06 September 2019 - 09:34 AM

i just dont know...more i lock a code more im confused i don know where i did wrong
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,903
  • Joined: 25-December 13

Re: ChoOrHan points fail

Posted 06 September 2019 - 09:51 AM

Quote

i don know where i did wrong

Yes, that happens a lot when writing programs. Now you need to debug the code to see where the problem is. I listed several things to consider in my last post:

Quote

What variables should hold the points that are always 0?
Where are the values in those variables changed?
Is the code that changes the values in the variables being executed?
If it is not executed then the values will stay at their initial values.

Can you answer these questions?
What variable has the count?
Where are the values changed?
Is that code being executed? >> Add a print statement next to where the values are changed that prints a message so you know if the statement is being executed.

If the statements to update the variables are not being executed, look in the code to see why not.
If there is an if statement, add a print statement to print the values of the variables used in the if statement.

This post has been edited by NormR: 06 September 2019 - 09:52 AM

Was This Post Helpful? 0
  • +
  • -

#5 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 22-October 18

Re: ChoOrHan points fail

Posted 06 September 2019 - 10:03 AM

so i did variable points and then in constructor player i did points on 0..then i changed points on addpoints(where i did points+=newpoint so i increse it) then i returend point on getPoint method...
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,903
  • Joined: 25-December 13

Re: ChoOrHan points fail

Posted 06 September 2019 - 10:14 AM

Quote

where i did points+=newpoint

Did you add a print statement next to that statement to show that the value of points was changed?
Was anything printed when you executed the program?
If nothing was printed, then the code to increment points was not executed.
If that statement was not executed, why not?
Check the code to see how the program's execution would skip executing the above statement.
Was This Post Helpful? 0
  • +
  • -

#7 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 22-October 18

Re: ChoOrHan points fail

Posted 06 September 2019 - 11:18 AM

yes i did print statement and will not execute...why not??? hmmm i dont know yet
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,903
  • Joined: 25-December 13

Re: ChoOrHan points fail

Posted 06 September 2019 - 11:28 AM

Quote

will not execute...why not?

Is there an if statement that controls whether or not the statement executes?
Add a print statement to show what the values tested in the if statement contain.
   System.out.println("someVar=" + someVar);  // show variables value
   if(someVar > 30) { //  if statement uses value of someVar
      //  code that executes if value of someVar is greater than 30
   }else{
      System.out.println("someVar < 30, someVar="+someVar);  // also have else statement
   }


This post has been edited by NormR: 06 September 2019 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

#9 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 22-October 18

Re: ChoOrHan points fail

Posted 06 September 2019 - 11:44 AM

now im totaly lost...tomorow i will do this code from skratch once again and then see what is wrong...tnx for answers anyway
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,903
  • Joined: 25-December 13

Re: ChoOrHan points fail

Posted 06 September 2019 - 12:03 PM

Writing the whole thing again seems like more work than necessary.
You need to see why the current code is not doing what you want.
Was This Post Helpful? 0
  • +
  • -

#11 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 22-October 18

Re: ChoOrHan points fail

Posted 06 September 2019 - 02:08 PM

yes i now....but i just dont now what i nedd to do that my current code would work...i dont know what id dont see
Was This Post Helpful? 0
  • +
  • -

#12 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 22-October 18

Re: ChoOrHan points fail

Posted 06 September 2019 - 02:48 PM

so where in the hell i even have a problem?? in class player under method makeguess() or addpoints()...or in main under function checkguess()??
Was This Post Helpful? 0
  • +
  • -

#13 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,903
  • Joined: 25-December 13

Re: ChoOrHan points fail

Posted 06 September 2019 - 02:49 PM

Quote

where in the hell i even have a problem?

You need to do more debugging to see what the program sees when it is executed. Add more print statements.
What statement(s) control whether the points variable is incremented?
Is there an if statement that controls if the code is executed?
Check the values of the variables that are used.

This post has been edited by NormR: 06 September 2019 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#14 kretze   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 22-October 18

Re: ChoOrHan points fail

Posted 06 September 2019 - 02:58 PM

why dont you just say this and this is wrong..you need to do this and code will work??...im not a seasoned programer im begginer special in Java programing
Was This Post Helpful? 0
  • +
  • -

#15 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 781
  • View blog
  • Posts: 5,903
  • Joined: 25-December 13

Re: ChoOrHan points fail

Posted 06 September 2019 - 03:18 PM

Quote

im begginer

I am trying to show you how to debug your program so you will be able to do it in the future.

You need to see what the code in your program is doing when it executes. One way to see is to add print statements that show the values of the variables when the code executes. If you are expecting a variable to hold a specific value when it executes but the variable holds a different value, the code will not work as you expect. To see the actual values in the variables, print them out. The print out will show you where the problem is.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2