|
HotJoe Java Help Forums
|
|
|
Please send email if you are having login problems - see the posts below for more info. |
|
| Author |
Message |
|
|
Hey there folks. I'm very new to java, and this program is my first attempt at a class with multiple methods. It's a very basic black jack program, and I'm having some trouble debugging it. I know it's a mess, but any help from someone more experienced than I would be much appreciated.
import java.util.Scanner;
import java.util.Random;
public class Player
{
public static double winbet(double x)
{
double win;
win = 2*x;
return(win);
}
public static double losebet(double x)
{
double lose;
lose = x*(-1);
return (lose);
}
public static double bankroll(double x)
{
double bankroll = 0;
bankroll = bankroll + x;
return bankroll;
}
public static void main(String[] args)
{
//preliminary actions in casino.
Scanner in = new Scanner(System.in);
System.out.print("Welcome to the casino! How much money would you like to put into your account?");
double balance = in.nextDouble();
bankroll(balance);
System.out.println("Transaction confirmed. Your current balance is $" + balance);
// end preliminary actions.
//Asks the player where or not they want to play blackjack
System.out.print("Would you like to play Black Jack? Yes(1) or No(2)?");
int choice = in.nextInt();
//end question.
while (choice==1 && balance >=0)
{
//asks the player how much they would like to bet.
System.out.print("How much would you like to bet?");
double bet = in.nextDouble();
if (bet > balance)
{
System.out.print("You do not have sufficient funds to cover that bet. please enter a new value.");
bet = in.nextDouble();
}
//basic game play code here
Random deck = new Random();
int draw, newdraw;
draw = deck.nextInt(11)+ 1;
System.out.println("You drew a "+draw+".");
int housedraw, newhousedraw;
housedraw = deck.nextInt(11)+1;
System.out.println("The house drew a "+housedraw+".");
System.out.print("Hit(1) or stay(2)?");
int answer = in.nextInt();
while (answer == 1 && housedraw <=17)
{
newdraw = deck.nextInt(11)+1;
System.out.println("You drew a "+newdraw+".");
draw = newdraw + draw;
newdraw = deck.nextInt(11)+1;
newhousedraw = deck.nextInt(11)+1;
System.out.println("The house drew a "+newhousedraw+".");
housedraw = newhousedraw + housedraw;
newhousedraw = deck.nextInt(11)+1;
System.out.print("Hit(1) or stay(2)?");
answer = in.nextInt();
if(answer == 2)
{
System.out.println("You are holding at "+ draw);
newhousedraw = deck.nextInt(11)+1;
System.out.println("The house drew a "+newhousedraw+".");
housedraw = newhousedraw + housedraw;
newhousedraw = deck.nextInt(11)+1;
System.out.print("Hit(1) or stay(2)?");
answer = in.nextInt();
}
//end basic gameplay code
//player constraints start
if (draw == 21)
{
winbet(bet);
bankroll(bet + balance);
System.out.println("Player Black Jack. Congratulations! your current balance is $" + (bet + balance)); break;
}
else if (draw >= 22)
{
losebet(bet);
bankroll(balance - bet);
System.out.println("Player Bust. Better luck next time! Your current balance is $" + (balance - bet)); break;
}
//player constraints end
//house constraints start
if (housedraw == 21)
{
losebet(bet);
bankroll(balance - bet);
System.out.println("House Black Jack. Better luck next time! Your curren balance is $" + (balance - bet)); break;
}
if (housedraw >= 22)
{
winbet(bet);
bankroll(bet + balance);
System.out.println("House bust! Congratulations! Your current balance is $"+ (bet + balance)); break;
}
//house constraints end
//if neither bust nor blackjack is obtained, the normal win/lose code is used
if (answer ==1 && housedraw >= 17 && housedraw < 21)
{
newdraw = deck.nextInt(11)+1;
System.out.println("You drew a "+newdraw+".");
draw = newdraw + draw;
System.out.println("House is holding at " + housedraw);
System.out.print("Hit(1) or stay(2)?");
answer = in.nextInt();
if (answer ==1)
{
newdraw = deck.nextInt(11)+1;
System.out.println("You drew a "+newdraw+".");
draw = newdraw + draw;
}
else
{
System.out.println("You are holding at " + draw + ".");
}
if (housedraw > draw || housedraw == draw)
{
losebet(bet);
bankroll(balance - bet);
System.out.println("House wins. Better luck next time! Your current balance is $"+ (balance - bet)); break;
}
else if (draw > housedraw && draw < 21)
{
winbet(bet);
bankroll(bet + balance);
System.out.println("You win! Congratulations! Your current balance is $"+ (bet + balance)); break;
}
}
else if (answer == 2 && housedraw >= 17 && housedraw < 21)
{
System.out.println("House is holding at " + housedraw);
System.out.print("Hit(1) or stay(2)?");
answer = in.nextInt();
if (answer ==1)
{
newdraw = deck.nextInt(11)+1;
System.out.println("You drew a "+newdraw+".");
draw = newdraw + draw;
}
else
{
System.out.println("You are holding at " + draw + ".");
}
if (housedraw > draw || housedraw == draw)
{
losebet(bet);
bankroll(balance - bet);
System.out.println("House wins. Better luck next time! Your current balance is $"+ (balance - bet)); break;
}
else if (draw > housedraw && draw < 21)
{
winbet(bet);
bankroll(bet + balance);
System.out.println("You win! Congratulations! Your current balance is $"+ (bet + balance)); break;
}
}
}
//end normal win/lose code
if (balance <= 0)
{
System.out.println("You acount has run out of money. If you would like to continue, \nplease deposit more money, or press 2 to quit.");
choice = in.nextInt();
}
System.out.print("Would you like to play again? Yes(1) or No(2)?");
choice = in.nextInt();
}
if (choice == 2)
{
System.out.println("Transaction cancelled. Have a nice day!");
}
}
}
|
 |
|
|
|
|
|
|
Powered by JForum 2.1.9 © JForum Team
This site run by Scott Dunbar of Xigole Systems. © 2005-2011 - Scott Dunbar
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners
hotjoe.com, xigole.com, and Scott Dunbar have no affiliation with Oracle
|