Question
String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"
}
int i = (int) (Math.random() * rank.length);
int j = (int) (Math.random() * suit.length);
System.out.println(rank[i] + " of " + suit[j]);
String[] deck = new String[ranks.length * suits.length];
for (int i = 0; i < ranks.length; i++)
for (int j = 0; j < suits.length; j++)
deck[suits.length*i + j] = rank[i] + " of " + suit[j];
System.out.println(rank[i] + " of " + suit[j]);
String t = deck[i];
deck[i] = deck[j];
deck[j] = t;
int N = deck.length;
for (int i = 0; i < N; i++)
{
int r = i + (int) (Math.random() * (N-i));
String t = deck[r];
deck[r] = deck[i];
deck[i] = t;
}
import java.awt.*;
Rectangle rect1, rect2, rect3;
PImage[] images =new PImage[13];
int imageIndex = 0;
void setup() {
size(500,500);
rect1 =new Rectangle(25,25,50,50);
rect2 =new Rectangle(125,25,50,50);
rect3 =new Rectangle(225,25,50,50);
images[0] = loadImage("clubs-2-75.png");
images[1] = loadImage("clubs-3-75.png");
images[2] = loadImage("clubs-4-75.png");
images[3] = loadImage("clubs-5-75.png");
images[4] = loadImage("clubs-6-75.png");
images[5] = loadImage("clubs-7-75.png");
images[6] = loadImage("clubs-8-75.png");
images[7] = loadImage("clubs-9-75.png");
images[8] = loadImage("clubs-10-75.png");
images[9] = loadImage("clubs-j-75.png");
images[10] = loadImage("clubs-q-75.png");
images[11] = loadImage("clubs-k-75.png");
images[12] = loadImage("clubs-a-75.png");
}
void draw() {
background(255);
stroke(0);
if (rect1.contains(mouseX,mouseY)) {
fill(200);
}else{
fill(100);
}
rect(rect1.x,rect1.y,rect1.width,rect1.height);
if(rect2.contains(mouseX,mouseY)) {
fill(200);
}else{
fill(100);
}
rect(rect2.x,rect2.y,rect2.width,rect2.height);
if (rect3.contains(mouseX,mouseY)) {
fill(200);
}else{
fill(100);
}
rect(rect3.x,rect3.y,rect3.width,rect3.height);
image(images[imageIndex],0,100);
}
void mousePressed() {
imageIndex =int(random(images.length));
}
btw processing is a form of java so if you dont know processing you could use tht
http://processing.org/
http://www.learningprocessing.com/
Explanation / Answer
The solution contains five components: BlackJack - GUI application Common - Common classes, enums and interfaces used by the framework Game - Component that takes care of blackjack game logic Player - Very simple computer player HumanPlayer - Simple interface for human players Common Component The Common component contains: Deck class - managing card deck Hand class - managing player's hand IPlayer interface - interface for blackjack players PlayerOptions enum - possible options on player's turn PlayerInGameStatus enum - current player status (playing or standing) PlayerEndGameStatus enum - player status at the end of the game (winner, loser...) IPlayer's methods are called by the Game component to notify the player about events. Game Component The Game component contains: PlayerStatus class - wrapper class for player; data such as current player's hand and current status are stored in this class to prevent cheating :) Game class - blackjack game logic and control PlayerDirectory class - dynamically loads the player's components Simplified algorithm of the game: Start a new game Shuffle the deck Begin a new round Deal two cards to all players Ask all players with the status "playing" if they want another card: if someone does, give them another card; if someone doesn't, change his status to "standing" Repeat step 5 while there are players with "playing" status Compare players' hands to declare winner and losers according to the rules of the game, updating player statistics and end game status Shuffle, deal cards and return them to the end of the deck If this is not the last round, jump to step 3. The described algorithm is implemented in the Round, BeginRound, PlayerTurn, GiveCardToPlayer and EndRound methods of the Game class. Game can be observed with events (members of the Game class): OnRoundEnd OnGameEnd OnPlayerAdded OnPlayerRemoved OnPlayerListCleared OnDealerChange Game runs in its own thread and has methods for synchronization with GUI animations, which are played at the end of a round. At round end, the game thread calls WaitForAnimation and waits until the GUI thread signals that the animation is finished by calling AnimationFinished. Methods StartGame, StopGame, PauseGame and ContinueGame are used to control the game. Loading and Instantiation of Players The PlayerDirectory class has the tasks of loading players' components and instantiation of players. In the class constructor, all assemblies from the .Players folder are checked and loaded if containing a player's class. The name of a player class must be MyPlayer and must be in the Player namespace. Other assemblies can be added by calling the LoadPlayer method and specifying the path to assembly. To create an instance of a player, just call CreateInstanceOfPlayer, specifying the assembly name and constructor parameters as needed. playerDirectory.CreateInstanceOfPlayer("PlayerWithNameAndAge", new object[]{ "Someone", 30 } ); Simple Player A simple player with simple logic: "If I have blackjack, I'll stand" "If I have 21, I'll stand" "If have more than or equal to 17, I'll stand" "If I have less than 17, I'll take another" ...can be implemented in a few lines of code. using Common; namespace Player { public class MyPlayer : IPlayer { private Hand _myHand = new Hand(); private string _name; public string Name { get { return _name; } } public void NewRound(bool newGame, bool isDealer, int dealersFirstCard, int numberOfDecks) { _myHand.Restart(); } public PlayerOptions Turn(int left) { if (_myHand.IsBlackJack || _myHand.Values.Contains(21)) return PlayerOptions.Stand; return _myHand.Values[0]