AntonZ
AntonZ

Reputation: 83

Shifting order list for playing card game

I am working on a Windows Form C# virtual playing card game, there are 10 players, and they all take it in turn to act. After each round, the person who acts first rotates as the dealer rotates (as with most playing card games the first player to act is to the left of the person who dealt).

As players can sit out after any round, my plan was to use a list to store the current active players (controlled by a check box for each player).

I want to use this list to determine which players need to make a move and in what order.

I keep track of which player is the dealer as seperate variable, but I am unsure on how I would correctly sort the list, or correctly sort the contents of the list based on who is currently the dealer.

An example game scenario is below.

List of active players {1,2,3,5,8,9,10}

In round one Player 1 would be the dealer, so the order of play would be 2 > 3 > 5 > 8 > 9 > 10 > 1.

However assuming no players leave the game, the order for round 3 would be 5 > 8 > 9 > 10 > 1 > 2 > 3.

Any advice on to achieve this would be greatly appreciated.

My initial thoughts were that at the end of each round, I could re-order the list but I am not sure if this is the right route or not.

Upvotes: 0

Views: 323

Answers (2)

Tomasz Radwaski
Tomasz Radwaski

Reputation: 202

I think a good solution would be to use queue in this situation, since instead of iterating through array to get next player you can simply use Queue.dequeue() to get next player.

asuming you store your active players Array and dealer like this:

    int[] activePlayers = { 1, 2, 3, 5, 8, 9, 10 };
    int dealer = 1;

I've made a simple method for you (well, not so simple, spaghetti code in fact but it works well).

        private Queue<int> getNextRoundQueue(int lastRoundDealer) {
        Queue<int> roundQueue = new Queue<int>();
        int indexOfDealer = -1;
        //if last round dealer is still active player
        if(activePlayers.Contains(lastRoundDealer)) {
            indexOfDealer = Array.IndexOf(activePlayers, lastRoundDealer);
        }
        //if last round dealer left get next player as a dealer
        //side note: there's gotta be a better way of doing that, but nothing came me to mind right now
        else {
            indexOfDealer = -1;
            for(int i = 0; i < activePlayers.Length; i++) {
                if(activePlayers[i] > lastRoundDealer) {
                    indexOfDealer = i;
                    break;
                }
            }
            if(indexOfDealer == -1) indexOfDealer = 0;
        }
        //create the queue
        //add dealer as first item into the queue
        roundQueue.Enqueue(activePlayers[indexOfDealer < activePlayers.Length ? indexOfDealer : indexOfDealer - activePlayers.Length]);
        for(int i = 0; i < activePlayers.Length - 1; i++) {
            //add next player to the queue
            roundQueue.Enqueue(activePlayers[indexOfDealer + i + 1 < activePlayers.Length ? indexOfDealer + i +  1: (indexOfDealer + i + 1) - activePlayers.Length]);
        }
        return roundQueue;
    }

It will prepare queue for next round for you, to use it you simply do:

        //you get the queue using my method, you pass last round dealer as argument
        Queue<int> roundQ = getNextRoundQueue(dealer);
        //first element of the queue is the dealer of next round
        dealer = roundQ.Dequeue();
        //while there are still players in the queue
        while(roundQ.Count > 0) {
            //get current player
            int currentPlayer = roundQ.Dequeue();
            // do something with him
        }

Upvotes: 0

Mong Zhu
Mong Zhu

Reputation: 23732

To rotate the entities in a list you would basically remove the first and add him at the end. Here is a little program to elucidate this procedure.

static void Main(string[] args)
{
    List<int> players = new List<int> { 1, 2, 3, 5, 8, 9, 10 };

    for (int i = 1; i < 5; i++)
    {
        Console.WriteLine("Round Nr: " + i);
        //rotate the List, shift dealer from first to last position
        int dealer = players.First();
        players.Remove(dealer);
        players.Add(dealer);

        Console.WriteLine("order of moving:");
        Console.WriteLine(String.Join(" > ", players));
    }

    Console.ReadKey();
}

Upvotes: 0

Related Questions