jeudi 11 novembre 2021

Lowest score for an elimination game

Here is my code for an Elimination game. The code listed works perfectly and just how it is supposed to. This does not need fixing, but is simply present for reference.

import java.util.Scanner;

public class Elimination {

    public static void main(String[] args) {
        try (Scanner in = new Scanner(System.in)) {
            boolean playAgain = true;
            while (playAgain = true) {
                Board B = new Board();
                int die1, die2;
                boolean legalMove;
                String choice=" ";
                while (!choice.equalsIgnoreCase("Q")) {
                    System.out.println("\n"+B);
                    die1 = (int)(1 + Math.random()*6);
                    die2 = (int)(1 + Math.random()*6);
                    legalMove=false;
                    System.out.println("Dice roll: " + die1 + " " + die2);
                    while (!legalMove) {
                        System.out.print("What is your move (V=values, S=sum, Q=quit)? ");
                        choice=in.next();
                        if (choice.equalsIgnoreCase("Q")) {
                            legalMove=true;
                            System.out.println("Do you want to play again (Y=yes, N=no)?");
                            String YorN= in.next();
                            if (YorN.equals("Y") || YorN.equals("y")) {
                                System.out.println("----------------- New Game -----------------");
                                playAgain=true;
                            }
                            else if (YorN.equals("N") || YorN.equals("n")) {
                                playAgain=false;
                                System.out.println("----------------- Game Ended -----------------");
                                System.out.println("Score = " + B.score());
                                System. exit(1);
                            }
                            else {
                                System.out.println("Try again.");
                            }
                        }
                        else if (choice.equalsIgnoreCase("S") && B.removeTile(die1+die2)) {
                            legalMove=true;
                        }
                        else if (choice.equalsIgnoreCase("V") && B.removeTiles(die1, die2)) {
                            legalMove=true;
                        }
                        else { 
                            System.out.println("Try again.");
                        }
                    }
                }
            }
        }
    }
}

I am trying to implement a score tracker for the code. The lowest score is favorable. I need that will show the lowest score for each game that is replayed.

It should look like this:

game is played score of 45- 
Score = 45 NEW LOWEST SCORE!
Lowest Score = 45

game is played again, score of 78-
Score = 78
Lowest Score = 45

Here is what I've come up with that has some issues. I am not sure where to place this, or if I should try a different approach, possibly with a do while loop.

int lowestScore = Integer.MAX_VALUE;
if (B.score() < lowestScore) {
    lowestScore = B.score();
    System.out.println("Score = " + B.score() + "  NEW LOW SCORE!");
    System.out.println("Lowest Score = " + lowestScore);
} 
else {
    System.out.println("Score = " + B.score());
    System.out.println("Lowest Score = " + lowestScore);
}

This output is incorrect. This is what the output looks like for the score code listed above.

game is played score of 45- 
Score = 45 NEW LOWEST SCORE!
Lowest Score = 45

game is played again, score of 78-
Score = 78 NEW LOWEST SCORE!
Lowest Score = 78

This is incorrect because the lowest score is not 78, yet that is what is output.

I think to best understand how the game or the code works would be to copy and paste it into a coding platform and working with it hands on. Looking for any help, thanks in advance.

Aucun commentaire:

Enregistrer un commentaire