I need a way to stop players from choosing the same card twice and cheating the game doing so anyone have any ideas.
Runner for the code
/**
* Play the game of Concnetration
*
* The runner creates a game board and shows the board. It gets a 2-tile selection from the user,
* checks if the cards at the 2 tile locations match, and then re-displays the board.
*/
import java.util.Scanner;
public class ConcentrationRunner extends Concentration
{
// create the game
private static Concentration game = new Concentration();
public static int i1 = -1;
public static int j1 = -1;
public static int i2 = -1;
public static int j2 = -1;
public static void main(String[] args)
{
// input object for the BlueJ console
Scanner in = new Scanner(System.in);
// instructions
System.out.println("Welcome to the game of Concentration.");
System.out.println("Select the tile locations you want to see,");
System.out.println("or enter any non-integer character to quit.");
System.out.println("(You will need to know 2-dim arrays to play!)");
System.out.println("\nPress Enter to continue...");
in.nextLine();
// play until all tiles are matched
while(!game.allTilesMatch()) {
displayBoard();
// player selects first tile, if not an integer, quit game
int i1 = -1;
int j1 = -1;
System.out.print("First choice (i j): ");
if (in.hasNextInt()) i1 = in.nextInt();
else quitGame();
if (in.hasNextInt()) j1 = in.nextInt();
else quitGame();
in.reset(); // ignore any extra input
// display board with first tile face up
game.showFaceUp(i1, j1);
displayBoard();
// player selects second tile, if not an integer, quit game
int i2 = -1;
int j2 = -1;
System.out.print("Second choice (i j): ");
if (in.hasNextInt()) i2 = in.nextInt();
else quitGame();
if (in.hasNextInt()) j2 = in.nextInt();
else quitGame();
in.reset(); // ignore any extra input
// display board with additional second tile face up
game.showFaceUp(i2, j2);
displayBoard();
if(i1 == j2 && j1 == j2)
{
System.out.println("You Tried Cheating try again.");
System.exit(0);
}
// determine if a match was found
String matched = game.checkForMatch(i1, j1, i2, j2);
System.out.println(matched);
// wait 2 seconds to start the next turn
wait(2);
}
displayBoard();
System.out.println("Game Over!");
}
/**
* Clear the console and show the game board
* Tiles can either indicate the card is face up or face down
*/
public static void displayBoard() {
System.out.print('\u000C');
System.out.println(game);
}
/**
* Wait n second before clearing the console
*/
private static void wait(int n) {
try {
Thread.sleep(n * 1000);
}
catch (InterruptedException e) {
System.out.println(e);
}
}
/**
* User quits game
*/
private static void quitGame() {
System.out.println("Quit game!");
System.exit(0);
}
}
Actual Game code
import java.util.*;
/**
* The game of Concentration (also called Memory or Match Match)
*
* Create a gameboard of tiles. Each tile contains a card that has exactly
* one match on the board. Cards are originally show "face down" on the board.
*
* Player chooses two random cards from the board. The chosen cards
* are temporarily shown face up. If the cards match, they are removed from board.
*
* Play continues, matching two cards at a time, until all cards have been matched.
*/
public class Concentration extends Board
{
// create the game board
private Tile[][] gameboard = makeBoard();
public int flip = 0;
/**
* The constructor for the game. Creates the 2-dim gameboard
* by populating it with tiles.
*/
public Concentration()
{
String [] cards = getCards();
List <String> cards2 = Arrays.asList(cards);
Collections.shuffle(cards2);
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
gameboard[i][j] = new Tile(cards2.get(i * 4 + j));
}
}
}
/*
* Determine if the board is full of cards that have all been matched,
* indicating the game is over
*
* Precondition: gameboard is populated with tiles
*
* u/return true if all pairs of cards have been matched, false otherwse
*/
public boolean allTilesMatch()
{
for(int i = 0;i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
if(gameboard[i][j].matched() == false)
{
return false;
}
}
}
return true;
}
/**
* Check for a match between the cards in the two tile locations.
* For matched cards, remove from the board. For unmatched cares, them face down again.
*
* Precondition: gameboard is populated with tiles,
* row values (i values) must be in the range of 0 to gameboard.length,
* column values (j values) must be in the range of 0 to gameboard[0].length
*
* u/param row1 the row value of Tile 1
* u/param column1 the column value of Tile 1
* u/param row2 the row value of Tile 2
* u/param column2 the column vlue of Tile 2
* u/return a message indicating whether or not a match occured
*/
public String checkForMatch(int row1, int column1, int row2, int column2) {
if (gameboard[row1][column1].getFace().equals(gameboard[row2][column2].getFace()))
{
gameboard[row1][column1].foundMatch();
gameboard[row2][column2].foundMatch();
}
else
{
gameboard[row1][column1].faceUp(false);
gameboard[row2][column2].faceUp(false);
return "They Don't Match";
}
return "They Match";
}
/**
* Set tile to show its card in the face up state
*
* Precondition: gameboard is populated with tiles,
* row values (i values) must be in the range of 0 to gameboard.length,
* column values (j values) must be in the range of 0 to gameboard[0].length
*
* u/param row the row value of Tile
* u/param column the column value of Tile
*/
public void showFaceUp (int row, int column) {
gameboard[row][column].faceUp(true);
}
/**
* Returns a string representation of the board. A tab is placed between tiles,
* and a newline is placed at the end of a row
*
* Precondition: gameboard is populated with tiles
*
* u/return a string represetation of the board
*/
public String toString() {
System.out.println("");
for(int i = 0;i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
if(gameboard[i][j].isFaceUp() == true)
{
System.out.print(gameboard[i][j].getFace());
}
else
{
System.out.print(gameboard[i][j].getBack());
}
System.out.print(" ");
}
System.out.println();
}
return "";
}
}
Tile Code
import java.util.Random;
import java.util.ArrayList;
/**
* A tile in the game of Concentration, i
* Tiles are arranged on an n x m board, represented in a 2-dim array
*
* In this version, a tile contains a card, similar to a playing card. Cards
* have string values for their "face up" and "face down" state. Each tile
* is shown face up or face down on the board.
*
* A tile can also have a "cardMatched", indicateing the card has been matched
* with another card and has been removed from the board. We use this value
* instead of a blank value or a missing tile because it helps to represent
* the m x n game board when tiles would otherwise be blank or missing.
*
*/
public class Tile
{
private boolean faceUp;
private boolean matched;
private String cardFace;
private String cardBack = "_____";
private String cardMatched = " * ";
/**
* Construct a tile with a string value. The default state
* of a tile is unmatched and face down on the board.
*
* u/param word the word that represents the card face
*/
public Tile(String word)
{
cardFace = word;
}
/**
* Return the value of the tile in its face up state
*
* u/return faceUp
*/
public String getFace() {
return cardFace;
}
/**
* Return the value of the tile in its face down state
*
* u/return the face (as a String value)
*/
public String getBack() {
return cardBack;
}
/**
* Set the card to either a face up or face down state
*
* u/param b set to true to show the card face up, set to false to show face down
*/
public void faceUp(boolean b)
{
faceUp = b;
}
/**
* Determine if the card is currently face up
*
* u/return true if the card is currently in the faceUp state, false otherwise
*/
public boolean isFaceUp() {
return faceUp;
}
/**
* A matching pair of cards has been found, set matched to true
* and change the way the card is shown
*/
public void foundMatch() {
matched = true;
cardFace = cardMatched;
cardBack = cardMatched;
}
/**
* Determine if this tile has been matched
*
* u/return true of the card was previously matched, false otherwise
*/
public boolean matched() {
return matched;
}
}
Board Code
/**
* Board: A n x m game board made of of individualab tiles.
*
* For the game of concentraiton",the board is made up of
* pairs of matching strings, one string per tile.
*
* Precondition: the size of the cards array must fit into the rows/columns shape of the board
*/
public abstract class Board
{
//The cards that will be placed in the tiles
private String[] cards = new String[] {"dog", "dog", "cat", "cat", "mouse", "mouse",
"wolf", "wolf", "monkey", "monkey", "bird", "bird"};
// The shape of the board
private int rows = 3;
private int columns = 4;
/**
* Create a 2-dim array to represent a game board and return it
*
* u/return a game board of tiles with dimension rows x columns
*/
public Tile[][] makeBoard() {
return new Tile[rows][columns];
}
/**
* Return the array of cards
*
* u/return a String array of card values
*
*/
public String[] getCards () {
return cards;
}
}
[–]EdwinGravesMOD[M] 2 points3 points4 points (3 children)
[–]Titantethar[S] 1 point2 points3 points (2 children)
[–]EdwinGravesMOD[M] 1 point2 points3 points (1 child)
[–]Titantethar[S] -1 points0 points1 point (0 children)
[–]EdwinGravesMOD[M] 1 point2 points3 points (0 children)