“Deal or No Deal” console game
$begingroup$
Can anyone check this code of mine for improvements? This was coded using Java with OOP concepts.
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 < lady.length; i++) {
lady[i] = new Model();
String name = models[i];
lady[i].setName(name);
}
}
public void Shuffle() {
Random rgen = new Random();
for (int i = 0; i < amounts.length - 1; i++) {
int Position = rgen.nextInt(amounts.length);
double temp = amounts[i];
amounts[i] = amounts[Position];
amounts[Position] = temp;
}
}
public void casesSetup() {
Shuffle();
for (int i = 0; i < briefcase.length; i++) {
if (i == 0) {
} else {
}
briefcase[i] = new Briefcase();
double value = amounts[i];
briefcase[i].setAmount(value);
briefcase[i].setFace(i);
}
}
public void showCases() {
for (int a = 0; a < briefcase.length; a++) {
if (a == 0) {
} else if (briefcase[a] == null) {
System.out.print("t[X]");
} else {
System.out.print("t[" + briefcase[a].getFace() + "]");
}
if (a % 5 == 0) {
System.out.println();
}
}
System.out.println();
}
public void Welcome() {
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
System.out.println("t~* Welcome ! ~*");
System.out.println("t~*~*~*~*~* Hosted by Kyel David ~*~*~*~*~*~*");
System.out.println("t~* Please Select from the Following Cases!~*");
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
}
public void starGame() {
boolean gamestatus = true;
casesSetup();
Welcome();
showCases();
setladies();
int choice = player.nUser();
myAmount = briefcase[choice].getAmount();
briefcase[choice] = null;
cases--;
while (gamestatus == true) {
showCases();
if (cases == 25 || cases == 19 || cases == 14 || cases == 10
|| cases == 7) {
for (a = b; a > 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();
}
}
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 $%.2fn",
myAmount, offer);
System.out.printf("tYou've won your case with an amount of: %.2f",
myAmount);
} else {
System.out.println("tYou Accepted the offer of Banker");
System.out
.printf("tYour case contains $%.2f and the bankers offer is $%.2fn",
myAmount, offer);
System.out.printf("tYou've won the offer of Banker: $%.2f", offer);
}
}
}
Player
Class
import java.util.Scanner;
public class Player {
Scanner input = new Scanner(System.in);
Banker banker = new Banker();
public boolean gamestatus() {
System.out.print("tAccept or Reject! [1]Accept [2]reject: ");
int temp = input.nextInt();
System.out.println();
if (temp == 1) {
return false;
} else {
return true;
}
}
public int nUser() {
boolean isOkay = false;
int nUser = 0;
while (isOkay == false) {
System.out.print("ntPlease Select Your Case!: ");
nUser = input.nextInt();
if (nUser < 0 || nUser >= 27) {
System.out.println("tInvalid input Try again");
} else {
isOkay = true;
}
}
return nUser;
}
public int Remove(int i, Briefcase c, String m) {
int nChoice = 0;
boolean inputisok = false;
while (inputisok == false) {
System.out.print("tPlease remove " + i + " case/s: ");
nChoice = input.nextInt();
if (nChoice < 0 || nChoice >= c.length || c[nChoice] == null) {
System.out.println();
System.out.println("tInvalid Input please Try againn");
} else {
System.out.println("tI'm " + m[nChoice]
+ " You just removed case # " + nChoice);
System.out.println("t|" + nChoice + "| contains $"
+ c[nChoice].getAmount() + "n");
inputisok = true;
}
}
return nChoice;
}
}
Banker
Class
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] == null) {
} else {
total = 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 nn", amount);
return amount;
}
}
Model
Class
public class Model {
private String mName;
public void setName(String mName) {
this.mName = mName;
}
public String getName() {
return mName;
}
}
Briefcase
Class
public class Briefcase {
private double amount;
private int face;
public void setAmount(double amount) {
this.amount = amount;
}
public double getAmount() {
return this.amount;
}
public void setFace(int face) {
this.face = face;
}
public int getFace() {
return face;
}
}
Main class
public class Play {
public static void main(String args) {
Game dnd = new Game();
dnd.starGame();
}
}
java object-oriented game
$endgroup$
add a comment |
$begingroup$
Can anyone check this code of mine for improvements? This was coded using Java with OOP concepts.
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 < lady.length; i++) {
lady[i] = new Model();
String name = models[i];
lady[i].setName(name);
}
}
public void Shuffle() {
Random rgen = new Random();
for (int i = 0; i < amounts.length - 1; i++) {
int Position = rgen.nextInt(amounts.length);
double temp = amounts[i];
amounts[i] = amounts[Position];
amounts[Position] = temp;
}
}
public void casesSetup() {
Shuffle();
for (int i = 0; i < briefcase.length; i++) {
if (i == 0) {
} else {
}
briefcase[i] = new Briefcase();
double value = amounts[i];
briefcase[i].setAmount(value);
briefcase[i].setFace(i);
}
}
public void showCases() {
for (int a = 0; a < briefcase.length; a++) {
if (a == 0) {
} else if (briefcase[a] == null) {
System.out.print("t[X]");
} else {
System.out.print("t[" + briefcase[a].getFace() + "]");
}
if (a % 5 == 0) {
System.out.println();
}
}
System.out.println();
}
public void Welcome() {
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
System.out.println("t~* Welcome ! ~*");
System.out.println("t~*~*~*~*~* Hosted by Kyel David ~*~*~*~*~*~*");
System.out.println("t~* Please Select from the Following Cases!~*");
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
}
public void starGame() {
boolean gamestatus = true;
casesSetup();
Welcome();
showCases();
setladies();
int choice = player.nUser();
myAmount = briefcase[choice].getAmount();
briefcase[choice] = null;
cases--;
while (gamestatus == true) {
showCases();
if (cases == 25 || cases == 19 || cases == 14 || cases == 10
|| cases == 7) {
for (a = b; a > 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();
}
}
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 $%.2fn",
myAmount, offer);
System.out.printf("tYou've won your case with an amount of: %.2f",
myAmount);
} else {
System.out.println("tYou Accepted the offer of Banker");
System.out
.printf("tYour case contains $%.2f and the bankers offer is $%.2fn",
myAmount, offer);
System.out.printf("tYou've won the offer of Banker: $%.2f", offer);
}
}
}
Player
Class
import java.util.Scanner;
public class Player {
Scanner input = new Scanner(System.in);
Banker banker = new Banker();
public boolean gamestatus() {
System.out.print("tAccept or Reject! [1]Accept [2]reject: ");
int temp = input.nextInt();
System.out.println();
if (temp == 1) {
return false;
} else {
return true;
}
}
public int nUser() {
boolean isOkay = false;
int nUser = 0;
while (isOkay == false) {
System.out.print("ntPlease Select Your Case!: ");
nUser = input.nextInt();
if (nUser < 0 || nUser >= 27) {
System.out.println("tInvalid input Try again");
} else {
isOkay = true;
}
}
return nUser;
}
public int Remove(int i, Briefcase c, String m) {
int nChoice = 0;
boolean inputisok = false;
while (inputisok == false) {
System.out.print("tPlease remove " + i + " case/s: ");
nChoice = input.nextInt();
if (nChoice < 0 || nChoice >= c.length || c[nChoice] == null) {
System.out.println();
System.out.println("tInvalid Input please Try againn");
} else {
System.out.println("tI'm " + m[nChoice]
+ " You just removed case # " + nChoice);
System.out.println("t|" + nChoice + "| contains $"
+ c[nChoice].getAmount() + "n");
inputisok = true;
}
}
return nChoice;
}
}
Banker
Class
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] == null) {
} else {
total = 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 nn", amount);
return amount;
}
}
Model
Class
public class Model {
private String mName;
public void setName(String mName) {
this.mName = mName;
}
public String getName() {
return mName;
}
}
Briefcase
Class
public class Briefcase {
private double amount;
private int face;
public void setAmount(double amount) {
this.amount = amount;
}
public double getAmount() {
return this.amount;
}
public void setFace(int face) {
this.face = face;
}
public int getFace() {
return face;
}
}
Main class
public class Play {
public static void main(String args) {
Game dnd = new Game();
dnd.starGame();
}
}
java object-oriented game
$endgroup$
add a comment |
$begingroup$
Can anyone check this code of mine for improvements? This was coded using Java with OOP concepts.
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 < lady.length; i++) {
lady[i] = new Model();
String name = models[i];
lady[i].setName(name);
}
}
public void Shuffle() {
Random rgen = new Random();
for (int i = 0; i < amounts.length - 1; i++) {
int Position = rgen.nextInt(amounts.length);
double temp = amounts[i];
amounts[i] = amounts[Position];
amounts[Position] = temp;
}
}
public void casesSetup() {
Shuffle();
for (int i = 0; i < briefcase.length; i++) {
if (i == 0) {
} else {
}
briefcase[i] = new Briefcase();
double value = amounts[i];
briefcase[i].setAmount(value);
briefcase[i].setFace(i);
}
}
public void showCases() {
for (int a = 0; a < briefcase.length; a++) {
if (a == 0) {
} else if (briefcase[a] == null) {
System.out.print("t[X]");
} else {
System.out.print("t[" + briefcase[a].getFace() + "]");
}
if (a % 5 == 0) {
System.out.println();
}
}
System.out.println();
}
public void Welcome() {
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
System.out.println("t~* Welcome ! ~*");
System.out.println("t~*~*~*~*~* Hosted by Kyel David ~*~*~*~*~*~*");
System.out.println("t~* Please Select from the Following Cases!~*");
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
}
public void starGame() {
boolean gamestatus = true;
casesSetup();
Welcome();
showCases();
setladies();
int choice = player.nUser();
myAmount = briefcase[choice].getAmount();
briefcase[choice] = null;
cases--;
while (gamestatus == true) {
showCases();
if (cases == 25 || cases == 19 || cases == 14 || cases == 10
|| cases == 7) {
for (a = b; a > 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();
}
}
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 $%.2fn",
myAmount, offer);
System.out.printf("tYou've won your case with an amount of: %.2f",
myAmount);
} else {
System.out.println("tYou Accepted the offer of Banker");
System.out
.printf("tYour case contains $%.2f and the bankers offer is $%.2fn",
myAmount, offer);
System.out.printf("tYou've won the offer of Banker: $%.2f", offer);
}
}
}
Player
Class
import java.util.Scanner;
public class Player {
Scanner input = new Scanner(System.in);
Banker banker = new Banker();
public boolean gamestatus() {
System.out.print("tAccept or Reject! [1]Accept [2]reject: ");
int temp = input.nextInt();
System.out.println();
if (temp == 1) {
return false;
} else {
return true;
}
}
public int nUser() {
boolean isOkay = false;
int nUser = 0;
while (isOkay == false) {
System.out.print("ntPlease Select Your Case!: ");
nUser = input.nextInt();
if (nUser < 0 || nUser >= 27) {
System.out.println("tInvalid input Try again");
} else {
isOkay = true;
}
}
return nUser;
}
public int Remove(int i, Briefcase c, String m) {
int nChoice = 0;
boolean inputisok = false;
while (inputisok == false) {
System.out.print("tPlease remove " + i + " case/s: ");
nChoice = input.nextInt();
if (nChoice < 0 || nChoice >= c.length || c[nChoice] == null) {
System.out.println();
System.out.println("tInvalid Input please Try againn");
} else {
System.out.println("tI'm " + m[nChoice]
+ " You just removed case # " + nChoice);
System.out.println("t|" + nChoice + "| contains $"
+ c[nChoice].getAmount() + "n");
inputisok = true;
}
}
return nChoice;
}
}
Banker
Class
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] == null) {
} else {
total = 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 nn", amount);
return amount;
}
}
Model
Class
public class Model {
private String mName;
public void setName(String mName) {
this.mName = mName;
}
public String getName() {
return mName;
}
}
Briefcase
Class
public class Briefcase {
private double amount;
private int face;
public void setAmount(double amount) {
this.amount = amount;
}
public double getAmount() {
return this.amount;
}
public void setFace(int face) {
this.face = face;
}
public int getFace() {
return face;
}
}
Main class
public class Play {
public static void main(String args) {
Game dnd = new Game();
dnd.starGame();
}
}
java object-oriented game
$endgroup$
Can anyone check this code of mine for improvements? This was coded using Java with OOP concepts.
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 < lady.length; i++) {
lady[i] = new Model();
String name = models[i];
lady[i].setName(name);
}
}
public void Shuffle() {
Random rgen = new Random();
for (int i = 0; i < amounts.length - 1; i++) {
int Position = rgen.nextInt(amounts.length);
double temp = amounts[i];
amounts[i] = amounts[Position];
amounts[Position] = temp;
}
}
public void casesSetup() {
Shuffle();
for (int i = 0; i < briefcase.length; i++) {
if (i == 0) {
} else {
}
briefcase[i] = new Briefcase();
double value = amounts[i];
briefcase[i].setAmount(value);
briefcase[i].setFace(i);
}
}
public void showCases() {
for (int a = 0; a < briefcase.length; a++) {
if (a == 0) {
} else if (briefcase[a] == null) {
System.out.print("t[X]");
} else {
System.out.print("t[" + briefcase[a].getFace() + "]");
}
if (a % 5 == 0) {
System.out.println();
}
}
System.out.println();
}
public void Welcome() {
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
System.out.println("t~* Welcome ! ~*");
System.out.println("t~*~*~*~*~* Hosted by Kyel David ~*~*~*~*~*~*");
System.out.println("t~* Please Select from the Following Cases!~*");
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
}
public void starGame() {
boolean gamestatus = true;
casesSetup();
Welcome();
showCases();
setladies();
int choice = player.nUser();
myAmount = briefcase[choice].getAmount();
briefcase[choice] = null;
cases--;
while (gamestatus == true) {
showCases();
if (cases == 25 || cases == 19 || cases == 14 || cases == 10
|| cases == 7) {
for (a = b; a > 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();
}
}
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 $%.2fn",
myAmount, offer);
System.out.printf("tYou've won your case with an amount of: %.2f",
myAmount);
} else {
System.out.println("tYou Accepted the offer of Banker");
System.out
.printf("tYour case contains $%.2f and the bankers offer is $%.2fn",
myAmount, offer);
System.out.printf("tYou've won the offer of Banker: $%.2f", offer);
}
}
}
Player
Class
import java.util.Scanner;
public class Player {
Scanner input = new Scanner(System.in);
Banker banker = new Banker();
public boolean gamestatus() {
System.out.print("tAccept or Reject! [1]Accept [2]reject: ");
int temp = input.nextInt();
System.out.println();
if (temp == 1) {
return false;
} else {
return true;
}
}
public int nUser() {
boolean isOkay = false;
int nUser = 0;
while (isOkay == false) {
System.out.print("ntPlease Select Your Case!: ");
nUser = input.nextInt();
if (nUser < 0 || nUser >= 27) {
System.out.println("tInvalid input Try again");
} else {
isOkay = true;
}
}
return nUser;
}
public int Remove(int i, Briefcase c, String m) {
int nChoice = 0;
boolean inputisok = false;
while (inputisok == false) {
System.out.print("tPlease remove " + i + " case/s: ");
nChoice = input.nextInt();
if (nChoice < 0 || nChoice >= c.length || c[nChoice] == null) {
System.out.println();
System.out.println("tInvalid Input please Try againn");
} else {
System.out.println("tI'm " + m[nChoice]
+ " You just removed case # " + nChoice);
System.out.println("t|" + nChoice + "| contains $"
+ c[nChoice].getAmount() + "n");
inputisok = true;
}
}
return nChoice;
}
}
Banker
Class
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] == null) {
} else {
total = 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 nn", amount);
return amount;
}
}
Model
Class
public class Model {
private String mName;
public void setName(String mName) {
this.mName = mName;
}
public String getName() {
return mName;
}
}
Briefcase
Class
public class Briefcase {
private double amount;
private int face;
public void setAmount(double amount) {
this.amount = amount;
}
public double getAmount() {
return this.amount;
}
public void setFace(int face) {
this.face = face;
}
public int getFace() {
return face;
}
}
Main class
public class Play {
public static void main(String args) {
Game dnd = new Game();
dnd.starGame();
}
}
java object-oriented game
java object-oriented game
edited Oct 9 '14 at 22:40
Jamal♦
30.3k11118227
30.3k11118227
asked Sep 10 '11 at 8:23
Andrew AsmerAndrew Asmer
2612
2612
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
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
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 List
s.
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 nn", 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<Integer> 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 < briefcases.length; i++) {
double value = amounts.get(i);
briefcases[i] = new Briefcase(value, i + 1, modelNames[i]);
}
}
public void showCases() {
for (int i = 0; i < briefcases.length; i++) {
System.out.print("t[" + briefcases[i] + "]");
if (i % 5 == 4) {
System.out.println();
}
}
System.out.println();
}
public void welcomeMessage() {
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
System.out.println("t~* Welcome ! ~*");
System.out.println("t~*~*~*~*~* Hosted by Kyel David ~*~*~*~*~*~*");
System.out.println("t~* Please Select from the Following Cases!~*");
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
}
public void startGame() {
boolean gamestatus = true;
casesSetup();
welcomeMessage();
showCases();
int choice = player.nUser();
myAmount = briefcases[choice].getAmount();
briefcases[choice].remove();
cases--;
while (gamestatus == true) {
showCases();
if (cases == 25 || cases == 19 || cases == 14 || cases == 10
|| cases == 7) {
for (a = b; a > 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 $%.2fn",
myAmount, offer);
System.out.printf("tYou've won your case with an amount of: %.2f",
myAmount);
} else {
System.out.println("tYou Accepted the offer of Banker");
System.out.printf("tYour case contains $%.2f and the bankers offer is $%.2fn",
myAmount, offer);
System.out.printf("tYou've won the offer of Banker: $%.2f", offer);
}
}
}
.
public class Play {
public static void main(String args) {
Game dnd = new Game();
dnd.startGame();
}
}
.
import java.util.Scanner;
public class Player {
Scanner input = new Scanner(System.in);
Banker banker = new Banker();
public boolean gamestatus() {
System.out.print("tAccept or Reject! [1]Accept [2]Reject: ");
int temp = input.nextInt();
System.out.println();
return temp != 1;
}
public int nUser() {
while (true) {
System.out.print("ntPlease Select Your Case!: ");
int nUser = input.nextInt() - 1;
if (nUser < 0 || nUser >= 26) {
System.out.println("tInvalid input Try again");
} else {
return nUser;
}
}
}
public int remove(int index, Briefcase briefCases) {
while (true) {
System.out.print("tPlease remove " + index + " case/s: ");
int nChoice = input.nextInt() - 1;
if (nChoice < 0 || nChoice >= briefCases.length || briefCases[nChoice].isRemoved()) {
System.out.println();
System.out.println("tInvalid Input please Try againn");
} else {
System.out.println("tI'm " + briefCases[nChoice].getModel()
+ ". You just removed case # " + (nChoice+1));
System.out.println("t|" + nChoice + "| contains $"
+ briefCases[nChoice].getAmount() + "n");
briefCases[nChoice].remove();
return nChoice;
}
}
}
}
Model
is gone. Briefcase
is more useful, especially you don't have to use null values to encode a removed briefcase. I changed the alignment of briefcase, so there is no more dummy value. Then I tried to simplify everything a little bit.
This is far from perfect, there is still room for improvements.
$endgroup$
$begingroup$
what more could I improve?
$endgroup$
– Andrew Asmer
Sep 10 '11 at 15:13
$begingroup$
@Andrew Asmer: updated
$endgroup$
– Landei
Sep 10 '11 at 18:43
$begingroup$
what it's does this do? if (! cases[i].isRemoved()
$endgroup$
– Andrew Asmer
Sep 11 '11 at 2:03
$begingroup$
and also, I just noticed that each while loop has this while (true I don't get that, and within that block of code it doesn't contain a boolean that retusn false :|
$endgroup$
– Andrew Asmer
Sep 11 '11 at 2:26
$begingroup$
1) aBriefcase
holds a booleanremoved value (instead of setting it to
null), and this expression just checks that this particular
Briefcase` isn't removed yet. 2) I just translated your code: The loop asks "forever" as long as it has no correct answer. You can leave a loop in 4 ways: By not fulfilling the loop condition, by throwing an exception, by usingbreak
and - as I did here - by callingreturn
.
$endgroup$
– Landei
Sep 11 '11 at 7:47
|
show 10 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f4723%2fdeal-or-no-deal-console-game%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
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
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 List
s.
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 nn", 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<Integer> 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 < briefcases.length; i++) {
double value = amounts.get(i);
briefcases[i] = new Briefcase(value, i + 1, modelNames[i]);
}
}
public void showCases() {
for (int i = 0; i < briefcases.length; i++) {
System.out.print("t[" + briefcases[i] + "]");
if (i % 5 == 4) {
System.out.println();
}
}
System.out.println();
}
public void welcomeMessage() {
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
System.out.println("t~* Welcome ! ~*");
System.out.println("t~*~*~*~*~* Hosted by Kyel David ~*~*~*~*~*~*");
System.out.println("t~* Please Select from the Following Cases!~*");
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
}
public void startGame() {
boolean gamestatus = true;
casesSetup();
welcomeMessage();
showCases();
int choice = player.nUser();
myAmount = briefcases[choice].getAmount();
briefcases[choice].remove();
cases--;
while (gamestatus == true) {
showCases();
if (cases == 25 || cases == 19 || cases == 14 || cases == 10
|| cases == 7) {
for (a = b; a > 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 $%.2fn",
myAmount, offer);
System.out.printf("tYou've won your case with an amount of: %.2f",
myAmount);
} else {
System.out.println("tYou Accepted the offer of Banker");
System.out.printf("tYour case contains $%.2f and the bankers offer is $%.2fn",
myAmount, offer);
System.out.printf("tYou've won the offer of Banker: $%.2f", offer);
}
}
}
.
public class Play {
public static void main(String args) {
Game dnd = new Game();
dnd.startGame();
}
}
.
import java.util.Scanner;
public class Player {
Scanner input = new Scanner(System.in);
Banker banker = new Banker();
public boolean gamestatus() {
System.out.print("tAccept or Reject! [1]Accept [2]Reject: ");
int temp = input.nextInt();
System.out.println();
return temp != 1;
}
public int nUser() {
while (true) {
System.out.print("ntPlease Select Your Case!: ");
int nUser = input.nextInt() - 1;
if (nUser < 0 || nUser >= 26) {
System.out.println("tInvalid input Try again");
} else {
return nUser;
}
}
}
public int remove(int index, Briefcase briefCases) {
while (true) {
System.out.print("tPlease remove " + index + " case/s: ");
int nChoice = input.nextInt() - 1;
if (nChoice < 0 || nChoice >= briefCases.length || briefCases[nChoice].isRemoved()) {
System.out.println();
System.out.println("tInvalid Input please Try againn");
} else {
System.out.println("tI'm " + briefCases[nChoice].getModel()
+ ". You just removed case # " + (nChoice+1));
System.out.println("t|" + nChoice + "| contains $"
+ briefCases[nChoice].getAmount() + "n");
briefCases[nChoice].remove();
return nChoice;
}
}
}
}
Model
is gone. Briefcase
is more useful, especially you don't have to use null values to encode a removed briefcase. I changed the alignment of briefcase, so there is no more dummy value. Then I tried to simplify everything a little bit.
This is far from perfect, there is still room for improvements.
$endgroup$
$begingroup$
what more could I improve?
$endgroup$
– Andrew Asmer
Sep 10 '11 at 15:13
$begingroup$
@Andrew Asmer: updated
$endgroup$
– Landei
Sep 10 '11 at 18:43
$begingroup$
what it's does this do? if (! cases[i].isRemoved()
$endgroup$
– Andrew Asmer
Sep 11 '11 at 2:03
$begingroup$
and also, I just noticed that each while loop has this while (true I don't get that, and within that block of code it doesn't contain a boolean that retusn false :|
$endgroup$
– Andrew Asmer
Sep 11 '11 at 2:26
$begingroup$
1) aBriefcase
holds a booleanremoved value (instead of setting it to
null), and this expression just checks that this particular
Briefcase` isn't removed yet. 2) I just translated your code: The loop asks "forever" as long as it has no correct answer. You can leave a loop in 4 ways: By not fulfilling the loop condition, by throwing an exception, by usingbreak
and - as I did here - by callingreturn
.
$endgroup$
– Landei
Sep 11 '11 at 7:47
|
show 10 more comments
$begingroup$
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
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 List
s.
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 nn", 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<Integer> 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 < briefcases.length; i++) {
double value = amounts.get(i);
briefcases[i] = new Briefcase(value, i + 1, modelNames[i]);
}
}
public void showCases() {
for (int i = 0; i < briefcases.length; i++) {
System.out.print("t[" + briefcases[i] + "]");
if (i % 5 == 4) {
System.out.println();
}
}
System.out.println();
}
public void welcomeMessage() {
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
System.out.println("t~* Welcome ! ~*");
System.out.println("t~*~*~*~*~* Hosted by Kyel David ~*~*~*~*~*~*");
System.out.println("t~* Please Select from the Following Cases!~*");
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
}
public void startGame() {
boolean gamestatus = true;
casesSetup();
welcomeMessage();
showCases();
int choice = player.nUser();
myAmount = briefcases[choice].getAmount();
briefcases[choice].remove();
cases--;
while (gamestatus == true) {
showCases();
if (cases == 25 || cases == 19 || cases == 14 || cases == 10
|| cases == 7) {
for (a = b; a > 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 $%.2fn",
myAmount, offer);
System.out.printf("tYou've won your case with an amount of: %.2f",
myAmount);
} else {
System.out.println("tYou Accepted the offer of Banker");
System.out.printf("tYour case contains $%.2f and the bankers offer is $%.2fn",
myAmount, offer);
System.out.printf("tYou've won the offer of Banker: $%.2f", offer);
}
}
}
.
public class Play {
public static void main(String args) {
Game dnd = new Game();
dnd.startGame();
}
}
.
import java.util.Scanner;
public class Player {
Scanner input = new Scanner(System.in);
Banker banker = new Banker();
public boolean gamestatus() {
System.out.print("tAccept or Reject! [1]Accept [2]Reject: ");
int temp = input.nextInt();
System.out.println();
return temp != 1;
}
public int nUser() {
while (true) {
System.out.print("ntPlease Select Your Case!: ");
int nUser = input.nextInt() - 1;
if (nUser < 0 || nUser >= 26) {
System.out.println("tInvalid input Try again");
} else {
return nUser;
}
}
}
public int remove(int index, Briefcase briefCases) {
while (true) {
System.out.print("tPlease remove " + index + " case/s: ");
int nChoice = input.nextInt() - 1;
if (nChoice < 0 || nChoice >= briefCases.length || briefCases[nChoice].isRemoved()) {
System.out.println();
System.out.println("tInvalid Input please Try againn");
} else {
System.out.println("tI'm " + briefCases[nChoice].getModel()
+ ". You just removed case # " + (nChoice+1));
System.out.println("t|" + nChoice + "| contains $"
+ briefCases[nChoice].getAmount() + "n");
briefCases[nChoice].remove();
return nChoice;
}
}
}
}
Model
is gone. Briefcase
is more useful, especially you don't have to use null values to encode a removed briefcase. I changed the alignment of briefcase, so there is no more dummy value. Then I tried to simplify everything a little bit.
This is far from perfect, there is still room for improvements.
$endgroup$
$begingroup$
what more could I improve?
$endgroup$
– Andrew Asmer
Sep 10 '11 at 15:13
$begingroup$
@Andrew Asmer: updated
$endgroup$
– Landei
Sep 10 '11 at 18:43
$begingroup$
what it's does this do? if (! cases[i].isRemoved()
$endgroup$
– Andrew Asmer
Sep 11 '11 at 2:03
$begingroup$
and also, I just noticed that each while loop has this while (true I don't get that, and within that block of code it doesn't contain a boolean that retusn false :|
$endgroup$
– Andrew Asmer
Sep 11 '11 at 2:26
$begingroup$
1) aBriefcase
holds a booleanremoved value (instead of setting it to
null), and this expression just checks that this particular
Briefcase` isn't removed yet. 2) I just translated your code: The loop asks "forever" as long as it has no correct answer. You can leave a loop in 4 ways: By not fulfilling the loop condition, by throwing an exception, by usingbreak
and - as I did here - by callingreturn
.
$endgroup$
– Landei
Sep 11 '11 at 7:47
|
show 10 more comments
$begingroup$
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
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 List
s.
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 nn", 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<Integer> 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 < briefcases.length; i++) {
double value = amounts.get(i);
briefcases[i] = new Briefcase(value, i + 1, modelNames[i]);
}
}
public void showCases() {
for (int i = 0; i < briefcases.length; i++) {
System.out.print("t[" + briefcases[i] + "]");
if (i % 5 == 4) {
System.out.println();
}
}
System.out.println();
}
public void welcomeMessage() {
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
System.out.println("t~* Welcome ! ~*");
System.out.println("t~*~*~*~*~* Hosted by Kyel David ~*~*~*~*~*~*");
System.out.println("t~* Please Select from the Following Cases!~*");
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
}
public void startGame() {
boolean gamestatus = true;
casesSetup();
welcomeMessage();
showCases();
int choice = player.nUser();
myAmount = briefcases[choice].getAmount();
briefcases[choice].remove();
cases--;
while (gamestatus == true) {
showCases();
if (cases == 25 || cases == 19 || cases == 14 || cases == 10
|| cases == 7) {
for (a = b; a > 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 $%.2fn",
myAmount, offer);
System.out.printf("tYou've won your case with an amount of: %.2f",
myAmount);
} else {
System.out.println("tYou Accepted the offer of Banker");
System.out.printf("tYour case contains $%.2f and the bankers offer is $%.2fn",
myAmount, offer);
System.out.printf("tYou've won the offer of Banker: $%.2f", offer);
}
}
}
.
public class Play {
public static void main(String args) {
Game dnd = new Game();
dnd.startGame();
}
}
.
import java.util.Scanner;
public class Player {
Scanner input = new Scanner(System.in);
Banker banker = new Banker();
public boolean gamestatus() {
System.out.print("tAccept or Reject! [1]Accept [2]Reject: ");
int temp = input.nextInt();
System.out.println();
return temp != 1;
}
public int nUser() {
while (true) {
System.out.print("ntPlease Select Your Case!: ");
int nUser = input.nextInt() - 1;
if (nUser < 0 || nUser >= 26) {
System.out.println("tInvalid input Try again");
} else {
return nUser;
}
}
}
public int remove(int index, Briefcase briefCases) {
while (true) {
System.out.print("tPlease remove " + index + " case/s: ");
int nChoice = input.nextInt() - 1;
if (nChoice < 0 || nChoice >= briefCases.length || briefCases[nChoice].isRemoved()) {
System.out.println();
System.out.println("tInvalid Input please Try againn");
} else {
System.out.println("tI'm " + briefCases[nChoice].getModel()
+ ". You just removed case # " + (nChoice+1));
System.out.println("t|" + nChoice + "| contains $"
+ briefCases[nChoice].getAmount() + "n");
briefCases[nChoice].remove();
return nChoice;
}
}
}
}
Model
is gone. Briefcase
is more useful, especially you don't have to use null values to encode a removed briefcase. I changed the alignment of briefcase, so there is no more dummy value. Then I tried to simplify everything a little bit.
This is far from perfect, there is still room for improvements.
$endgroup$
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
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 List
s.
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 nn", 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<Integer> 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 < briefcases.length; i++) {
double value = amounts.get(i);
briefcases[i] = new Briefcase(value, i + 1, modelNames[i]);
}
}
public void showCases() {
for (int i = 0; i < briefcases.length; i++) {
System.out.print("t[" + briefcases[i] + "]");
if (i % 5 == 4) {
System.out.println();
}
}
System.out.println();
}
public void welcomeMessage() {
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
System.out.println("t~* Welcome ! ~*");
System.out.println("t~*~*~*~*~* Hosted by Kyel David ~*~*~*~*~*~*");
System.out.println("t~* Please Select from the Following Cases!~*");
System.out.println("t~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*");
}
public void startGame() {
boolean gamestatus = true;
casesSetup();
welcomeMessage();
showCases();
int choice = player.nUser();
myAmount = briefcases[choice].getAmount();
briefcases[choice].remove();
cases--;
while (gamestatus == true) {
showCases();
if (cases == 25 || cases == 19 || cases == 14 || cases == 10
|| cases == 7) {
for (a = b; a > 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 $%.2fn",
myAmount, offer);
System.out.printf("tYou've won your case with an amount of: %.2f",
myAmount);
} else {
System.out.println("tYou Accepted the offer of Banker");
System.out.printf("tYour case contains $%.2f and the bankers offer is $%.2fn",
myAmount, offer);
System.out.printf("tYou've won the offer of Banker: $%.2f", offer);
}
}
}
.
public class Play {
public static void main(String args) {
Game dnd = new Game();
dnd.startGame();
}
}
.
import java.util.Scanner;
public class Player {
Scanner input = new Scanner(System.in);
Banker banker = new Banker();
public boolean gamestatus() {
System.out.print("tAccept or Reject! [1]Accept [2]Reject: ");
int temp = input.nextInt();
System.out.println();
return temp != 1;
}
public int nUser() {
while (true) {
System.out.print("ntPlease Select Your Case!: ");
int nUser = input.nextInt() - 1;
if (nUser < 0 || nUser >= 26) {
System.out.println("tInvalid input Try again");
} else {
return nUser;
}
}
}
public int remove(int index, Briefcase briefCases) {
while (true) {
System.out.print("tPlease remove " + index + " case/s: ");
int nChoice = input.nextInt() - 1;
if (nChoice < 0 || nChoice >= briefCases.length || briefCases[nChoice].isRemoved()) {
System.out.println();
System.out.println("tInvalid Input please Try againn");
} else {
System.out.println("tI'm " + briefCases[nChoice].getModel()
+ ". You just removed case # " + (nChoice+1));
System.out.println("t|" + nChoice + "| contains $"
+ briefCases[nChoice].getAmount() + "n");
briefCases[nChoice].remove();
return nChoice;
}
}
}
}
Model
is gone. Briefcase
is more useful, especially you don't have to use null values to encode a removed briefcase. I changed the alignment of briefcase, so there is no more dummy value. Then I tried to simplify everything a little bit.
This is far from perfect, there is still room for improvements.
edited Sep 10 '11 at 18:42
answered Sep 10 '11 at 10:51
LandeiLandei
6,40711834
6,40711834
$begingroup$
what more could I improve?
$endgroup$
– Andrew Asmer
Sep 10 '11 at 15:13
$begingroup$
@Andrew Asmer: updated
$endgroup$
– Landei
Sep 10 '11 at 18:43
$begingroup$
what it's does this do? if (! cases[i].isRemoved()
$endgroup$
– Andrew Asmer
Sep 11 '11 at 2:03
$begingroup$
and also, I just noticed that each while loop has this while (true I don't get that, and within that block of code it doesn't contain a boolean that retusn false :|
$endgroup$
– Andrew Asmer
Sep 11 '11 at 2:26
$begingroup$
1) aBriefcase
holds a booleanremoved value (instead of setting it to
null), and this expression just checks that this particular
Briefcase` isn't removed yet. 2) I just translated your code: The loop asks "forever" as long as it has no correct answer. You can leave a loop in 4 ways: By not fulfilling the loop condition, by throwing an exception, by usingbreak
and - as I did here - by callingreturn
.
$endgroup$
– Landei
Sep 11 '11 at 7:47
|
show 10 more comments
$begingroup$
what more could I improve?
$endgroup$
– Andrew Asmer
Sep 10 '11 at 15:13
$begingroup$
@Andrew Asmer: updated
$endgroup$
– Landei
Sep 10 '11 at 18:43
$begingroup$
what it's does this do? if (! cases[i].isRemoved()
$endgroup$
– Andrew Asmer
Sep 11 '11 at 2:03
$begingroup$
and also, I just noticed that each while loop has this while (true I don't get that, and within that block of code it doesn't contain a boolean that retusn false :|
$endgroup$
– Andrew Asmer
Sep 11 '11 at 2:26
$begingroup$
1) aBriefcase
holds a booleanremoved value (instead of setting it to
null), and this expression just checks that this particular
Briefcase` isn't removed yet. 2) I just translated your code: The loop asks "forever" as long as it has no correct answer. You can leave a loop in 4 ways: By not fulfilling the loop condition, by throwing an exception, by usingbreak
and - as I did here - by callingreturn
.
$endgroup$
– Landei
Sep 11 '11 at 7:47
$begingroup$
what more could I improve?
$endgroup$
– Andrew Asmer
Sep 10 '11 at 15:13
$begingroup$
what more could I improve?
$endgroup$
– Andrew Asmer
Sep 10 '11 at 15:13
$begingroup$
@Andrew Asmer: updated
$endgroup$
– Landei
Sep 10 '11 at 18:43
$begingroup$
@Andrew Asmer: updated
$endgroup$
– Landei
Sep 10 '11 at 18:43
$begingroup$
what it's does this do? if (! cases[i].isRemoved()
$endgroup$
– Andrew Asmer
Sep 11 '11 at 2:03
$begingroup$
what it's does this do? if (! cases[i].isRemoved()
$endgroup$
– Andrew Asmer
Sep 11 '11 at 2:03
$begingroup$
and also, I just noticed that each while loop has this while (true I don't get that, and within that block of code it doesn't contain a boolean that retusn false :|
$endgroup$
– Andrew Asmer
Sep 11 '11 at 2:26
$begingroup$
and also, I just noticed that each while loop has this while (true I don't get that, and within that block of code it doesn't contain a boolean that retusn false :|
$endgroup$
– Andrew Asmer
Sep 11 '11 at 2:26
$begingroup$
1) a
Briefcase
holds a boolean removed value (instead of setting it to
null), and this expression just checks that this particular
Briefcase` isn't removed yet. 2) I just translated your code: The loop asks "forever" as long as it has no correct answer. You can leave a loop in 4 ways: By not fulfilling the loop condition, by throwing an exception, by using break
and - as I did here - by calling return
.$endgroup$
– Landei
Sep 11 '11 at 7:47
$begingroup$
1) a
Briefcase
holds a boolean removed value (instead of setting it to
null), and this expression just checks that this particular
Briefcase` isn't removed yet. 2) I just translated your code: The loop asks "forever" as long as it has no correct answer. You can leave a loop in 4 ways: By not fulfilling the loop condition, by throwing an exception, by using break
and - as I did here - by calling return
.$endgroup$
– Landei
Sep 11 '11 at 7:47
|
show 10 more comments
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f4723%2fdeal-or-no-deal-console-game%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown