Blackjack – Java Programming Reviewed by Momizat on . So after I went through a few more tutorials, I decided to create another app. I've integrated it to my original app, so in the welcome screen, it will ask if I So after I went through a few more tutorials, I decided to create another app. I've integrated it to my original app, so in the welcome screen, it will ask if I Rating:
You Are Here: Home » Applications » Java Programming » Blackjack – Java Programming

Blackjack – Java Programming

So after I went through a few more tutorials, I decided to create another app. I’ve integrated it to my original app, so in the welcome screen, it will ask if I want to use the Calculator or play Blackjack.

My first attempt at building a Blackjack game via Java programming. It works and is quite simple at the moment. The more I update it, the more complex it will get!

Here is the source code for the game. It’s very simple and probably very messy! But hey… I’m still learning 🙂

package Blackjacks;

import java.util.Random;
import java.util.Scanner;

public class Blackjack {

	public static void main(String[] args) {

		//Scanner//
		Scanner scan = new Scanner(System.in);

		//Game Background Object//
		BJBackground b = new BJBackground();

		//Game Randomizer Object//
		Random r = new Random();

		//Dealing Cards and Generating Computers Cards//
		System.out.println("Welcome to Blackjack.");
		System.out.println("Would you like to play Blackjack? Yes or No");
		String input = scan.next();
		if (input.equalsIgnoreCase("yes")) {
            b.play(true);
		}
		else return;	// Assuming they type 'no' or something else, we can just quit the program here.

		//Create Random Card Total for Computer//
		int computer = 14 + r.nextInt(9);
        //Stores Card Total for Computer//
	   	b.setcomputer(computer); 

		//Creates Random Number for First Card//
		int fcard = 1 + r.nextInt(11);
		//Stores Card Number on First Card int//
        b.setfcard(fcard);
        //Creates Random Number for Second Card//
        int scard = 1 + r.nextInt(10);
        //Stores Card Number for Second Card int//
        b.setscard(scard);
        //Updates and Stores Player Score//
        int player = scard + fcard;
        b.setplayer(player);
		System.out.println("Your first card was " + b.getfcard() + " and your second card was " + b.getscard());

		//While Loop//
		while (b.play()) {
               System.out.println("Your total is " + b.getplayer());
               if (b.getplayer() == 21) {
            	   System.out.println("You have 21! You hit Blackjack!");
               }
               else System.out.println("Would you like another card? Hit or Stand");
               input = scan.next();

	        	   while (input.equalsIgnoreCase("Hit")) {
	            		//Creates Random Number for New Card//
	           		   	int ncard = 1 + r.nextInt(10);

	           		   	b.setncard(ncard);
	           		   	b.setplayer(ncard);

	           		   	System.out.println("Your new card is " + b.getncard());
	           		   	System.out.println("Your new total is " + b.getplayer());

	           		   	b.resetncard();

	           		   	if (b.getplayer() <= 21); {
		           		System.out.println("Would you like another card? Hit or Stand");
		           		input = scan.next();
		           		}
	        	   }

	        	   if (input.equalsIgnoreCase("Stand")) {
	        		   if ( b.getplayer() > 21 ) {
				       System.out.println("You had " + b.getplayer() + " and the computer had " + b.getcomputer() + ". The computer beat you..");
				       return;
				       }
				       if ( b.getplayer() > b.getcomputer() ) {
				       System.out.println("You had " + b.getplayer() + " and the computer had " + b.getcomputer() + ". You win!");
				       return;
				       }
				       if ( b.getplayer() < b.getcomputer() ) {
				       System.out.println("You had " + b.getplayer() + " and the computer had " + b.getcomputer() + ". You lose!");
				       return;
				       }
				       if ( b.getplayer() == b.getcomputer() ) {
				       System.out.println("You had " + b.getplayer() + " and the computer had " + b.getcomputer() + ". You drew!");
				       return;
	        	   }
        	   }
		}
	}
}

package Blackjacks;

public class BJBackground {

	//First Card//
	private int fcard = 0;

	//Second Card//
	private int scard = 0;

	//
	private int ncard = 0;

	//Player Card Total//
	private int player = (fcard + scard + ncard);

	//Computer Cards//
	private int computer = 0;

	//Boolean to Play//
    private boolean play = true;

	public void play(boolean play) {
	   this.play = play;
	}
	public boolean play() {
	   return play;
	}
	//Boolean for New card//
	private boolean newcard = true;

	public boolean newcard() {
		return newcard;
	}
	//Sets First Card's new amount//
	public void setfcard(int amount) {
	        this.fcard += amount;
	}
	//Allows Blackjack Class to know what the First Cards number is when called//
	public int getfcard() {
	        return fcard;
	}
	//Sets Second Card's new amount//
	public void setscard(int amount) {
	        this.scard += amount;
	}
	//Allows Blackjack Class to know what the Second Cards number is when called//
	public int getscard() {
	        return scard;
	}
	//Sets new Card's amount//
	public void setncard(int amount) {
			this.ncard +=amount;
	}
	public int getncard() {
			return ncard;
	}
	public void resetncard() {
        ncard = 0;
	}
	public void setplayer(int amount) {
        this.player += amount;
	}
	public int getplayer() {
			return player;
	}
	//Sets Computer Score//
	public void setcomputer(int amount) {
        this.computer += amount;
	}
	//Allows Blackjack Class to know what the Computer score is//
    public int getcomputer() {
        return computer;
    }

}

If anyone has any tips on how I can improve this, please do let me know!

I’m trying to figure out a way to allow you to take more than just one card at the start… Hopefully I’ll figure it out soon!

Clip to Evernote

About The Author

Number of Entries : 73
  • Elizar Denz

    Great and fantastic jobs!!! And thanks for this source code…

Scroll to top