Skip to content

Instantly share code, notes, and snippets.

@Gzoref
Created November 29, 2018 04:21
Show Gist options
  • Save Gzoref/09783dd0e1a0921e2646e39bec893d06 to your computer and use it in GitHub Desktop.
Save Gzoref/09783dd0e1a0921e2646e39bec893d06 to your computer and use it in GitHub Desktop.
First to one game.
package DiceGame;
import java.util.Random;
/*
* Name: Geoffrey Zoref
* Date: 9/7/2018
* Project: Classes and Objects - Ch. 6
*/
public class Dice {
private int numberOfSides;
private int value;
public Dice(int die) {
this.numberOfSides = die;
}
public int rollDice() {
int result;
Random random = new Random();
result = random.nextInt(numberOfSides) + 1;
return result;
}
}
/**
* This game is meant for two or more players. In the game, each player starts out with 50 points, as each player takes
* a turn rolling a pair of dice; the amount generated by the dice is subtracted from the player’s points. The first
* player with exactly one point remaining wins. If a player’s remaining points minus the amount generated by the dice
* results in a value less than one, then the amount should be added to the player’s points. (As an alternative, the
* game can be played with a set number turns. In this case, the player with the amount of points
* closest to one, when all rounds have been played, wins.
* <p>
* )Write a program that simulates the game being played by two players.
* <p>
* Use the Die class that was presented in Chapter 4 to simulate the dice. Write a Player class to simulate the
* players
*/
package DiceGame;/*
* Name: Geoffrey Zoref
* Date: 9/7/2018
* Project: Classes and Objects - Ch. 6
*/
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Dice die = new Dice(6);
//Player player;
Scanner input = new Scanner(System.in);
System.out.println("Player 1 name: ");
String player1 = input.nextLine();
System.out.println("Player 2 name: ");
String player2 = input.nextLine();
Player player = new Player(player1,player2 ,die.rollDice());
System.out.println(player.pointsSubstracted());
}
}
/**
* This game is meant for two or more players. In the game, each player starts out with 50 points, as each player takes
* a turn rolling a pair of dice; the amount generated by the dice is subtracted from the player’s points. The first
* player with exactly one point remaining wins.
* <p>
* If a player’s remaining points minus the amount generated by the dice results in a value less than one, then the
* amount should be added to the player’s points. (As an alternative, the game can be played with a set number
* turns. In this case, the player with the amount of points closest to one, when all rounds have been played,
* wins.)Write a program that simulates the game being played by two players. Use the Die class that was
* presented in Chapter 4 to simulate the dice. Write a Player class to simulate the players
*/
package DiceGame;
import java.util.*;
/*
* Name: Geoffrey Zoref
* Date: 10/10/2018
* Project: Classes and Objects - Ch. 6
*/
public class Player {
private String player1, player2;
private Dice die;
private int points = 50;
private Dice dice;
public Player(Dice dice, String player1) {
this.dice = dice;
this.player1 = player1;
this.player2 = player2;
}
public Player(String player1, String player2, Dice die) {
this.player1 = player1;
this.player2 = player2;
this.die = die;
}
public Player(String player1, String player2, int i) {
}
public boolean pointsSubstracted() {
boolean end = false;
die.rollDice();
if(points - dice.rollDice() == 1) {
points -= dice.rollDice();
end = true;
}else if(points - dice.rollDice() < 1)
points += dice.rollDice();
return end;
}
public String toString() {
String str = player1 + "rolled a " + dice.rollDice() + "\nand has " + points + " points.\n" + player2 + "rolled a " + dice.rollDice() + "\nand has " + points + " points.";
return str;
}
}
/**
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment