Reputation: 13
I'm currently working on a console game with a group of 3 people including myself who are creating the game "Black jack" for a school project but we have an issue in the program within java class 'Menu.java' I made a method to check the current value of the card every time the player selects "HIT" within a switch statement in line 63 which then calls the method called playerGetCard() in the Dealer.java class which then calls the method n.addValue(x); in line 143 and returns it into the Menu.java class which then adds up the number of the value of the random card given in the Dealer.java in a setter method which was renamed to addValue in line 22 within class Menu.java, because I want to add up all the numbers that the player is given and as soon as someone selects "HIT" in the Menu.Java class in line 37 it calls a method called "handValuePlayerCheck" in line 26 that checks to see if the current value of the card hits above 21 if it does then the game ends and changes the boolean named "setLose" the setter method to true in line 14, but for some reason the if statement refuses to output the System.out.println("YOU LOST"); and also doesn't change the boolean to true which ends the game within the menu() method on line 55 under Menu.java class.
I've tried everything such as not using a setter or getter method and also trying to use the "this." method but that doesn't seem to work either, does anyone have an idea why this doesn't seem to update the boolean value called "lose"?
Tried not using Getter / Setter methods.
Changed the Getter / Setter methods into WinOrLose.java class but it didn't work either also tried to place it in the same class which checks the numbers in the first place in Menu.Java class in line 26
Also tried using this.(variableName); but it didn't seem to change anything.
Tried renaming the variables but nothing seems to change.
Also tried checking if a variable is overwriting a value with the same name but I found nothing that could be overwriting anything.
Main.java
package com.tony;
public class Main {
public static void main(String[] args) {
Dealer d = new Dealer();
Menu menu = new Menu();
d.Cards();
menu.menu();
}
}
Dealer.java
package com.tony;
import java.util.ArrayList;
import java.util.Random;
import java.util.HashMap;
public class Dealer {
String cardName;
private int randomInt;
private int value;
private WinOrLose wl = new WinOrLose();
private Menu m = new Menu();
private HashMap<Integer, String> hmap = new HashMap<Integer, String>();
public void Cards(){
int counter = 1;
for(int i = 0; i<9; i++){
counter++;
hmap.put(i, counter + " of diamonds");
}
counter = 1;
for(int i = 9; i<18; i++){
counter++;
hmap.put(i, counter + " of clubs");
}
counter = 1;
for(int i = 18; i<27; i++){
counter++;
hmap.put(i, counter + " of hearts");
}
counter = 1;
for(int i = 27; i<36; i++){
counter++;
hmap.put(i, counter + " of spades");
}
for(int i = 36; i<37; i++){
hmap.put(i, "Ace of diamonds");
}
for(int i = 37; i<38; i++){
hmap.put(i, "Ace of clubs");
}
for(int i = 38; i<39; i++){
hmap.put(i, "Ace of hearts ");
}
for(int i = 39; i<40; i++){
hmap.put(i, "Ace of spades");
}
for(int i = 40; i<41; i++){
hmap.put(i, "Jack of diamonds");
}
for(int i = 41; i<42; i++){
hmap.put(i, "Jack of clubs");
}
for(int i = 42; i<43; i++){
hmap.put(i, "Jack of hearts");
}
for(int i = 43; i<44; i++){
hmap.put(i, "Jack of spades");
}
for(int i = 44; i<45; i++){
hmap.put(i, "Queen of spades");
}
for(int i = 45; i<46; i++){
hmap.put(i, "Queen of diamonds");
}
for(int i = 46; i<47; i++){
hmap.put(i, "Queen of clubs");
}
for(int i = 47; i<48; i++){
hmap.put(i, "Queen of hearts");
}
for(int i = 48; i<49; i++){
hmap.put(i, "King of spades");
}
for(int i = 49; i<50; i++){
hmap.put(i, "King of diamonds");
}
for(int i = 50; i<51; i++){
hmap.put(i, "King of clubs");
}
for(int i = 51; i<52; i++){
hmap.put(i, "King of hearts");
}
}
public void dealCard(){ // Once hit has been selected this will randomly generate a number
this.randomInt = (int) (Math.random()*52+1);
setCardName(hmap.get(this.randomInt));
}
public String getCardName() {
return cardName;
}
public void setCardName(String cardName) {
this.cardName = cardName;
}
public void playerGetCard() { // that card and it's value and adds it to m.addValue();
ArrayList<String> player = new ArrayList<String>();
player.add(getCardName());
if(getCardName().contains("Ace")){
System.out.println("This is an " + getCardName());
//Check players total card number if goes over add 1
//if doesn't add 10
if(m.getValue() <= 11){
m.addValue(11);
}else{
m.addValue(1);
}
}else if(getCardName().contains("Jack") || getCardName().contains("Queen") || getCardName().contains("King")) {
System.out.println("This is a " + getCardName());
// Check max in winorlose
m.addValue(10);
}else {
int n = Integer.parseInt(getCardName().substring(0, 1));
System.out.println(n);
m.addValue(n);
// check max in winorlose
System.out.println("Name of random card is " + getCardName());
}
System.out.println("Your total hand value is: " + m.getValue());
}
}
Menu.java
package com.tony;
import java.util.Scanner;
public class Menu {
private boolean lose;
private int value;
public boolean isLose() {
return lose;
}
public void setLose(boolean lose) {
this.lose = lose;
}
public int getValue() {
return value;
}
public void addValue(int value) { // This takes the value of the randomly given card and adds it here.
this.value = this.value + value;
}
public void handValuePlayerCheck(){ // Checks if the value of the cards that were given goes over 21 if it does then change boolean lose to false and display "You lost"
System.out.println(isLose());
if(this.value > 21){
System.out.println("YOU LOST");
setLose(true);
}
}
public void menu(){
Scanner i = new Scanner(System.in);
Player p = new Player();
Dealer d = new Dealer();
System.out.println("Welcome to black jack!");
d.Cards();
System.out.println("What is your name?");
p.setName(i.nextLine());
System.out.println("Let's Begin!");
System.out.println();
System.out.println("1: Hit");
System.out.println("2: Split");
System.out.println("3: Hold");
System.out.println("4: Double Down");
System.out.println("5: Surrender");
System.out.println("Your turn!");
while(true){
if (isLose()) {
System.out.println("You lost! Try again!");
break; // IF lose variable is set to TRUE then the game will end using the break command and display "You lost! Try Again!"
}
boolean has = i.hasNextInt();
if (has) {
switch (i.nextInt()) { //This calls the methods case 1: is the "HIT" option.
case 1:
handValuePlayerCheck();
d.dealCard();
d.playerGetCard();
break;
}
}
}
}
}
Upvotes: 1
Views: 1710
Reputation: 567
Well, in your main method you declare a Menu and a Dealer object which instantiates two Menu objects. Also in your menu() method you are creating a new Dealer instance.
What you should do is to move your menu() method from your Menu class to Dealer class and edit it a bit.
Menu.java
//the other methods and instance variables
//you should move your menu() method, I just commented it out
//for your conveniance
/*
public void menu(){
Scanner i = new Scanner(System.in);
Player p = new Player();
Dealer d = new Dealer();
System.out.println("Welcome to black jack!");
d.Cards();
System.out.println("What is your name?");
p.setName(i.nextLine());
System.out.println("Let's Begin!");
System.out.println();
System.out.println("1: Hit");
System.out.println("2: Split");
System.out.println("3: Hold");
System.out.println("4: Double Down");
System.out.println("5: Surrender");
System.out.println("Your turn!");
while(true){
if (isLose()) {
System.out.println("You lost! Try again!");
break; // IF lose variable is set to TRUE then the game will end using the break command and display "You lost! Try Again!"
}
boolean has = i.hasNextInt();
if (has) {
switch (i.nextInt()) { //This calls the methods case 1: is the "HIT" option.
case 1:
handValuePlayerCheck();
d.dealCard();
d.playerGetCard();
break;
}
}
}
}
*/
Dealer.java
//the other methods and instance variables
public void menu() {
Scanner i = new Scanner(System.in);
Player p = new Player();
System.out.println("Welcome to black jack!");
Cards(); //formerly, d.Cards()
System.out.println("What is your name?");
p.setName(i.nextLine());
System.out.println("Let's Begin!");
System.out.println();
System.out.println("1: Hit");
System.out.println("2: Split");
System.out.println("3: Hold");
System.out.println("4: Double Down");
System.out.println("5: Surrender");
System.out.println("Your turn!");
while(true){
if (m.isLose()) {
System.out.println("You lost! Try again!");
break; // IF lose variable is set to TRUE then the game will end using the break command and display "You lost! Try Again!"
}
boolean has = i.hasNextInt();
if (has) {
switch (i.nextInt()) { //This calls the methods case 1: is the "HIT" option.
case 1:
m.handValuePlayerCheck();
dealCard(); //formerly d.dealCard()
playerGetCard(); //formerly d.playerGetCard()
break;
}
}
}
}
And your main method should be like;
public static void main( String[] args ) {
Dealer d = new Dealer();
d.Cards();
d.menu();
}
Upvotes: 0
Reputation: 760
Your code ist a little confusing. But you have multiple instances of your classes.
Main problem is, that you have two menu instances. One which ist doing the check and which is printing the menu and one instance is created inside your Dealer instance which gets the value increased.
But you also created multiple Dealer instances. You created one in main which is not used later ...
You should define clearly what instances you need and which instances need each other ... So instead of creating new instances of Dealer and Menu inside the constructors, you could create the instances inside main and set the instance variables through setters.
Upvotes: 0
Reputation: 371
You are using two different instances of your menu class. (two sepearate objects)
One in your main method and one in your Dealer class.
So when you are adding Value, you are adding it two the one in the Dealer class.
But the value in your menu class always stays 0.
You can see this when you print the value in your Menu classes handValuePlayerCheck function.
Upvotes: 2