0
votes

I am in a Beginner CompSci course at university - Intro to Java.
I am looking for some insight as to why using boolean values to check if a roll is a TRIPLE or PAIR is supposed to make my code more efficient / readable / somehow better for this specific assignment.
We haven't learned how to use arrays yet.

We are to create a two-player game of chance against the computer.
The players each roll THREE 12-sided dice.

To win, you must roll a triple or pair. Otherwise the roll is Nothing.

Triple > Pair > Nothing

In case of 2 Triples: High value wins.
In case of 2 Pairs: High pair value wins.
In case of 2 identical Pairs: third dice is compared, and higher value wins.


The assignment DOESN'T SPECIFY what to do in cases of
identical winning rolls, e.g.:
2 identical triples
2 identical pairs, with an identical 3rd die.

However, I decided to include those cases.
Mostly because it would just bother me to not include all the cases.

We are supposed to use these pre-written method headings (and can use additional methods, if desired):

// returns true if the roll is a TRIPLE
public static boolean isTriple(int d1, int d2, int d3)

// returns true if the roll is a PAIR
public static boolean isPair(int d1, int d2, int d3)

// determine win, tie, lose. return either: -1, 0, or 1
public static int determineRound(int p1, int p2, int p3, int o1, int o2, int o3)

I wrote a program that uses a formula to assign a numerical score to EVERY POSSIBLE ROLL, which preserves the rank of each roll.
Lower score --> lower rank
Higher score --> higher rank

Then, the program compares the player's score to the computer's score.
High score wins.

My SPECIFIC question, is why should I be using booleans here?

And then a follow-up question: What is the point of having the determineRound method, which uses the six dice INT values as input, instead of the boolean values as input?

The relevant portions of my program are displayed below.

Thanks for your time!!

main{

  int p1, p2, p3; // player's 3 dice
  int o1, o2, o3; // opponent's 3 dice

  int playerScore = getScore(p1, p2, p3);    
  int opponentScore = getScore(o1, o2, o3);  

  if (playerScore > opponentScore) {
    System.out.println("Congrats, you win!");
  } else if (playerScore < opponentScore) {
    System.out.println("Sorry - you lose.");
  } else {
    System.out.println("It's a tie.");
  }
} // end main


/***
* Takes value of 3 dice as input.    
* Computes and returns a score.  
* Note: The formulas / resulting scores aren't specific,  
* they just preserve the RANK of roll possibilities from worst 
* to best.
* i.e., lower scores have lower rank 
*/

public static int getScore(int d1, int d2, int d3) {
  int score = 0; // default score

  if ((d1==d2) && (d2==d3)) {              
    score = (((100 * d1) + d1) * 100 );  // score for TRIPLE
  } else if ((d1==d2) && (d2!=d3)) {         
    score = ((100 * d1) + d3);           // score for PAIR d1, d2
  } else if ((d2==d3) && (d3!=d1)) {        
    score = ((100 * d2) + d1);           // score for PAIR d2, d3
  } else if ((d3==d1) && (d1!=d2)) {      
    score = ((100 * d3) + d2);           // score for PAIR d1, d3
  }
  return score;  // score for NOT TRIPLE and NOT PAIR
  } // end getScore
1
Your SPECIFIC question doesn't seem to be that specific as you don't mention what the booleans would be used for. - Jacob G.
I thought the commented headings were pretty specific, but they check if a roll constitutes a Pair or a Triple. - user7876264
Well you should probably be using the booleans there because the specifications require you to. Also, if the method only returns either true or false, then a boolean is perfect. - Jacob G.
@Jacob G. "Well you should probably be using the booleans there because the specifications require you to" I am asking why this assignment requires me to use booleans. The way I wrote the program if much more efficient and easy to read, and doesn't involve adding a redundant method for booleans. - user7876264
Only your professor would be able to answer why they decided to write the program that way, I'm not sure what answer you're looking for on here. - Jacob G.

1 Answers

0
votes

A computer program has two audiences -

  • the computer that's expected to run it,
  • the programmer who's expected to read, understand and maintain it in the future.

The word boolean is a short-hand way of communicating something to that programmer - namely that this variable, or this method's return value, can be either true or false, but nothing else.