diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..3f3893a --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..b423c54 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + blackjack_java + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/BlackJack/.gitignore b/BlackJack/.gitignore new file mode 100644 index 0000000..b2f49d0 --- /dev/null +++ b/BlackJack/.gitignore @@ -0,0 +1 @@ +/Program.class diff --git a/BlackJack/Program.java b/BlackJack/Program.java index 8ae21e5..dee3e5e 100644 --- a/BlackJack/Program.java +++ b/BlackJack/Program.java @@ -1,6 +1,7 @@ package BlackJack; import BlackJack.model.Game; +import BlackJack.model.rules.BasicRulesFactory; import BlackJack.view.*; import BlackJack.controller.*; @@ -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()); } } \ No newline at end of file diff --git a/BlackJack/controller/.gitignore b/BlackJack/controller/.gitignore new file mode 100644 index 0000000..d4656c8 --- /dev/null +++ b/BlackJack/controller/.gitignore @@ -0,0 +1,5 @@ +/PlayGame.class +/PlayGame$Days.class +/PlayGame$Choice.class +/Choice.class +/InputChoices.class diff --git a/BlackJack/controller/InputChoices.java b/BlackJack/controller/InputChoices.java new file mode 100644 index 0000000..6814afd --- /dev/null +++ b/BlackJack/controller/InputChoices.java @@ -0,0 +1,8 @@ +package BlackJack.controller; + +public enum InputChoices { + Play, + Hit, + Stand, + Quit +} diff --git a/BlackJack/controller/PlayGame.java b/BlackJack/controller/PlayGame.java index 5d1ebe0..c23a10e 100644 --- a/BlackJack/controller/PlayGame.java +++ b/BlackJack/controller/PlayGame.java @@ -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()); + + } } \ No newline at end of file diff --git a/BlackJack/model/.gitignore b/BlackJack/model/.gitignore new file mode 100644 index 0000000..e96f67c --- /dev/null +++ b/BlackJack/model/.gitignore @@ -0,0 +1,7 @@ +/Card$Color.class +/Card$Value.class +/Card.class +/Dealer.class +/Deck.class +/Game.class +/Player.class diff --git a/BlackJack/model/Dealer.java b/BlackJack/model/Dealer.java index 2d45b16..142b53f 100644 --- a/BlackJack/model/Dealer.java +++ b/BlackJack/model/Dealer.java @@ -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); @@ -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() { @@ -58,4 +51,20 @@ public boolean IsGameOver() { return false; } -} \ No newline at end of file + 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(); + } +} diff --git a/BlackJack/model/Deck.java b/BlackJack/model/Deck.java index 62a52a6..858e817 100644 --- a/BlackJack/model/Deck.java +++ b/BlackJack/model/Deck.java @@ -46,6 +46,5 @@ private void Shuffle() m_cards.remove(index); AddCard(c); } - } - + } } \ No newline at end of file diff --git a/BlackJack/model/Game.java b/BlackJack/model/Game.java index 3ba16ac..824008b 100644 --- a/BlackJack/model/Game.java +++ b/BlackJack/model/Game.java @@ -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(); } @@ -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 GetDealerHand() @@ -58,5 +59,8 @@ public int GetPlayerScore() return m_player.CalcScore(); } - -} \ No newline at end of file + public void AddObservableValue(IObserver m_observer) { + m_dealer.AddObserver(m_observer); + m_player.AddObserver(m_observer); + } +} diff --git a/BlackJack/model/IObserver.class b/BlackJack/model/IObserver.class new file mode 100644 index 0000000..0c77374 Binary files /dev/null and b/BlackJack/model/IObserver.class differ diff --git a/BlackJack/model/IObserver.java b/BlackJack/model/IObserver.java new file mode 100644 index 0000000..3f81b48 --- /dev/null +++ b/BlackJack/model/IObserver.java @@ -0,0 +1,7 @@ +package BlackJack.model; + +public abstract class IObserver { + + public abstract void update(); + +} diff --git a/BlackJack/model/Player.java b/BlackJack/model/Player.java index 5e3a6f0..fa0f13f 100644 --- a/BlackJack/model/Player.java +++ b/BlackJack/model/Player.java @@ -1,18 +1,19 @@ package BlackJack.model; import java.util.List; +import java.util.ArrayList; import java.util.LinkedList; public class Player { private List m_hand; + private ArrayList m_observers; protected final int g_maxScore = 21; public Player() { - m_hand = new LinkedList(); - System.out.println("Hello List World"); + m_observers = new ArrayList(); } public void DealCard(Card a_addToHand) @@ -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(); + } + } } \ No newline at end of file diff --git a/BlackJack/model/rules/.gitignore b/BlackJack/model/rules/.gitignore new file mode 100644 index 0000000..f3dd349 --- /dev/null +++ b/BlackJack/model/rules/.gitignore @@ -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 diff --git a/BlackJack/model/rules/AmericanNewGameStrategy.java b/BlackJack/model/rules/AmericanNewGameStrategy.java index 17e77e4..c552ac9 100644 --- a/BlackJack/model/rules/AmericanNewGameStrategy.java +++ b/BlackJack/model/rules/AmericanNewGameStrategy.java @@ -1,31 +1,23 @@ package BlackJack.model.rules; -import BlackJack.model.Deck; import BlackJack.model.Dealer; import BlackJack.model.Player; -import BlackJack.model.Card; class AmericanNewGameStrategy implements INewGameStrategy { - public boolean NewGame(Deck a_deck, Dealer a_dealer, Player a_player) { - Card c; - - c = a_deck.GetCard(); - c.Show(true); - a_player.DealCard(c); - - c = a_deck.GetCard(); - c.Show(true); - a_dealer.DealCard(c); - - c = a_deck.GetCard(); - c.Show(true); - a_player.DealCard(c); - - c = a_deck.GetCard(); - c.Show(false); - a_dealer.DealCard(c); - - return true; - } + public boolean NewGame( Dealer a_dealer, Player a_player) { + int i = 0; + + while (i < 3) { + if (i == 1) + a_dealer.DealCard(a_dealer, true); + else { + a_dealer.DealCard(a_player, true); + } + i++; + } + a_dealer.DealCard(a_dealer, false); + + return true; + } } \ No newline at end of file diff --git a/BlackJack/model/rules/BasicRulesFactory.java b/BlackJack/model/rules/BasicRulesFactory.java new file mode 100644 index 0000000..3d7cf9d --- /dev/null +++ b/BlackJack/model/rules/BasicRulesFactory.java @@ -0,0 +1,20 @@ +package BlackJack.model.rules; + +public class BasicRulesFactory implements IRulesFactory { + + + public IHitStrategy GetHitStrategy() { + return new BasicHitStrategy(); + } + + + public INewGameStrategy GetNewGameStrategy() { + return new AmericanNewGameStrategy(); + } + + + public IWinStrategy GetWinStrategy() { + return new PlayerWinStrategy(); + } + +} diff --git a/BlackJack/model/rules/DealerWinStrategy.java b/BlackJack/model/rules/DealerWinStrategy.java new file mode 100644 index 0000000..023af1b --- /dev/null +++ b/BlackJack/model/rules/DealerWinStrategy.java @@ -0,0 +1,16 @@ +package BlackJack.model.rules; + +public class DealerWinStrategy implements IWinStrategy { + + @Override + public boolean IsDealerWinner(int a_playerScore, int a_dealerScore, int g_maxScore) { + if (a_playerScore > g_maxScore) { + return true; + } + else if(a_dealerScore > g_maxScore) { + return false; + } + return a_dealerScore >= a_playerScore; + } + +} diff --git a/BlackJack/model/rules/INewGameStrategy.java b/BlackJack/model/rules/INewGameStrategy.java index 8cc0903..671aa87 100644 --- a/BlackJack/model/rules/INewGameStrategy.java +++ b/BlackJack/model/rules/INewGameStrategy.java @@ -1,9 +1,8 @@ package BlackJack.model.rules; -import BlackJack.model.Deck; import BlackJack.model.Dealer; import BlackJack.model.Player; public interface INewGameStrategy { - boolean NewGame(Deck a_deck, Dealer a_dealer, Player a_player); + boolean NewGame(Dealer a_dealer, Player a_player); } \ No newline at end of file diff --git a/BlackJack/model/rules/IRulesFactory.java b/BlackJack/model/rules/IRulesFactory.java new file mode 100644 index 0000000..741f221 --- /dev/null +++ b/BlackJack/model/rules/IRulesFactory.java @@ -0,0 +1,11 @@ +package BlackJack.model.rules; + +public interface IRulesFactory { + + public IHitStrategy GetHitStrategy(); + + public INewGameStrategy GetNewGameStrategy(); + + public IWinStrategy GetWinStrategy(); + +} diff --git a/BlackJack/model/rules/IWinStrategy.java b/BlackJack/model/rules/IWinStrategy.java new file mode 100644 index 0000000..94959d8 --- /dev/null +++ b/BlackJack/model/rules/IWinStrategy.java @@ -0,0 +1,8 @@ +package BlackJack.model.rules; + +public interface IWinStrategy { + + boolean IsDealerWinner(int a_playerScore, int a_dealerScore, int g_maxScore); + + +} diff --git a/BlackJack/model/rules/InternationalNewGameStrategy.java b/BlackJack/model/rules/InternationalNewGameStrategy.java index 3ff4a5d..cbb98f9 100644 --- a/BlackJack/model/rules/InternationalNewGameStrategy.java +++ b/BlackJack/model/rules/InternationalNewGameStrategy.java @@ -1,27 +1,23 @@ package BlackJack.model.rules; -import BlackJack.model.Deck; import BlackJack.model.Dealer; import BlackJack.model.Player; -import BlackJack.model.Card; class InternationalNewGameStrategy implements INewGameStrategy { - public boolean NewGame(Deck a_deck, Dealer a_dealer, Player a_player) { - Card c; - - c = a_deck.GetCard(); - c.Show(true); - a_player.DealCard(c); - - c = a_deck.GetCard(); - c.Show(true); - a_dealer.DealCard(c); - - c = a_deck.GetCard(); - c.Show(true); - a_player.DealCard(c); - - return true; - } + public boolean NewGame(Dealer a_dealer, Player a_player) { + int i = 0; + + while (i < 3) { + if (i == 1) + a_dealer.DealCard(a_dealer, true); + else { + a_dealer.DealCard(a_player, true); + } + i++; + } + a_dealer.DealCard(a_dealer, false); + + return true; + } } \ No newline at end of file diff --git a/BlackJack/model/rules/PlayerWinStrategy.java b/BlackJack/model/rules/PlayerWinStrategy.java new file mode 100644 index 0000000..fc52945 --- /dev/null +++ b/BlackJack/model/rules/PlayerWinStrategy.java @@ -0,0 +1,16 @@ +package BlackJack.model.rules; + +public class PlayerWinStrategy implements IWinStrategy { + + @Override + public boolean IsDealerWinner(int a_playerScore, int a_dealerScore, int g_maxScore) { + if (a_playerScore > g_maxScore) { + return true; + } + else if(a_dealerScore > g_maxScore) { + return false; + } + return a_dealerScore > a_playerScore; + } + +} diff --git a/BlackJack/model/rules/RulesFactory.java b/BlackJack/model/rules/RulesFactory.java deleted file mode 100644 index 79210b5..0000000 --- a/BlackJack/model/rules/RulesFactory.java +++ /dev/null @@ -1,12 +0,0 @@ -package BlackJack.model.rules; - -public class RulesFactory { - - public IHitStrategy GetHitRule() { - return new BasicHitStrategy(); - } - - public INewGameStrategy GetNewGameRule() { - return new AmericanNewGameStrategy(); - } -} \ No newline at end of file diff --git a/BlackJack/model/rules/SoftAmericanRules.java b/BlackJack/model/rules/SoftAmericanRules.java new file mode 100644 index 0000000..680d2a9 --- /dev/null +++ b/BlackJack/model/rules/SoftAmericanRules.java @@ -0,0 +1,17 @@ +package BlackJack.model.rules; + +public class SoftAmericanRules implements IRulesFactory{ + + public INewGameStrategy GetNewGameStrategy() { + return new InternationalNewGameStrategy(); + } + + public IHitStrategy GetHitStrategy() { + return new SoftHitStrategy(); + } + + public IWinStrategy GetWinStrategy() { + return new DealerWinStrategy(); + } + +} \ No newline at end of file diff --git a/BlackJack/model/rules/SoftHitStrategy.java b/BlackJack/model/rules/SoftHitStrategy.java new file mode 100644 index 0000000..3df1130 --- /dev/null +++ b/BlackJack/model/rules/SoftHitStrategy.java @@ -0,0 +1,29 @@ +package BlackJack.model.rules; + +import BlackJack.model.Player; + +public class SoftHitStrategy implements IHitStrategy { + + private final int g_scoreLimit = 17; + + public boolean DoHit(Player a_dealer) { + + int count = a_dealer.CalcScore(); + + if(count < g_scoreLimit) { + return true; + } + + else if(count == 17) { + int numberOfAces = a_dealer.Aces(); + for (int i = 1; i <= numberOfAces; i++) { + if (count - (i * 10) < g_scoreLimit) { + return true; + } + } + } + + return false; + } + +} diff --git a/BlackJack/view/.gitignore b/BlackJack/view/.gitignore new file mode 100644 index 0000000..6e0eb83 --- /dev/null +++ b/BlackJack/view/.gitignore @@ -0,0 +1,4 @@ +/IView.class +/SimpleView.class +/SwedishView.class +/CLS.class diff --git a/BlackJack/view/IView.java b/BlackJack/view/IView.java index 986a191..669e3df 100644 --- a/BlackJack/view/IView.java +++ b/BlackJack/view/IView.java @@ -1,9 +1,11 @@ package BlackJack.view; +import BlackJack.controller.InputChoices; + public interface IView { void DisplayWelcomeMessage(); - int GetInput(); + InputChoices GetInput(); void DisplayCard(BlackJack.model.Card a_card); void DisplayPlayerHand(Iterable a_hand, int a_score); void DisplayDealerHand(Iterable a_hand, int a_score); diff --git a/BlackJack/view/SimpleView.java b/BlackJack/view/SimpleView.java index 8b5fd6d..14ea6d8 100644 --- a/BlackJack/view/SimpleView.java +++ b/BlackJack/view/SimpleView.java @@ -1,66 +1,66 @@ package BlackJack.view; -public class SimpleView implements IView -{ +import java.util.Scanner; - public void DisplayWelcomeMessage() - { - for(int i = 0; i < 50; i++) {System.out.print("\n");}; - System.out.println("Hello Black Jack World"); - System.out.println("Type 'p' to Play, 'h' to Hit, 's' to Stand or 'q' to Quit\n"); - } +import BlackJack.controller.InputChoices; - public int GetInput() - { - try { - int c = System.in.read(); - while (c == '\r' || c =='\n') { - c = System.in.read(); - } - return c; - } catch (java.io.IOException e) { - System.out.println("" + e); - return 0; - } - } +public class SimpleView implements IView { + private Scanner scan = new Scanner(System.in); - public void DisplayCard(BlackJack.model.Card a_card) - { - System.out.println("" + a_card.GetValue() + " of " + a_card.GetColor()); - } + public void DisplayWelcomeMessage() { - public void DisplayPlayerHand(Iterable a_hand, int a_score) - { - DisplayHand("Player", a_hand, a_score); - } + System.out.println("Hello Black Jack World"); + System.out.println("Type 'p' to Play, 'h' to Hit, 's' to Stand or 'q' to Quit\n"); + } - public void DisplayDealerHand(Iterable a_hand, int a_score) - { - DisplayHand("Dealer", a_hand, a_score); - } + public InputChoices GetInput() { + try { + String c = scan.next(); - private void DisplayHand(String a_name, Iterable a_hand, int a_score) - { - System.out.println(a_name + " Has: "); - for(BlackJack.model.Card c : a_hand) - { - DisplayCard(c); - } - System.out.println("Score: " + a_score); - System.out.println(""); - } + if (c.equals("p")) { + return InputChoices.Play; + } else if (c.equals("s")) { + return InputChoices.Stand; + } else if (c.equals("h")) { + return InputChoices.Hit; + } else if (c.equals("q")) { + return InputChoices.Quit; + } + } catch (Exception e) { + System.out.println("Wrong Input!" + e); + } + return null; + } - public void DisplayGameOver(boolean a_dealerIsWinner) - { - System.out.println("GameOver: "); - if (a_dealerIsWinner) - { - System.out.println("Dealer Won!"); - } - else - { - System.out.println("You Won!"); - } - - } - } + public void DisplayCard(BlackJack.model.Card a_card) { + System.out.println("" + a_card.GetValue() + " of " + a_card.GetColor()); + } + + public void DisplayPlayerHand(Iterable a_hand, int a_score) { + DisplayHand("Player", a_hand, a_score); + } + + public void DisplayDealerHand(Iterable a_hand, int a_score) { + DisplayHand("Dealer", a_hand, a_score); + } + + private void DisplayHand(String a_name, Iterable a_hand, int a_score) { + System.out.println(a_name + " Has: "); + for (BlackJack.model.Card c : a_hand) { + DisplayCard(c); + } + System.out.println("Score: " + a_score); + System.out.println(""); + } + + public void DisplayGameOver(boolean a_dealerIsWinner) { + System.out.println("GameOver: "); + if (a_dealerIsWinner) { + System.out.println("Dealer Won!"); + } else { + System.out.println("You Won!"); + } + + } + +} diff --git a/BlackJack/view/SwedishView.java b/BlackJack/view/SwedishView.java index 67449a5..03de321 100644 --- a/BlackJack/view/SwedishView.java +++ b/BlackJack/view/SwedishView.java @@ -1,30 +1,39 @@ package BlackJack.view; +import java.util.Scanner; + +import BlackJack.controller.InputChoices; + + public class SwedishView implements IView { + private Scanner scan = new Scanner(System.in); public void DisplayWelcomeMessage() { - - for(int i = 0; i < 50; i++) {System.out.print("\n");}; System.out.println("Hej Black Jack Världen"); System.out.println("----------------------"); System.out.println("Skriv 'p' för att Spela, 'h' för nytt kort, 's' för att stanna 'q' för att avsluta\n"); } - public int GetInput() - { - try { - int c = System.in.read(); - while (c == '\r' || c =='\n') { - c = System.in.read(); - } - return c; - } catch (java.io.IOException e) { - System.out.println("" + e); - return 0; - } - } + public InputChoices GetInput() { + try { + String c = scan.next(); + + if (c.equals("p")) { + return InputChoices.Play; + } else if (c.equals("s")) { + return InputChoices.Stand; + } else if (c.equals("h")) { + return InputChoices.Hit; + } else if (c.equals("q")) { + return InputChoices.Quit; + } + } catch (Exception e) { + System.out.println("Ogiltig val!" + e); + } + return null; + } public void DisplayCard(BlackJack.model.Card a_card) {