//War executed on equal cards, will lay off 3 cards and check the fourth protected WarOutcome MultiCardWar(PlayBoard playBoard) { var outcome = new WarOutcome(); //Pop the amount of cards for (int i = 0; i < _warCardCount; i++) { if (playBoard.PopCardPlayer1() == null) { outcome.WinningPlayer = 2; outcome.ExitBecauseOutOfCards = true; return(outcome); } if (playBoard.PopCardPlayer2() == null) { outcome.WinningPlayer = 1; outcome.ExitBecauseOutOfCards = true; return(outcome); } } //Try getting a normal run var singleRes = SingleCardWar(playBoard); if (singleRes.WinningPlayer == null) { Log("War resulted in more war!"); return(MultiCardWar(playBoard)); } outcome.WinningPlayer = singleRes.WinningPlayer.Value; return(outcome); }
public WarGameEndResult RunWar(PlayBoard playBoard) { var warRes = new WarGameEndResult() { NrOfRounds = 0 }; while (true) { Log(playBoard.EchoCards()); //Check if the players can still play if (!playBoard.CardsPlayer1.Any()) { warRes.WinningPlayer = 2; break; } if (!playBoard.CardsPlayer2.Any()) { warRes.WinningPlayer = 1; break; } //Play another round warRes.NrOfRounds++; var warCheck = SingleCardWar(playBoard); if (warCheck == null) { throw new Exception("One of the players ran out, this is not valid at this location"); } if (!warCheck.CausesWar) { Log("No war, returning cards"); playBoard.PlayerWin(warCheck.WinningPlayer.Value); continue; } else { Log("This is war!"); //warRes.NrOfRounds++; //War means extra turn! var multiWar = MultiCardWar(playBoard); if (!multiWar.ExitBecauseOutOfCards) { Log($"Player {multiWar.WinningPlayer.Value} won the multicard war"); playBoard.PlayerWin(multiWar.WinningPlayer.Value); } else { Log("Multicard exited because user ran out of cards! -> PAT"); warRes.WinningPlayer = null; break; } } } Log($"War is over: {warRes}"); return(warRes); }
protected WarCheck SingleCardWar(PlayBoard playBoard) { var c1 = playBoard.PopCardPlayer1(); var c2 = playBoard.PopCardPlayer2(); Log($"Playing-> p1: {c1} p2: {c2}"); if (c1 == null || c2 == null) { return(null); } return(new WarCheck() { P1Card = c1, P2Card = c2, CausesWar = c1.Strength == c2.Strength, WinningPlayer = GetWinningPlayer(c1, c2) }); }
private PlayBoard ReadPlayBoard() { var playBoard = new PlayBoard(); int nrOfCardsP1 = int.Parse(ReadLine()); for (int i = 0; i < nrOfCardsP1; i++) { var card = new Card(ReadLine()); playBoard.CardsPlayer1.Add(card); } int nrOfCardsP2 = int.Parse(ReadLine()); for (int i = 0; i < nrOfCardsP2; i++) { var card = new Card(ReadLine()); playBoard.CardsPlayer2.Add(card); } return(playBoard); }