Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dicegames;
- import java.util.Scanner;
- public class Pig {
- private int score1, score2;
- private Die die;
- private Scanner scan;
- private int rounds = 1;
- private int maxPoints;
- private boolean finished = false;
- // Default constructor
- public Pig() {
- this(100);
- }
- // Constructor with parameter
- public Pig(int maxPoints) {
- this.maxPoints = maxPoints;
- score1 = score2 = 0;
- die = new Die();
- scan = new Scanner(System.in);
- }
- /**
- * Welcome message of the game that goes over the rules, which will change
- * depending on what the field variable, maxPoints, is set to.
- */
- public void welcomeToTheGame() {
- System.out.println("Hver spiller skiftes til at kaste med en terning "
- + "indtil han enten kaster 1, eller beslutter sig for at\r\n"
- + "stoppe. Hvis han slår en 1’er, får han ingen point i denne "
- + "omgang. Hvis han beslutter sig for at\r\n"
- + "stoppe inden har slår en 1’er, lægges summen af alle hans " + "kast i denne tur sammen med hans\r\n"
- + "samlede antal point, og turen går videre til næste " + "spiller. Den første spiller der samlet når "
- + maxPoints + "\r\n" + "point har vundet.\r\n" + "");
- }
- /**
- * Method that is called when a game is over. This method is only called when a
- * game is finished.
- */
- public void gameOver() {
- if (rounds % 2 == 1) {
- System.out.println("Spiller 1 vandt med " + score1 + " points");
- } else {
- System.out.println("Spiller 2 vandt med " + score2 + " points");
- }
- finished = true;
- scan.close();
- }
- /**
- * The method for resolving an actor's turn if it was decided that the game is
- * versus a computer.
- */
- private void takeTurnVsPC() {
- boolean turnOver = false;
- int points = 0;
- while (!turnOver) {
- die.roll();
- int roll = die.getFaceValue();
- if (rounds % 2 == 1) {
- System.out.println("Du har kastet: " + roll);
- } else {
- System.out.println("Computeren har kastet: " + roll);
- }
- if (roll == 1 && rounds % 2 == 1) {
- System.out.println("Du har kastet en 1'er. Din tur er slut. Øv.");
- System.out.println();
- turnOver = true;
- } else if (roll == 1 && rounds % 2 == 0) {
- System.out.println("Computeren har kastet en 1'er. Dens tur er slut. Oof.");
- System.out.println();
- turnOver = true;
- } else {
- points += roll;
- if (rounds % 2 == 1) {
- System.out.println("Du har i denne runde opnået " + points + " points");
- } else {
- System.out.println("Computeren har i denne runde opnået " + points + " points");
- }
- if (rounds % 2 == 1 && score1 + points >= maxPoints) {
- score1 += points;
- System.out.print(", hvilket sammen med dine points giver " + score1 + "!");
- gameOver();
- turnOver = true;
- } else if (rounds % 2 == 0 && score2 + points >= maxPoints) {
- score2 += points;
- gameOver();
- turnOver = true;
- } else if (rounds % 2 == 1) {
- System.out.println("Vil du kaste terningen igen? Angiv Ja eller Nej: ");
- String goOn = scan.nextLine();
- if (goOn.equalsIgnoreCase("Nej")) {
- turnOver = true;
- score1 += points;
- System.out.println("Nu har du en score på: " + score1);
- }
- /**
- * Here is the computer opponent's algorithm: 20 is chosen as the computer's
- * border value for determining the end of a turn, since the average amount of
- * wins equals the average amount of loses --> 1/6 of the time all points are
- * lost, and the rest of the 5/6 of the time there is an average win of
- * (2+3+4+5+6)/5 = 4 points. Given that 5/6 * 4 = 20/6 there will a equilibrium
- * between potential loss and gain when 1/6 * x = 20/6 --> x = 20. The computer
- * plays it safe, so it decides to secure it's points if they're exactly 20.
- */
- } else {
- if (points >= 20) {
- turnOver = true;
- score2 += points;
- System.out.println("Computeren stopper sin tur her. Nu har computeren en score på: " + score2);
- }
- }
- }
- }
- rounds++;
- }
- // Method for starting the game
- public void startGame() {
- welcomeToTheGame();
- while (!finished) {
- System.out.println("############################################################################");
- System.out.println("Det er nu runde " + rounds);
- if (rounds % 2 == 1) {
- System.out.println("Det er spiller 1 tur, med " + score1 + " points indtil videre.");
- } else {
- System.out.println("Det er spiller 2 tur, med " + score2 + " points indtil videre.");
- }
- takeTurnVsPC();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement