Question
JAVAIn a card game of Pairs the goal is to turn over pairs of matching cards. https://en.wikipedia.org/wiki/Concentration_(game) Here are the rules for the variation of
JAVAIn a card game of Pairs the goal is to turn over pairs of matching cards.
https://en.wikipedia.org/wiki/Concentration_(game) Here are the rules for the variation of Pairs we consider. At the start of the game, there are n cards face-down, where n is a multiple of 4. There are 4 cards of each type, and the cards are labeled with letters a, b, . . . . For example, if n==24, there are 6 types of cards: a, b, c, d, e, and f. Say 1*4<=n and n<=4*26. At each turn, the player flips 2 cards, one at a time, that are face-down. If the 2 flips are of the same type, the matched cards are left face-up. If the 2 flips mismatch, the mismatched cards are returned to the face-down position. The game ends when all cards are matched, and the score is the total number of flips made. (Flipping a pair of cards counts as 2 flips, so the best possible score is n flips.)
Write a pubic class titled MatchCardGame with the following members that implement this game of Pairs. MatchCardGame should have no other public fields, methods, and constructors aside from the ones specified. However, it may and should have additional private fields, methods, or constructors.
The field
public final int n ;
is the size of the game set by the constructor.
***********************************************************************************
The constructor
public MatchCardGame (int n );
initializes a card game with a total of n cards. Assume n is a multiple of 4 and that 4<=n && n<=4*26. Without shuffling (explained in Problem 2) cards 0,1,2, and 3 should be a, cards 4,5,6, and 7 should be b, and so on.
*********************************************************************
The method
public String boardToString ();
converts the state of the board to an appropriate String representation. You have freedom to choose your own representation, but it must reasonably represent the state of the game.
*********************************************************************
The method
public boolean flip (int i );
plays card number i. If card i cannot be played because its face-up, or if i is an invalid card number, then return false. If i is a card number that can be played, play card i and return true.
******************************************************************
The method
public boolean wasMatch ();
returns true if the previous pair was a match and returns false otherwise. This method should be called only after flip has been called an even number of times and before flipMismatch is called.
******************************************************************
The method
public char previousFlipIdentity ();
returns the face of the previously flipped card as a char. This method should only be called after a card has been flipped.
******************************************************************
The method
public void flipMismatch ();
reverts the a mismatched pair to face-down position. This method should only be called after a 2 calls of flip results in a mismatch.
******************************************************************
The method
public boolean gameOver ();
returns true if all cards have been matched and the game is over and returns false otherwise.
******************************************************************
The method
public int getFlips ();
returns the total number of card flips that have been performed so far.
******************************************************************
Remark. MatchCardGame represents the physical state of the game, not the player. MatchCardGame has nothing to do with game strategies a human or AI player might employ. Remark. The problem specifies how these methods should be used. For example, the input n of MatchCardGames constructor needs to be a multiple of 4. You do not have to do any sort of error handling when these requirements are violated, although using asserts will help debugging.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started