Skip to content

Commit 6215e4a

Browse files
committed
Implemented all AI behaviour, added final win condition, clearer names
1 parent cf4e3aa commit 6215e4a

File tree

11 files changed

+137
-120
lines changed

11 files changed

+137
-120
lines changed

CardDeck/Deck/Card/Colour/ClassicColours.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
{
55
public ClassicColours() { }
66

7-
public readonly RGBColour Red = new("Red", 255, 0, 0);
8-
public readonly RGBColour Green = new("Green", 0, 255, 0);
9-
public readonly RGBColour Blue = new("Blue", 0, 0, 255);
10-
public readonly RGBColour Yellow = new("Yellow", 255, 255, 0);
11-
public readonly RGBColour Black = new("Black", 0, 0, 0);
7+
public readonly RGBColour Red = RGBColour.Red;
8+
public readonly RGBColour Green = RGBColour.Green;
9+
public readonly RGBColour Blue = RGBColour.Blue;
10+
public readonly RGBColour Yellow = RGBColour.Yellow;
11+
public readonly RGBColour None = RGBColour.None;
1212

13-
public string SetName { get; } = "Default Colours";
13+
public string SetName { get; } = "Classic Colours";
1414

1515
public IEnumerable<RGBColour> GetColours()
1616
{
1717
yield return Red;
1818
yield return Green;
1919
yield return Blue;
2020
yield return Yellow;
21-
yield return Black;
21+
yield return None;
2222
}
2323
}

CardDeck/Deck/Card/Colour/IColourSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public interface IColourSet
44
{
5-
public static readonly IColourSet Default = new ClassicColours();
5+
public static readonly IColourSet Classic = new ClassicColours();
66

77
public string SetName { get; }
88

CardDeck/Deck/Card/Colour/RGBColour.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Deck.Deck.Card.Colour;
44

55
public readonly struct RGBColour
66
{
7-
public static readonly RGBColour Default = new RGBColour();
7+
public static readonly RGBColour None = new();
88

99
public static readonly RGBColour White = new("White", 255, 255, 255);
1010
public static readonly RGBColour Black = new("Black", 0, 0, 0);
@@ -24,7 +24,7 @@ public RGBColour(string _name, byte _r, byte _g, byte _b)
2424
}
2525
public RGBColour()
2626
{
27-
Name = "";
27+
Name = "None";
2828
R = default;
2929
G = default;
3030
B = default;
@@ -47,12 +47,13 @@ public RGBColour()
4747
CardColour.Blue => Blue,
4848
CardColour.Red => Red,
4949
CardColour.Green => Green,
50+
CardColour.None => None,
5051
_ => throw new ArgumentOutOfRangeException(nameof(_colour))
5152
};
5253

5354
public override string ToString()
5455
{
55-
return string.Format("R: {0}, G: {1}, B: {2}", R, G, B);
56+
return string.Format("Colour: {0}\nR: {1}, G: {2}, B: {3}", Name, R, G, B);
5657
}
5758

5859
public override bool Equals([NotNullWhen(true)] object? _obj)

GameOne/Extensions/CardExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ public static byte MapToScore(this CardSubType _subType)
6464
}
6565
return _score;
6666
}
67-
public static bool CanPlay(this GameCard _card, GameCard _other, bool _strictPlay = true)
67+
public static bool CanPlay(this GameCard _card, GameCard _comparison, bool _strictPlay = true)
6868
{
69-
var _discardDescription = _other.Description;
69+
var _discardDescription = _comparison.Description;
7070
var _desiredDescription = _card.Description;
71-
var _discardData = _other.Data;
71+
var _discardData = _comparison.Data;
7272
var _desiredData = _card.Data;
7373

7474
if(_card.InUse && _strictPlay)

GameOne/Extensions/IEnumerableExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,27 @@
22

33
internal static class IEnumerableExtensions
44
{
5+
public static IEnumerable<(T _self, int _index)> WithIndex<T>(this IEnumerable<T> _self)
6+
{
7+
if(_self == null)
8+
{
9+
throw new NullReferenceException($"{nameof(_self)} is null");
10+
}
11+
12+
return _self.Select((T _element, int _index) => (_element, _index));
13+
}
514
public static IEnumerable<(T1 _first, T2 _second)> EnumerateMany<T1, T2>
615
(this IEnumerable<T1> _first, IEnumerable<T2> _second)
716
{
17+
if(_first == null)
18+
{
19+
throw new NullReferenceException($"{nameof(_first)} is null");
20+
}
21+
if(_second == null)
22+
{
23+
throw new NullReferenceException($"{nameof(_second)} is null");
24+
}
25+
826
using var _firstIE = _first.GetEnumerator();
927
using var _secondIE = _second.GetEnumerator();
1028

GameOne/Game/DeckFactory.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ public static CardGroupDescription GetDefaultSpecialDescription()
1010
{
1111
var _descriptions = new CardDescription[5];
1212

13-
ReadOnlySpan<RGBColour> _colours = IColourSet.Default.GetColours().ToArray();
13+
ReadOnlySpan<RGBColour> _colours = IColourSet.Classic.GetColours().ToArray();
1414

1515
for(int i = 0; i < _colours.Length - 1; i++)
1616
{
1717
var _cardDescriptionBuilder =
18-
new CardDescriptionBuilder(Globals.SpecialType, IColourSet.Default, _colours[i]);
18+
new CardDescriptionBuilder(Globals.SpecialType, IColourSet.Classic, _colours[i]);
1919
_descriptions[i] = _cardDescriptionBuilder
2020
.WithType(Globals.PlusTwoSubType, 2).WithType(Globals.ReverseSubType, 2)
2121
.WithType(Globals.SkipSubType, 2).Build();
2222
}
2323

2424
var _wildCardsBuilder =
25-
new CardDescriptionBuilder(Globals.SpecialType, IColourSet.Default, _colours[^1]);
25+
new CardDescriptionBuilder(Globals.SpecialType, IColourSet.Classic, _colours[^1]);
2626
_descriptions[4] = _wildCardsBuilder
2727
.WithType(Globals.WildSubType, 4).WithType(Globals.WildPlusFourSubType, 4)
2828
.Build();
@@ -31,14 +31,14 @@ public static CardGroupDescription GetDefaultSpecialDescription()
3131
}
3232
public static CardGroupDescription GetDefaultNumericDescription()
3333
{
34-
ReadOnlySpan<RGBColour> _colours = IColourSet.Default.GetColours().ToArray();
34+
ReadOnlySpan<RGBColour> _colours = IColourSet.Classic.GetColours().ToArray();
3535

3636
var _descriptions = new CardDescription[_colours.Length - 1];
3737

3838
for(int i = 0; i < _descriptions.Length; i++)
3939
{
4040
var _cardDescriptionBuilder =
41-
new CardDescriptionBuilder(Globals.NumericType, IColourSet.Default, _colours[i]);
41+
new CardDescriptionBuilder(Globals.NumericType, IColourSet.Classic, _colours[i]);
4242
_cardDescriptionBuilder
4343
.WithType(Globals.ZeroSubType, 1);
4444

GameOne/Game/GameManager.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ public GameManager()
4343

4444
public void Run()
4545
{
46+
var _winner = manager.AllPlayers().FirstOrDefault(x => x.Score > 500);
47+
if(_winner != null)
48+
{
49+
Console.WriteLine($"{manager.CurrentPlayer} has won the game");
50+
return;
51+
}
52+
4653
if(playing)
4754
{
4855
return;
@@ -59,7 +66,12 @@ public void Run()
5966

6067
threadEvent.Reset();
6168

62-
Console.WriteLine(manager.CurrentPlayer.Name + $" Won\nPoints: {manager.CurrentPlayer.SumOfCardsScores}");
69+
var _winningPlayer = manager.CurrentPlayer;
70+
71+
Console.WriteLine(_winningPlayer.Name + $" Won\nPoints: {_winningPlayer.Score}");
72+
73+
playing = false;
74+
Run();
6375
}
6476
public void Render(Player _player)
6577
{
@@ -74,16 +86,25 @@ private void Game()
7486
var _nonAI = manager.NonAI;
7587
lock(lockObject)
7688
{
77-
while(!manager.GameOver)
89+
while(true)
7890
{
7991
threadEvent.WaitOne();
8092

8193
Render(_nonAI);
8294
manager.CurrentPlayer.Play(Render);
8395

96+
if(manager.GameOver)
97+
{
98+
break;
99+
}
84100

85101
manager.NextPlayer();
86102
}
87103
}
104+
if(manager.AllPlayers().Any(x => x.Score > 500))
105+
{
106+
return;
107+
}
108+
Game();
88109
}
89110
}

GameOne/Game/RoundManager.cs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,18 @@ public RoundManager(CardDeck _deck, int _playerCount, int _startingCards)
5555
private int skipPlayerCount;
5656
private bool reverseOrder;
5757

58-
5958
public void EvaluatePostPlay()
6059
{
6160
OnWin(CurrentPlayer);
6261
}
6362
private void OnWin(Player _player)
6463
{
65-
if(_player.Cards.Length != 0)
64+
if(_player.TotalCards != 0)
6665
{
6766
return;
6867
}
68+
_player.Score += AllPlayers().Where(x => x != _player).Sum(x => x.SumOfCardsScores);
6969
GameOver = true;
70-
_player.Score += AllPlayers().Sum(x => x.SumOfCardsScores);
7170
}
7271
public ReadOnlySpan<GameCard> GetMultipleCards(int _amount)
7372
{
@@ -95,18 +94,6 @@ public GameCard GetTopCard()
9594

9695
return _card;
9796
}
98-
public void SkipPlayer()
99-
{
100-
var _discardCard = PeekDiscardPileTopCard();
101-
if(_discardCard.Data.SubType != Globals.WildPlusFourSubType
102-
|| _discardCard.Data.SubType != Globals.PlusTwoSubType
103-
|| _discardCard.Data.SubType != Globals.SkipSubType)
104-
{
105-
return;
106-
}
107-
108-
skipPlayerCount++;
109-
}
11097
public void SetWildColour(RGBColour _colour)
11198
{
11299
var _card = DiscardPile.Peek();
@@ -148,7 +135,6 @@ public bool AddToDiscard(GameCard _card)
148135
/// <summary>
149136
/// Returns current player and increments player handle
150137
/// </summary>
151-
/// <returns></returns>
152138
public Player NextPlayer()
153139
{
154140
var _player = CurrentPlayer;
@@ -165,21 +151,24 @@ public Player NextPlayer()
165151
{
166152
playerIndex = (byte)((playerIndex + 1) % playersLength);
167153
}
168-
CurrentPlayer = players[playerIndex];
169154

170155
if(skipPlayerCount > 0)
171156
{
172157
skipPlayerCount--;
173158
_player = NextPlayer();
174159
}
175160

161+
CurrentPlayer = players[playerIndex];
176162
return _player;
177163
}
178164
public Player PeekPlayer(int _offset)
179165
{
180166
int _index;
181167
int _modValue;
182-
if(reverseOrder || _offset < 0)
168+
169+
var _checkReverse = reverseOrder ^ (_offset < 0);
170+
171+
if(_checkReverse)
183172
{
184173
_offset = Math.Abs(_offset);
185174
_modValue = playerIndex - _offset;
@@ -207,14 +196,13 @@ public bool ExecuteCardBehaviour(GameCard _card)
207196
if(_subType == Globals.PlusTwoSubType)
208197
{
209198
var _nextPlayer = PeekPlayer(1);
210-
_nextPlayer.GiveCard(GetTopCard());
211-
_nextPlayer.GiveCard(GetTopCard());
212-
SkipPlayer();
199+
_nextPlayer.GiveCards(GetMultipleCards(2));
200+
IncrementSkipCount();
213201
return false;
214202
}
215203
if(_subType == Globals.SkipSubType)
216204
{
217-
SkipPlayer();
205+
IncrementSkipCount();
218206
return false;
219207
}
220208
if(_subType == Globals.ReverseSubType)
@@ -229,22 +217,23 @@ public bool ExecuteCardBehaviour(GameCard _card)
229217
if(_subType == Globals.WildPlusFourSubType)
230218
{
231219
var _nextPlayer = PeekPlayer(1);
232-
_nextPlayer.GiveCard(GetTopCard());
233-
_nextPlayer.GiveCard(GetTopCard());
234-
_nextPlayer.GiveCard(GetTopCard());
235-
_nextPlayer.GiveCard(GetTopCard());
236-
SkipPlayer();
220+
_nextPlayer.GiveCards(GetMultipleCards(4));
221+
IncrementSkipCount();
237222
return true;
238223
}
239224
throw new NotSupportedException(nameof(_subType) + " is not a supported type");
240225
}
241-
private IEnumerable<Player> AllPlayers()
226+
public IEnumerable<Player> AllPlayers()
242227
{
243228
for(int i = 0; i < playersLength; i++)
244229
{
245230
yield return PeekPlayer(i);
246231
}
247232
}
233+
private void IncrementSkipCount()
234+
{
235+
skipPlayerCount++;
236+
}
248237
private void Shuffle()
249238
{
250239
PickupDeck.Shuffle();

0 commit comments

Comments
 (0)