Coding Challenge: Best Have a Card Up Your Sleeve

Our newest coding challenge runs through January, ending on Jan. 31, 2014, so you should have plenty of time for it. It’s about a card game called Sevens.

hidden seven cardsThe game is for three to seven players. To set it up, deal all 52 cards between the players. Depending on the number playing, and whether or not 52 is divisible by that, they may not all get the same number of cards: If there are three players, the first player ends up with 18 cards and the other two players get 17 each. If there are four players, they receive 13 cards each, etc.

Whichever player has the seven of diamonds goes first, placing the card on the table, face up. (Cards are always placed face up.) The next player can:

  • Play a card that’s of the same suit and one number lower than the lowest number that’s been played in that suit.
  • Play a card of the same suit and one number higher than the highest that’s been played in that suit.
  • Play a seven.

So next to the seven of diamonds, a player could place a six or eight of diamonds, or another seven.

Say the six of diamonds is played, then the next player can play the five or eight of diamonds, or a seven. If you don’t have a playable card then you pass and it becomes the next player’s turn. However, if you have a card that’s playable, you have to play it. When another seven is played, it’s next highest and next lowest cards become additional options for the next player. So if the seven of spades is played after the six of diamonds, the following player can play the six or eight of spades, the five or eight of diamonds or another seven.

The winner of the game is the player who gets rid of all of their cards first. kings and aces are bad cards because all the cards between them and the seven must be played before them. The skill in the game is in holding onto specific cards to keep you opponents from playing their end cards (kings or aces) before you.

As a Programming Contest

You must write a program in C#, C++ or Java to play a card on your turn. I’ve written a match play program that keeps track of the cards in play and the cards in each hand. It runs each of the programs in a game one by one for that player’s turn.

A player’s program is only called if he or she has a playable card. (Otherwise the player is skipped.) Your program should read a text file called input.txt that’s in the same location as the compiled exe. The file will contain five lines of text set up as follows:

  • Line 1: The cards in your hand
  • Lines 2 through 5: The cards that have been played, one line per suit

The cards in your hand run from ace (low), two, three up to 10, jack, queen and king. Two characters (VS) are used to represent your cards: V is value (A,2,3,4,5,6,7,8,9,T,J,Q,or K) and S is suit (C,D,H or S). So 3D stands for the three of diamonds and TS stands for the 10 of spades.

In a four player game, the first line of the input file on a player’s first turn has 13 VS values with a space between each pair. For example:

4H 5H 7D AC QH 9D 3S JH KC 7C KH 8D QD

Each of the four suit lines starts with a suit letter (C,D,H and S) followed by zero, one or two card values. If there are no values then it means the seven hasn’t been played for that suit. If there’s a single value then it will be the seven. And if there are two values, they are the lowest and highest cards that have been played in that suit. For example, the four suit lines could be:





In that example, the seven of diamonds has been played as well as all the hearts from four up to nine (ie four, five, six, seven, eight and nine). No spades or clubs have been played.

Your Move

Your program must write the card, represented by its VS characters, into the file move.txt, which should be in the same location as your exe. If the card written is not in your hand then the computer will play a random playable card from your hand. If you played 8D, the next player would receive the board as this:





Much later in the game, the board might be:





In that example, the clubs from three to 10 have been played and all spades are on the table. So the playable cards would be 2C, JC, 6D, 9D, 3H and TH.

Remember, your program won’t be called if you have no playable cards. It just skips your turn.

Played Games

Once a week, on Sunday, I’ll run a number of games. Each game will have three or four players, picked at random. I’ll aim to have as many games of four as possible. So with 10 players, there would be a game of four and two games of three. With 11 players, there would be two games of four and a game of three. With 15 players there would be three four-player games and one three-player game.

The players will go around, taking turns as if playing with cards. If your program wins a game then you get a point. Each player will play as many games as there are players — so In a week with ten players you could score a maximum of 10 points. Have your program ready for Sunday, Jan. 5, when the first round of games is run, for the largest amount of possible points.

Please submit your entry zipped up as an attachment to this email address and please don’t include any executable binaries such as exes or dlls.

Submitting Code and Compiling

The competition PC is an i7 950 with four cores doubled to eight by hyper threading and 24 GB of ram. You are allowed to use multithreading/multitasking etc.

I’ll use Visual Studio 2012 for compiling C++ and C#. Please include all the project and solution files such as .suo, so I can open the solution and build it. Code built with earlier versions (VS 2003, VS 2005, VS 2008 and VS 2010) is acceptable.

If you use GCC/G++, please let me know and include your preferred command line to optimize it. I use MinGW to build that on my Windows PC. All programs are compiled in Release mode, so please specify any optimization flags for GCC/G++ or specify them in the solution.

For Java, I have Java 7 installed on my PC. If you use any third-party open source libraries that need to be installed, please let me know. I try to compile/run them using javac but I can build projects in Eclipse if needed.

The prize is glory and a Dice USB wall adapter. At the end of the competition we’ll publish a discussion of the algorithms used and highlight a winner. Be sure to include your name and website in the source code comments. By entering this contest you agree to allow Dice to publish your source code with credit to you as the copyright owner.

Submitting Newer Entries

It’s acceptable to submit newer versions throughout the month of January, right up until the deadline. If you’ve sent in several, then the most recent will be used.

The Results

We’ll publish a few updates throughout the competition, and will post the final results shortly after the competition ends.


16 Responses to “Coding Challenge: Best Have a Card Up Your Sleeve”

    • Richard Vasquez

      My take on this is that there’s no current suit, per se. The only time I see it being relevant is if it’s your turn and you have the 7D in your hand, thus opening up the game.

      After that, 3 other players could build up on diamonds, and when it’s your turn, the only valid card you have to play is the 7H, so you break hearts that round.

      • @Richard I think u got my point but to elaborate a bit consider the example taken above: Somewhere in the middle of game the board is like
        Now, I have cards 9D, 3H and 7C. Obviously 7C is ruled out but what abt the other two card. How will i know if the suit is diamond or heart?

        • Richard Vasquez

          Up at the top of this contest, there’s a link to the Wikipedia listing of the rules; if you don’t want to scroll up.

          Given your scenario, you would be able to play any one of your set of 9D, 3H, 7C. If someone played 8D, 4H, or 9H right before you, it doesn’t dictate what you _have_ to play. Only your valid cards determine that. You could open up clubs with the 7C. You could expand Diamonds or Hearts.

          I’m wondering if underlying the question is “How do I know what the previous players played right before me?”

          For this particular contest, I’d say the answer is, you don’t. I’m coding my player (in part) to take in a specified universe each time it’s invoked. There exists ‘n’ cards in my hand. There exists 4 sets of cards for each suit that have 0-13 cards in them. What am I going to do with these ‘n’ cards? Rinse/Lather/Repeat.

  1. Teresa Carrigan

    For the factorial coding challenge, you had requested that Java entries not use package names. Are there any similar rules for this one? Specifically, do I need to cram everything into a single .java file or are multiple classes fine? And thanks for the new challenge – I enjoyed doing the last one very much.

  2. In your starting example above you show a hand that has a 7D but you show in the provided board a D78. This makes me wonder if I’m responsible for removing the already played cards from my hand prior to determining my play. Question: Is your program only going to provide an input.txt file with the cards left in my hand along with what is on the deck or is it my responsibility to remove already played cards from my hand?

  3. James Hall

    I don’t think this was addressed. Our programs read from input.txt and write to move.txt, can we save state information at all? Can I write to a file like abc123.txt and save information on what I’ve gleaned from each round?

      • Richard Vasquez

        I tend to agree. I’m not sure I see a _strong_ advantage in it for various reasons I’ll get into after the contest ends (not revealing my strategy past that), but since we’re writing files anyway, I don’t see why one couldn’t write out their “brain” state.

        Of course, if I was evil, I’d go and either have a map of initial files for each player directory, or a restore directory and ensure the directory only had the specified jar/exe/dll files each turn. 😀

  4. Teresa Carrigan

    I’m eager to see the preliminary results. While I think my strategy is solid, there’s going to be a lot of random chance involved. Some hands are impossible to win (4 kings and 4 aces) and some are impossible to lose (all the 7s, 8s, 6s, and enough 5s and 9s to fill out your hand). I’m really looking forward to seeing a discussion of the various strategies used.

      • Richard Vasquez

        Ayup. During my testing, I had various dummy players to help move the game along; one of them being “Just pick a random valid card” and that would end up ruining my submitted player’s beautiful strategy.

        I just wish I’d had more time for my initial submission. Ah, well. I’ll see if I can improve things this weekend since we’ve got to the end of the month.