Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is the code for PlayCard.java: public class PlayCard { public static void main(String[] args) { //set up reader to take inputs java.util.Scanner reader =

This is the code for PlayCard.java:

public class PlayCard {

public static void main(String[] args) {

//set up reader to take inputs

java.util.Scanner reader = new java.util.Scanner (System.in);

int n = 16; //game size

MatchCardGame g1 = new MatchCardGame(n);

//g1.shuffleCards();

while(!g1.gameOver()) {

//print board status

System.out.println(g1.boardToString());

//ask for a card to flip until we get a valid one

System.out.println("Which card to play?");

while(!g1.flip(reader.nextInt())) {}

//print board status

System.out.println(g1.boardToString());

//ask for a card to flip until we get a valid one

while(!g1.flip(reader.nextInt())) {}

//say whether the 2 cards were a match

if(g1.wasMatch()) {

System.out.println("Was a match!");

} else {

//print board to show mismatched cards

System.out.println(g1.boardToString());

System.out.println("Was not a match.");

//flip back the mismatched cards

g1.flipMismatch();

}

}

//Report the score

System.out.println("The game took " + g1.getFlips() + " flips.");

//Using the AIs

//int count;

//MatchCardGame g2 = new MatchCardGame(n);

//g2.shuffleCards();

//count = playRandom(g2);

//System.out.println("The bad AI took " + count + " flips.");

//MatchCardGame g3 = new MatchCardGame(n);

//g3.shuffleCards();

//count = playGood(g3);

//System.out.println("The good AI took " + count + " flips.");

//Using MCs

//int N = 1000;

//System.out.println("The bad AI took " + randomMC(N) + " flips on average.");

//System.out.println("The good AI took " + goodMC(N) + " flips on average.");

}

}

JAVAIn a card game of Pairs the goal is to turn over pairs of matching cards. Copy the starter code PlayCard.java. It should give you an idea of how MatchCardGame is used.

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

International Baccalaureate Computer Science HL And SL Option A Databases Part I Basic Concepts

Authors: H Sarah Shakibi PhD

1st Edition

1542457084, 978-1542457088

More Books

Students also viewed these Databases questions