12
\$\begingroup\$

I am fairly new to this, so please tell me whether this is a good way to write code or if I'm missing anything.

import ehs.*;
import java.util.Scanner;
public class blackjack
{
  public static void main(String[] args)
{
  BlackJack();
}

public static void BlackJack()
{
  boolean again;
  int money = money();
  int cheat = hiddenCheat();
  do{
  int cheatCode = cheatMenu(cheat);
  int multiplier = activate(cheatCode);
  int bet = bet(money, cheatCode);
  Deck deck = dealDeck();
  int com = comDeal(deck, cheatCode);
  int user = userDeal(deck, cheatCode);
  int userTotal = userHit(user, deck, cheatCode);
  int comTotal = comHit(com, deck, userTotal, cheatCode);
  int winner = whoWon(userTotal, comTotal);
  money = winnings(winner, bet, money, multiplier, cheatCode);
  cheat = hiddenCheat();}
  while(money > 0);
  bye();
}

public static int money()
{
  System.out.println("You will start with 50 dollars");
  System.out.println();
  return 50;
}

public static int hiddenCheat()
{
  Delay.wait(100);
  Scanner kb = new Scanner(System.in);
  System.out.print("Ready To Play (y)es (n)o  -> ");
  String again = kb.nextLine();
  while (!again.equals("n") && !again.equals("y") && !again.equals("cheat")){
  System.out.print("Ready To Play (y)es (n)o  -> ");
  again = kb.nextLine();}
  if ((again).equals("n"))
    System.exit(0);
  if (again .equals("y"))
    return 0;
  if ((again).equals("cheat"))
    return 1;
  return 0;
}

public static int cheatMenu(int cheat)
{
  if(cheat == 0)
    return 0;
  System.out.println();
  System.out.println("HIDDEN CHEAT MENU");
  System.out.println();
  System.out.println("1. Score Multiplier");
  System.out.println();
  System.out.println("2. Unlimited Money");
  System.out.println();
  System.out.println("3. Com Alwyas Busts");
  System.out.println();
  System.out.println("4. 21 Everytime");
  System.out.println();
  System.out.println("5. Turn Off All Cheats");
  System.out.println();
  Scanner kb = new Scanner(System.in);
  System.out.print("Which cheat would you like? - > ");
  int code = kb.nextInt();
  while(code != 1 && code != 2 && code != 3 && code != 4 && code != 5){
    System.out.print("Not a valid cheat code, try again - > ");
    code = kb.nextInt();}
  return code;  
}

public static int activate(int code)
{
  if(code == 0)
    return 1;
  Scanner kb = new Scanner(System.in);
  System.out.println();
  if(code == 1){
    System.out.print("what Would you like you Score Multiplier to be - > ");
    int score = kb.nextInt();
    System.out.print("Score Multiplier Activated (" + score + ")");
    System.out.println();  
    return score;
  }
  if(code == 2){
    System.out.println("Unlimited Money Activated");
    System.out.println();}
  if(code == 3){
    System.out.println("Com Alwyas Busts Activated");
    System.out.println();}
  if(code == 4){
    System.out.println("21 Everytime Activated");
    System.out.println();}
  if(code == 5){
    System.out.println("All Cheats Deactivated");
    System.out.println();}
 return 1;  
}

public static int bet(int money, int code)
{
  if(code == 2){
    money = 999999999;
    System.out.println("You now have " + money + " dollars");}
  Scanner kb = new Scanner(System.in);
  System.out.println();
  System.out.print("How much would you like to bet - > ");
  int bet = kb.nextInt();
  while(bet > money){
    System.out.println("You dont have that much money");
    System.out.println();
    System.out.print("How much would you like to bet - > ");
    bet = kb.nextInt();}
  return bet;  
}

public static Deck dealDeck()
{
  Deck deck = new Deck();
  deck.shuffle();
  return deck;
}

public static int comDeal(Deck deck, int code) 
{
  Card c1 = deck.dealOne();
  Card c2 = deck.dealOne();
  int total = 0;
  total = total + c1.BJvalue() + c2.BJvalue();
  if(code == 3){
    while(total != 16){
       total = 0;
       c1 = deck.dealOne();
       c2 = deck.dealOne();
       total = c1.BJvalue() + c2.BJvalue();}}
  System.out.println();
  System.out.println("Com Cards...");
  Delay.wait(100);
  System.out.println();
  System.out.print(c1.face() + " of " + c1.suit());
  System.out.println(" and blank");
  if(c1.BJvalue() == 1 || c2.BJvalue() == 1){
    if(c1.BJvalue() == 1 && c2.BJvalue() == 1){
      total = 12;}
    else if(c1.BJvalue() == 1){
      total = 11 + c2.BJvalue();}
    else total = 11 + c1.BJvalue();}
  return total;
}

public static int userDeal(Deck deck, int code) 
{
  Scanner kb = new Scanner(System.in);
  System.out.println();
  Delay.wait(300);
  Card c1 = deck.dealOne();
  Card c2 = deck.dealOne();
  int total = 0;
  total = total + c1.BJvalue() + c2.BJvalue();
  int ace = 0;
  int ace2 = 0;
  if(c1.BJvalue() == 1 || c2.BJvalue() == 1){
    System.out.println();
    System.out.print(c1.face() + " of " + c1.suit());
    System.out.println(" and " + c2.face() + " of " + c2.suit());
    if(c1.BJvalue() == 1 && c2.BJvalue() == 1){
    System.out.println();
    System.out.print("Would you like your first ace to be (11) or (1) -> ");
    ace = kb.nextInt();
    while(ace != 1 && ace != 11){
      System.out.print("Has to be either (11) or (1) -> ");
      ace = kb.nextInt();}
    System.out.println();
    System.out.print("Would you like your second ace to be (11) or (1) -> ");
    ace2 = kb.nextInt();
    while(ace2 != 1 && ace2 != 11){
      System.out.print("Has to be either (11) or (1) -> ");
      ace2 = kb.nextInt();}
    total = ace + ace2;}
    if(c1.BJvalue() == 1){
      System.out.println();
      System.out.print("Would you like your ace to be (11) or (1) -> ");
      ace = kb.nextInt();
      while(ace != 1 && ace != 11){
        System.out.print("Has to be either (11) or (1) -> ");
        ace = kb.nextInt();}
    total = ace + c2.BJvalue();}
    if(c2.BJvalue() == 1){
      System.out.println();
      System.out.print("Would you like your ace to be (11) or (1) -> ");
      ace2 = kb.nextInt();
      while(ace2 != 1 && ace2 != 11){
        System.out.print("Has to be either (11) or (1) -> ");
        ace2 = kb.nextInt();}
    total = ace2 + c1.BJvalue();}}
  if(code == 4){
    while(total < 10){
      total = 0;
      c1 = deck.dealOne();
      c2 = deck.dealOne();
      total = c1.BJvalue() + c2.BJvalue();}}
  System.out.println("Your Cards...");
  Delay.wait(100);
  System.out.println();
  System.out.print(c1.face() + " of " + c1.suit());
  System.out.println(" and " + c2.face() + " of " + c2.suit() + " (" + total + ")");
  System.out.println();
  return total;  
}

public static int userHit(int total, Deck deck, int code) 
{
  Scanner kb = new Scanner(System.in);
  int ace = 0;
  boolean again = true;
  again = again("Hit?");
  if(again == false)
    return total;
  int cheat = total;
  do{
    Card c1 = deck.dealOne();
    int total2 = total;
    total = total + c1.BJvalue();
    if(c1.BJvalue() == 1){
      System.out.println();
      System.out.print(c1.face() + " of " + c1.suit());
      System.out.println();
      System.out.println();
      System.out.print("Would you like your ace to be (11) or (1) -> ");
      ace = kb.nextInt();
      while(ace != 1 && ace != 11){
        System.out.print("Has to be either (11) or (1) -> ");
        ace = kb.nextInt();}
      total = total2 + ace;}
    if(code == 4){
      while(total != 21){
         c1 = deck.dealOne();
         total = cheat + c1.BJvalue();}}
    Delay.wait(100);
    System.out.println();
    System.out.println(c1.face() + " of " + c1.suit() + " (" + total + ")");
    System.out.println();
    if(total < 21)
      again = again("Hit?");
    if(total > 21){
      System.out.println();
      System.out.println("BUSTED");
      System.out.println();
      return total;}}
   while(again && total < 21);
   return total;
}

public static int comHit(int total, Deck deck , int user, int code) 
{
  int total2 = total;
  if(user > 21)
    return total;
  System.out.println("Com's Turn");
  while(total <= 17){
    Card c1 = deck.dealOne();
    if(code == 3){
      while (c1.BJvalue() < 6){
        c1 = deck.dealOne();}}
    total = total + c1.BJvalue();
    if(c1.BJvalue() == 1){
      if(total2 <= 10){
        total = total2 + 11;}}
    Delay.wait(200);
    System.out.println();
    System.out.println(c1.face() + " of " + c1.suit() + " (" + total + ")");}
  while(user > total){
    Card c1 = deck.dealOne();
    total = total + c1.BJvalue();
    Delay.wait(200);
    System.out.println();
    System.out.println(c1.face() + " of " + c1.suit() + " (" + total + ")");}
    if(total > 21){
      System.out.println();
      System.out.println("BUSTED");
      System.out.println();
      return total;}
    return total;  
}

public static int whoWon(int total, int comtotal)
{
  if (total == comtotal)
    return 2;
  if (total > 21)
    return 0;
  if (comtotal > 21)
    return 1;
  if (total <= 21 && comtotal <= 21){
    if(total > comtotal)
      return 1;
    else
      return 0;}
  return 3;
}

public static int winnings(int winner, int bet, int money, int multiplier, int code)
{
  if(code == 2)
    money = 999999999;
  Delay.wait(200);
  if(winner == 1){
    System.out.println("You Won " + (bet * 2) * multiplier + " Dollars");
    System.out.println();
    Delay.wait(100);
    System.out.println("You Now Have " + (money + bet) * multiplier + " Dollars");
    System.out.println();
    return (money + bet) * multiplier;}
  else if(winner == 2){
    System.out.println();
    System.out.println("Push");
    System.out.println();
    return money;}
  else{
    System.out.println();
    System.out.println("You Lost " + bet + " Dollars");
    System.out.println();
    System.out.println("You Now Have " + (money - bet) + " Dollars");
    System.out.println();
    return money - bet;}
}

public static boolean again(String msg)
{
  boolean local;
  Scanner kb = new Scanner(System.in);
  System.out.print(msg + " (y)es (n)o  -> ");
  char again = kb.next().charAt(0);
  while (again != 'n' && again != 'y'){
  System.out.print(msg + " (y)es (n)o  -> ");
  again = kb.next().charAt(0);}
  if (again == 'n')
    local = false;
  else local = true;
  return local;
}

public static void bye()
{
  System.out.println("You are out of money, come back later");
}
}
\$\endgroup\$
9
\$\begingroup\$

That is going to be a different answer from everybody, really. I personally am not keen on the way you have typed it because of your code layout and the positioning of your variables. However, I know a lot of people who write their code exactly as you have done it.

Honestly, you will reach a point where you realize there is no 'right' way to write code. There is a clean way of writing code, and there is a way that will be encouraged as it is more readable, but whichever way you find easiest is right.

However, there are areas that clearly need improving. For example, you have included the extract Scanner kb = new Scanner(System.in); over 4 times within your code, each in different methods, whereas you could instead define it only once before the methods. This is known as an instance variable, where you define the variable inside a class, but outside of a method.

Then, you have System.out.println(); after printing a message so that you can leave a line, when you could do System.out.println("My Message!\n"), so that is something you can do.

But, as I said before, you need to choose a format that you are happy with, and just stick to that. Use things like camel-case and capitalising class names, but as long as you and others can read it, you are okay.

Just for practice, I am editing your code to make it more compact. I will post it in about 20 minutes.

Please tell me if you understand.

\$\endgroup\$
  • \$\begingroup\$ Thank you so much for this response, this is exactly what I was looking for, very clear and constructive help. \$\endgroup\$ – user3808597 Dec 20 '14 at 22:14
  • \$\begingroup\$ Just out of curiosity, are you new to Java? \$\endgroup\$ – mgthomas99 Dec 20 '14 at 22:15
  • \$\begingroup\$ Yes, I'm fairly new, almost a month in. \$\endgroup\$ – user3808597 Dec 20 '14 at 22:17
  • 2
    \$\begingroup\$ Ah, very nice. There's a cool website I sometimes go on, www.codingbat.com , it presents you with not-too-hard Java problems to solve. But if you need any help, you can just post it here or message me, I don't mind. \$\endgroup\$ – mgthomas99 Dec 20 '14 at 22:24
  • \$\begingroup\$ Hey @GeorgeEpicGen, welcome to Code Review! Nice answer. \$\endgroup\$ – RubberDuck Dec 20 '14 at 22:25
7
\$\begingroup\$

Also not a Java dev, so this is a readability thing

Watch your indentation. Particularly, this.

  do{
  int cheatCode = cheatMenu(cheat);
  int multiplier = activate(cheatCode);
  int bet = bet(money, cheatCode);
  Deck deck = dealDeck();
  int com = comDeal(deck, cheatCode);
  int user = userDeal(deck, cheatCode);
  int userTotal = userHit(user, deck, cheatCode);
  int comTotal = comHit(com, deck, userTotal, cheatCode);
  int winner = whoWon(userTotal, comTotal);
  money = winnings(winner, bet, money, multiplier, cheatCode);
  cheat = hiddenCheat();}
  while(money > 0);
  bye();
}

Everything inside of that do loop should be one level deeper.


I took a harder look at your code and noticed this.

public static int money()
{
    System.out.println("You will start with 50 dollars");
    System.out.println();
    return 50;
}

This seems to be in violation of the Single Responsibility Principle. It does two things. It prints a message to the user and returns the initial value. I would much rather see your main routine look something like this. (Remember, I don't know Java, consider it psuedo code)

int money = 50;
PrintStartingMoneyMessage(money);

Where you would dynamically concatenate the value of money into the message. This has the added benefit of making so that if you change the initial value, your message automatically changes with it. The value and message can never get out of sync because you forgot to do something.

\$\endgroup\$
  • \$\begingroup\$ You're welcome @user3808597! I added some more information to my answer. I hope you find it helpful as well. \$\endgroup\$ – RubberDuck Dec 21 '14 at 13:15
6
\$\begingroup\$

If you'll just be printing hard-coded text, then there's no need for a method:

public static void bye()
{
    System.out.println("You are out of money, come back later");
}

Just print this inline, which will still be clear.

\$\endgroup\$
  • \$\begingroup\$ I did think of that, but I was trying to stay consistent of only using methods in my main method. I do agree that it would make more sense just to print it, thank you \$\endgroup\$ – user3808597 Dec 20 '14 at 22:12
5
\$\begingroup\$

Method names should start with a verb: blackJack or money or even hiddenCheat aren't really descriptive names.

Also as a C# dev it's harder for me to put myself in a Java mindset when the braces are all C#-style... on the other hand, it's nice that you're consistent with them.

if(...)
    ...

You should always use braces with if scopes; not properly bracing ifs is more error-prone.

\$\endgroup\$
  • 1
    \$\begingroup\$ "It's harder to put on my Java mindset". You mean your hat? \$\endgroup\$ – RubberDuck Dec 21 '14 at 0:48
  • \$\begingroup\$ Do you have any evidence to back up the claim "not properly bracing ifs is more error-prone."? \$\endgroup\$ – Pharap Dec 21 '14 at 12:49
  • \$\begingroup\$ @Pharap heard of Apple's SSL "goto fail" bug? \$\endgroup\$ – Mathieu Guindon Dec 21 '14 at 12:52
  • \$\begingroup\$ @Mat'sMug That's not exactly an example of a well-written piece of code though. The line 'goto fail' appears over 40 times on a single page, there were missing #endifs, the use of if statements wasn't even consistent (some lines had the statement follow on the same line, others had it indented underneath, others actually used braces), the indentation was inconsistent and a reasonable chunk of the code was case fall-through. I'm amazed Apple's SSL system didn't fall down sooner opensource.apple.com/source/Security/Security-55471/…. \$\endgroup\$ – Pharap Dec 21 '14 at 13:15
5
\$\begingroup\$

You should be using the Java Bracing style

if (condition) {
     //Operation
}

and not the C# bracing style

\$\endgroup\$
  • 3
    \$\begingroup\$ Java and C# developers are pretty nitpicky about the bracing, it helps us tell what language we are looking at. \$\endgroup\$ – Malachi Dec 20 '14 at 22:18
  • \$\begingroup\$ Related: en.wikipedia.org/wiki/Indent_style#Styles Though I'll admit I'm one of those horrible people who uses a variant of Allman style no matter what language I'm writing in. \$\endgroup\$ – Pharap Dec 21 '14 at 12:47
3
\$\begingroup\$

I agree with most everything that has been said here, but feel there is something that needs to be stressed.

When you write code, you're actually writing for two different audiences, the computer and those humans that are going to read your code. You'll find that computers are FAR less picky about code than humans (smile). As long as your code is syntactically correct, the computer won't complain at at.

Those pesky humans are another story, however. Some of them even have the gall to suggest that the code should be as readable and clear as possible. Well, ok, even I'm in that camp.

Clarity in your code will not only help others understand your code, you will find that it helps YOU, too. Most of the suggestions presented here would seem to help with the clarity of your code. The only suggestion I would take exception to is the one to use the 'Java Bracing' style. This has been hotly debated elsewhere and both sides seem to have lots of followers. My preference is to use the 'Vertical' bracing style where the begin and end braces are both at the same character positions on their respective lines with the code indented as shown below:

if (condition)
{
    //condition-code
}

I also put the begin and end braces alone on their respective lines (again, for readability). I find it far more readable than the other style.

But, pick a style YOU like that works for YOU and use it consistently. That will serve you best in the end. You will probably find that your personal 'style' will evolve as you read (and write) more code. Just try to make sure that it 'evolves' to your benefit.

Mark Ross (markross7@comcast.net)

\$\endgroup\$
  • \$\begingroup\$ "Any fool can write code that a computer can understand. Good programmers write code that humans can understand" - Martin Fowler. You should write code that is easy for programmers to read, never mind the computer. +1 \$\endgroup\$ – mgthomas99 Apr 3 '18 at 10:31
1
\$\begingroup\$

First off, I'm a stickler for grammar/spelling. You have a spelling error in your activate function. Check for what the com player always does...

When you have the computer deal its cards, and the user plays, you are handling aces differently... Take for example this segment from the computer deal:

 if(c1.BJvalue() == 1 || c2.BJvalue() == 1){
    if(c1.BJvalue() == 1 && c2.BJvalue() == 1){
      total = 12;}
    else if(c1.BJvalue() == 1){
      total = 11 + c2.BJvalue();}
    else total = 11 + c1.BJvalue();}
  return total;

I don't know Java, but it looks as though the second if statement never executes - There is an OR statement, then it checks for an AND statement. This does not look right to me. For the Player, you ask the player whether he wants the first Ace to be a 1 or eleven. You also repeat this for the second ace, and potentially any other Ace. One way to solve this is to have a function that checks what has been dealt, and return the upper value. Pseudo code below (again, I don't do Java):

int cardTotalValues(player) {
  int cardValue1 = player.card1;
  ...
  int cardValue5 = player.card5;
  boolean hasAceInHand = player.hasAceInHand;
  int totalCardValue = cardValue1 + cardvalue2 + ... + cardValue5;
  if (hasAceInHand == true AND totalCardValue >= 22) {
    return totalCardValue - 10;
  } else {
    return totalCardValue;
  }
}

Essentially, you only need to worry about taking away the 10 points if there's an Ace, and the player would normally bust (card value is greater than 21). Again, pseudo code, but I hope you understand the reasoning behind it.

The other added value is that you do not need to worry about as many things - Pass on the player object (you do have one, right?) that contains up to the 5 cards the player has. This can be either the computer, or the human player, or expanded to be a multiplayer Blackjack. Check the cards whenever a change in that player's hand occurs, and just look for the return value.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.