среда, 13 марта 2013 г.

Nasty Old Dog: Blackjack Almost a Real Game

I need to use the Scanner class to get user input there is no System.in.getLine


I defined uinp to be a scanner for System.in


Ladbrokes bonus poker night fundraiser flyer the

I replaced the System.in.getLine's with uinp.nextLine()


The String userinp doesn't need a constructor just " "


There is no toInt in the Integer class use getInteger instead


import java.util.Scanner;


public class CardDeck {


int deck[] = new int[52];


String faceVal = "A23456789TJQK";


String suit = "HDSC";


public CardDeck() {


for (int i = 0; i < 52; i++) {


deck[i] = i;


}


}


void shuffle() {


int rindex;


int swap;


for (int i = 0; i < 52; i++) {


// each time through the loop there are less numbers to randomize


// 52 - i to be exact. But then everything from 0 to i-1 has already


// been selected at random so add i to the random number to get the


// appropriate index


rindex = (int) ((Math.random() * ((double) (52 - i))) + i);


swap = deck[i];


deck[i] = deck[rindex];


deck[rindex] = swap;


}


}


public String getCardText(int crd) {


int card_val = crd % 13;


int card_suit = crd % 4;


return this.faceVal.substring(card_val, card_val + 1)


+ this.suit.substring(card_suit, card_suit + 1);


//return this.faceVal.substring(card_val, card_val+1).concat(


//this.suit.substring(card_suit, card_suit+1));


}


void display() {


int card_val;


int card_suit;


for (int i = 0; i < 52; i++) {


//card_val = deck[i]%13;


//card_suit = deck[i]%4;


System.out.print(this.getCardText(deck[i]) + " ");


}


System.out.println();


}


int playerHand[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};


int dealerHand[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};


int dcards = 0; // the number of cards in the dealer hand so far


int pcards = 0; // the number of cards in the player hand so far


int deckidx = 0; // the location of the next card to be dealt


public int deal() {


if (this.deckidx < 52) {


return this.deck[this.deckidx++]; // return the top card and increment to the next card


} else {


return this.deck[this.deckidx - 1];


}


}


public void dealPlayer() {


this.playerHand[pcards] = deal();


this.pcards++;


}


public void dealDealer() {


this.dealerHand[dcards] = deal();


this.dcards++;


}


public void displayHands() {


System.out.println("Player Hand ");


for (int i = 0; i < this.pcards; i++) {


System.out.print(this.getCardText(this.playerHand[i]) + " ");


}


System.out.println();


System.out.println("Dealer Hand: ");


for (int j = 0; j < this.dcards; j++) {


System.out.print(this.getCardText(this.dealerHand[j]) + " ");


}


System.out.println();


System.out.println();


}


public static void main(String[] args) {


CardDeck card = new CardDeck();


card.display();


card.shuffle();


card.display();


card.dealPlayer();


card.dealDealer();


card.dealPlayer();


card.dealDealer();


card.displayHands();


// Start game intialize bankroll to 500


// Start game loop on bet means quit


// CardDeck card = new CardDeck();


String user_inp = " ";


int bankroll = 1000;


int bet = 0; // if the user bets 0 quit the game loop


// Need a Scanner for user input


Scanner uinp = new Scanner(System.in);


// game loop just loop forever


while (true) {


// Tell the user the amount of the bank roll


System.out.println("Bankroll = " + bankroll);


// get bet


System.out.print("Enter the amount you want to bet: ");


user_inp = uinp.nextLine();


bet = Integer.getInteger(user_inp);


// bet == 0 is the signal to quit loop and end program


if (bet == 0) {


break;


}


card.shuffle();


card.dealPlayer();


card.dealDealer();


card.dealPlayer();


card.dealDealer();


card.displayHands();


// Need a loop to handle HIT or STAND commands


while (true) {


System.out.print("Player Hand hit or stand? ");


user_inp = uinp.nextLine();


if (user_inp.equals("HIT")) {


card.dealPlayer();


card.displayHands();


} else if (user_inp.equals("STAND")) {


break;


} else {


// If we end up here it's because of a typo or wiseguy


// print error response and go back to looping


System.out.println("I don't understand: " + user_inp);


}


}


// Manually control dealer hand the same way


// We will swap this out for code that will run the dealer rules


// automatically


while (true) {


System.out.print("Dealer Hand hit or stand? ");


user_inp = uinp.nextLine();


if (user_inp.equals("HIT")) {


card.dealDealer();


card.displayHands();


} else if (user_inp.equals("STAND")) {


break;


} else {


// If we end up here it's because of a typo or wiseguy


// print error response and go back to looping


System.out.println("I don't understand: " + user_inp);


}


}


// Code to score the game and settle the bet will go here


}


}


}

Ladbrokes bonus poker night fundraiser flyer the

Комментариев нет:

Отправить комментарий