Hello,
So I have started to learn java from scratch, and I love it, any time spare I try to create programs (nothing complex yet)

The last one I have done is the game Pontoon, blackjack or 21, many names.

It works to how I would like it to, but only to the knowledge that I have.
I was wondering if anyone could give me tips on how to be more effective in my language writing, making the code shorter etc


I am aware that using arrays would be a lot more effective than the methods I have used to create the cards. But at present we have not been taught arrays so I didnt want to use anything I have yet been taught.

I also only use notepad and command prompt so nothing is fancy in text or colour, and why for example line 15 and 16 are the way they are (user freindlyness) so that text isnt cut out half way through a word

Thank you for any feedback.

Is it ok to post complete code like this? sorry if its not

import java.util.Scanner;
public class Pontoon
{
  public static void main(String[] args)
  {	
  //create Scanner
    Scanner myKeyboard = new Scanner(System.in);

  //initilize hands for player and comp at 0
    int playerHand = 0;
    int compHand = 0;
  
  //describe program to user
    System.out.print("Program to play the game pontoon (or 21) against the computer,"); 
    System.out.print(" if the game score");
    System.out.println(" is a tie the computer wins\n");

  //tell user what the values are
    System.out.println("The card values are as follows:");
    System.out.println("    Cards Two - Ten are valued 2 - 10");
    System.out.println("    Jack, Queen and King are valued 10");
    System.out.println("    Ace is valued at 11\n");

  //begin when user is ready
System.out.print("\n"+"Press enter to begin.");
String begin = myKeyboard.nextLine();


  //deal initial hands
    String playerFirstCard = rank();
    int playerCardOne = value(playerFirstCard);
    System.out.println("\n"+"Your first card is: "+playerFirstCard);

    String compFirstCard = rank();
    int compCardOne = value(compFirstCard);
    System.out.println("The computers first card is: "+compFirstCard);

System.out.print("\n"+"Press enter to continue.");
begin = myKeyboard.nextLine();

    String playerSecondCard = rank();
    int playerCardTwo = value(playerSecondCard);
    System.out.println("\n"+"Your second card is: "+playerSecondCard);
    playerHand = playerHand + (playerCardOne+playerCardTwo);

    String compSecondCard = rank();
    int compCardTwo = value(compSecondCard);
    System.out.println("The computers second card is: *hidden*");
    compHand = compHand + (compCardOne+compCardTwo);
   
    System.out.println("\n"+"Your score is: "+playerHand);

  //BLACKJACK (21 in first 2 cards)
    if ((playerCardOne+playerCardTwo)==21)
    {
      System.out.println("You have BLACKJACK");
    }
 
  //deal additional cards to player
  //initilize additional card values
    String playerNextCard;
    int playerCardNext = 0;
    System.out.print("\n"+"Would you like another card (y/n)? ");
    String additionalCard = myKeyboard.nextLine();
    while ((playerHand <= 21)&&(additionalCard.equalsIgnoreCase("y")))
    {
      playerNextCard = rank();
      playerCardNext = value(playerNextCard);
      System.out.println("\n"+"Your next card is: "+playerNextCard);
      playerHand = playerHand + playerCardNext;
      System.out.println("Your new score is: "+playerHand);
      if (playerHand > 21)
      {
	System.out.println("You are bust, no more cards dealt");
      }
      else
      {
      System.out.print("\n"+"Would you like another card (y/n)? ");
      additionalCard = myKeyboard.nextLine();
      }       
    }

  //pause in the game
System.out.print("\n"+"It is now the computers turn, please press enter to continue.");
begin = myKeyboard.nextLine();
		
  //deal additional cards to computer
  //initilize additional card values
    String compNextCard;
    int compCardNext = 0;
    System.out.println("\n"+"The computers second card is: "+compSecondCard);
    System.out.println("\n"+"The computers score is: "+compHand);

System.out.print("\n"+"Press enter to continue.\n");
begin = myKeyboard.nextLine();

    if ((compCardOne+compCardTwo)==21)	
    {
      System.out.println("The computer has BLACKJACK");
    }

    while (compHand < 15)
    {
      compNextCard = rank();
      compCardNext = value(compNextCard);
      System.out.println("\n"+"The computer will take another card");
      System.out.println("The computers next card is: "+compNextCard);
      compHand = compHand + compCardNext;
      System.out.println("The computers new score is: "+compHand); 
System.out.print("\n"+"Press enter to continue.\n");
begin = myKeyboard.nextLine(); 
    }
    System.out.println();


// for testing purposes
/*
System.out.println("Please enter values for each players hand for testing");
System.out.print("players score: ");
int playerHand = myKeyboard.nextInt();
System.out.print("comps score: ");
int compHand = myKeyboard.nextInt();
*/


  //final hand totals
    System.out.println("Your final score is: "+playerHand);
    System.out.println("The computers final score is: "+compHand+"\n");

  //check if anyone is bust
    if (playerHand > 21)
    {
      System.out.println("You are bust");
    }
    if (compHand > 21)
    {
      System.out.println("The computer is bust");
    }
    if ((playerHand > 21) && (compHand > 21))
    {
      System.out.println("\n"+"Both players are bust, no winner");
    }

  //check for winner if no-one bust
    if ((compHand > 21) && (playerHand <= 21))
    {
      System.out.println("You win!");
    }
    if ((playerHand > 21) && (compHand <= 21))
    {
      System.out.println("The computer wins!");
    }
    if ((compHand == playerHand) && (compHand <= 21))
    {
      System.out.println("Hands are tied, computer wins");
    }
    if ((compHand <= 21) && (compHand > playerHand))
    {
      System.out.println("The computer wins");
    }
    if ((playerHand <= 21) && (playerHand > compHand))
    {
      System.out.println("You win!");
    }

  }

//method for random number generator 1-13
  public static int random()
  {
    int number = (int)(Math.random()*13)+1;

    return number;   
  }

//method to turn random number between 1-13 into a card name
  public static String rank()
  {
    int a = random();
    String b = "a";
    switch (a)
    {
      case 1:
      b = "Ace";
      break;
      case 2:
      b = "Two";
      break;
      case 3:
      b = "Three";
      break;
      case 4:
      b = "Four";
      break;
      case 5:
      b = "Five";
      break;
      case 6:
      b = "Six";
      break;
      case 7:
      b = "Seven";
      break;
      case 8:
      b = "Eight";
      break;
      case 9:
      b = "Nine";
      break;
      case 10:
      b = "Ten";
      break;
      case 11:
      b = "Jack";
      break;
      case 12:
      b = "Queen";
      break;
      case 13:
      b = "King";
      break;
    }
    return b;
  }

//method to turn the rank into a value depending on the card name
  public static int value(String a)
  {
    //random number between 1 - 13 

    int b = 0;
    if (a.equals("Ace"))
    {
      b = 11;
    }
    if (a.equals("Two"))
    {
      b = 2;
    }
    if (a.equals("Three"))
    {
      b = 3;
    }   
    if (a.equals("Four"))
    {
      b = 4;
    }    
    if (a.equals("Five"))
    {
      b = 5;
    }
    if (a.equals("Six"))
    {
      b = 6;
    }
    if (a.equals("Seven"))
    {
      b = 7;
    }
    if (a.equals("Eight"))
    {
      b = 8;
    }
    if (a.equals("Nine"))
    {
      b = 9;
    }
    if (a.equals("Ten"))
    {
      b = 10;
    }
    if (a.equals("Jack"))
    {
      b = 10;
    }
    if (a.equals("Queen"))
    {
      b = 10;
    }
    if (a.equals("King"))
    {
      b = 10;
    }

    return b;
  }
}

Recommended Answers

All 9 Replies

Given the restriction of not using stuff you haven't been formally taught yet, I think you're doing very well. That's nice code, properly designed, formatted, commented etc. It looks quite professional. We see a lot worse code posted here.
The sooner you can start working with more of the language the better.

I've started learning Java from Scratch and this looks awesome to me, i wouldn't change anything, if you really feel like it's annoying you, try Bluej's auto layout feature, may make it look a little neater to you if anything, but it looks great.

if you are just learning, I would not recommend starting to use features of IDE's that (re-)write your code for you. yes, they come in handy, but, and especially when learning, since you will get the results, but, not necessarily in a 'developer-friendly' way (now I'm thinking about the GUI wysiwyg functionality), so it won't just be harder to edit (if you would want to), the readability of the code is also very ... bad.

but, as JamesCherrill and Ntropy already said, your code doesn't look bad. and for the code as you have it, using arrays might make it a bit more efficiënt (especially if you need to store all the steps), but for now, you're doing very well without them.

a little tip that may make your code a bit more efficiënt, well, it won't make much difference in smaller applications, but it can save a lot of overhead when you're running larger applications :)

I'm just putting a simple example here, not an actual snippet out of your code, but compare this logic to your 'value' method.

public int getValue(String month){
  int returnVal;
  if ( month.equals("January")){
    returnVal = 1;
  }
  if ( month.equals("March")){
    returnVal = 3;
  }
  if ( month.equals("August")){
    returnVal = 8;
  }
  // it's kind of obvious I didn't put all the possibilities here :)
  return returnVal;
}

one way to improve this (and make it easier) would be using an Enum, but I doubt you've seen this already, so we 'd better explain this later :)

in the code above, there is one minor effect you may want to avoid:
let's say you entered "January", so you want to return the value 1. Your first if statement will pick this up and set the returnVal to 1. But, even though you already know the value to return, your code will still perform all the next if-statements, even though by the logic implemented, you can easily predict that they will all return false.

a possible workaround would be using nested-if statements or the else-if statement:

public int getValue(String month){
  int returnVal;
  if ( month.equals("January")){
    returnVal = 1;
  }else{
     if ( month.equals("March")){
       returnVal = 3;
     }
     else if ( month.equals("August")){
        returnVal = 8;
     }
  }
  // it's kind of obvious I didn't put all the possibilities here :)
  return returnVal;
}

or, for a method like this, where you can have several values based on the result of a conditional expression (or any other reason for that matter), you can put multiple return statements in the method:

public int getValue(String month){
  if ( month.equals("January"))
    return 1;
  if ( month.equals("March"))
    return 3;
  if ( month.equals("August"))
    return 8;
   // if you would just put the code above, you would get a compiler error, because
   // when you just have this code and you would enter "December", non of your return 
   // statements would be called. the compiler realizes this and 'll tell you it doesn't
   // find a required return statement.
   // so, either you can throw an Exception (which you'll learn later (I think, I don't
   // know where in your course you are)), or, you can return a default value, 
   // -1, for instance
  return -1;
}

anyway, for someone who's just starting, you're doing great, so I can say (and I think I'm speaking for more people than just me) I'm really curious about your next posts and snippets :)

Member Avatar for hfx642

Arrays and Loops are a developer's best friends.
Once you start to use them, you'll wonder how you coded without them.
Your code will be MUCH shorter and a lot more efficient.
They are simple and (wait for it)... easy to use.

Thank you all for your feedback, its good to know that I am on track and that I have a good way of presenting my code. Im glad its understandable :)

Well I can't wait to learn arrays as I have read they are way more efficent, will come back and edit my old programs with arrays when I learn about it.

Also interested to find out what an Enum is now..

Well I had a go with what Stultuske said about in making the last method better, this is what I done, and it works etc, so just posting to show.

public static int value(String a)  
{    
//random number between 1 - 13      
   
if (a.equals("Ace"))    
      return 11;       
if (a.equals("Two"))    
      return 2;       
if (a.equals("Three"))     
      return 3;           
if (a.equals("Four"))    
      return 4;            
if (a.equals("Five"))    
      return 5;        
if (a.equals("Six"))    
      return 6;        
if (a.equals("Seven"))    
      return 7;        
if (a.equals("Eight"))    
      return 8;        
if (a.equals("Nine"))    
      return 9;        
if (a.equals("Ten"))    
      return 10;        
if (a.equals("Jack"))    
      return 10;        
if (a.equals("Queen"))    
      return 10;        
if (a.equals("King"))    
      return 10;         
return -1;  
}

and that I believe now is the shortest I can get it without arrays. But thank you for that advice, it is very helpful and will become useful in the future I am sure :)

Just to be sure, with this method above, it is not possible to use a switch statement instead of if?
This is because a switch can only work on a char not a whole String. Is this correct?

no.
until quite recently, you could just use primitives (char, integer,..) as a parameter for a switch statement. Java 7, however, has as one of the new features, that you can use a String object as a parameter on which to run a switch statement, but, since not everybody has updated to Java 7 yet, you won't see too many examples of it yet.

Ok so I tried the switch and it does work :) Thats great to know to, heres the snipet...
took out the numbers inbetween as you get the picture.

public static int value(String a)  
{    
//random number between 1 - 13      

int b = 0;
switch (a)
{
case "Ace":
b = 11;
break;
case "Two":
b = 2;
break;
case "King":
b = 10;
break;
}
return b;
}

Other than not everyone having java 7 yet, and so for some users this code would error, which would be the most efficent?
The if statement I posted last time or this switch statement? Like which would run the best in a program? if that makes sense

Strange though... as I was taught that to use Switch you needed to go thorugh the whole trouble on taking the first char from the String etc... good to know that you dont now :) thanks.

depends on how you write your code.
the switch will automatically detect what case you have, but if you have your if's as you originally had them, he'll run every if ( expression ) even after the right one was found.

next to that, with proper indentation, imho using a switch statement is easier to read and maintain than if-else (when you're using nested-ifs and else-if statements especially)

Ah ok, that makes sense. Thank you very much for all your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.