dev
This commit is contained in:
parent
c775c431f5
commit
1012b84e5a
|
|
@ -0,0 +1,20 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GameHandler.UnitTests.Helper
|
||||
{
|
||||
public static class IEnumerableExtension
|
||||
{
|
||||
public static IEnumerable AsWeakEnumerable(this IEnumerable source)
|
||||
{
|
||||
foreach (object o in source)
|
||||
{
|
||||
yield return o;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +1,9 @@
|
|||
using GameModel;
|
||||
using GameHandler.UnitTests.Helper;
|
||||
using GameModel;
|
||||
using GameModel.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NuGet.Frameworks;
|
||||
using System.Collections;
|
||||
using System.Numerics;
|
||||
|
||||
namespace GameHandler.UnitTests
|
||||
{
|
||||
|
|
@ -23,8 +22,7 @@ namespace GameHandler.UnitTests
|
|||
[TestCase(9, PinState.Down)]
|
||||
public void PinPicture_IndexTest(int pinNumber, PinState pinState)
|
||||
{
|
||||
var p = new PinPicture();
|
||||
p[pinNumber] = pinState;
|
||||
var p = PinPicture.Create(pinNumber, pinState);
|
||||
Assert.That(p[pinNumber],Is.EqualTo(PinState.Down));
|
||||
}
|
||||
|
||||
|
|
@ -35,19 +33,152 @@ namespace GameHandler.UnitTests
|
|||
[TestCase(11)]
|
||||
public void Get_Invalid_PinNumber_ThrowsException(int pinNumber)
|
||||
{
|
||||
var p = new PinPicture();
|
||||
var p = PinPicture.Create();
|
||||
Assert.That(() => p[pinNumber],Throws.TypeOf<InvalidPinIndexException>());
|
||||
}
|
||||
|
||||
//[Test]
|
||||
//[TestCase(0)]
|
||||
//[TestCase(-1)]
|
||||
//[TestCase(10)]
|
||||
//[TestCase(11)]
|
||||
//public void Set_Invalid_PinNumber_ThrowsException(int pinNumber)
|
||||
//{
|
||||
// var p = PinPicture.Create();
|
||||
// Assert.That(() => p[pinNumber] = PinState.Down, Throws.TypeOf<InvalidPinIndexException>());
|
||||
//}
|
||||
|
||||
[Test]
|
||||
[TestCase(0)]
|
||||
[TestCase(-1)]
|
||||
[TestCase(10)]
|
||||
[TestCase(11)]
|
||||
public void Set_Invalid_PinNumber_ThrowsException(int pinNumber)
|
||||
public void Access_PinStates()
|
||||
{
|
||||
var p = new PinPicture();
|
||||
Assert.That(() => p[pinNumber] = PinState.Down, Throws.TypeOf<InvalidPinIndexException>());
|
||||
var p = new PinPicture(PinState.Down, PinState.Up, PinState.Down, PinState.Up, PinState.Down, PinState.Up, PinState.Down, PinState.Up, PinState.Down);
|
||||
Assert.That(p.PinState1, Is.EqualTo(PinState.Down));
|
||||
Assert.That(p.PinState2, Is.EqualTo(PinState.Up));
|
||||
Assert.That(p.PinState3, Is.EqualTo(PinState.Down));
|
||||
Assert.That(p.PinState4, Is.EqualTo(PinState.Up));
|
||||
Assert.That(p.PinState5, Is.EqualTo(PinState.Down));
|
||||
Assert.That(p.PinState6, Is.EqualTo(PinState.Up));
|
||||
Assert.That(p.PinState7, Is.EqualTo(PinState.Down));
|
||||
Assert.That(p.PinState8, Is.EqualTo(PinState.Up));
|
||||
Assert.That(p.PinState9, Is.EqualTo(PinState.Down));
|
||||
Assert.That(p.UpCount, Is.EqualTo(4));
|
||||
Assert.That(p.DownCount, Is.EqualTo(5));
|
||||
Assert.That(p.AllUp, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DefaultPinPicature_IsAllUp_True()
|
||||
{
|
||||
var p = PinPicture.Create();
|
||||
Assert.That(p.AllUp,Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void LoopOverPinStates()
|
||||
{
|
||||
var p = PinPicture.Create();
|
||||
var i = 0;
|
||||
foreach (var item in p)
|
||||
{
|
||||
i++;
|
||||
Assert.That(item, Is.EqualTo(p[i]));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UseLinqOnPinPictore()
|
||||
{
|
||||
var p = new PinPicture(PinState.Down, PinState.Up, PinState.Down, PinState.Up, PinState.Down, PinState.Up, PinState.Down, PinState.Up, PinState.Down);
|
||||
Assert.That(p.First, Is.EqualTo(PinState.Down));
|
||||
Assert.That(p.Skip(1).First, Is.EqualTo(PinState.Up));
|
||||
Assert.That(p.Last, Is.EqualTo(PinState.Down));
|
||||
|
||||
var items = p.Take(9).ToArray();
|
||||
CollectionAssert.AreEqual(items, new[] { PinState.Down, PinState.Up, PinState.Down, PinState.Up, PinState.Down, PinState.Up, PinState.Down, PinState.Up, PinState.Down });
|
||||
|
||||
var enumerator = p.GetEnumerator();
|
||||
enumerator.MoveNext();
|
||||
enumerator.MoveNext();
|
||||
Assert.That(enumerator.Current, Is.EqualTo(PinState.Up));
|
||||
|
||||
// cover GetEnumerator method https://stackoverflow.com/questions/1510031/how-do-you-test-the-ienumerable-getenumerator-method
|
||||
IEnumerable weak = p.AsWeakEnumerable();
|
||||
var sequence = weak.Cast<PinState>().Take(9).ToArray();
|
||||
CollectionAssert.AreEqual(sequence, new[] { PinState.Down, PinState.Up, PinState.Down, PinState.Up, PinState.Down, PinState.Up, PinState.Down, PinState.Up, PinState.Down });
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PicturePlusSinkIsSamePicture()
|
||||
{
|
||||
var p = PinPicture.Create();
|
||||
var p2 = p + new PinThrow(null, false, true);
|
||||
Assert.That(p,Is.EqualTo(p2));
|
||||
}
|
||||
|
||||
//[Test]
|
||||
//public void PicturePlusSamePinsThrowException()
|
||||
//{
|
||||
// var p1 = new PinPicture();
|
||||
// p1.PinState1 = PinState.Down;
|
||||
|
||||
// var p2 = new PinPicture();
|
||||
// p2.PinState1 = PinState.Down;
|
||||
// var pinThrow = new PinThrow(p2, false, false);
|
||||
|
||||
// Assert.That(() => p1 + pinThrow, Throws.InstanceOf<InvalidPinCalcExcetion>());
|
||||
//}
|
||||
|
||||
[Test]
|
||||
public void Addition_CreatesCumulatedPinPicture()
|
||||
{
|
||||
var p1 = PinPicture.Create();
|
||||
var throwPic1 = PinPicture.Create(1, PinState.Down);
|
||||
var pinThrow1 = new PinThrow(throwPic1, false, false);
|
||||
|
||||
var throwPic2 = PinPicture.Create(2, PinState.Down);
|
||||
var pinThrow2 = new PinThrow(throwPic2, false, false);
|
||||
|
||||
var throwPic3 = PinPicture.Create(3, PinState.Down);
|
||||
var pinThrow3 = new PinThrow(throwPic3, false, false);
|
||||
|
||||
var throwPic4 = PinPicture.Create(4, PinState.Down);
|
||||
var pinThrow4 = new PinThrow(throwPic4, false, false);
|
||||
|
||||
var throwPic5 = PinPicture.Create(5, PinState.Down);
|
||||
var pinThrow5 = new PinThrow(throwPic5, false, false);
|
||||
|
||||
var throwPic6 = PinPicture.Create(6, PinState.Down);
|
||||
var pinThrow6 = new PinThrow(throwPic6, false, false);
|
||||
|
||||
var throwPic7 = PinPicture.Create(7, PinState.Down);
|
||||
var pinThrow7 = new PinThrow(throwPic7, false, false);
|
||||
|
||||
var throwPic8 = PinPicture.Create(8, PinState.Down);
|
||||
var pinThrow8 = new PinThrow(throwPic8, false, false);
|
||||
|
||||
var throwPic9 = PinPicture.Create(9, PinState.Down);
|
||||
var pinThrow9 = new PinThrow(throwPic9, false, false);
|
||||
|
||||
var p2 = p1 + pinThrow1;
|
||||
var p3 = p2 + pinThrow2;
|
||||
var p4 = p3 + pinThrow3;
|
||||
var p5 = p4 + pinThrow4;
|
||||
var p6 = p5 + pinThrow5;
|
||||
var p7 = p6 + pinThrow6;
|
||||
var p8 = p7 + pinThrow7;
|
||||
var p9 = p8 + pinThrow8;
|
||||
var p10 = p9 + pinThrow9;
|
||||
|
||||
Assert.That(p2.DownCount, Is.EqualTo(1));
|
||||
Assert.That(p3.DownCount, Is.EqualTo(2));
|
||||
Assert.That(p4.DownCount, Is.EqualTo(3));
|
||||
Assert.That(p5.DownCount, Is.EqualTo(4));
|
||||
Assert.That(p6.DownCount, Is.EqualTo(5));
|
||||
Assert.That(p7.DownCount, Is.EqualTo(6));
|
||||
Assert.That(p8.DownCount, Is.EqualTo(7));
|
||||
Assert.That(p9.DownCount, Is.EqualTo(8));
|
||||
Assert.That(p10.DownCount, Is.EqualTo(9));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ namespace GameHandler
|
|||
{
|
||||
public BoardState Init(ThrowMode throwMode)
|
||||
{
|
||||
var picture = new PinPicture();
|
||||
var picture = PinPicture.Create();
|
||||
|
||||
return new BoardState(throwMode,picture);
|
||||
}
|
||||
|
|
@ -33,7 +33,7 @@ namespace GameHandler
|
|||
{
|
||||
if (currentState.ThrowMode == ThrowMode.Reposition)
|
||||
{
|
||||
return currentState with { PinPicture = new PinPicture() };
|
||||
return currentState with { PinPicture = PinPicture.Create() };
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GameModel.Exceptions
|
||||
{
|
||||
public class InvalidPinCalcExcetion : KoogleException
|
||||
{
|
||||
public InvalidPinCalcExcetion() : base("pinstate cannot be calculated")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,30 +4,33 @@ using System.Collections;
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GameModel
|
||||
{
|
||||
public record PinPicture : IEnumerable<PinState>, IEnumerable
|
||||
public record PinPicture(PinState PinState1, PinState PinState2, PinState PinState3, PinState PinState4, PinState PinState5, PinState PinState6, PinState PinState7, PinState PinState8, PinState PinState9)
|
||||
: IEnumerable<PinState>, IEnumerable
|
||||
{
|
||||
private readonly List<PinState> pins = new List<PinState>();
|
||||
public PinPicture() : this(PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up)
|
||||
//private readonly List<PinState> pins = new List<PinState>();
|
||||
public static PinPicture Create(PinState pinState1, PinState pinState2, PinState pinState3, PinState pinState4, PinState pinState5, PinState pinState6, PinState pinState7, PinState pinState8, PinState pinState9)
|
||||
{
|
||||
|
||||
return new PinPicture(pinState1, pinState2, pinState3, pinState4, pinState5, pinState6, pinState7, pinState8, pinState9);
|
||||
}
|
||||
|
||||
public PinPicture(PinState pinState1, PinState pinState2, PinState pinState3, PinState pinState4, PinState pinState5, PinState pinState6, PinState pinState7, PinState pinState8, PinState pinState9)
|
||||
public static PinPicture Create()
|
||||
{
|
||||
pins.Add(pinState1);
|
||||
pins.Add(pinState2);
|
||||
pins.Add(pinState3);
|
||||
pins.Add(pinState4);
|
||||
pins.Add(pinState5);
|
||||
pins.Add(pinState6);
|
||||
pins.Add(pinState7);
|
||||
pins.Add(pinState8);
|
||||
pins.Add(pinState9);
|
||||
return PinPicture.Create(PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up);
|
||||
//pins.Add(PinState1);
|
||||
//pins.Add(pinState2);
|
||||
//pins.Add(pinState3);
|
||||
//pins.Add(pinState4);
|
||||
//pins.Add(pinState5);
|
||||
//pins.Add(pinState6);
|
||||
//pins.Add(pinState7);
|
||||
//pins.Add(pinState8);
|
||||
//pins.Add(pinState9);
|
||||
//ValidatePinPicture(pins);
|
||||
}
|
||||
|
||||
|
|
@ -37,27 +40,40 @@ namespace GameModel
|
|||
// ValidatePinPicture(this.pins);
|
||||
//}
|
||||
|
||||
private void ValidatePinPicture(IEnumerable<PinState> pins)
|
||||
//private void ValidatePinPicture(IEnumerable<PinState> pins)
|
||||
//{
|
||||
//if (pins.Count() != 9)
|
||||
//{
|
||||
// throw new InvalidPinPictureException();
|
||||
//}
|
||||
|
||||
//if (pins.All(_ => _ == PinState.Down))
|
||||
//{
|
||||
// throw new InvalidPinPictureException();
|
||||
//}
|
||||
|
||||
//if (pins.Count(_ => _ == PinState.Up) == 0)
|
||||
//{
|
||||
// throw new InvalidPinPictureException();
|
||||
//}
|
||||
|
||||
//if (ThrowCounter >= ThrowsPerRound)
|
||||
//{
|
||||
// throw new InvalidPinPictureException();
|
||||
//}
|
||||
//}
|
||||
|
||||
List<PinState> _pins;
|
||||
private List<PinState> pins
|
||||
{
|
||||
//if (pins.Count() != 9)
|
||||
//{
|
||||
// throw new InvalidPinPictureException();
|
||||
//}
|
||||
|
||||
//if (pins.All(_ => _ == PinState.Down))
|
||||
//{
|
||||
// throw new InvalidPinPictureException();
|
||||
//}
|
||||
|
||||
//if (pins.Count(_ => _ == PinState.Up) == 0)
|
||||
//{
|
||||
// throw new InvalidPinPictureException();
|
||||
//}
|
||||
|
||||
//if (ThrowCounter >= ThrowsPerRound)
|
||||
//{
|
||||
// throw new InvalidPinPictureException();
|
||||
//}
|
||||
get
|
||||
{
|
||||
if (_pins == null)
|
||||
{
|
||||
_pins = new List<PinState>(new[] { PinState1, PinState2, PinState3, PinState4, PinState5, PinState6, PinState7, PinState8, PinState9 });
|
||||
}
|
||||
return _pins;
|
||||
}
|
||||
}
|
||||
|
||||
public PinState this[int index]
|
||||
|
|
@ -71,25 +87,35 @@ namespace GameModel
|
|||
|
||||
return pins[index - 1];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (index < 1 || index > 9)
|
||||
{
|
||||
throw new InvalidPinIndexException();
|
||||
}
|
||||
pins[index - 1] = value;
|
||||
}
|
||||
//set
|
||||
//{
|
||||
// if (index < 1 || index > 9)
|
||||
// {
|
||||
// throw new InvalidPinIndexException();
|
||||
// }
|
||||
// pins[index - 1] = value;
|
||||
//}
|
||||
}
|
||||
|
||||
public PinState PinState1 { get { return this[1]; } set { this[1] = value; } }
|
||||
public PinState PinState2 { get { return this[2]; } set { this[2] = value; } }
|
||||
public PinState PinState3 { get { return this[3]; } set { this[3] = value; } }
|
||||
public PinState PinState4 { get { return this[4]; } set { this[4] = value; } }
|
||||
public PinState PinState5 { get { return this[5]; } set { this[5] = value; } }
|
||||
public PinState PinState6 { get { return this[6]; } set { this[6] = value; } }
|
||||
public PinState PinState7 { get { return this[7]; } set { this[7] = value; } }
|
||||
public PinState PinState8 { get { return this[8]; } set { this[8] = value; } }
|
||||
public PinState PinState9 { get { return this[9]; } set { this[9] = value; } }
|
||||
//public PinState PinState1 { get; init; }
|
||||
//public PinState PinState2 { get; init; }
|
||||
//public PinState PinState3 { get; init; }
|
||||
//public PinState PinState4 { get; init; }
|
||||
//public PinState PinState5 { get; init; }
|
||||
//public PinState PinState6 { get; init; }
|
||||
//public PinState PinState7 { get; init; }
|
||||
//public PinState PinState8 { get; init; }
|
||||
//public PinState PinState9 { get; init; }
|
||||
|
||||
////public PinState PinState1 { get { return this[1]; } set { this[1] = value; } }
|
||||
//public PinState PinState2 { get { return this[2]; } set { this[2] = value; } }
|
||||
//public PinState PinState3 { get { return this[3]; } set { this[3] = value; } }
|
||||
//public PinState PinState4 { get { return this[4]; } set { this[4] = value; } }
|
||||
//public PinState PinState5 { get { return this[5]; } set { this[5] = value; } }
|
||||
//public PinState PinState6 { get { return this[6]; } set { this[6] = value; } }
|
||||
//public PinState PinState7 { get { return this[7]; } set { this[7] = value; } }
|
||||
//public PinState PinState8 { get { return this[8]; } set { this[8] = value; } }
|
||||
//public PinState PinState9 { get { return this[9]; } set { this[9] = value; } }
|
||||
|
||||
public int UpCount
|
||||
{
|
||||
|
|
@ -115,6 +141,11 @@ namespace GameModel
|
|||
if (pinThrow.IsSink)
|
||||
return pic with { };
|
||||
|
||||
//if (pic.PinState1 == PinState.Down && pinThrow.PicPicture.PinState1 == PinState.Down)
|
||||
//{
|
||||
// throw new InvalidPinCalcExcetion();
|
||||
//}
|
||||
|
||||
return pic with
|
||||
{
|
||||
PinState1 = pinThrow.PicPicture.PinState1 == PinState.Down ? PinState.Down : pic.PinState1,
|
||||
|
|
@ -141,5 +172,17 @@ namespace GameModel
|
|||
{
|
||||
return this.GetEnumerator();
|
||||
}
|
||||
|
||||
public static PinPicture Create(int pinNumber, PinState pinState)
|
||||
{
|
||||
var states = new List<PinState>(new[] { PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up, PinState.Up }).ToArray();
|
||||
states[pinNumber - 1] = pinState;
|
||||
return PinPicture.Create(states);
|
||||
}
|
||||
|
||||
private static PinPicture Create(PinState[] states)
|
||||
{
|
||||
return PinPicture.Create(states[0], states[1], states[2], states[3], states[4], states[5], states[6], states[7], states[8]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue