Looking for "card playing" algorithm
-
In a card game involving 2-4 computer players, does an algorithm exist for determining which of the player(s) has the highest card (e.g., 2 < 3 <...< K < A)? With two players, it's simply:
if (player1 > player2)
player1 wins
else if (player2 > player1)
player2 wins
else
tieBut that does not scale well at all for 3 or 4 players. When I tried something for 3 players, it was ugly and nearly unmanageable. 4 players was not even attempted. Thanks. DC
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
In a card game involving 2-4 computer players, does an algorithm exist for determining which of the player(s) has the highest card (e.g., 2 < 3 <...< K < A)? With two players, it's simply:
if (player1 > player2)
player1 wins
else if (player2 > player1)
player2 wins
else
tieBut that does not scale well at all for 3 or 4 players. When I tried something for 3 players, it was ugly and nearly unmanageable. 4 players was not even attempted. Thanks. DC
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
It sounds for me you need something like a sorting algorhythm ... Now you can sort the Players according to their points ...
-
In a card game involving 2-4 computer players, does an algorithm exist for determining which of the player(s) has the highest card (e.g., 2 < 3 <...< K < A)? With two players, it's simply:
if (player1 > player2)
player1 wins
else if (player2 > player1)
player2 wins
else
tieBut that does not scale well at all for 3 or 4 players. When I tried something for 3 players, it was ugly and nearly unmanageable. 4 players was not even attempted. Thanks. DC
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
You don't need a full sort. It would work, but it's overkill. Essentially you have an array (or list, or collection of some sort) of card values, indexed by player. The standard linear-time algorithm for finding the max or min of an array and remembering where you saw it...
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
In a card game involving 2-4 computer players, does an algorithm exist for determining which of the player(s) has the highest card (e.g., 2 < 3 <...< K < A)? With two players, it's simply:
if (player1 > player2)
player1 wins
else if (player2 > player1)
player2 wins
else
tieBut that does not scale well at all for 3 or 4 players. When I tried something for 3 players, it was ugly and nearly unmanageable. 4 players was not even attempted. Thanks. DC
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
You have a "list" of players. The list is always processed in order; so the "first" element in the list should be the the player that plays first, etc. After the last player, you cycle back to the first. A "pass" just bumps the player index. I wrote a GO game on that basis; for 2 to 7 players; humans and bots. 7 was an arbitrary limit (The Warring States). The "top" player is the one with the .Max something; unless it's tied.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
-
You don't need a full sort. It would work, but it's overkill. Essentially you have an array (or list, or collection of some sort) of card values, indexed by player. The standard linear-time algorithm for finding the max or min of an array and remembering where you saw it...
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
Very roughly in pseudo-code for traditional languages (no particular language)
nPlayers = 7; // Or however many players there are
int cardForPlayer[nPlayers] = (cards for each of the players);
int bestPlayerNo = 1;
int bestScore = cardForPlayer[0]; // [0] is location of 1st player's card
for (int playerIndex = 1; playerIndex++; playerIndex < nPlayers) // playerIndex is player no - 1. Start at 1 (player 2) as we have already got player 1 as best found thus far
if (cardForPlayer[playerIndex] > bestScore)
{
bestScore = cardForPlayer[playerIndex];
bestPlayerNo = playerIndex + 1;
}For a simple functional style language (no particular language)
cardForPlayer = List p1card, p2card, ..., p7card
bestScore = max cardForPlayer
bestPlayerNo = (FindIndex cardForPlayer bestScore) + 1 -
Very roughly in pseudo-code for traditional languages (no particular language)
nPlayers = 7; // Or however many players there are
int cardForPlayer[nPlayers] = (cards for each of the players);
int bestPlayerNo = 1;
int bestScore = cardForPlayer[0]; // [0] is location of 1st player's card
for (int playerIndex = 1; playerIndex++; playerIndex < nPlayers) // playerIndex is player no - 1. Start at 1 (player 2) as we have already got player 1 as best found thus far
if (cardForPlayer[playerIndex] > bestScore)
{
bestScore = cardForPlayer[playerIndex];
bestPlayerNo = playerIndex + 1;
}For a simple functional style language (no particular language)
cardForPlayer = List p1card, p2card, ..., p7card
bestScore = max cardForPlayer
bestPlayerNo = (FindIndex cardForPlayer bestScore) + 1Give OP's seniority and rep, I didn't condescend to spelling out the algorithm. I've lost track of the number of times and different languages I've implemented it... There is a useful variant where you start with a sentinel value ("minus infinity" or whatever) and compare every one including the first. This handles the case where there is no item that qualifies for selection. In this example, it would be easy to skip a player if he has no card. What if nobody had any cards?
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012