Category talk

Shuffle

From Shadow Era Wiki


Code Summary and Detailed Code

FILE REFERENCES IN THE GAME
TERMS TO LOCATE SHUFFLE REFERENCE

GameController.cs: Manages the overall game flow and player actions, including initiating the shuffle process:
`SomeGameAction()`, `GameStateActivate(GameState.GameStateType.mulliganDraw)`.

GameModel.cs: Contains the main logic for managing the deck and card operations, including shuffling:
`public static void ShuffleDeck(int side)`, `public static void OnDeckShuffled(int side)`, `public static void Draw(int side)`, `public static void HandleEmptyDeck(int side)`.

Gameplay.cs: Responsible for gameplay mechanics and player interactions, including shuffling the player's deck:
`public void ShuffleOurDeck()`.

Utility.cs: Provides utility functions for common operations, such as shuffling lists:
`public static void ShuffleList<T>(List<T> list)`.

SpecificCard.cs: Handles card-specific logic and interactions, including callbacks for various game events:
`public void CallbackOn(CallbackOnEnum method, object[] param)`, `OnDeckShuffled(int side)` (relevant for handling shuffle events).

  • Shuffle
  • Deck
  • Draw
  • GameState
  • ShuffleDeck
SUMMARY OF THE SHUFFLE PROCESS:
  1. `SomeGameAction()` :
    Initiates the shuffle process and activates the appropriate game state for the player.
  2. `ShuffleDeck(int side)` :
    Handles the shuffling of the deck for the specified player side.
  3. `OnDeckShuffled(int side)` :
    Manages the event triggered when a deck is shuffled, allowing for necessary game updates and notifications.
  4. `Draw(int side)` :
    Handles the card drawing process for the specified player side, checking if the deck is empty before proceeding.
  5. `HandleEmptyDeck(int side)` :
    Manages the consequences when a player attempts to draw a card from an empty deck, such as inflicting damage to the hero.
  6. `ShuffleList<T>(List<T> list)` :
    Utility function that randomly shuffles the elements of the provided list.
  7. `ShuffleOurDeck()` :
    Calls the shuffle function for the current player's deck during gameplay.
  8. `CallbackOn(CallbackOnEnum method, object[] param)` :
    Handles various game events through callbacks, including actions related to deck shuffling.
DETAILED CODE
This code is a collection of all the functions involved in the shuffle mechanic, though it doesn't exist as a single block in any one file.

The complete shuffle logic is primarily found in GameModel.cs.

// 1. Initiate the shuffle process
// In GameController.cs
public void SomeGameAction()
{
    // Other game logic...
    GameModel.ShuffleDeck(); // Call to shuffle the deck
    GameModel.GameStateActivate(GameState.GameStateType.mulliganDraw); // Activate the mulligan draw state
}
 
// 2. Shuffle the deck
// In GameModel.cs
public static void ShuffleDeck(int side)
{
    List<Card> deck = model.players[side].deck; // Retrieve the cards from the deck
 
    Utility.ShuffleList(deck); // Call to the utility function to shuffle
 
    model.players[side].deck = deck; // Update the shuffled deck
 
    OnDeckShuffled(side); // Call the deck shuffled event
}
 
// 3. Handle the deck shuffled event
// In GameModel.cs
public static void OnDeckShuffled(int side)
{
    // Notify that the deck has been shuffled
    CallbackOn(CallbackOnEnum.OnDeckShuffled, new object[] { side }); // Use a callback to handle the shuffle effect
}
 
// 4. Utility function for shuffling
// In Utility.cs
public static void ShuffleList<T>(List<T> list)
{
    List<T> list2 = new List<T>();
    while (list.Count > 0)
    {
        int num = UnityEngine.Random.Range(0, list.Count);
        list2.Add(list[num]);
        list.RemoveAt(num); // Remove the element from the original list
    }
    for (int i = 0; i < list2.Count; i++)
    {
        list.Add(list2[i]); // Put the shuffled elements back into the original list
    }
}
 
// 5. Drawing cards and checking the deck state
// In GameModel.cs
public static void Draw(int side)
{
    // Check if the deck has cards before drawing
    if (DeckCount(side) == 0) 
    {
        // Handle the case where the deck is empty (e.g., deal damage to the hero)
        HandleEmptyDeck(side);
        return; // Exit the function if the deck is empty
    }
 
    // Logic to draw a card
    ShadowEraCard card = GetDeck(side, DeckCount(side) - 1); // Get the last card from the deck
    AddHand(card); // Add the card to the player's hand
 
    // Notify that the card has been drawn
    OnCardDrawn(side); 
}
 
// 6. Handling the consequences of an empty deck
public static void HandleEmptyDeck(int side)
{
    // Apply consequences, such as dealing damage to the hero
    // Example: model.players[side].hero.TakeDamage(damageAmount);
}
 
// 7. Callback calls
// In SpecificCard.cs
public void CallbackOn(CallbackOnEnum method, object[] param)
{
    switch (method)
    {
        case CallbackOnEnum.OnDeckShuffled:
            OnDeckShuffled((int)param[0]); // Call the appropriate method when the deck is shuffled
            break;
        // Handle other game events...
    }
}
 
// 8. Other calls related to shuffling in gameplay
// In Gameplay.cs
public void ShuffleOurDeck()
{
    GameModel.ShuffleDeck(); // Call to shuffle the current player's deck
}

Mulligan

*Explanation of `GameStateActivate(GameState.GameStateType.mulliganDraw):`

In card games, a "mulligan" is a mechanism that allows players to redraw their initial cards before the game begins. This gives them the chance to improve their hand by exchanging unwanted cards or all cards in hand.
`GameStateType` is an enumeration (enum) that defines different states of the game. Each state represents a specific moment or phase in the course of the game.
`mulliganDraw` is one of these states, indicating that the player is in the mulligan phase, where they the game choose for the player to redraw certain cards.

The logic within this case is responsible for managing the actions that need to occur when the game is in the mulligan state.
This may include operations such as:
Shuffling the deck: Before allowing the game to redraw, the game ensures that the deck is shuffled correctly to guarantee the randomness of the new cards.
Modifying the state of cards: Ensuring that the cards being redrawn are handled correctly within the system.
This does not include:
Updating the user interface: Indicating to the player if they can declare "Mulligan" after the shuffle and allowing the selection of all or some cards to redraw.