Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Looking for "card playing" algorithm

Looking for "card playing" algorithm

Scheduled Pinned Locked Moved Algorithms
game-devalgorithmsquestion
6 Posts 5 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Crow
    wrote on last edited by
    #1

    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
    tie

    But 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

    R P L 3 Replies Last reply
    0
    • D David Crow

      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
      tie

      But 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

      R Offline
      R Offline
      Ralf Meier
      wrote on last edited by
      #2

      It sounds for me you need something like a sorting algorhythm ... Now you can sort the Players according to their points ...

      1 Reply Last reply
      0
      • D David Crow

        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
        tie

        But 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

        P Offline
        P Offline
        Peter_in_2780
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • D David Crow

          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
          tie

          But 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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • P Peter_in_2780

            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

            J Offline
            J Offline
            jsc42
            wrote on last edited by
            #5

            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

            P 1 Reply Last reply
            0
            • J jsc42

              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

              P Offline
              P Offline
              Peter_in_2780
              wrote on last edited by
              #6

              Give 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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups