Compare commits

..

2 Commits

Author SHA1 Message Date
2a44fd5f5b Finish up the project 2021-02-18 20:42:54 -05:00
f6cfb5794a Fix pointer bug 2021-02-18 20:12:22 -05:00
5 changed files with 164 additions and 64 deletions

View File

@ -4,13 +4,12 @@ public class DiceCup{
private ArrayList<MyDie> dice;
private int creditBalance;
public DiceCup(int Dx) {
dice = new ArrayList<MyDie>();
creditBalance = 10;
MyDie die;
for (int i = 0; i <3; i++){
if (Dx == 8){
die = new EightDie();
}
@ -18,17 +17,14 @@ public class DiceCup{
die = new SixDie();
}
for (int i = 0; i <3; i++){
dice.add(die);
}
}
/* Accessors */
public int getCredits(){
return creditBalance;
}
public ArrayList<MyDie> getDice(){
return dice;
}
@ -41,26 +37,17 @@ public class DiceCup{
}
/* Actions */
public void roll(){
for (MyDie die:dice){
die.roll();
}
}
public void updateCredits(){
if (enoughCredits()){
creditBalance -=1;
checkTriplets();
checkDouble();
checkDoubles();
checkLarge();
}
else{
System.out.println("You do not have enough credits! \n");
}
}
public boolean enoughCredits(){
if (creditBalance > 0){
return true;
@ -69,10 +56,10 @@ public class DiceCup{
return false;
}
}
public void testRoll(int [] values){
//roll dice until desired 3 int array happens
if (enoughCredits()){
creditBalance -= 1;
for (int i =0; i<dice.size(); i++){
while (dice.get(i).getValue() != values[i]){
dice.get(i).roll();
@ -82,15 +69,13 @@ public class DiceCup{
}
/* Win conditions */
public void checkTriplets(){
if ( dice.get(0).compareTo(dice.get(1)) == 0 &&
dice.get(0).compareTo(dice.get(2)) == 0){
creditBalance +=1;
}
}
public void checkDouble(){
public void checkDoubles(){
if ( dice.get(0).compareTo(dice.get(1)) == 0 ||
dice.get(0).compareTo(dice.get(2)) == 0 ||
dice.get(1).compareTo(dice.get(2)) == 0){

View File

@ -10,10 +10,10 @@ public class GameManager{
public String toString(){
ArrayList<MyDie> dice = diceCup.getDice();
return "GameManager{\n" +
"Die 1 = " + dice.get(0).getValue() +
return "GameManager{" +
"\nDie 1 = " + dice.get(0).getValue() +
"\nDie 2 = " + dice.get(1).getValue() +
"Die 3 = " + dice.get(2).getValue() + "}\n";
"\nDie 3 = " + dice.get(2).getValue() + "}";
}
public DiceCup getDice(){
@ -25,37 +25,41 @@ public class GameManager{
Scanner scnr = new Scanner(System.in);
int diceSides;
System.out.println("Do you want to play with 6 or 8 dice?\n");
System.out.println("Do you want to play with 6 or 8 dice?");
diceSides = scnr.nextInt();
scnr.nextLine();
GameManager game = new GameManager(diceSides);
String userInput;
while (true){
System.out.println("Type Roll to play, Stop to end\n");
userInput = scnr.next();
System.out.println("Type Roll to play, Stop to end");
userInput = scnr.nextLine();
if (userInput.toLowerCase() == "stop"){
break;}
if (userInput.toLowerCase() == "roll"){
System.out.println("Credits before bet: " +
game.diceCup.getCredits());
// Roll Dice
game.diceCup.roll();
game.diceCup.updateCredits();
if (userInput.toLowerCase().equals("stop")){break;}
if (game.diceCup.enoughCredits()){
System.out.println(game.toString());
if (userInput.toLowerCase().equals("roll")){
System.out.println("\nCredits before bet: " +
game.diceCup.getCredits());
// Roll Dice
game.getDice().roll();
game.getDice().updateCredits();
System.out.println("Total sum of Dice: " +
game.diceCup.getTotal());
System.out.println("Credits after bet: " +
game.diceCup.getCredits());
System.out.println(game.toString());
System.out.println("Total sum of Dice: " +
game.diceCup.getTotal());
System.out.println("Credits after bet: " +
game.diceCup.getCredits() + "\n");
}
}
else{
System.out.println("You do not have enough credits! \n");
}
}
scnr.close();
}

View File

@ -31,9 +31,9 @@ public class MyDiceGameTest {
// confirm large is rolled
GameManager game = new GameManager(6);
int credits = game.getDice().getCredits();
game.getDice().testRoll(new int[]{6, 6, 5});
game.getDice().testRoll(new int[]{6, 4, 5});
game.getDice().checkLarge();
Assert.assertEquals("Dice => (6,6,5): credits should stay the same",
Assert.assertEquals("Dice => (6,4,5): credits should stay the same",
game.getDice().getCredits(), credits);
// confirm large is not rolled
@ -44,38 +44,68 @@ public class MyDiceGameTest {
Assert.assertEquals("Dice => (1,2,3): credits should decrease by one",
game2.getDice().getCredits(), credits - 1);
// confirm large rolled with three of kind
GameManager game3 = new GameManager(6);
credits = game3.getDice().getCredits();
game3.getDice().testRoll(new int[]{4, 4, 4});
game3.getDice().checkLarge();
game3.getDice().checkTriplets();
Assert.assertEquals("Dice => (4,4,4): credits should increased by one",
game3.getDice().getCredits(), credits + 1);
// confirm large is not rolled with three of kind
GameManager game4 = new GameManager(6);
credits = game4.getDice().getCredits();
game4.getDice().testRoll(new int[]{3, 3, 3});
game4.getDice().checkLarge();
game4.getDice().checkTriplets();
Assert.assertEquals("Dice => (3,3,3): credits should stay the same",
game4.getDice().getCredits(), credits);
}
/******************************************************
* Test Triplets
* Test Doubles
*****************************************************/
@Test
public void testTriplets() {
// confirm triplets is rolled
public void testDoubles() {
// confirm doubles is rolled. 1st Combo
GameManager game = new GameManager(6);
int credits = game.getDice().getCredits();
game.getDice().testRoll(new int[]{1, 1, 1});
game.getDice().checkTriplets();
Assert.assertEquals("Dice => (1,1,1): credits should stay the same",
game.getDice().testRoll(new int[]{1, 1, 2});
game.getDice().checkDoubles();
Assert.assertEquals("Dice => (1,1,2): credits should stay the same",
game.getDice().getCredits(), credits);
// confirm doubles is rolled. 2nd Combo
GameManager game2 = new GameManager(6);
int credits2 = game2.getDice().getCredits();
game2.getDice().testRoll(new int[]{2, 1, 1});
game2.getDice().checkDoubles();
Assert.assertEquals("Dice => (1,1,1): credits should stay the same",
game2.getDice().getCredits(), credits2);
}
/******************************************************
* Test Combination of Points Awarded
*****************************************************/
@Test
public void testCombinations(){
// confirm large and double are called
GameManager game = new GameManager(6);
int credits = game.getDice().getCredits();
game.getDice().testRoll(new int[]{6, 6, 3});
game.getDice().checkDoubles();
game.getDice().checkLarge();
Assert.assertEquals("Dice => (6,6,3): credits should increase by one",
game.getDice().getCredits(), credits + 1);
// confirm large, triplets and doubles are given points
GameManager game3 = new GameManager(6);
credits = game3.getDice().getCredits();
game3.getDice().testRoll(new int[]{4, 4, 4});
game3.getDice().checkLarge();
game3.getDice().checkTriplets();
game3.getDice().checkDoubles();
Assert.assertEquals("Dice => (4,4,4): credits should increased by two",
game3.getDice().getCredits(), credits + 2);
// confirm triplets and doubles are called but not large
GameManager game4 = new GameManager(6);
credits = game4.getDice().getCredits();
game4.getDice().testRoll(new int[]{3, 3, 3});
game4.getDice().checkLarge();
game4.getDice().checkTriplets();
game4.getDice().checkDoubles();
Assert.assertEquals("Dice => (3,3,3): credits should increase by one",
game4.getDice().getCredits(), credits+1);
}
}

Binary file not shown.

View File

@ -0,0 +1,81 @@
import static org.junit.Assert.*;
import org.junit.*;
/*******************************************
* The test class for Chuck
*
* @author Resendiz
* @version February 2021
******************************************/
public class MyDiceGameTest {
/******************************************************
* Test initial values of the constructor
*****************************************************/
@Test
public void testConstructor() {
GameManager game = new GameManager(6);
int credits = game.getDice().getCredits();
// confirm there is only one copy of Title 1
Assert.assertEquals("Game should start with 10 credits",
credits, 10);
}
/******************************************************
* Test Large
*****************************************************/
@Test
public void testLarge() {
// confirm large is rolled
GameManager game = new GameManager(6);
int credits = game.getDice().getCredits();
game.getDice().testRoll(new int[]{6, 6, 5});
game.getDice().checkLarge();
Assert.assertEquals("Dice => (6,6,5): credits should stay the same",
game.getDice().getCredits(), credits);
// confirm large is not rolled
GameManager game2 = new GameManager(6);
credits = game2.getDice().getCredits();
game2.getDice().testRoll(new int[]{1, 2, 3});
game2.getDice().checkLarge();
Assert.assertEquals("Dice => (1,2,3): credits should decrease by one",
game2.getDice().getCredits(), credits - 1);
// confirm large rolled with three of kind
GameManager game3 = new GameManager(6);
credits = game3.getDice().getCredits();
game3.getDice().testRoll(new int[]{4, 4, 4});
game3.getDice().checkLarge();
game3.getDice().checkTriplets();
Assert.assertEquals("Dice => (4,4,4): credits should increased by one",
game3.getDice().getCredits(), credits + 1);
// confirm large is not rolled with three of kind
GameManager game4 = new GameManager(6);
credits = game4.getDice().getCredits();
game4.getDice().testRoll(new int[]{3, 3, 3});
game4.getDice().checkLarge();
game4.getDice().checkTriplets();
Assert.assertEquals("Dice => (3,3,3): credits should stay the same",
game4.getDice().getCredits(), credits);
}
/******************************************************
* Test Triplets
*****************************************************/
@Test
public void testTriplets() {
// confirm triplets is rolled
GameManager game = new GameManager(6);
int credits = game.getDice().getCredits();
game.getDice().testRoll(new int[]{1, 1, 1});
game.getDice().checkTriplets();
Assert.assertEquals("Dice => (1,1,1): credits should stay the same",
game.getDice().getCredits(), credits);
}
}