I am building a blackjack program using threads in Java. I have three players, Andrew, Billy and Carla. I am supposed to output their score result after a round of cards. Where they're supposed to beat the dealer with a score that is greater than 15 but less than or equal to 21. I am having a problem outputting their results because I am trying to use the random class to produce different scores each round. Here's my code
package blackjack.client;
import blackjack.system.*;
public class Blackjack21 {
private static int cardTotal;
public static void main(String[]args){
CardCounter cardCounter = new CardCounter();
BlackJackThread b1 = new BlackJackThread("Andrew", cardTotal);
BlackJackThread b2 = new BlackJackThread("Billy", cardTotal);
BlackJackThread b3 = new BlackJackThread("Carla", cardTotal);
b1.start();
b2.start();
b3.start();
}
}
package blackjack.system;
public class BlackJackThread extends Thread{
private CardCounter cardCounter = new CardCounter();
private String playerName;
private int cardTotal;
public BlackJackThread(String playerName, int cardTotal){
this.playerName = playerName;
this.cardTotal = cardTotal;
}
public void run(){
cardCounter.checkForWin(playerName, cardTotal);
}
}
package blackjack.system;
import java.util.Random;
import java.util.*;
public class CardCounter {
public synchronized void checkForWin(String player, int cardTotal){
Object[] suitNumber = {2,3,4,5,6,7,8,9,"jack","king","queen"};
String suit[] = { "Clubs", "Diamonds", "Hearts", "Spades"};
List<Object>playersHand = new ArrayList<Object>();
Random suitType = new Random();
int n = suitType.nextInt(4);
Random numberOnSuit = new Random();
int m = numberOnSuit.nextInt(11);
for(int i=0; i<=3; i++){
playersHand.add(suitNumber[i]+""+suit[i]);
if((suitNumber[m] == "jack")||(suitNumber[m]=="king"||suitNumber[m]=="queen")){
cardTotal+= 10;
}else{
String wrapperObject =cardTotal+""+suitNumber[m];
Integer unboxObject = new Integer(wrapperObject);
cardTotal+=unboxObject;
}
}
if((cardTotal > 15) && (cardTotal <= 21)){
System.out.println(player+"'s hand:"+playersHand);
System.out.println(player+" "+cardTotal+" "+"beats dealer");
}else{
System.out.println(player+"'s hand:"+playersHand);
System.out.println(player+" "+cardTotal+" "+"loses");
}
}
}
Here's my output. I want to ensure that all three players have different scores but different outcomes every time I run the thread
Aucun commentaire:
Enregistrer un commentaire