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!
My First Java App – Java Programming
Yeah so like, I know this isn't really a big deal to most people but I'm starting college in a couple of weeks and wanted to get a head start. So I decided to get a few online tutorials on Java programming and found something perfect.
It's a set of video tutorials done by a guy named Bucky Roberts on YouTube. The video tutorials are really easy to follow and give you a basic idea of how Java programming works - especially for us newbies. You should check him out! Here is a link to his YouTube channel:
http://www.youtube.com/user/thenewboston
Anyway, after an hour or so of watching his videos and following along, I have managed to somewhat make the backbone of a calculator and blackjack game. No interface or nothing yet, but that'll come later as I continue watching his videos.
Here is the source code for any of you who care:
import java.util.Scanner;
public class Welcome {
public static void main(String args[]){
//User Introduction//
Scanner input = new Scanner(System.in);
Calculator calcObject = new Calculator();
System.out.println("Hi, what is your name?");
String name = input.nextLine();
Scanner input2 = new Scanner(System.in);
System.out.println("Hi, " + name + ". Would you like to use our calulator? <Y/N>");
String answer = input2.nextLine();
if (answer.equals("Y"))
calcObject.calculator(name);
else if (answer.equals("y"))
calcObject.calculator(name);
else if (answer.equals("N"))
System.out.println("Aww! Okay " + name + ". Goodbye...
");
else if (answer.equals("n"))
System.out.println("Aww! Okay " + name + ". Goodbye...
");
}
}
import java.util.*;
public class Calculator {
public void calculator(String name) {
char again = 'Y';
while(again == 'Y' || again == 'y')
{
Scanner calc = new Scanner(System.in);
double fnum, snum, answer = 0;
String calcu;
System.out.println("Enter first number(s) for calculation: ");
fnum = calc.nextDouble();
System.out.println("What would you like to do? Use one of the following: +,-,* or /");
Scanner mdsa = new Scanner(System.in);
calcu = mdsa.next();
System.out.println("Enter second number for calculation: ");
snum = calc.nextDouble();
{
if (calcu.equals("+")) answer = fnum + snum;
else if (calcu.equals("-")) answer = fnum - snum;
else if (calcu.equals("*")) answer = fnum * snum;
else if (calcu.equals("/")) answer = fnum / snum;
}
System.out.println(name + ", the answer to your calculation is: " + answer);
Scanner response = new Scanner(System.in);
System.out.print("Would you like to do another calculation? <Y/N>");
again = response.nextLine().charAt(0);
}
}
}
Yes it is very basic, but then again I've only just started learning Java programming over the last couple of hours, so don't sue me.
As I continue watching his videos, I'll update this post with any additions I make to the source code. Hopefully I'll be able to make a nice looking calculator by the end of it... or at least until I give up! Hehe.
Well yeah. I'm having a lot of fun with this!
Check back later...
