Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>blackjack_java</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
1 change: 1 addition & 0 deletions BlackJack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/Program.class
7 changes: 4 additions & 3 deletions BlackJack/Program.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package BlackJack;

import BlackJack.model.Game;
import BlackJack.model.rules.BasicRulesFactory;
import BlackJack.view.*;
import BlackJack.controller.*;

Expand All @@ -10,10 +11,10 @@ public class Program
public static void main(String[] a_args)
{

Game g = new Game();
Game g = new Game(new BasicRulesFactory());
IView v = new SimpleView(); //new SwedishView();
PlayGame ctrl = new PlayGame();
PlayGame ctrl = new PlayGame(g,v);

while (ctrl.Play(g, v));
while (ctrl.Play());
}
}
5 changes: 5 additions & 0 deletions BlackJack/controller/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/PlayGame.class
/PlayGame$Days.class
/PlayGame$Choice.class
/Choice.class
/InputChoices.class
8 changes: 8 additions & 0 deletions BlackJack/controller/InputChoices.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package BlackJack.controller;

public enum InputChoices {
Play,
Hit,
Stand,
Quit
}
79 changes: 49 additions & 30 deletions BlackJack/controller/PlayGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,54 @@

import BlackJack.view.IView;
import BlackJack.model.Game;
import BlackJack.model.IObserver;

public class PlayGame {

public boolean Play(Game a_game, IView a_view) {
a_view.DisplayWelcomeMessage();

a_view.DisplayDealerHand(a_game.GetDealerHand(), a_game.GetDealerScore());
a_view.DisplayPlayerHand(a_game.GetPlayerHand(), a_game.GetPlayerScore());

if (a_game.IsGameOver())
{
a_view.DisplayGameOver(a_game.IsDealerWinner());
}

int input = a_view.GetInput();

if (input == 'p')
{
a_game.NewGame();
}
else if (input == 'h')
{
a_game.Hit();
}
else if (input == 's')
{
a_game.Stand();
}

return input != 'q';
}
public class PlayGame extends IObserver{

private Game a_game;
private IView a_view;

public PlayGame(Game a_game, IView a_view) {
this.a_game = a_game;
this.a_view = a_view;
a_game.AddObservableValue(this); // make the Game an Observer
}

public boolean Play() {
a_view.DisplayWelcomeMessage();

if (a_game.IsGameOver()) {
a_view.DisplayGameOver(a_game.IsDealerWinner());
System.exit(0);
}

InputChoices input = a_view.GetInput();

switch (input) {
case Play:
a_game.NewGame();
break;
case Hit:
a_game.Hit();
break;
case Stand:
a_game.Stand();
break;
case Quit: return false;
}
return true;

}

public void update() { // Update method gets called whenever a new card is dealt
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

a_view.DisplayDealerHand(a_game.GetDealerHand(), a_game.GetDealerScore());
a_view.DisplayPlayerHand(a_game.GetPlayerHand(), a_game.GetPlayerScore());

}
}
7 changes: 7 additions & 0 deletions BlackJack/model/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/Card$Color.class
/Card$Value.class
/Card.class
/Dealer.class
/Deck.class
/Game.class
/Player.class
41 changes: 25 additions & 16 deletions BlackJack/model/Dealer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ public class Dealer extends Player {
private Deck m_deck;
private INewGameStrategy m_newGameRule;
private IHitStrategy m_hitRule;
private IWinStrategy m_winStrat;

public Dealer(RulesFactory a_rulesFactory) {
public Dealer(IRulesFactory a_rulesFactory) {

m_newGameRule = a_rulesFactory.GetNewGameRule();
m_hitRule = a_rulesFactory.GetHitRule();
m_newGameRule = a_rulesFactory.GetNewGameStrategy();
m_hitRule = a_rulesFactory.GetHitStrategy();
m_winStrat = a_rulesFactory.GetWinStrategy();

/*for(Card c : m_deck.GetCards()) {
c.Show(true);
Expand All @@ -25,30 +27,21 @@ public boolean NewGame(Player a_player) {
m_deck = new Deck();
ClearHand();
a_player.ClearHand();
return m_newGameRule.NewGame(m_deck, this, a_player);
return m_newGameRule.NewGame(this, a_player);
}
return false;
}

public boolean Hit(Player a_player) {
if (m_deck != null && a_player.CalcScore() < g_maxScore && !IsGameOver()) {
Card c;
c = m_deck.GetCard();
c.Show(true);
a_player.DealCard(c);

DealCard(a_player, true);
return true;
}
return false;
}

public boolean IsDealerWinner(Player a_player) {
if (a_player.CalcScore() > g_maxScore) {
return true;
} else if (CalcScore() > g_maxScore) {
return false;
}
return CalcScore() >= a_player.CalcScore();
return m_winStrat.IsDealerWinner(a_player.CalcScore(), this.CalcScore(), g_maxScore);
}

public boolean IsGameOver() {
Expand All @@ -58,4 +51,20 @@ public boolean IsGameOver() {
return false;
}

}
public boolean Stand() { //updated from sequence diagram
if (m_deck != null) {
ShowHand();
}
while(m_hitRule.DoHit(this)) {
Hit(this);
}
return false;
}

public void DealCard(Player a_player, boolean bool) {
Card c = m_deck.GetCard();
c.Show(bool);
a_player.DealCard(c);
NotifyObservers();
}
}
3 changes: 1 addition & 2 deletions BlackJack/model/Deck.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ private void Shuffle()
m_cards.remove(index);
AddCard(c);
}
}

}
}
16 changes: 10 additions & 6 deletions BlackJack/model/Game.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package BlackJack.model;

import BlackJack.model.rules.*;

public class Game {

private Dealer m_dealer;
private Player m_player;

public Game()
public Game(IRulesFactory a_rulesFactory)
{
m_dealer = new Dealer(new BlackJack.model.rules.RulesFactory());
m_dealer = new Dealer(a_rulesFactory);
m_player = new Player();
}

Expand All @@ -34,8 +36,7 @@ public boolean Hit()

public boolean Stand()
{
// TODO: Implement this according to Game_Stand.sequencediagram
return true;
return m_dealer.Stand(); //updated from sequence diagram
}

public Iterable<Card> GetDealerHand()
Expand All @@ -58,5 +59,8 @@ public int GetPlayerScore()
return m_player.CalcScore();
}


}
public void AddObservableValue(IObserver m_observer) {
m_dealer.AddObserver(m_observer);
m_player.AddObserver(m_observer);
}
}
Binary file added BlackJack/model/IObserver.class
Binary file not shown.
7 changes: 7 additions & 0 deletions BlackJack/model/IObserver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package BlackJack.model;

public abstract class IObserver {

public abstract void update();

}
29 changes: 27 additions & 2 deletions BlackJack/model/Player.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package BlackJack.model;

import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;

public class Player {

private List<Card> m_hand;
private ArrayList<IObserver> m_observers;
protected final int g_maxScore = 21;

public Player()
{

m_hand = new LinkedList<Card>();
System.out.println("Hello List World");
m_observers = new ArrayList<IObserver>();
}

public void DealCard(Card a_addToHand)
Expand Down Expand Up @@ -71,4 +72,28 @@ public int CalcScore()

return score;
}

public int Aces() {
int aces = 0;
for (Card card : this.GetHand()) {
if (card.GetValue().equals("Ace")) {
aces = aces + 1;
}
}
return aces;
}

public void AddObserver(IObserver o) {
m_observers.add(o);
}

public void RemoveObserver(IObserver o) {
m_observers.remove(o);
}

public void NotifyObservers() {
for(IObserver m_observer : m_observers) {
m_observer.update();
}
}
}
14 changes: 14 additions & 0 deletions BlackJack/model/rules/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/AmericanNewGameStrategy.class
/BasicHitStrategy.class
/IHitStrategy.class
/INewGameStrategy.class
/InternationalNewGameStrategy.class
/RulesFactory.class
/SoftHitStrategy.class
/SoftRulesFactory.class
/SoftAmericanRules.class
/IWinStrategy.class
/PlayerWinStrategy.class
/BasicRulesFactory.class
/IRulesFactory.class
/DealerWinStrategy.class
Loading