HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Beginning Blackjack program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
beginningprogramblackjack

Problem

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.ou

Solution

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.

Context

StackExchange Code Review Q#74332, answer score: 9

Revisions (0)

No revisions yet.