For my project, the goal is to make a memory game that keeps track of 2 players and their scores. I have the game set up to generate and then shuffle a deck of 52 normal playing cards, and for my matching all I'm concerned about is the numeric part of the card(i.e. the 6 in 6 of diamonds). I just about have the entire game done, but I'm having some trouble coming up with a valid boolean method that will tell the players whether or not a card has already been matched or not. I need the method to basically permanently have a card as flipped, or true in booleans case, if it has already been matched with another card. I can't simply remove the card because I'm using an arrayList and removing it would mess up the indexes for my arrayList. My code is below. Can anyone help me out with this? I should also say that I've tried about 3 different methods of doing this, and none of them seem to work. I can't seem to get the emthod to flip over a specific index.
import java.util.*; public class Card { private int value = 0; private int suit = 0; public Card(int value, int suit) { this.value = value; this.suit = suit; } public String toString() { return getValue() + " of " + getSuit(); } public String getValue() { if (value == 1) { return "Ace"; } else if (value == 11) { return "Jack"; } else if (value == 12) { return "Queen"; } else if (value == 13) { return "King"; } else return new Integer(value).toString(); } public String getSuit() { if(suit == 1) { return "Spades"; } else if(suit == 2) { return "Clubs"; } else if(suit == 3) { return "Hearts"; } else { return "Diamonds"; } } public void setSuit(int suit) { this.suit = suit; } }
import java.util.*; public class Game { private ArrayList<Card> cardDeck; private int cards = 52; private Player player1; private Player player2; private String playerOne = null; private String playerTwo = null; private String card1, card2 = null; private int choice1, choice2 = 0; private int score = 0; private Scanner keyboard = new Scanner(System.in); public Game() { } public void deckGenerate() { int i, j = 0; for (i = 1; i <= 4; i++) { for(j = 1; j <= 13; j++) { Card card = new Card(i ,j); cardDeck.add(card); } } } public void deckShuffle() { int i = 0; Random randomCard = new Random(); for(i = 0; i < cards-1; i++) { int newIndex = randomCard.nextInt(cards-i)+i; Card tempCard = cardDeck.get(i); cardDeck.set(i, cardDeck.get(newIndex)); cardDeck.set(newIndex, tempCard); } } public void gameStart() { cardDeck = new ArrayList<Card>(cards); deckGenerate(); deckShuffle(); System.out.println("this is a 2 player memory game. The goal of this game is to reach 100 points before your opponent."); System.out.println("You choose 2 cards from a deck of 52, and for each match you receive 20 points. You will not match every card."); System.out.println("Enter player one's name."); playerOne = keyboard.nextLine(); System.out.println("Enter player two's name."); playerTwo = keyboard.nextLine(); player1 = new Player(playerOne); player2 = new Player(playerTwo); Player current = player1; do { gamePlay(current); if (current == player1) current = player2; else current = player1; }while (gameEnd(score) == false); } public void gamePlay(Player current) { System.out.println("Please choose your first number, between 1 and 52."); choice1 = keyboard.nextInt(); card1 = variableSetOne(choice1); System.out.println("Please choose your second number, between 1 and 52."); choice2 = keyboard.nextInt(); card2 = variableSetTwo(choice2); System.out.println(card1); System.out.println(card2); if (card1.equals(card2)) { System.out.println("Its a match!"); score = score + 20; } else System.out.println("Sorry, its not a match."); System.out.println("Your current score is " + score); } public boolean gameEnd(int score) { if (score == 100) return true; else return false; } public String variableSetOne(int choice1) { String pick = null; if (choice1 == 1) pick = cardDeck.get(0).getValue(); else if (choice1 == 2) pick = cardDeck.get(1).getValue(); else if (choice1 == 3) pick = cardDeck.get(2).getValue(); else if (choice1 == 4) pick = cardDeck.get(3).getValue(); else if (choice1 == 5) pick = cardDeck.get(4).getValue(); else if (choice1 == 6) pick = cardDeck.get(5).getValue(); else if (choice1 == 7) pick = cardDeck.get(6).getValue(); else if (choice1 == 8) pick = cardDeck.get(7).getValue(); else if (choice1 == 9) pick = cardDeck.get(8).getValue(); else if (choice1 == 10) pick = cardDeck.get(9).getValue(); else if (choice1 == 11) pick = cardDeck.get(10).getValue(); else if (choice1 == 12) pick = cardDeck.get(11).getValue(); else if (choice1 == 13) pick = cardDeck.get(12).getValue(); else if (choice1 == 14) pick = cardDeck.get(13).getValue(); else if (choice1 == 15) pick = cardDeck.get(14).getValue(); else if (choice1 == 16) pick = cardDeck.get(15).getValue(); else if (choice1 == 17) pick = cardDeck.get(16).getValue(); else if (choice1 == 18) pick = cardDeck.get(17).getValue(); else if (choice1 == 19) pick = cardDeck.get(18).getValue(); else if (choice1 == 20) pick = cardDeck.get(19).getValue(); else if (choice1 == 21) pick = cardDeck.get(20).getValue(); else if (choice1 == 22) pick = cardDeck.get(21).getValue(); else if (choice1 == 23) pick = cardDeck.get(22).getValue(); else if (choice1 == 24) pick = cardDeck.get(23).getValue(); else if (choice1 == 25) pick = cardDeck.get(24).getValue(); else if (choice1 == 26) pick = cardDeck.get(25).getValue(); else if (choice1 == 27) pick = cardDeck.get(26).getValue(); else if (choice1 == 28) pick = cardDeck.get(27).getValue(); else if (choice1 == 29) pick = cardDeck.get(28).getValue(); else if (choice1 == 30) pick = cardDeck.get(29).getValue(); else if (choice1 == 31) pick = cardDeck.get(30).getValue(); else if (choice1 == 32) pick = cardDeck.get(31).getValue(); else if (choice1 == 33) pick = cardDeck.get(32).getValue(); else if (choice1 == 34) pick = cardDeck.get(33).getValue(); else if (choice1 == 35) pick = cardDeck.get(34).getValue(); else if (choice1 == 36) pick = cardDeck.get(35).getValue(); else if (choice1 == 37) pick = cardDeck.get(36).getValue(); else if (choice1 == 38) pick = cardDeck.get(37).getValue(); else if (choice1 == 39) pick = cardDeck.get(38).getValue(); else if (choice1 == 40) pick = cardDeck.get(39).getValue(); else if (choice1 == 41) pick = cardDeck.get(40).getValue(); else if (choice1 == 42) pick = cardDeck.get(41).getValue(); else if (choice1 == 43) pick = cardDeck.get(42).getValue(); else if (choice1 == 44) pick = cardDeck.get(43).getValue(); else if (choice1 == 45) pick = cardDeck.get(44).getValue(); else if (choice1 == 46) pick = cardDeck.get(45).getValue(); else if (choice1 == 47) pick = cardDeck.get(46).getValue(); else if (choice1 == 48) pick = cardDeck.get(47).getValue(); else if (choice1 == 49) pick = cardDeck.get(48).getValue(); else if (choice1 == 50) pick = cardDeck.get(49).getValue(); else if (choice1 == 51) pick = cardDeck.get(50).getValue(); else if (choice1 == 52) pick = cardDeck.get(51).getValue(); return pick; } public String variableSetTwo(int choice2) { String pick = null; if (choice2 == 1) pick = cardDeck.get(0).getValue(); else if (choice2 == 2) pick = cardDeck.get(1).getValue(); else if (choice2 == 3) pick = cardDeck.get(2).getValue(); else if (choice2 == 4) pick = cardDeck.get(3).getValue(); else if (choice2 == 5) pick = cardDeck.get(4).getValue(); else if (choice2 == 6) pick = cardDeck.get(5).getValue(); else if (choice2 == 7) pick = cardDeck.get(6).getValue(); else if (choice2 == 8) pick = cardDeck.get(7).getValue(); else if (choice2 == 9) pick = cardDeck.get(8).getValue(); else if (choice2 == 10) pick = cardDeck.get(9).getValue(); else if (choice2 == 11) pick = cardDeck.get(10).getValue(); else if (choice2 == 12) pick = cardDeck.get(11).getValue(); else if (choice2 == 13) pick = cardDeck.get(12).getValue(); else if (choice2 == 14) pick = cardDeck.get(13).getValue(); else if (choice2 == 15) pick = cardDeck.get(14).getValue(); else if (choice2 == 16) pick = cardDeck.get(15).getValue(); else if (choice2 == 17) pick = cardDeck.get(16).getValue(); else if (choice2 == 18) pick = cardDeck.get(17).getValue(); else if (choice2 == 19) pick = cardDeck.get(18).getValue(); else if (choice2 == 20) pick = cardDeck.get(19).getValue(); else if (choice2 == 21) pick = cardDeck.get(20).getValue(); else if (choice2 == 22) pick = cardDeck.get(21).getValue(); else if (choice2 == 23) pick = cardDeck.get(22).getValue(); else if (choice2 == 24) pick = cardDeck.get(23).getValue(); else if (choice2 == 25) pick = cardDeck.get(24).getValue(); else if (choice2 == 26) pick = cardDeck.get(25).getValue(); else if (choice2 == 27) pick = cardDeck.get(26).getValue(); else if (choice2 == 28) pick = cardDeck.get(27).getValue(); else if (choice2 == 29) pick = cardDeck.get(28).getValue(); else if (choice2 == 30) pick = cardDeck.get(29).getValue(); else if (choice2 == 31) pick = cardDeck.get(30).getValue(); else if (choice2 == 32) pick = cardDeck.get(31).getValue(); else if (choice2 == 33) pick = cardDeck.get(32).getValue(); else if (choice2 == 34) pick = cardDeck.get(33).getValue(); else if (choice2 == 35) pick = cardDeck.get(34).getValue(); else if (choice2 == 36) pick = cardDeck.get(35).getValue(); else if (choice2 == 37) pick = cardDeck.get(36).getValue(); else if (choice2 == 38) pick = cardDeck.get(37).getValue(); else if (choice2 == 39) pick = cardDeck.get(38).getValue(); else if (choice2 == 40) pick = cardDeck.get(39).getValue(); else if (choice2 == 41) pick = cardDeck.get(40).getValue(); else if (choice2 == 42) pick = cardDeck.get(41).getValue(); else if (choice2 == 43) pick = cardDeck.get(42).getValue(); else if (choice2 == 44) pick = cardDeck.get(43).getValue(); else if (choice2 == 45) pick = cardDeck.get(44).getValue(); else if (choice2 == 46) pick = cardDeck.get(45).getValue(); else if (choice2 == 47) pick = cardDeck.get(46).getValue(); else if (choice2 == 48) pick = cardDeck.get(47).getValue(); else if (choice2 == 49) pick = cardDeck.get(48).getValue(); else if (choice2 == 50) pick = cardDeck.get(49).getValue(); else if (choice2 == 51) pick = cardDeck.get(50).getValue(); else if (choice2 == 52) pick = cardDeck.get(51).getValue(); return pick; } }
import java.util.*; public class Player { private String playerName = null; public Player(String playerName) { this.playerName = playerName; } public String getName() { return playerName; } }
import java.util.*; public class PrimaryGame { public static void main(String[] args) { Game newGame = new Game(); newGame.gameStart(); } }