Example #1
0
		public void StartNewLeg_AfterLegFinished()
		{
			var match = new Match("1", new[] { "p1", "p2" }, 20, 2);
			match.AddThrow(ThrowResult.Double(10));
			match.AddThrow(ThrowResult.Double(10));
			Assert.AreEqual(2, match.Legs.Count);
			Assert.IsTrue(match.CurrentLeg.Finished);
		}
		public void Execute()
		{
			var players1 = ui.Prompt("Enter player 1 name");
			var players2 = ui.Prompt("Enter player 2 name");
			var initialScore = ui.PromptInt("Enter initial score", "301");
			var legsCount = ui.PromptInt("Enter legs count", "3");
			var match = new Match(DateTime.Now.Ticks.ToString(), new[] { players1, players2 }, initialScore, legsCount);
			EnterMatch(match);
			repo.SaveOrUpdate(match);
		}
		private void EnterMatch(Match match)
		{
			while (!match.Finished)
			{
				ShowCurrentLegDetails(match);
				var throwResult = ui.Prompt("Enter next throw result", ThrowResult.IsValid, ThrowResult.Parse, "");
				match.AddThrow(throwResult);
			}
			Console.WriteLine("Match is finished! Player {0} wins!", match.Winner);
			MatchSummaryView.Show(match);
			Console.WriteLine();
		}
		public static void Show(Match match)
		{
			Console.WriteLine("Total".PadLeft(20) + " | " + string.Join("  ", match.Legs.Select((leg, i) => i + 1)));
			var playerIndex = 0;
			foreach (var player in match.PlayerNames)
			{
				var winsCount = match.Legs.Count(l => l.Finished && l.WinnerIndex == playerIndex);
				// ReSharper disable once AccessToModifiedClosure (playerIndex)
				var legsScores = string.Join("  ", match.Legs.Select(l => l.Finished && l.WinnerIndex == playerIndex ? "1" : "0"));
				Console.WriteLine(player.PadLeft(10) + " " + winsCount.ToString().PadLeft(9) + " | " + legsScores);
				playerIndex++;
			}
		}
Example #5
0
		public void BeSerializable()
		{
			var match = new Match("1", new[] { "p1", "p2" }, 20, 2);
			match.AddThrow(ThrowResult.Double(10));
			match.AddThrow(ThrowResult.Double(10));

			var text = JsonConvert.SerializeObject(match, Formatting.Indented, new ThrowJsonConverter());
			Console.WriteLine(text);
			var deserializedMatch = JsonConvert.DeserializeObject<Match>(text, new ThrowJsonConverter());
			var text2 = JsonConvert.SerializeObject(deserializedMatch, Formatting.Indented, new ThrowJsonConverter());
			Console.WriteLine(text2);
			Assert.AreEqual(text, text2);
		}
Example #6
0
		public void ChangePlayer_AfterTurnFinished()
		{
			var match = new Match("1", new[] { "p1", "p2" }, 301, 2);
			match.AddThrow(ThrowResult.Single(1));
			match.AddThrow(ThrowResult.Single(2));
			match.AddThrow(ThrowResult.Single(3));
			// Player 1 turn is finished

			match.AddThrow(ThrowResult.Single(4));

			Assert.AreEqual(1, match.CurrentLeg.GetPlayerTurns(0).Count);
			Assert.AreEqual(1, match.CurrentLeg.GetPlayerTurns(1).Count);
		}
		private void ShowCurrentLegDetails(Match match)
		{
			var leg = match.CurrentLeg;
			var legIndex = match.Legs.Count - 1;
			foreach (var name in match.PlayerNames)
			{
				var legPlayerIndex = match.GetLegPlayerIndex(name, legIndex);
				var scores = new[] { match.InitialScore }.Concat(leg.GetPlayerTurns(legPlayerIndex).Select(t => t.ScoreAfter));
				Console.WriteLine(name.PadLeft(10) + " " + string.Join(" ", scores.Select(s => s.ToString().PadLeft(3))));
			}
			if (!leg.Finished)
				Console.WriteLine("Current player: " + match.GetPlayerName(legIndex, leg.CurrentPlayerIndex) + ", score: " +
				                  leg.CurrentPlayer.Score);
			else
			{
				Console.WriteLine("Finished");
			}
		}
		private static string Serialize(Match match)
		{
			return JsonConvert.SerializeObject(match, Formatting.Indented, new ThrowJsonConverter());
		}
		public void SaveOrUpdate(Match match)
		{
			File.WriteAllText(GetFilename(match.Id), Serialize(match));
		}
Example #10
0
		protected bool Equals(Match other)
		{
			return string.Equals(Id, other.Id);
		}
Example #11
0
		public void SetUp()
		{
			emptyMatch = new Match("1", new[] { "p1", "p2" }, 301, 2);
		}