Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by nick ( 16 years ago )
ode is my own work, it was written without consulting a tutor or code written by other students. Nicholas Hokanson*/
import java.util.Scanner;
public class blakjack {
public static String cardNumToString(int x){
String a = "";
if (x > 2 && x < 11){
String b = "" + x;
return b;
}
else {
switch (x){
case 1: a = "A"; break;
case 11: a = "J";break;
case 12: a = "Q";break;
case 13: a = "K";break;
}
return a;
}
}
public static int randomCard(){
int x = 1 + (int)(Math.random()*13);
return x;
}
public static boolean search(int [] a){
int i = 0;
while (i < a.length){
if (a[i] ==1)
return true;
i++;
}
return false;
}
public static int totalHand(int [] cards) {
int a = 0;
int sum = 0;
while (a < cards.length){
if (cards [a] > 10){
cards [a] = 10;
}
sum = cards[a] +sum;
a++;
}
return sum;
}
public static void printHand(int [] cards){
for (int x = 0; x < cards.length; x++){
System.out.print(cardNumToString(cards[x]) + " ");
}
int sum = totalHand(cards);
if (search(cards) && sum < 11){
sum = sum + 10;
System.out.println(" is a soft " + sum);
}
else if (sum > 21){
System.out.println("is a BUST at " + sum);
}
else {
System.out.println(" is a " + sum);
}
}
public static int [] addCard(int [] cards){
int [] arrayNew = new int [cards.length + 1];
int i = 0;
while (i < arrayNew.length - 1){
arrayNew[i] = cards[i];
i++;
}
arrayNew[i] = randomCard();
return arrayNew;
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int [] dealer = new int[11];
for (int i = 0; i < 2; i++){
dealer[i] = randomCard();
}
int [] player = new int[11];
for (int i = 0; i < 2; i++){
player[i] = randomCard();
}
int stayhit = 1;
System.out.println("Dealer: ? " + cardNumToString(dealer[1]));
System.out.println();
System.out.print("Player: " );
printHand(player);
System.out.println();
while (totalHand(player) < 21){
System.out.print("Enter 0 to stay, 1 to hit: ");
stayhit = input.nextInt();
if (stayhit == 0){
break;
}
if (stayhit == 1){
System.out.print("Player:" );
player = addCard(player);
totalHand(player);
printHand(player);
continue;
}
}
if (totalHand(player) < 21){
if (totalHand(dealer)>17){
printHand(dealer);
}
while ( totalHand(dealer) < 17){
if (totalHand(dealer) < 17 ){
dealer = addCard(dealer);
System.out.print("Dealer: " );
totalHand(dealer);
printHand(dealer);
System.out.println();
}
}
if (totalHand(dealer) > 21){
System.out.println("Dealer Busted! You Win!");
}
else if (totalHand(player) >= totalHand(dealer)){
System.out.println("You Win!");
}
else{
System.out.print("Dealer wins.");
}
}
else{
System.out.println("You lose!");
}
}
}
Revise this Paste