This is python and I do not know so I need help. A common memory matching game p
ID: 3671221 • Letter: T
Question
This is python and I do not know so I need help.
A common memory matching game played by young children is to start with a deck of cards that contains identical pairs. For example, given six cards in the deck, two might be labeled 1, two labeled 2 and two labeled 3. The cards are shuffled and placed face down on a board. A player then selects two cards that are face down, turns them face up, and if the cards match they are left face up. If the two cards do not match, they are returned to their original face down position. The game continues until all cards are face up.
Write a program that plays the memory matching game. When it start, the program prompts the user for the number of rows and columns for the game board that contains the cards. The total number of cards must be even. Assume that the board dimensions are at most 8 by 9 or 9 by 8. Your cards must be numbered from 1 through (number of rows * number of columns) / / 2. Your program allows the player to specify the cards that she would like to select through a coordinate system as shown in the sample run below. All the cards that are face down are indicated by *. For example, in the following layout of a 4 by 4 game board
the pair of 8 which are face up are at coordinates (1,1) and (3,2). Pay attention to how the coordinates are specified: row number followed by column number, both start at index 1, not zero. A sample run of the game is as follows:
Another sample run is as follows:
A third sample run is as follows:
Design Requirments: You need to use three classes: Card, Deck and Game. Card stores both the card's value and face (a string or Boolean variable to indicate whether the card is facing up or down). Deck contains the cards needed for the game. It will contain among its methods a method to deal a card, another for shuffling the deck, and a method that returns number of cards left in the deck. These two classes are not identical to the classes Card and Deck discussed in the book but have many things in common. The class Game simulates playing a single game and represents the interaction between the user and the other classes. Its instance members store a 2D list (of card objects) representing the game board where the cards are placed, number of rows and number of columns of the game board. Among the instance methods of the Game class: play(), which simulates playing the game; isGameOver(), which checks whether or not the game is over; displayBoard(), which displays the board; populateBoard(), which creates the initial 2D list of identical pairs of cards with all the cards facing down. Most probably, you will need to write other instance methods as you see appropriate.
Use the following test program:
The format of the output and input of your program must be the same as what is shown in the sample runs above.
Explanation / Answer
import java.util.*;
import java.lang.*;
public class cardg2{
public static void main(String[] args){
String pl="";
Scanner kb=new Scanner(System.in);
System.out.println("Enter the card coordinate for each card when requested");
System.out.println("e.g '12' for first and '21' for second. range must be between 1-4 only");
System.out.println();
do
{
brd();
System.out.println("you've won");
System.out.println("Play again? Yes/No(Y/N)");
pl=kb.next();
}while (pl.equals("y")||pl.equals("Y"));
System.out.println("Thank You for playing");
}
//print the board
public static void brd(){
int cards[][]=new int[4][4];
boolean cs[][]=new boolean[4][4];
cards=shuf();
System.out.println(" | 1 2 3 4 ");
System.out.println("---+---------");
for(int i=0;i<4;++i)
{
System.out.print(" " +(i+1) +" | ");
for(int j=0;j<4;++j)
{
System.out.print("* ");
cs[i][j]=false;
}
System.out.println();
}
System.out.println();
str(cs,cards);
}
//game starts here
public static void str(boolean[][] cs,int[][] cards){
boolean ov=false;
Scanner kb=new Scanner(System.in);
char rw0,rw1,cl0,cl1;
int r1,c1;
int r2=0,c2=0,tmr=5000;
//Timer timer=new Timer(tmr);
do
{
do
{
System.out.println("Please insert the 1st card:");
String rw=new String(kb.next());
rw0=rw.charAt(0);cl0=rw.charAt(1);
r1=Character.digit(rw0,5);c1=Character.digit(cl0,5);
if(cs[r1-1][c1-1] == true)
{
System.out.println("This card is already flipped! Select another card.");
}
}while(cs[r1-1][c1-1]!= false);
do
{
System.out.println("Please insert the 2nd card:");
String rw11=new String(kb.next());
rw1=rw11.charAt(0);cl1=rw11.charAt(1);
r2=Character.digit(rw1,5);c2=Character.digit(cl1,5);
if(cs[r2-1][c2-1] == true)
{
System.out.println("This card is already flipped! Select another card.");
}
if((r1==r2)&&(c1==c2)){
System.out.println("This card is already chosen! Select another card.");
}
}while((cs[r2-1][c2-1]!= false)||((r1==r2)&&(c1==c2)));
r1--;
c1--;
r2--;
c2--;
System.out.println();
System.out.println(" | 1 2 3 4 ");
System.out.println("---+---------");
for (int r=0; r<4; r++)
{
System.out.print(" " +(r+1) +" | ");
for (int c=0; c<4; c++)
{
if ((r==r1)&&(c==c1))
{
System.out.print(cards[r][c] +" ");
}
else if((r==r2)&&(c==c2))
{
System.out.print(cards[r][c] +" ");
}
else if (cs[r][c] == true)
{
System.out.print(cards[r][c] +" ");
}
else
{
System.out.print("* ");
}
}
System.out.println();
}
System.out.println();
if (cards[r1][c1]==cards[r2][c2]) // to keep the card flipped
{
System.out.println("Cards Matched!");
cs[r1][c1] = true;
cs[r2][c2] = true;
}
//should have timer delay here
//System.out.println("Insert any key to continue.");
//String asd=kb.next();
try
{
Thread.sleep(2000);
}catch (InterruptedException ie)
{
System.out.println(ie.getMessage());
}
for (int b=0; b<=20; b++){
System.out.println();
}
System.out.println(" | 1 2 3 4 ");
System.out.println("---+---------");
for (int r=0; r<4; r++) // reprint the board,for user to make new guess
{
System.out.print(" " +(r+1) +" | ");
for (int c=0; c<4; c++)
{
if (cs[r][c] == true)
{
System.out.print(cards[r][c] +" ");
}
else
{
System.out.print("* ");
}
}
System.out.println();
}
System.out.println();
ov = true;
for (int r=0; r<4; r++) // check all card status, they all should be true/flipped to end the game.
{
for (int c=0; c<4; c++)
{
if(cs[r][c]==false)
{
ov = false;
c=5;
}
}
if(ov == false)
{
r=5;
}
}
} while(ov != true); // loop, the only way to get out is to finish the game!
}
//shuffle the cards
public static int[][] shuf(){
int start[]={1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8};
int cards[][]=new int[4][4];
Random ran=new Random();
int tmp,i;
for (int s=0; s<=20; s++)
{
for (int x=0; x<16; x++) //randomize the card placements
{
i=ran.nextInt(100000)%15;
tmp=start[x];
start[x]=start[i];
start[i]=tmp;
}
}
i=0;
for (int r=0; r<4; r++) // put values in cards here
{
for (int c=0; c<4; c++)
{
cards[r][c]=start[i];
i=i+1;
}
}
return cards;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.