Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decisions without if statements

Tags:

java

Here are the instructions:

/*
 * Write a method called isGameOver. The method should have three parameters, 
 * all integers and in this order: the player's score, the number of lives 
 * the player has remaining, the game level. The method should return true 
 * if the game is over and false otherwise, according to the following game
 * rule. The game continues only when the player has at least one life 
 * remaining and one of these conditions is met: 
 *   --The player is on level 1 and his/her score at least 1000. 
 *   --The player is on level 2 and his/her score at least 2000. 
 *   --The player is on any level and his/her score at least 3000. 
 *   
 * For example, the method call isGameOver(1500, 1, 2) should return true. 
 * 
 * DO NOT USE AN IF STATEMENT

And here is my code:

public static String isGameOver(int score, int lives, int level) {

    while (level == 1 && level < 1001)
        return "false";

    while (level == 2 && level < 2001)
        return "false";

     while (level == 3 && level < 3001)
        return "false";

    return "true";          
}

It's obviously not working, and I just have this feeling it's because I used a while loop. How can I make a decision without using an if statement?

like image 407
Elchapo Avatar asked Oct 22 '25 22:10

Elchapo


2 Answers

If you want a readable solution, then you can pretty much directly translate the problem description into code.

/**
* The game continues only when the player...
**/
private static boolean gameContinues(int score, int lives, int level) {
    // has at least one life
    boolean stillAlive = lives >= 1; 
    // is on level 1 and his/her score at least 1000
    boolean cond1 = (level == 1 && score >= 1000); 
    // is on level 2 and his/her score at least 2000
    boolean cond2 = (level == 2 && score >= 2000);
    // is on any level and his/her score at least 3000
    boolean cond3 = score >= 3000;

    // return true if has at least one life remaining and one of the conditions is met
    return stillAlive && (cond1 || cond2 || cond3);
}

// The function you want just returns the inverse of what is defined
public static boolean isGameOver(int score, int lives, int level) {
    return !gameContinues(score, lives, level);
}
like image 106
OneCricketeer Avatar answered Oct 24 '25 12:10

OneCricketeer


How about

public static boolean isGameOver(int score, int lives, int level) {
    return lives<1 || (level<3 && score<1000*level) || (score<3000 && level>2);
}

Testing this

isGameOver(900,1,1) // true
isGameOver(1900,1,1) // false
isGameOver(1900,1,2) // true
isGameOver(2900,1,2) // false
isGameOver(3000,1,5) // false
isGameOver(3000,0,5) // true

The three conditions or-ed together will return true if at least one is true. We check in order

  1. are we out of lives (lives<1)?
  2. did we fail the score conditions for levels 1 and 2 (score conditions are just 1000 points per level)?
  3. did we fail the score condition for levels 3 and up (which all have the 3000 points requirement)?

If any of these conditions are true, we return true. Otherwise we return false.

like image 43
Matthew Avatar answered Oct 24 '25 14:10

Matthew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!