From 702413403a21d924e580181dfe92fb510f5309c4 Mon Sep 17 00:00:00 2001 From: Pranav Date: Thu, 26 Oct 2017 15:33:52 +0200 Subject: [PATCH 01/14] new change? --- .classpath | 6 ++++++ .project | 17 +++++++++++++++++ BlackJack/.gitignore | 1 + BlackJack/controller/.gitignore | 1 + BlackJack/model/.gitignore | 7 +++++++ BlackJack/model/rules/.gitignore | 6 ++++++ BlackJack/view/.gitignore | 3 +++ 7 files changed, 41 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 BlackJack/.gitignore create mode 100644 BlackJack/controller/.gitignore create mode 100644 BlackJack/model/.gitignore create mode 100644 BlackJack/model/rules/.gitignore create mode 100644 BlackJack/view/.gitignore 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/controller/.gitignore b/BlackJack/controller/.gitignore new file mode 100644 index 0000000..db0bebf --- /dev/null +++ b/BlackJack/controller/.gitignore @@ -0,0 +1 @@ +/PlayGame.class 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/rules/.gitignore b/BlackJack/model/rules/.gitignore new file mode 100644 index 0000000..9953055 --- /dev/null +++ b/BlackJack/model/rules/.gitignore @@ -0,0 +1,6 @@ +/AmericanNewGameStrategy.class +/BasicHitStrategy.class +/IHitStrategy.class +/INewGameStrategy.class +/InternationalNewGameStrategy.class +/RulesFactory.class diff --git a/BlackJack/view/.gitignore b/BlackJack/view/.gitignore new file mode 100644 index 0000000..1a74674 --- /dev/null +++ b/BlackJack/view/.gitignore @@ -0,0 +1,3 @@ +/IView.class +/SimpleView.class +/SwedishView.class From ca1a4ac8504173514de6301bbe04f2895288dc28 Mon Sep 17 00:00:00 2001 From: miiro19 Date: Thu, 26 Oct 2017 15:39:10 +0200 Subject: [PATCH 02/14] Update Game.java --- BlackJack/model/Game.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/BlackJack/model/Game.java b/BlackJack/model/Game.java index 3ba16ac..1ed7216 100644 --- a/BlackJack/model/Game.java +++ b/BlackJack/model/Game.java @@ -34,8 +34,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() @@ -59,4 +58,4 @@ public int GetPlayerScore() } -} \ No newline at end of file +} From 9080287da019f48a133c8c1a0b3b4c5fe0497272 Mon Sep 17 00:00:00 2001 From: miiro19 Date: Thu, 26 Oct 2017 15:39:37 +0200 Subject: [PATCH 03/14] Update Dealer.java --- BlackJack/model/Dealer.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/BlackJack/model/Dealer.java b/BlackJack/model/Dealer.java index 2d45b16..fca5b42 100644 --- a/BlackJack/model/Dealer.java +++ b/BlackJack/model/Dealer.java @@ -58,4 +58,13 @@ 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; + } +} From b1b66136fafba29fdbad8f42257747caec9c609a Mon Sep 17 00:00:00 2001 From: Pranav Date: Thu, 26 Oct 2017 19:01:36 +0200 Subject: [PATCH 04/14] Soft Hit Strategy implementation trial --- BlackJack/model/Player.java | 10 ++++++++ BlackJack/model/rules/.gitignore | 1 + BlackJack/model/rules/SoftHitStrategy.java | 29 ++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 BlackJack/model/rules/SoftHitStrategy.java diff --git a/BlackJack/model/Player.java b/BlackJack/model/Player.java index 5e3a6f0..16f6e76 100644 --- a/BlackJack/model/Player.java +++ b/BlackJack/model/Player.java @@ -71,4 +71,14 @@ 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; + } } \ No newline at end of file diff --git a/BlackJack/model/rules/.gitignore b/BlackJack/model/rules/.gitignore index 9953055..324ff9b 100644 --- a/BlackJack/model/rules/.gitignore +++ b/BlackJack/model/rules/.gitignore @@ -4,3 +4,4 @@ /INewGameStrategy.class /InternationalNewGameStrategy.class /RulesFactory.class +/SoftHitStrategy.class diff --git a/BlackJack/model/rules/SoftHitStrategy.java b/BlackJack/model/rules/SoftHitStrategy.java new file mode 100644 index 0000000..f26bd0e --- /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 < a_dealer.CalcScore()) { + 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; + } + +} From 82b11aaafbad34343e7353ad85697dcd9c7bfdf8 Mon Sep 17 00:00:00 2001 From: Pranav Date: Thu, 26 Oct 2017 19:19:14 +0200 Subject: [PATCH 05/14] Soft Rules impolementation change --- BlackJack/model/rules/.gitignore | 4 ++++ BlackJack/model/rules/IWinStrategy.java | 8 ++++++++ BlackJack/model/rules/PlayerWinStrategy.java | 16 ++++++++++++++++ BlackJack/model/rules/SoftAmericanRules.java | 20 ++++++++++++++++++++ BlackJack/model/rules/SoftRulesFactory.java | 11 +++++++++++ 5 files changed, 59 insertions(+) create mode 100644 BlackJack/model/rules/IWinStrategy.java create mode 100644 BlackJack/model/rules/PlayerWinStrategy.java create mode 100644 BlackJack/model/rules/SoftAmericanRules.java create mode 100644 BlackJack/model/rules/SoftRulesFactory.java diff --git a/BlackJack/model/rules/.gitignore b/BlackJack/model/rules/.gitignore index 324ff9b..ca5782b 100644 --- a/BlackJack/model/rules/.gitignore +++ b/BlackJack/model/rules/.gitignore @@ -5,3 +5,7 @@ /InternationalNewGameStrategy.class /RulesFactory.class /SoftHitStrategy.class +/SoftRulesFactory.class +/SoftAmericanRules.class +/IWinStrategy.class +/PlayerWinStrategy.class 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/PlayerWinStrategy.java b/BlackJack/model/rules/PlayerWinStrategy.java new file mode 100644 index 0000000..298239b --- /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_dealerScore > g_maxScore) { + return false; + } + else if(a_playerScore > g_maxScore) { + return true; + } + return a_dealerScore > a_playerScore; + } + +} diff --git a/BlackJack/model/rules/SoftAmericanRules.java b/BlackJack/model/rules/SoftAmericanRules.java new file mode 100644 index 0000000..c55aa90 --- /dev/null +++ b/BlackJack/model/rules/SoftAmericanRules.java @@ -0,0 +1,20 @@ +package BlackJack.model.rules; + +public class SoftAmericanRules implements SoftRulesFactory{ + + @Override + public INewGameStrategy GetNewGameRule() { + return new AmericanNewGameStrategy(); + } + + @Override + public IHitStrategy GetHitStrategy() { + return new SoftHitStrategy(); + } + + @Override + public IWinStrategy GetWinStrategy() { + return new PlayerWinStrategy(); + } + +} diff --git a/BlackJack/model/rules/SoftRulesFactory.java b/BlackJack/model/rules/SoftRulesFactory.java new file mode 100644 index 0000000..e685914 --- /dev/null +++ b/BlackJack/model/rules/SoftRulesFactory.java @@ -0,0 +1,11 @@ +package BlackJack.model.rules; + +public interface SoftRulesFactory { + + INewGameStrategy GetNewGameRule(); + + IHitStrategy GetHitStrategy(); + + IWinStrategy GetWinStrategy(); + +} From 3bfff6bb0b24c085341182c789146461f6ea5854 Mon Sep 17 00:00:00 2001 From: Pranav Date: Thu, 26 Oct 2017 19:22:13 +0200 Subject: [PATCH 06/14] small mistake in soft hit strat --- BlackJack/model/rules/SoftHitStrategy.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BlackJack/model/rules/SoftHitStrategy.java b/BlackJack/model/rules/SoftHitStrategy.java index f26bd0e..3df1130 100644 --- a/BlackJack/model/rules/SoftHitStrategy.java +++ b/BlackJack/model/rules/SoftHitStrategy.java @@ -10,7 +10,7 @@ public boolean DoHit(Player a_dealer) { int count = a_dealer.CalcScore(); - if(count < a_dealer.CalcScore()) { + if(count < g_scoreLimit) { return true; } From bb6a921b3558645fc9f289c1eb605aa5fe143c9d Mon Sep 17 00:00:00 2001 From: Pranav Date: Thu, 26 Oct 2017 19:50:54 +0200 Subject: [PATCH 07/14] New Changes to rules --- BlackJack/model/Dealer.java | 8 +++++--- BlackJack/model/Game.java | 6 ++++-- BlackJack/model/rules/.gitignore | 2 ++ BlackJack/model/rules/BasicRulesFactory.java | 20 ++++++++++++++++++++ BlackJack/model/rules/IRulesFactory.java | 11 +++++++++++ BlackJack/model/rules/RulesFactory.java | 12 ------------ 6 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 BlackJack/model/rules/BasicRulesFactory.java create mode 100644 BlackJack/model/rules/IRulesFactory.java delete mode 100644 BlackJack/model/rules/RulesFactory.java diff --git a/BlackJack/model/Dealer.java b/BlackJack/model/Dealer.java index fca5b42..e518152 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); diff --git a/BlackJack/model/Game.java b/BlackJack/model/Game.java index 1ed7216..eb9060d 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(); } diff --git a/BlackJack/model/rules/.gitignore b/BlackJack/model/rules/.gitignore index ca5782b..3c4dea9 100644 --- a/BlackJack/model/rules/.gitignore +++ b/BlackJack/model/rules/.gitignore @@ -9,3 +9,5 @@ /SoftAmericanRules.class /IWinStrategy.class /PlayerWinStrategy.class +/BasicRulesFactory.class +/IRulesFactory.class diff --git a/BlackJack/model/rules/BasicRulesFactory.java b/BlackJack/model/rules/BasicRulesFactory.java new file mode 100644 index 0000000..038bab5 --- /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()S; + } + +} diff --git a/BlackJack/model/rules/IRulesFactory.java b/BlackJack/model/rules/IRulesFactory.java new file mode 100644 index 0000000..68defdd --- /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/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 From d40f90f77451e859d340b504be192aec9882c9e7 Mon Sep 17 00:00:00 2001 From: Stefan Date: Thu, 26 Oct 2017 20:56:55 +0200 Subject: [PATCH 08/14] First Shange --- BlackJack/Program.java | 4 +- BlackJack/controller/.gitignore | 4 + BlackJack/controller/InputChoices.java | 8 ++ BlackJack/controller/PlayGame.java | 76 +++++++++++------ BlackJack/model/Player.java | 2 - BlackJack/view/.gitignore | 1 + BlackJack/view/IView.java | 4 +- BlackJack/view/SimpleView.java | 114 ++++++++++++------------- BlackJack/view/SwedishView.java | 38 ++++++--- 9 files changed, 147 insertions(+), 104 deletions(-) create mode 100644 BlackJack/controller/InputChoices.java diff --git a/BlackJack/Program.java b/BlackJack/Program.java index 8ae21e5..6ae4d20 100644 --- a/BlackJack/Program.java +++ b/BlackJack/Program.java @@ -12,8 +12,8 @@ public static void main(String[] a_args) Game g = new Game(); 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 index db0bebf..d4656c8 100644 --- a/BlackJack/controller/.gitignore +++ b/BlackJack/controller/.gitignore @@ -1 +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..fe8898a 100644 --- a/BlackJack/controller/PlayGame.java +++ b/BlackJack/controller/PlayGame.java @@ -5,32 +5,52 @@ 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'; - } + 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; + } + + 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(); + deal(); + break; + case Hit: + a_game.Hit(); + deal(); + break; + case Stand: + a_game.Stand(); + deal(); + break; + case Quit: return false; + } + return true; + + } + + public void deal() { + 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/Player.java b/BlackJack/model/Player.java index 5e3a6f0..006ef28 100644 --- a/BlackJack/model/Player.java +++ b/BlackJack/model/Player.java @@ -10,9 +10,7 @@ public class Player { public Player() { - m_hand = new LinkedList(); - System.out.println("Hello List World"); } public void DealCard(Card a_addToHand) diff --git a/BlackJack/view/.gitignore b/BlackJack/view/.gitignore index 1a74674..6e0eb83 100644 --- a/BlackJack/view/.gitignore +++ b/BlackJack/view/.gitignore @@ -1,3 +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..0bcabec 100644 --- a/BlackJack/view/SwedishView.java +++ b/BlackJack/view/SwedishView.java @@ -1,10 +1,15 @@ 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"); @@ -12,19 +17,24 @@ public void DisplayWelcomeMessage() 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("Wrong Input!" + e); + } + return null; + } public void DisplayCard(BlackJack.model.Card a_card) { From a729abe574841c15e91d9510cccb9299b166a4d6 Mon Sep 17 00:00:00 2001 From: Stefan Date: Thu, 26 Oct 2017 21:09:40 +0200 Subject: [PATCH 09/14] Minus 50 lines --- BlackJack/view/SwedishView.java | 1 - 1 file changed, 1 deletion(-) diff --git a/BlackJack/view/SwedishView.java b/BlackJack/view/SwedishView.java index 0bcabec..645a191 100644 --- a/BlackJack/view/SwedishView.java +++ b/BlackJack/view/SwedishView.java @@ -10,7 +10,6 @@ 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("----------------------"); From 81ad03754b6a8070d5c0292877c8e6e92afceb89 Mon Sep 17 00:00:00 2001 From: Pranav Date: Thu, 26 Oct 2017 21:22:13 +0200 Subject: [PATCH 10/14] blargh blargh --- BlackJack/Program.java | 3 ++- BlackJack/model/rules/BasicRulesFactory.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/BlackJack/Program.java b/BlackJack/Program.java index 8ae21e5..25ba0e0 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,7 +11,7 @@ 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(); diff --git a/BlackJack/model/rules/BasicRulesFactory.java b/BlackJack/model/rules/BasicRulesFactory.java index 038bab5..0ff1572 100644 --- a/BlackJack/model/rules/BasicRulesFactory.java +++ b/BlackJack/model/rules/BasicRulesFactory.java @@ -14,7 +14,7 @@ public INewGameStrategy getNewGameStrategy() { public IWinStrategy GetWinStrategy() { - return new PlayerWinStrategy()S; + return new PlayerWinStrategy(); } } From 9d5c5e7aa671a65993a6b4f2c3c76c96c1508422 Mon Sep 17 00:00:00 2001 From: Pranav Date: Thu, 26 Oct 2017 23:01:12 +0200 Subject: [PATCH 11/14] blargh blargh --- BlackJack/model/Dealer.java | 9 ++------- BlackJack/model/rules/.gitignore | 1 + BlackJack/model/rules/BasicRulesFactory.java | 4 ++-- BlackJack/model/rules/DealerWinStrategy.java | 16 ++++++++++++++++ BlackJack/model/rules/IRulesFactory.java | 2 +- BlackJack/model/rules/PlayerWinStrategy.java | 8 ++++---- BlackJack/model/rules/SoftAmericanRules.java | 11 ++++------- 7 files changed, 30 insertions(+), 21 deletions(-) create mode 100644 BlackJack/model/rules/DealerWinStrategy.java diff --git a/BlackJack/model/Dealer.java b/BlackJack/model/Dealer.java index e518152..1264ca2 100644 --- a/BlackJack/model/Dealer.java +++ b/BlackJack/model/Dealer.java @@ -11,7 +11,7 @@ public class Dealer extends Player { public Dealer(IRulesFactory a_rulesFactory) { - m_newGameRule = a_rulesFactory.getNewGameStrategy(); + m_newGameRule = a_rulesFactory.GetNewGameStrategy(); m_hitRule = a_rulesFactory.GetHitStrategy(); m_winStrat = a_rulesFactory.GetWinStrategy(); @@ -45,12 +45,7 @@ public boolean Hit(Player a_player) { } 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() { diff --git a/BlackJack/model/rules/.gitignore b/BlackJack/model/rules/.gitignore index 3c4dea9..f3dd349 100644 --- a/BlackJack/model/rules/.gitignore +++ b/BlackJack/model/rules/.gitignore @@ -11,3 +11,4 @@ /PlayerWinStrategy.class /BasicRulesFactory.class /IRulesFactory.class +/DealerWinStrategy.class diff --git a/BlackJack/model/rules/BasicRulesFactory.java b/BlackJack/model/rules/BasicRulesFactory.java index 0ff1572..3d7cf9d 100644 --- a/BlackJack/model/rules/BasicRulesFactory.java +++ b/BlackJack/model/rules/BasicRulesFactory.java @@ -8,13 +8,13 @@ public IHitStrategy GetHitStrategy() { } - public INewGameStrategy getNewGameStrategy() { + public INewGameStrategy GetNewGameStrategy() { return new AmericanNewGameStrategy(); } public IWinStrategy GetWinStrategy() { - return new PlayerWinStrategy(); + 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/IRulesFactory.java b/BlackJack/model/rules/IRulesFactory.java index 68defdd..741f221 100644 --- a/BlackJack/model/rules/IRulesFactory.java +++ b/BlackJack/model/rules/IRulesFactory.java @@ -4,7 +4,7 @@ public interface IRulesFactory { public IHitStrategy GetHitStrategy(); - public INewGameStrategy getNewGameStrategy(); + public INewGameStrategy GetNewGameStrategy(); public IWinStrategy GetWinStrategy(); diff --git a/BlackJack/model/rules/PlayerWinStrategy.java b/BlackJack/model/rules/PlayerWinStrategy.java index 298239b..fc52945 100644 --- a/BlackJack/model/rules/PlayerWinStrategy.java +++ b/BlackJack/model/rules/PlayerWinStrategy.java @@ -4,12 +4,12 @@ public class PlayerWinStrategy implements IWinStrategy { @Override public boolean IsDealerWinner(int a_playerScore, int a_dealerScore, int g_maxScore) { - if (a_dealerScore > g_maxScore) { - return false; - } - else if(a_playerScore > 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/SoftAmericanRules.java b/BlackJack/model/rules/SoftAmericanRules.java index c55aa90..f9262ea 100644 --- a/BlackJack/model/rules/SoftAmericanRules.java +++ b/BlackJack/model/rules/SoftAmericanRules.java @@ -1,20 +1,17 @@ package BlackJack.model.rules; -public class SoftAmericanRules implements SoftRulesFactory{ +public class SoftAmericanRules implements IRulesFactory{ - @Override - public INewGameStrategy GetNewGameRule() { + public INewGameStrategy GetNewGameStrategy() { return new AmericanNewGameStrategy(); } - @Override public IHitStrategy GetHitStrategy() { return new SoftHitStrategy(); } - @Override public IWinStrategy GetWinStrategy() { - return new PlayerWinStrategy(); + return new DealerWinStrategy(); } -} +} \ No newline at end of file From 03e4d535f028b033aef85a7808f8dcfbfcf2ff79 Mon Sep 17 00:00:00 2001 From: Pranav Date: Thu, 26 Oct 2017 23:35:34 +0200 Subject: [PATCH 12/14] Refactor code --- BlackJack/model/Dealer.java | 14 ++++--- BlackJack/model/Deck.java | 3 +- .../model/rules/AmericanNewGameStrategy.java | 38 ++++++++----------- BlackJack/model/rules/INewGameStrategy.java | 3 +- .../rules/InternationalNewGameStrategy.java | 34 ++++++++--------- BlackJack/model/rules/SoftAmericanRules.java | 2 +- BlackJack/model/rules/SoftRulesFactory.java | 11 ------ 7 files changed, 41 insertions(+), 64 deletions(-) delete mode 100644 BlackJack/model/rules/SoftRulesFactory.java diff --git a/BlackJack/model/Dealer.java b/BlackJack/model/Dealer.java index 1264ca2..af89fdf 100644 --- a/BlackJack/model/Dealer.java +++ b/BlackJack/model/Dealer.java @@ -27,18 +27,14 @@ 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; @@ -64,4 +60,10 @@ public boolean Stand() { //updated from sequence diagram } return false; } + + public void DealCard(Player a_player, boolean bool) { + Card c = m_deck.GetCard(); + c.Show(bool); + a_player.DealCard(c); + } } 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/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/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/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/SoftAmericanRules.java b/BlackJack/model/rules/SoftAmericanRules.java index f9262ea..680d2a9 100644 --- a/BlackJack/model/rules/SoftAmericanRules.java +++ b/BlackJack/model/rules/SoftAmericanRules.java @@ -3,7 +3,7 @@ public class SoftAmericanRules implements IRulesFactory{ public INewGameStrategy GetNewGameStrategy() { - return new AmericanNewGameStrategy(); + return new InternationalNewGameStrategy(); } public IHitStrategy GetHitStrategy() { diff --git a/BlackJack/model/rules/SoftRulesFactory.java b/BlackJack/model/rules/SoftRulesFactory.java deleted file mode 100644 index e685914..0000000 --- a/BlackJack/model/rules/SoftRulesFactory.java +++ /dev/null @@ -1,11 +0,0 @@ -package BlackJack.model.rules; - -public interface SoftRulesFactory { - - INewGameStrategy GetNewGameRule(); - - IHitStrategy GetHitStrategy(); - - IWinStrategy GetWinStrategy(); - -} From 7301345b9b6b383ed06b8db06553f029bcd6c5b6 Mon Sep 17 00:00:00 2001 From: miiro19 Date: Fri, 27 Oct 2017 11:27:13 +0200 Subject: [PATCH 13/14] Added Observer Pattern --- BlackJack/controller/PlayGame.java | 11 +++++------ BlackJack/model/Dealer.java | 1 + BlackJack/model/Game.java | 5 ++++- BlackJack/model/IObserver.java | 7 +++++++ BlackJack/model/Player.java | 17 +++++++++++++++++ BlackJack/view/SwedishView.java | 2 +- 6 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 BlackJack/model/IObserver.java diff --git a/BlackJack/controller/PlayGame.java b/BlackJack/controller/PlayGame.java index fe8898a..22a0ad2 100644 --- a/BlackJack/controller/PlayGame.java +++ b/BlackJack/controller/PlayGame.java @@ -2,8 +2,9 @@ import BlackJack.view.IView; import BlackJack.model.Game; +import BlackJack.model.IObserver; -public class PlayGame { +public class PlayGame extends IObserver{ private Game a_game; private IView a_view; @@ -11,6 +12,7 @@ public class PlayGame { public PlayGame(Game a_game, IView a_view) { this.a_game = a_game; this.a_view = a_view; + a_game.AddObservableValue(this); } public boolean Play() { @@ -26,15 +28,12 @@ public boolean Play() { switch (input) { case Play: a_game.NewGame(); - deal(); break; case Hit: a_game.Hit(); - deal(); break; case Stand: a_game.Stand(); - deal(); break; case Quit: return false; } @@ -42,9 +41,9 @@ public boolean Play() { } - public void deal() { + public void update() { try { - Thread.sleep(1000); + Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/BlackJack/model/Dealer.java b/BlackJack/model/Dealer.java index af89fdf..142b53f 100644 --- a/BlackJack/model/Dealer.java +++ b/BlackJack/model/Dealer.java @@ -65,5 +65,6 @@ 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/Game.java b/BlackJack/model/Game.java index eb9060d..824008b 100644 --- a/BlackJack/model/Game.java +++ b/BlackJack/model/Game.java @@ -59,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); + } } 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 e6c8013..fa0f13f 100644 --- a/BlackJack/model/Player.java +++ b/BlackJack/model/Player.java @@ -1,16 +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(); + m_observers = new ArrayList(); } public void DealCard(Card a_addToHand) @@ -79,4 +82,18 @@ public int Aces() { } 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/view/SwedishView.java b/BlackJack/view/SwedishView.java index 645a191..03de321 100644 --- a/BlackJack/view/SwedishView.java +++ b/BlackJack/view/SwedishView.java @@ -30,7 +30,7 @@ public InputChoices GetInput() { return InputChoices.Quit; } } catch (Exception e) { - System.out.println("Wrong Input!" + e); + System.out.println("Ogiltig val!" + e); } return null; } From 65eeea80c134241d9380b80490ef63eef9ac159c Mon Sep 17 00:00:00 2001 From: miiro19 Date: Fri, 27 Oct 2017 12:02:51 +0200 Subject: [PATCH 14/14] add coment --- BlackJack/controller/PlayGame.java | 6 +++--- BlackJack/model/IObserver.class | Bin 0 -> 301 bytes 2 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 BlackJack/model/IObserver.class diff --git a/BlackJack/controller/PlayGame.java b/BlackJack/controller/PlayGame.java index 22a0ad2..c23a10e 100644 --- a/BlackJack/controller/PlayGame.java +++ b/BlackJack/controller/PlayGame.java @@ -12,7 +12,7 @@ public class PlayGame extends IObserver{ public PlayGame(Game a_game, IView a_view) { this.a_game = a_game; this.a_view = a_view; - a_game.AddObservableValue(this); + a_game.AddObservableValue(this); // make the Game an Observer } public boolean Play() { @@ -41,9 +41,9 @@ public boolean Play() { } - public void update() { + public void update() { // Update method gets called whenever a new card is dealt try { - Thread.sleep(1500); + Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/BlackJack/model/IObserver.class b/BlackJack/model/IObserver.class new file mode 100644 index 0000000000000000000000000000000000000000..0c773747002c3a4d29a07fe316041c709e702630 GIT binary patch literal 301 zcmZ`!u}%U(5PfqToSq11XRPeh!fos@7MPeoE>b|~_l}J&E_Y_Rxz3#t9A{Ob-9mdC@?9~mpX^3( zn*D2F$X(6LQr8@j&Z@>2=H7;Ry+4mv!Crg@n&C}A#1~Bv6K0Am