-
Notifications
You must be signed in to change notification settings - Fork 1
/
StaircaseRummyBehaviour.cs
813 lines (713 loc) · 27 KB
/
StaircaseRummyBehaviour.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Text;
namespace FreeCell
{
/// <summary>
/// This class implements all of the freecell game logic. Edit this to implement another game type.
/// </summary>
public class StaircaseRummyBehavior : MonoBehaviour
{
#region Constants
/// <summary>
/// The max amount of players. For best results, keep this number between 2 and 4.
/// </summary>
public const int MaxPlayers = 4;
/// <summary>
/// The max amount of cards each player has per hand. Traditionally set to 7.
/// Keep this number odd for best results.
/// </summary>
public const int MaxHandCards = 7;
/// <summary>
/// The amount of Staircase playing cells. Traditionally set to 4.
/// </summary>
public const int StairCells = 4;
/// <summary>
/// The max amount of decks being used.
/// Keep it at this value for best results.
/// </summary>
public const int PlayingDecks = StairCells/2;
/// <summary>
/// The max amount of discard cells.
/// </summary>
public const int MaxJunkCells = 3;
#endregion
#region GameValues
public static StaircaseRummyBehavior Instance;
/// <summary>
/// The max cards in a players staircase pile. For best results, keep this number odd.
/// </summary>
public static int MaxStaircaseStack = 13;
/// <summary>
/// Random number seed used to shuffle the cards.
/// </summary>
public static int Seed = 100;
/// <summary>
/// Number of suits that the player will be dealt. Traditionally this is four suits for a normal deck of cards.
/// Staircase Rummy uses two decks, so a total of 8 suits are used.
/// </summary>
public static int Suits = 4 * PlayingDecks;
/// <summary>
/// Sets the distance between the cards for each play deck. Adjust this to suit your card faces.
/// </summary>
public float PlayStackVerticalSpace = -0.5f;
/// <summary>
/// True if the game has been won.
/// </summary>
public bool HasWon;
#region Moves
/// <summary>
/// Maintains the list of moves for the undo system.
/// </summary>
public Stack<Move> Moves = new Stack<Move>();
Stack<Move> redoMoves = new Stack<Move>();
bool isRedoing;
bool isAutocompleteRunning;
#endregion
#region Actions
/// <summary>
/// Called when the game is won. Play your victory screen from here.
/// </summary>
public Action OnWin;
/// <summary>
/// This is called when a card dragable flag is changed.
/// </summary>
public Action<Card, bool> OnCardDragableUpdate;
#endregion
/// <summary>
/// Time that the game started. Use to calcuate play time.
/// </summary>
public float StartTime;
#endregion
#region Decks
/// <summary>
/// The list of cards from the drawing deck. Drawing is done automatically at the end of each turn.
/// </summary>
public Deck Dealer;
/// <summary>
/// The Staircase (Goal) stack of cards for S, W, N and E players.
/// This is filled automatically by the game at the start. Top card is displayed.
/// </summary>
#region StaircaseStacks
public Stack<Deck> Sstaircase;
public Stack<Deck> Wstaircase;
public Stack<Deck> Nstaircase;
public Stack<Deck> Estaircase;
#endregion
/// <summary>
/// The list of hand cards for South (S), West (W), North (N) and East (E) players.
/// Filled automatically by the game at the start, and whenthe player ends their turn by discarding.
/// </summary>
#region HandCards
public List<Deck> ShandCards;
public List<Deck> WhandCards;
public List<Deck> NhandCards;
public List<Deck> EhandCards;
#endregion
/// <summary>
/// The Junk (Discard) pile of cards for S, W, N and E players.
/// The players turn ends if they place a card here. Traditionally up to 3 cards only.
/// </summary>
#region JunkPiles
public List<Deck> Sjunk;
public List<Deck> Wjunk;
public List<Deck> Njunk;
public List<Deck> Ejunk;
#endregion
/// <summary>
/// The list of play cells. This list will be filled as cards are played.
/// </summary>
#region PlayCells
public List<Deck> SplayCell;
public List<Deck> NplayCell; // A -> K
public List<Deck> EplayCell;
public List<Deck> WplayCell; // K -> A
#endregion
#endregion
public StaircaseRummyBehavior()
{
Instance = this;
}
void Start()
{
StartCoroutine(NewGame(Seed));
}
#region UpdateAuto
// Modify the right click or rid it altoghether
void Update()
{
if (Input.GetMouseButtonDown(1)) // right click
Autocomplete();
}
/// <summary>
/// Moves allowed cards to the goal cells.
/// </summary>
/// <param name="onlySafe">If true it won't move cards that you still might need to place other cards on.</param>
public void Autocomplete(bool onlySafe = false)
{
if (!isAutocompleteRunning)
StartCoroutine(CheckForFoundationMoves(onlySafe));
}
#endregion
/// <summary>
/// Force the game to be won. Call this directly to test the win functionality.
/// </summary>
public void Win()
{
HasWon = true;
GetComponent<AudioSource>().Play();
StartCoroutine(DoWinAnimation());
if (OnWin != null)
OnWin();
}
public void ClearStaircases()
{
Sstaircase.Clear();
Wstaircase.Clear();
Nstaircase.Clear();
Estaircase.Clear();
}
public void ClearHands()
{
ShandCards.Clear();
WhandCards.Clear();
NhandCards.Clear();
EhandCards.Clear();
}
public void ClearJunk()
{
Sjunk.Clear();
Wjunk.Clear();
Njunk.Clear();
Ejunk.Clear();
}
public void ClearPlayCells()
{
SplayCell.Clear();
WplayCell.Clear();
NplayCell.Clear();
EplayCell.Clear();
}
/// <summary>
/// Sets up the game and creates a new deal based on the given seed.
/// </summary>
public IEnumerator NewGame(int seed)
{
yield return new WaitForEndOfFrame(); // to give itween a chance to avoid it's initalization bug.
GetComponent<AudioSource>().Play();
HasWon = false;
foreach (var card in FindObjectsOfType<Card>()) {
Destroy(card.gameObject);
}
foreach (var deck in FindObjectsOfType<Deck>()) {
if (deck != Dealer)
Destroy(deck.gameObject);
}
GameBehavior.Cards.Clear();
Moves = new Stack<Move>();
redoMoves = new Stack<Move>();
ClearStaircases();
for (int i = 0; i < Cells; i++)
{
CreateFreeCell();
}
GoalCells.Clear();
var startPositionX = 0.625f + 0.05f;
var xSpacing = Suits <= 6 ? 1.24f : 6.2f / (Suits - 1);
for (int i = 0; i < Suits; i++)
{
var deck = (Instantiate(GameBehavior.Instance.DeckPrefab) as GameObject).GetComponent<Deck>();
deck.CardSpacerY = 0.001f;
deck.MaxCardsSpace = 15;
deck.transform.position = new Vector3(startPositionX + (i * xSpacing), 3.3f, 0);
deck.transform.parent = transform;
deck.Type = DeckType.Goal;
deck.Index = i;
GoalCells.Add(deck);
}
PlayCells.Clear();
startPositionX = ((Stacks - 1) / 2f) * -1.24f;
for (int i = 0; i < Stacks; i++)
{
var deck = (Instantiate(GameBehavior.Instance.DeckPrefab) as GameObject).GetComponent<Deck>();
deck.enabled = true;
deck.CardSpacerY = PlayStackVerticalSpace;
deck.MaxCardsSpace = 13;
deck.transform.position = new Vector3(startPositionX + (i * 1.24f), 1.6f, 0);
deck.transform.parent = transform;
deck.Type = DeckType.Play;
PlayCells.Add(deck);
}
Dealer.CreateCards(Suits);
var random = new System.Random(seed);
Dealer.Shuffle(3, random);
Dealer.FlipAllCards();
Dealer.CardSpacerX = 0;
Dealer.MaxCardsSpace = 20;
foreach (var card in Dealer.Cards)
{
card.Visible = true;
}
var count = 0;
while (Dealer.Cards.Count > 0)
{
Dealer.Draw(PlayCells[count % PlayCells.Count], 1);
count++;
}
if (Difficulty < 0)
{
for (int i = 0; i < -Difficulty * Suits; i++)
{
// find all aces thru threes that aren't on top of deck already
var potentialCards = (from c in GameBehavior.Cards where c.Number <= 3 && c.Deck.TopCard != c select c).ToArray();
if (potentialCards.Length > 0)
{
var card = potentialCards[random.Next(0, potentialCards.Length)];
//print("Pushing up " + card.ToString());
card.SetDeck(card.Deck.Cards.IndexOf(card) + 1);
}
}
}
else
{
for (int i = 0; i < Difficulty * Suits; i++)
{
// find all aces thru threes that aren't on bottom of deck already
var potentialCards = (from c in GameBehavior.Cards where c.Number <= 3 && c.Deck.BottomCard != c select c).ToArray();
if (potentialCards.Length > 0)
{
var card = potentialCards[random.Next(0, potentialCards.Length)];
//print("Pushing down " + card.ToString());
card.SetDeck(card.Deck.Cards.IndexOf(card) - 1);
}
}
}
CalculateDragableCards();
StartTime = Time.time;
}
/// <summary>
/// Creates a new free cell deck.
/// </summary>
/// <returns>The created deck.</returns>
public Deck CreateFreeCell()
{
if (FreeCellBehavior.Instance.FreeCells.Count >= FreeCellBehavior.MaxFreeCells)
return null;
var deck = (Instantiate(GameBehavior.Instance.DeckPrefab) as GameObject).GetComponent<Deck>();
deck.CardSpacerY = 0.001f;
deck.MaxCardsSpace = 15;
deck.transform.parent = transform;
deck.Type = DeckType.Free;
deck.GetComponent<SpriteRenderer>().sprite = GameBehavior.Instance.CardSprites[53];
FreeCells.Add(deck);
LayoutFreeCells();
return deck;
}
void LayoutFreeCells()
{
var startPositionX = 0.625f - 0.05f - 1.25f;
var count = 0;
foreach (var deck in FreeCells)
{
deck.transform.position = new Vector3(startPositionX - (count * 1.24f), 3.3f, 0);
count++;
}
}
/// <summary>
/// Counts the number of availble slots to place cards.
/// </summary>
int CountEmpty(List<Deck> decks)
{
var result = 0;
foreach (var deck in decks)
{
if (deck.Cards.Count == 0)
result++;
}
return result;
}
/// <summary>
/// Calculates how many cards can be dragged at once.
/// </summary>
int MovableStackLimit(bool isTargetEmptyPlayCell)
{
// source http://www.solitairecentral.com/articles/FreecellPowerMovesExplained.html
return (1 + CountEmpty(FreeCells)) * (int)Math.Pow(2, CountEmpty(PlayCells) - (isTargetEmptyPlayCell ? 1 : 0));
}
/// <summary>
/// Most of the freecell logic is actually implemented here. This method restricts what cards can be dropped on each deck.
/// </summary>
/// <returns>True if the card was successfully dragged, false if the target deck is not a valid move.</returns>
public bool CardDrag(Card card, Deck targetDeck)
{
if (card.Deck != targetDeck)
{
var isSingleCard = card.Deck.TopCard == card;
// To Free Cells
foreach (var deck in FreeCells)
{
if (deck == targetDeck)
{
if (isSingleCard && targetDeck.Cards.Count == 0)
{
DoMove(Move.Single(card, targetDeck));
return true;
}
}
}
// To Play Cells
foreach (var deck in PlayCells)
{
if (deck == targetDeck)
{
if (targetDeck.Cards.Count == 0 || (targetDeck.TopCard.Color != card.Color && card.Number + 1 == targetDeck.TopCard.Number))
{
if (DoStackMove(card, targetDeck))
return true;
}
}
}
// To Goal Cells
foreach (var deck in GoalCells)
{
if (deck == targetDeck)
{
if (isSingleCard)
{
if (targetDeck.Cards.Count == 0)
{
if (card.Rank == CardRank.Ace)
{
DoMove(Move.Single(card, targetDeck));
return true;
}
}
else
{
if (targetDeck.TopCard.Suit == card.Suit && card.Number - 1 == targetDeck.TopCard.Number)
{
DoMove(Move.Single(card, targetDeck));
return true;
}
}
}
}
}
}
return false;
}
bool DoStackMove(Card card, Deck targetDeck)
{
var stackSize = card.Deck.Cards.Count - card.Deck.Cards.IndexOf(card);
var limit = MovableStackLimit(targetDeck.Cards.Count == 0);
if (stackSize <= limit)
{
var move = Move.Stack(card.Deck, targetDeck);
for (int i = card.Deck.Cards.IndexOf(card); i < card.Deck.Cards.Count; i++)
move.Cards.Add(card.Deck.Cards[i]);
DoMove(move);
return true;
}
return false;
}
/// <summary>
/// Actually performs a move. All moves should be called thru this to maintain behavior and undo.
/// </summary>
public void DoMove(Move move)
{
move.Execute();
Moves.Push(move);
if (!HasWon)
{
CalculateDragableCards();
if (!CheckWin())
Autocomplete(true);
}
}
/// <summary>
/// Called when a card is double clicked on.
///
/// If it can be moved to a goal deck it will be moved there. Other wise it will move to a free cell or an empty play cell.
/// </summary>
/// <param name="card"></param>
public void OnDoubleClick(Card card)
{
if (card.IsDragable)
{
if (card.Deck.TopCard == card)
{
// see if it can go on a goal deck
if (card.Rank == CardRank.Ace)
{
foreach (var goalDeck in GoalCells)
{
if (!goalDeck.HasCards)
{
DoMove(Move.Single(card, goalDeck));
return;
}
}
}
else
{
foreach (var goalDeck in GoalCells)
{
if (goalDeck.HasCards && goalDeck.TopCard.Suit == card.Suit && goalDeck.TopCard.Number == card.Number - 1)
{
DoMove(Move.Single(card, goalDeck));
return;
}
}
}
}
// then see if it can go on a play deck with cards
foreach (var deck in PlayCells)
{
if (deck != card.Deck &&
deck.HasCards &&
deck.TopCard.Number == card.Number + 1 &&
deck.TopCard.Color != card.Color)
{
if (DoStackMove(card, deck))
return;
}
}
if (card.Deck.TopCard == card && card.Deck.Type != DeckType.Free)
{
// then see if it can go on a free cell
foreach (var deck in FreeCells)
{
if (!deck.HasCards)
{
DoMove(Move.Single(card, deck));
return;
}
}
}
// then see if it can go on an empty play deck
foreach (var deck in PlayCells)
{
if (!deck.HasCards)
{
if (DoStackMove(card, deck))
return;
}
}
}
}
/// <summary>
/// Check to see if the game has been won.
/// </summary>
/// <returns>True if all the cards are in the goal cells.</returns>
bool CheckWin()
{
foreach (var deck in GoalCells)
{
if (deck.Cards.Count < 13)
return false;
}
Win();
return true;
}
/// <summary>
/// Plays the win animation.
/// </summary>
IEnumerator DoWinAnimation()
{
yield return new WaitForSeconds(0.5f);
redoMoves.Clear();
while (HasWon)
{
if (isRedoing)
{
if (redoMoves.Count > 0)
Redo();
else
isRedoing = false;
}
else
{
if (Moves.Count > 0)
Undo();
else
isRedoing = true;
}
yield return new WaitForSeconds(0.15f);
}
}
/// <summary>
/// Check for correct running cards and adjust dragable cards and checks for completed full stacks
/// </summary>
protected void CalculateDragableCards()
{
var maxMovableStackLimit = MovableStackLimit(false);
//print(maxMovableStackLimit);
//Loop on each stack
foreach (var playCell in PlayCells)
{
//Loop on each card from bottom up, and make it enabled until a wrong card placement or an invisible card occur
var correctOrder = true;
var correctCount = 0;
for (int j = playCell.Cards.Count - 1; j >= 0; j--)
{
playCell.Cards[j].IsDragable = correctOrder;
if (OnCardDragableUpdate != null)
{
OnCardDragableUpdate(playCell.Cards[j], correctOrder);
}
if (correctOrder) //If we still in a correct order state check for the next card
{
if ((j != 0) &&
(
(playCell.Cards[j - 1].Visible == false) ||
(IsWrongPlacement(playCell.Cards[j], playCell.Cards[j - 1])) ||
correctCount >= maxMovableStackLimit - 1
))
{
correctOrder = false;
}
correctCount++;
}
}
}
// Free cells and goal cells are draggable too (cancel attacks)
foreach (var freecell in FreeCells)
{
if (freecell.TopCard != null)
{
freecell.TopCard.IsDragable = true;
if (OnCardDragableUpdate != null)
OnCardDragableUpdate(freecell.TopCard, true);
}
}
foreach (var goalCell in GoalCells)
{
if (goalCell.TopCard != null)
{
goalCell.TopCard.IsDragable = true;
if (OnCardDragableUpdate != null)
OnCardDragableUpdate(goalCell.TopCard, true);
}
}
}
/// <summary>
/// Return false if the two cards are not in the same suit and in ordered numbers
/// </summary>
/// <param name="card1"></param>
/// <param name="card2"></param>
/// <returns></returns>
public bool IsWrongPlacement(Card card1, Card card2)
{
if ((card1.Color != card2.Color) && (card1.Number + 1 == card2.Number))
return false;
else
return true;
}
/// <summary>
/// Undoes the last move.
/// </summary>
public void Undo()
{
if (Moves.Count > 0)
{
var move = Moves.Pop();
move.Undo();
CalculateDragableCards();
redoMoves.Push(move);
}
}
/// <summary>
/// Used by the win animation to replay the game moves that have been undone.
/// </summary>
void Redo()
{
if (redoMoves.Count > 0)
{
var move = redoMoves.Pop();
DoMove(move);
}
}
/// <summary>
/// Check for and move all cards that can go to the goal decks.
/// </summary>
IEnumerator CheckForFoundationMoves(bool onlySafe)
{
isAutocompleteRunning = true;
var recurse = false;
foreach (var deck in FreeCells)
{
if (CheckDeck(deck, onlySafe))
{
yield return new WaitForSeconds(0.1f);
recurse = true;
}
}
foreach (var deck in PlayCells)
{
if (CheckDeck(deck, onlySafe))
{
yield return new WaitForSeconds(0.1f);
recurse = true;
}
}
if (recurse && !HasWon)
StartCoroutine(CheckForFoundationMoves(onlySafe));
else
isAutocompleteRunning = false;
}
/// <summary>
/// Check a deck for cards that can go to a goal deck and move them.
/// </summary>
bool CheckDeck(Deck deck, bool onlySafe)
{
if (deck.HasCards)
{
var cardToMove = deck.TopCard;
var goalDeck = GetGoalDeckForCard(cardToMove);
if (goalDeck != null)
{
if (!onlySafe || IsCardSafeToMove(cardToMove))
{
DoMove(Move.Single(deck.TopCard, goalDeck));
return true;
}
}
}
return false;
}
bool IsCardSafeToMove(Card cardToMove)
{
// Check for cards of opposite color and rank one lower
var rank = (CardRank)cardToMove.Rank - 1;
var color = cardToMove.Color == CardColor.Black ? CardColor.Red : CardColor.Black;
foreach (var deck in PlayCells)
{
if (deck.Has(rank, color))
return false;
}
foreach (var cell in FreeCells)
{
if (cell.Has(rank, color))
return false;
}
return true;
}
/// <summary>
/// Finds the goal deck that a card will go to.
/// </summary>
public Deck GetGoalDeckForCard(Card card)
{
foreach (var goalDeck in GoalCells)
{
if (!goalDeck.HasCards && card.Rank == CardRank.Ace)
return goalDeck;
if (goalDeck.HasCards && goalDeck.TopCard.Suit == card.Suit && goalDeck.TopCard.Number == card.Number - 1)
return goalDeck;
}
return null;
}
}
}