patternjavaModerate
"Deal or No Deal" console game
Viewed 0 times
consolegamedeal
Problem
Can anyone check this code of mine for improvements? This was coded using Java with OOP concepts.
```
import java.util.Random;
public class Game {
Player player = new Player();
Banker banker = new Banker();
private int a = 0;
private int b = 6;
private double myAmount = 0;
private double offer = 0;
private int turn = 1;
private int cases = 26;
private double amounts[] = { 23, 1, 5, 10, 25, 50, 75, 100,
200, 300, 400, 500, 750, 1000, 5000, 10000, 25000, 50000, 75000,
100000, 300000, 400000, 500000, 750000, 1000000, 250000, 800 };
private String models[] = { "Nayer", "Michelle", "Obama", "Rosey", "Miney",
"Ashley", "Maria", "Ozawa", "Audrey", "Kristen", "Kim",
"Kardashian", "Kourtney", "Ann", "Macy", "Tori", "Sam", "Monica",
"Jin", "Koi", "jill", "Usher", "Justin Bieber", "Lindsay Lohan",
"Hazell", "Buttercup", "Don Amalia", "Kojic!" };
Briefcase briefcase[] = new Briefcase[27];
Model lady[] = new Model[27];
public void setladies() {
for (int i = 0; i 0; a--) {
int r = player.Remove(a, briefcase, models);
briefcase[r] = null;
cases--;
}
b--;
turn++;
banker.setOffer(turn,briefcase,myAmount);
offer = banker.getOffer(turn, briefcase, myAmount);
gamestatus = player.gamestatus();
} else if (cases == 1) {
int r = player.Remove(1, briefcase, models);
briefcase[r] = null;
gamestatus = false;
} else {
int r = player.Remove(1, briefcase, models);
briefcase[r] = null;
cases--;
banker.setOffer(turn,briefcase,myAmount);
offer = banker.getOffer(turn, briefcase, myAmount);
gamestatus = player.gamestatus()
Game Class```
import java.util.Random;
public class Game {
Player player = new Player();
Banker banker = new Banker();
private int a = 0;
private int b = 6;
private double myAmount = 0;
private double offer = 0;
private int turn = 1;
private int cases = 26;
private double amounts[] = { 23, 1, 5, 10, 25, 50, 75, 100,
200, 300, 400, 500, 750, 1000, 5000, 10000, 25000, 50000, 75000,
100000, 300000, 400000, 500000, 750000, 1000000, 250000, 800 };
private String models[] = { "Nayer", "Michelle", "Obama", "Rosey", "Miney",
"Ashley", "Maria", "Ozawa", "Audrey", "Kristen", "Kim",
"Kardashian", "Kourtney", "Ann", "Macy", "Tori", "Sam", "Monica",
"Jin", "Koi", "jill", "Usher", "Justin Bieber", "Lindsay Lohan",
"Hazell", "Buttercup", "Don Amalia", "Kojic!" };
Briefcase briefcase[] = new Briefcase[27];
Model lady[] = new Model[27];
public void setladies() {
for (int i = 0; i 0; a--) {
int r = player.Remove(a, briefcase, models);
briefcase[r] = null;
cases--;
}
b--;
turn++;
banker.setOffer(turn,briefcase,myAmount);
offer = banker.getOffer(turn, briefcase, myAmount);
gamestatus = player.gamestatus();
} else if (cases == 1) {
int r = player.Remove(1, briefcase, models);
briefcase[r] = null;
gamestatus = false;
} else {
int r = player.Remove(1, briefcase, models);
briefcase[r] = null;
cases--;
banker.setOffer(turn,briefcase,myAmount);
offer = banker.getOffer(turn, briefcase, myAmount);
gamestatus = player.gamestatus()
Solution
There are a few things, I'll expand on this over time. First, always write method and variable names in lowerCamelCase. Everything starting with an uppercase letter wil be considered a class or a constant by other programmers.
Then, don't use mechanically getters and setters. E.g. consider
Do you ever change the name of a
You could opt for implementing
[Some Random Thoughts]
Try to simplify logical expressions as much as possible:
is the same as simply
Use API functions where possible. E.g. there is
What is with the empty {} in
[Suggestion]
.
.
```
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Game {
private Player player = new Player();
private Banker banker = new Banker();
private int a = 0;
private int b = 6;
private double myAmount = 0;
private double offer = 0;
private int turn = 1;
private int cases = 26;
private Briefcase briefcases[];
public void casesSetup() {
String[] modelNames = {"Michelle", "Obama", "Rosey", "Miney",
"Ashley", "Maria", "Ozawa", "Audrey", "Kristen", "Kim",
"Kardashian", "Kourtney", "Ann", "Macy", "Tori", "Sam", "Monica",
"Jin", "Koi", "jill", "Usher", "Justin Bieber", "Lindsay Lohan",
"Hazell", "Buttercup", "Don Amalia", "Kojic!"};
List amounts = Arrays.asList(1, 5, 10, 25, 50, 75, 100,
200, 300, 400, 500, 750, 1000, 5000, 10000, 25000, 50000, 75000,
100000, 300000, 400000, 500000, 750000, 1000000, 250000, 800);
Collections.shuffle(amounts);
briefcases = new Briefcase[amounts.size()];
for (int i = 0; i 0; a--) {
player.remove(a, briefcases);
cases--;
}
b--;
turn++;
banker.setOffer(turn, briefcases, myAmount);
offer = banker.getOffer(turn, briefcases, myAmount);
gamestatus = player.gamestatus();
} else if (cases == 1) {
player.remove(1, briefcases);
gamestatus = false;
} else {
player.remove(1, briefcases);
cases--;
banker.setOffer(turn, briefcases, myAmount);
offer = banker.getOffer(turn, briefcases, myAmount);
gamestatus = player.gamestatus();
}
}
finishgame();
}
public void finishgame() {
if (cases == 1) {
System.out.println("\tYou Rejected the Offer of Banker");
System.out.printf("\tYour case contains $%.2f and the bankers offer is $%.2f\n",
myAmount, offer);
System.out.printf("\tYou've won your case with an amount of: %.2f",
myAmount);
} else {
System
Then, don't use mechanically getters and setters. E.g. consider
public class Model {
private String mName;
public void setName(String mName) {
this.mName = mName;
}
public String getName() {
return mName;
}
}Do you ever change the name of a
Model afterwards? No? Is it okay to have a Model without name? No? Then a correct implementation is:public class Model {
private final String mName;
public Model(String mName) {
this.mName = mName;
}
public String getName() {
return mName;
}
}You could opt for implementing
toString instead of getName, or even for making the member variable public (because it's final, and String is immutable). [Some Random Thoughts]
Try to simplify logical expressions as much as possible:
if (temp == 1) {
return false;
} else {
return true;
}is the same as simply
return temp != 1;.Use API functions where possible. E.g. there is
java.util.Collections.shuffle for Lists.What is with the empty {} in
casesSetup and showCases?[Suggestion]
public class Banker {
private double total = 0;
private int a = 0;
private double amount =0;
double average = 0;
public void setOffer(int turn, Briefcase[] cases, double myAmount) {
for (int i = 0; i < cases.length; i++) {
if (! cases[i].isRemoved()) {
total += cases[i].getAmount();
a++;
}
}
average = myAmount + total / a;
amount = average*turn/ 10;
}
public double getOffer(int turn, Briefcase[] cases, double myAmount) {
setOffer(turn, cases, myAmount);
System.out.printf("\tThe Bankers Offer is: %.2f \n\n", amount);
return amount;
}
}.
public class Briefcase {
private final double amount;
private final String model;
private boolean removed = false;
private String face;
public Briefcase(double amount, int face, String model) {
this.face = Integer.toString(face);
this.amount = amount;
this.model = model;
}
public double getAmount() {
return amount;
}
@Override
public String toString() {
return face;
}
public String getModel() {
return model;
}
public void remove() {
removed = true;
face = "X";
}
public boolean isRemoved() {
return removed;
}
}.
```
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Game {
private Player player = new Player();
private Banker banker = new Banker();
private int a = 0;
private int b = 6;
private double myAmount = 0;
private double offer = 0;
private int turn = 1;
private int cases = 26;
private Briefcase briefcases[];
public void casesSetup() {
String[] modelNames = {"Michelle", "Obama", "Rosey", "Miney",
"Ashley", "Maria", "Ozawa", "Audrey", "Kristen", "Kim",
"Kardashian", "Kourtney", "Ann", "Macy", "Tori", "Sam", "Monica",
"Jin", "Koi", "jill", "Usher", "Justin Bieber", "Lindsay Lohan",
"Hazell", "Buttercup", "Don Amalia", "Kojic!"};
List amounts = Arrays.asList(1, 5, 10, 25, 50, 75, 100,
200, 300, 400, 500, 750, 1000, 5000, 10000, 25000, 50000, 75000,
100000, 300000, 400000, 500000, 750000, 1000000, 250000, 800);
Collections.shuffle(amounts);
briefcases = new Briefcase[amounts.size()];
for (int i = 0; i 0; a--) {
player.remove(a, briefcases);
cases--;
}
b--;
turn++;
banker.setOffer(turn, briefcases, myAmount);
offer = banker.getOffer(turn, briefcases, myAmount);
gamestatus = player.gamestatus();
} else if (cases == 1) {
player.remove(1, briefcases);
gamestatus = false;
} else {
player.remove(1, briefcases);
cases--;
banker.setOffer(turn, briefcases, myAmount);
offer = banker.getOffer(turn, briefcases, myAmount);
gamestatus = player.gamestatus();
}
}
finishgame();
}
public void finishgame() {
if (cases == 1) {
System.out.println("\tYou Rejected the Offer of Banker");
System.out.printf("\tYour case contains $%.2f and the bankers offer is $%.2f\n",
myAmount, offer);
System.out.printf("\tYou've won your case with an amount of: %.2f",
myAmount);
} else {
System
Code Snippets
public class Model {
private String mName;
public void setName(String mName) {
this.mName = mName;
}
public String getName() {
return mName;
}
}public class Model {
private final String mName;
public Model(String mName) {
this.mName = mName;
}
public String getName() {
return mName;
}
}if (temp == 1) {
return false;
} else {
return true;
}public class Banker {
private double total = 0;
private int a = 0;
private double amount =0;
double average = 0;
public void setOffer(int turn, Briefcase[] cases, double myAmount) {
for (int i = 0; i < cases.length; i++) {
if (! cases[i].isRemoved()) {
total += cases[i].getAmount();
a++;
}
}
average = myAmount + total / a;
amount = average*turn/ 10;
}
public double getOffer(int turn, Briefcase[] cases, double myAmount) {
setOffer(turn, cases, myAmount);
System.out.printf("\tThe Bankers Offer is: %.2f \n\n", amount);
return amount;
}
}public class Briefcase {
private final double amount;
private final String model;
private boolean removed = false;
private String face;
public Briefcase(double amount, int face, String model) {
this.face = Integer.toString(face);
this.amount = amount;
this.model = model;
}
public double getAmount() {
return amount;
}
@Override
public String toString() {
return face;
}
public String getModel() {
return model;
}
public void remove() {
removed = true;
face = "X";
}
public boolean isRemoved() {
return removed;
}
}Context
StackExchange Code Review Q#4723, answer score: 11
Revisions (0)
No revisions yet.