Skip to content

Commit cf4e3aa

Browse files
committed
Simplified colour to just RGBColour.
Implemented more AI behaviour. Start of a win 'screen'
1 parent 463c4d0 commit cf4e3aa

File tree

14 files changed

+256
-92
lines changed

14 files changed

+256
-92
lines changed

CardDeck/Deck/Card/CardDescription.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Deck.Deck.Card;
55

66
public record CardDescription
77
{
8-
public CardDescription(CardType _type, IColour _colour, IColourSet _colourSet, Dictionary<CardSubType, byte> _cardCountMapping)
8+
public CardDescription(CardType _type, RGBColour _colour, IColourSet _colourSet, Dictionary<CardSubType, byte> _cardCountMapping)
99
{
1010
Type = _type;
1111
Colour = _colour;
@@ -15,7 +15,7 @@ public CardDescription(CardType _type, IColour _colour, IColourSet _colourSet, D
1515
}
1616

1717
public CardType Type { get; }
18-
public IColour Colour { get; set; }
18+
public RGBColour Colour { get; set; }
1919
public IColourSet ColourSet { get; }
2020
public FrozenDictionary<CardSubType, byte> CardCountMapping { get; }
2121
public int TotalCount { get; }

CardDeck/Deck/Card/Colour/IColour.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

CardDeck/Deck/Card/Colour/RGBColour.cs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Deck.Deck.Card.Colour;
44

5-
public readonly struct RGBColour : IColour
5+
public readonly struct RGBColour
66
{
7-
public static readonly RGBColour Default = new();
7+
public static readonly RGBColour Default = new RGBColour();
88

99
public static readonly RGBColour White = new("White", 255, 255, 255);
1010
public static readonly RGBColour Black = new("Black", 0, 0, 0);
@@ -19,18 +19,27 @@ public RGBColour(string _name, byte _r, byte _g, byte _b)
1919
R = _r;
2020
G = _g;
2121
B = _b;
22+
23+
hash = HashCode.Combine(Name, R, G, B, HasValue);
2224
}
2325
public RGBColour()
2426
{
2527
Name = "";
28+
R = default;
29+
G = default;
30+
B = default;
2631
HasValue = false;
32+
33+
hash = HashCode.Combine(Name, R, G, B, HasValue);
2734
}
2835

36+
private readonly int hash;
37+
2938
public string Name { get; }
39+
public bool HasValue { get; } = true;
3040
public byte R { get; }
3141
public byte G { get; }
3242
public byte B { get; }
33-
public bool HasValue { get; } = true;
3443

3544
public static RGBColour GetClassicColour(CardColour _colour) => _colour switch
3645
{
@@ -41,35 +50,34 @@ public RGBColour()
4150
_ => throw new ArgumentOutOfRangeException(nameof(_colour))
4251
};
4352

44-
public static bool operator ==(RGBColour _this, RGBColour _other)
45-
{
46-
return _this.R == _other.R && _this.G == _other.G && _this.B == _other.B;
47-
}
48-
public static bool operator !=(RGBColour _this, RGBColour _other)
49-
{
50-
return !(_this == _other);
51-
}
52-
5353
public override string ToString()
5454
{
5555
return string.Format("R: {0}, G: {1}, B: {2}", R, G, B);
5656
}
5757

58-
public override bool Equals([NotNullWhen(true)] object? obj)
58+
public override bool Equals([NotNullWhen(true)] object? _obj)
5959
{
60-
if(obj == null)
60+
var _colour = _obj as RGBColour?;
61+
if(!_colour.HasValue)
6162
{
6263
return false;
6364
}
64-
if(obj is not RGBColour _colour)
65-
{
66-
return false;
67-
}
68-
return _colour == this;
65+
66+
return GetHashCode() == _colour.GetHashCode();
6967
}
7068

7169
public override int GetHashCode()
7270
{
73-
return HashCode.Combine(R, G, B);
71+
return hash;
72+
}
73+
74+
public static bool operator ==(RGBColour _left, RGBColour _right)
75+
{
76+
return _left.Equals(_right);
77+
}
78+
79+
public static bool operator !=(RGBColour _left, RGBColour _right)
80+
{
81+
return !(_left == _right);
7482
}
7583
}

CardDeck/Deck/CardDeck.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public CardDeck(ReadOnlySpan<GameCard> _cards, IRandomizer _randomizer, int _max
2929
randomizer = _randomizer;
3030
}
3131

32-
public int RemainingCards => cards.Length - position;
32+
public int RemainingCards => cards.Length - (position + 1);
3333
public ReadOnlySpan<GameCard> Cards => cards;
3434

3535
private int position;
@@ -48,9 +48,14 @@ public void Shuffle()
4848
randomizer.Randomize(cards);
4949
}
5050

51+
public void SetCurrent(GameCard _card)
52+
{
53+
cards[position] = _card;
54+
}
5155
public GameCard Peek()
5256
{
53-
return Current();
57+
var _curent = Current();
58+
return _curent;
5459
}
5560
public bool AddRange(ReadOnlySpan<GameCard> _cards)
5661
{

CardDeck/Deck/CardDescriptionBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public sealed class CardDescriptionBuilder
88
{
99
private const byte DEFAULTCOUNT = 0;
1010

11-
public CardDescriptionBuilder(CardType _cardType, IColourSet _set, IColour _cardColour)
11+
public CardDescriptionBuilder(CardType _cardType, IColourSet _set, RGBColour _cardColour)
1212
{
1313
set = _set;
1414
type = _cardType;
@@ -22,7 +22,7 @@ public CardDescriptionBuilder(CardType _cardType, IColourSet _set, IColour _card
2222

2323
private readonly CardType type;
2424
private readonly IColourSet set;
25-
private readonly IColour colour;
25+
private readonly RGBColour colour;
2626
private readonly HashSet<CardSubType> methodCallMap;
2727
private readonly Dictionary<CardSubType, byte> cardCountPerSubType;
2828

GameOne/Extensions/CardExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static bool CanPlay(this GameCard _card, GameCard _other, bool _strictPla
7676
return false;
7777
}
7878

79-
// Same Colour
79+
// Same Colour
8080
return _discardDescription.Colour == _desiredDescription.Colour
8181
// Same Sub Type
8282
|| _discardData.SubType == _desiredData.SubType
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace GameOne.Extensions;
2+
3+
internal static class IEnumerableExtensions
4+
{
5+
public static IEnumerable<(T1 _first, T2 _second)> EnumerateMany<T1, T2>
6+
(this IEnumerable<T1> _first, IEnumerable<T2> _second)
7+
{
8+
using var _firstIE = _first.GetEnumerator();
9+
using var _secondIE = _second.GetEnumerator();
10+
11+
while(_firstIE.MoveNext() && _secondIE.MoveNext())
12+
{
13+
yield return (_firstIE.Current, _secondIE.Current);
14+
}
15+
}
16+
}

GameOne/Game/GameManager.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ internal sealed class GameManager
1313

1414
private static readonly object lockObject = new();
1515

16+
private readonly ManualResetEvent threadEvent = new(false);
17+
1618
public GameManager()
1719
{
1820
var _options = new DeckOptions([DeckFactory.GetDefaultNumericDescription(), DeckFactory.GetDefaultSpecialDescription()]);
@@ -48,10 +50,16 @@ public void Run()
4850
playing = true;
4951

5052
var _gameThread = new Thread(Game);
51-
5253
_gameThread.Start();
5354

54-
while(!manager.GameOver) { }
55+
while(!manager.GameOver)
56+
{
57+
threadEvent.Set();
58+
}
59+
60+
threadEvent.Reset();
61+
62+
Console.WriteLine(manager.CurrentPlayer.Name + $" Won\nPoints: {manager.CurrentPlayer.SumOfCardsScores}");
5563
}
5664
public void Render(Player _player)
5765
{
@@ -68,10 +76,12 @@ private void Game()
6876
{
6977
while(!manager.GameOver)
7078
{
71-
Render(_nonAI);
79+
threadEvent.WaitOne();
7280

81+
Render(_nonAI);
7382
manager.CurrentPlayer.Play(Render);
7483

84+
7585
manager.NextPlayer();
7686
}
7787
}

GameOne/Game/RoundManager.cs

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

58-
public void OnWin(Player _player)
58+
59+
public void EvaluatePostPlay()
60+
{
61+
OnWin(CurrentPlayer);
62+
}
63+
private void OnWin(Player _player)
5964
{
6065
if(_player.Cards.Length != 0)
6166
{
6267
return;
6368
}
6469
GameOver = true;
70+
_player.Score += AllPlayers().Sum(x => x.SumOfCardsScores);
6571
}
6672
public ReadOnlySpan<GameCard> GetMultipleCards(int _amount)
6773
{
@@ -91,22 +97,26 @@ public GameCard GetTopCard()
9197
}
9298
public void SkipPlayer()
9399
{
94-
if(PeekDiscardPileTopCard().Data.SubType != Globals.PlusTwoSubType)
100+
var _discardCard = PeekDiscardPileTopCard();
101+
if(_discardCard.Data.SubType != Globals.WildPlusFourSubType
102+
|| _discardCard.Data.SubType != Globals.PlusTwoSubType
103+
|| _discardCard.Data.SubType != Globals.SkipSubType)
95104
{
96105
return;
97106
}
98107

99108
skipPlayerCount++;
100109
}
101-
public void SetWildColour(IColour _colour)
110+
public void SetWildColour(RGBColour _colour)
102111
{
103112
var _card = DiscardPile.Peek();
104-
if(_card.Data.SubType != Globals.WildSubType || _card.Data.SubType != Globals.WildPlusFourSubType)
113+
if(_card.Data.SubType != Globals.WildSubType && _card.Data.SubType != Globals.WildPlusFourSubType)
105114
{
106115
return;
107116
}
108117

109118
_card.Description = _card.Description with { Colour = _colour };
119+
DiscardPile.SetCurrent(_card);
110120
}
111121
[MethodImpl(MethodImplOptions.AggressiveInlining)]
112122
public GameCard PeekDiscardPileTopCard() =>
@@ -159,7 +169,7 @@ public Player NextPlayer()
159169

160170
if(skipPlayerCount > 0)
161171
{
162-
skipPlayerCount++;
172+
skipPlayerCount--;
163173
_player = NextPlayer();
164174
}
165175

@@ -168,19 +178,21 @@ public Player NextPlayer()
168178
public Player PeekPlayer(int _offset)
169179
{
170180
int _index;
171-
if(reverseOrder)
181+
int _modValue;
182+
if(reverseOrder || _offset < 0)
172183
{
173-
var _minus = playerIndex - _offset;
174-
while(_minus < 0)
184+
_offset = Math.Abs(_offset);
185+
_modValue = playerIndex - _offset;
186+
while(_modValue < 0)
175187
{
176-
_minus = playersLength + _minus;
188+
_modValue = playersLength + _modValue;
177189
}
178-
_index = (byte)(_minus % playersLength);
179190
}
180191
else
181192
{
182-
_index = (byte)((playerIndex + _offset) % playersLength);
193+
_modValue = playerIndex + _offset;
183194
}
195+
_index = (byte)(_modValue % playersLength);
184196
return players[_index];
185197
}
186198
public bool ExecuteCardBehaviour(GameCard _card)
@@ -196,6 +208,7 @@ public bool ExecuteCardBehaviour(GameCard _card)
196208
{
197209
var _nextPlayer = PeekPlayer(1);
198210
_nextPlayer.GiveCard(GetTopCard());
211+
_nextPlayer.GiveCard(GetTopCard());
199212
SkipPlayer();
200213
return false;
201214
}
@@ -225,10 +238,35 @@ public bool ExecuteCardBehaviour(GameCard _card)
225238
}
226239
throw new NotSupportedException(nameof(_subType) + " is not a supported type");
227240
}
241+
private IEnumerable<Player> AllPlayers()
242+
{
243+
for(int i = 0; i < playersLength; i++)
244+
{
245+
yield return PeekPlayer(i);
246+
}
247+
}
228248
private void Shuffle()
229249
{
230250
PickupDeck.Shuffle();
231251
DiscardPile.Clear();
252+
253+
if(!PickupDeck.TryNextFree(out var _card))
254+
{
255+
throw new Exception("Failed to shuffle");
256+
}
257+
258+
DiscardPile.Add(_card);
259+
260+
// Make sure first playing card is NUMERICTYPE
261+
while(_card.Description.Type == Globals.SpecialType)
262+
{
263+
if(!PickupDeck.TryNextFree(out _card))
264+
{
265+
throw new Exception("Failed to init, try adding more cards");
266+
}
267+
268+
DiscardPile.Add(_card);
269+
}
232270
}
233271
private Player[] CreateAndGivePlayersCards(int _playerCount, int _cardsPerPlayer)
234272
{

GameOne/Renderer/CardRender.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static string Render(GameCard _card)
3939

4040
_builder.Insert(_centreIndex + _beginIndex, _cardType);
4141
_builder.Remove(_centreIndex + _beginIndex + _cardTypeLength, _cardTypeLength);
42+
_builder.AppendFormat("\nColour: {0}", _card.Description.Colour.Name);
4243

4344
return _builder.ToString();
4445
}

GameOne/Renderer/PlayerRender.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static string FormatPlayers(ReadOnlySpan<Player> _players, Player _curren
1717
foreach(var _player in _players)
1818
{
1919
var _name = _player.Name;
20-
var _score = _player.Score;
20+
var _score = _player.SumOfCardsScores;
2121

2222
var _nameLength = _name.Length;
2323
var _totalNameSegmentLength = NAME.Length + _nameLength;

0 commit comments

Comments
 (0)