Main image of article 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. The 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:

C



D7



H49



S

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:

C



D78



H49



S

Much later in the game, the board might be:

C3T



D78



H49



SAK

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.