View Single Post
  #7 (permalink)  
Old 07-27-2007, 08:58 PM
Sircedric88 Sircedric88 is offline
Member
 
Join Date: Jul 2007
Location: Las Vegas
Posts: 14
Sircedric88 is on a distinguished road
Send a message via AIM to Sircedric88
Here;s the code for my blackjack game.
I used a code called EasyInput to get the user input.

Code:
import java.util.*; public class Program { public static void main(String[] args) { int total = 0;// player's total int comp = 0; // computer's total int choice = 0; char playagain = 'y'; while (playagain == 'y') { System.out.print("Here are your first two cards: "); total += drawcard(); // first card System.out.print("and "); total += drawcard(); System.out.println(""); System.out.print("Computer cards: "); comp += drawcard(); //computer draws first card //System.out.print(comp); //shows first card drawn comp += computerdrawcard(comp); while (total <= 21)//loop to ask for another card - PLAYER TURN BEGINS { System.out.print("\nWould you like a hit (1) or to stay (2)"); choice = EasyInput.getInt(); EasyInput.getln(); if (choice == 1) { total += drawcard(); } else { break; } if (total > 21) { System.out.println("Busted!"); break; } } while (comp <= 18) { comp += computerdrawcard(comp); } System.out.print("Your score: "); System.out.println(total); System.out.print("Computer Score: "); System.out.print(comp + "\n"); System.out.println("Would you like to play again? (y/n)"); playagain = EasyInput.getChar(); EasyInput.getln(); if (playagain == 'y') { total = 0; comp = 0; choice = 0; } else break; } } // Player drawcard public static int drawcard() { int cards = (int)(Math.random() * 12)+1; switch (cards) { case 1: {System.out.print("King "); return 10;} case 2: {System.out.print("Queen "); return 10;} case 3: {System.out.print("Jack "); return 10;} case 4: {System.out.print("Ten "); return 10;} case 5: {System.out.print("Nine "); return 9;} case 6: {System.out.print("Eight "); return 8;} case 7: {System.out.print("Seven "); return 7;} case 8: {System.out.print("Six "); return 6;} case 9: {System.out.print("Five "); return 5;} case 10:{System.out.print("Four "); return 4;} case 11:{System.out.print("Three "); return 3;} case 12:{System.out.print("Two "); return 2;} case 13:{System.out.print("Ace "); System.out.print("Do you want 1 or 11?"); int ace = EasyInput.getInt (); if (ace == 1) {return 1;} else {return 11;} } default: {System.out.print(""); } break; } return 0; } // Computer draw card public static int computerdrawcard(int compscore) { int cards = (int)(Math.random() * 12) + 1; switch (cards) { case 1: { return 10; } case 2: { return 10; } case 3: { return 10; } case 4: { return 10; } case 5: { return 9; } case 6: { return 8; } case 7: { return 7; } case 8: { return 6; } case 9: { return 5; } case 10: { return 4; } case 11: { return 3; } case 12: { return 2; } case 13: { if (compscore + 11>21) { //if 11 will bust the computer, then it will choose 1 return 1;} else { return 11; } } default: { System.out.print(""); } break; } return 0; } }
Reply With Quote