Sunday, March 21, 2010

Card Match program

This is a project to do that I found in Absolute Java, a textbook for learning to program in Java. It's a card match game, where there is a set of "cards" which are turned over and the user must pick one to turn over and then try to find it's match in the rest of the cards. When a match is found, those 2 cards stay face up, and the game is over when all cards are turned over. Hope it's to your liking!

Just copy all these code snippets into separate text files with the same name as the headings, compile them, and then run "java CardMatchDemo", and the program will begin.

CardMatchDemo.java
 /**     This program will run a game called CardMatch, where the user must pick  
coordinates of 'cards' and match them to their twins, until all cards have
been matched.
It uses the CardMatch class and the Card class.
@MasudKhan
Finished: July 14, 2009
Last Updated: July 14, 2009
*/
import java.util.Scanner;
public class CardMatchDemo
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
CardMatch test = new CardMatch();
System.out.println("\t\tWelcome to CARD MATCH!");
test.shuffle();
test.allFaceDown();
int rowTemp, columnTemp;
String quit = "n";
do
{
test.displayField();
test.pickCard();
test.displayField();
rowTemp = test.getRowPick();
columnTemp = test.getColumnPick();
test.pickCard();
test.displayField();
if(test.equalCardNumber(rowTemp, columnTemp,
test.getRowPick(), test.getColumnPick()))
System.out.println("\n\t\t\t MATCH!\n");
else
{
System.out.println("\n\t\t\tNO MATCH\n");
test.setFaceDown(rowTemp, columnTemp);
test.setFaceDown(test.getRowPick(), test.getColumnPick());
}
if(test.isGameDone())
{
System.out.println("\t\t\tYOU WIN!");
System.out.println("\t\t\t QUIT?");
quit = keyboard.next();
test.shuffle();
test.allFaceDown();
}
} while(!test.isGameDone() && (quit.toLowerCase()).equals("n"));
}
}


CardMatch.java
 /**     This class uses the Card class to create a card matching game.  
It's main purpose is to store and manipulate an array that serves
as the game's playing field
*/
import java.util.Scanner;
public class CardMatch
{
private static int rowPick, columnPick;
private Card[][] field;
public CardMatch()
{
field = new Card[4][4];
fillField();
}
/**
Fills the matrix field with 16 cards with values from 1-8,
two of each.
*/
private void fillField()
{
int i, j;
for(i = 0; i < field.length; i++)
for(j = 0; j < field[i].length; j++)
field[i][j] = Card.createCard();
}
/**
Precondition: field must be initialized and filled.
Postcondition: switches the cards in the 2D array around as if
to shuffle them.
*/
public void shuffle()
{
int randomRow, randomRow2;
int randomColumn, randomColumn2;
for(int i = 0; i < 10000; i++)
{// lots of switching cards, to make sure it's well shuffled
randomRow = ((int)(Math.random() * 4)); // random # between 1-4 incl.
randomColumn = ((int)(Math.random() * 4));
Card tempCard = new Card(field[randomRow][randomColumn]); //buffer card
randomRow2 = ((int)(Math.random() * 4));
randomColumn2 = ((int)(Math.random() * 4));
// switch the card places
field[randomRow][randomColumn] = new Card(field[randomRow2][randomColumn2]);
field[randomRow2][randomColumn2] = new Card(tempCard);
}
}
/**
sets all cards to faceUp = false.
used for restarting a game.
*/
public void allFaceDown()
{
for(int i = 0; i < field.length; i++)
for(int j = 0; j < field[i].length; j++)
field[i][j].setFaceDown();
}
/**
sets all cards to faceUp = true.
used to give up on a game. Currently not in use by the program.
*/
public void allFaceUp()
{
for(int i = 0; i < field.length; i++)
for(int j = 0; j < field[i].length; j++)
field[i][j].setFaceUp();
}
/**
displays the current field of cards - MUST BE FIXED TO INCORPORATE FACEDOWN CARDS
*/
public void displayField()
{
for(int i = 0; i < field.length; i++)
{
System.out.print("\t\t\t");
for(int j = 0; j < field[i].length; j++)
{
if(field[i][j].isFaceUp())
System.out.print(field[i][j].getCardNumber() + " ");
else
System.out.print("* ");
}
System.out.println("\n");
}
}
/**
Prompts user for row and column choice for which card to flip over.
*/
private int[] inputUserPick()
{
// CREATE A SENTINEL TO QUIT
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("Enter the number of the row you choose,");
System.out.println("followed by a space, and the column.");
rowPick = keyboard.nextInt() - 1;
columnPick = keyboard.nextInt() - 1;
if(field[rowPick][columnPick].isFaceUp())
System.out.println("That one's already picked. try again.");
} while(field[rowPick][columnPick].isFaceUp());
int[] temp = new int[2];
temp[0] = rowPick;
temp[1] = columnPick;
return temp;
}
/**
User inputs coordinates of pick, and that pick is turned faceUp.
*/
public void pickCard()
{
inputUserPick();
field[rowPick][columnPick].setFaceUp();
}
/**
Returns true if faceUp = true for all cards.
Returns false otherwise.
*/
public boolean isGameDone()
{
boolean temp = true;
for(int i = 0; i < field.length; i++)
for(int j = 0; j < field[i].length; j++)
if(!field[i][j].isFaceUp())
temp = false;
return temp;
}
/**
Returns the static variable rowPick.
*/
public int getRowPick()
{
return rowPick;
}
/**
Returns the static variable columnPick.
*/
public int getColumnPick()
{
return columnPick;
}
public boolean equalCardNumber(int rowTemp, int columnTemp,
int rowCurrent, int columnCurrent)
{
return(field[rowTemp][columnTemp].getCardNumber() ==
field[rowCurrent][columnCurrent].getCardNumber());
}
/**
sets the card referred to by the argument coordinates to isFaceUp() = false.
*/
public void setFaceDown(int row, int column)
{
field[row][column].setFaceDown();
}
}


Card.java
 /**     This class stores info for an object that will be used as a  
card in a card-matching game.
*/
public class Card
{
private int cardNumber;
private boolean faceUp;
public static int cardNumberIterator = 0;
/**
This constructor should never be used. Only written to follow
style guidelines of always writing a no-arg constructor.
*/
public Card()
{
setCardNumber(0);
setFaceDown();
}
public Card(int initialCardValue)
{
setCardNumber(initialCardValue);
setFaceDown();
}
public Card(Card original)
{
setCardNumber(original.getCardNumber());
setFaceDown();
}
/**
Returns a newly created card with a value between 1 and 8,
faceDown. Also iterates the value of the next card to be
created.
*/
public static Card createCard()
{
if(Card.cardNumberIterator == 8)
Card.cardNumberIterator = 0;
Card.cardNumberIterator++;
return new Card(Card.cardNumberIterator);
}
/**
Sets cardNumber to the Value associated with the argument's value
*/
public void setCardNumber(int initialCardNumber)
{
if(initialCardNumber < 1 || initialCardNumber > 8)
{
System.out.println("Invalid card number. Exiting.");
System.exit(0);
}
cardNumber = initialCardNumber;
}
/**
sets faceUp to false
*/
public void setFaceDown()
{
faceUp = false;
}
/**
sets faceUp to true
*/
public void setFaceUp()
{
faceUp = true;
}
/**
Returns cardNumber
*/
public int getCardNumber()
{
return cardNumber;
}
/**
Returns true if faceUp is true, and false otherwise
*/
public boolean isFaceUp()
{
return faceUp;
}
}


Have fun!

No comments:

Post a Comment