CPE 103 Lab Activity - Chapter 5


Accordion Solitaire is a simple card game played with a standard 52-card deck.

To start, the entire deck of 52 cards is shuffled and dealt on the table in a single row.

To make a move, a card can be placed on top of the card immediately to its left, or a card three spots to the left, if the cards match in suit or rank.

When a card is moved, any cards it previously covered are moved along with it. Once a card is covered, it cannot be uncovered.

When there are no valid moves left, the player's score is the number of cards remaining on the table.  

For example, here is an initial table configuration.  The cards are displayed in the first row, and the second row contains labels for the card positions.

HK H7 SQ S5 D7 CT S4 C3 S6 HQ S9 H8 CQ C1 D8 HT S3 H5 H1 S2 D3 H3 D2 H6 CJ D5 ST D1 H9 DT C9 DQ CK S7 C6 C5 H2 C7 H4 SK DJ SJ D9 C4 C8 C2 S8 DK D6 S1 D4 HJ
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z


A player move is designated with a 2-character string.  The first character is the card position, and the second character is either '1' or '3' for the number of spots to move.

Thus, from the starting arrangement above, b1, s1, g3, v1, d1, T1 would each be an example of a valid move.  d3 is an invalid move.  (Incorrectly formatted moves are also invalid, such as 1d.)

Part 1


Create an Accordion class. Write a method that finds all the valid moves in a given table configuration.
The method signature is:
    public String findValidMoves(Card[] table, int tableLength)

The Card class is provided in Enums Lab. The instructor solved the entire lab without altering the Card class, but you may modify it if you wish. However, if you modify it, you must write JUnit tests for it.

The return value is a string containing the valid 2-character moves separated by one blank space.

For example, given this table:
HK H7 SQ SK
the method could return the string "b1 d1 d3"

You must write a JUnit test of your method.
Don't use Lists in your solution.


Part 2

Write a method for the Accordion class that simulates playing a complete game.
The method signature is:
    public Card[] play(Card[] table, String moveSequence)

table is an array of length 1 - 52 containing some arrangement of Cards.
moveSequence is a sequence of moves in the order they are to be played, separated by one blank space.  Invalid moves should be ignored.
The method returns the cards left on the table after all the moves are made.  The length of the returned array must equal the number of cards.

You must write a JUnit test of your method.
Don't use Lists in your solution.
You may use or modify the code from the Card and Deck classes in the Enums Lab.
Tip: System.arraycopy() is your friend.


Submission

Submit printouts of all your source code files, your JUnit tests, and a sample test execution.