title: Matching Card Game in Java
body: <p><strong>Hi,</strong></p>
  
  <p><strong>I'm making a matching card game and I don't understand how to actually do it. I was hoping someone could help explain what I can do to figure this out.</strong></p>
  
  <p><strong>The following are the specifications:</strong></p>
  
  <p>In a card game of Pairs the goal is to turn over pairs of matching cards.</p>
  
  <p><a href="https://en.wikipedia.org/wiki/Concentration_(game)" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Concentration_(game)</a>
  Here are the rules for the variation of Pairs we consider.</p>
  
  <p>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&lt;=n and n&lt;=4*26.</p>
  
  <p>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.)</p>
  
  <p>Write a pubic class titled MatchCardGame with the following members that implement this game
  of Pairs. The starter code in PlayCard.java should give you an idea of how MatchCardGame is
  used.</p>
  
  <p>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.</p>
  
  <p>The field</p>
  
  <p>public final int n;</p>
  
  <p>is the size of the game set by the constructor.</p>
  
  <p>The constructor</p>
  
  <p>public MatchCardGame(int n);</p>
  
  <p>initializes a card game with a total of n cards. Assume n is a multiple of 4 and that 4&lt;=n &amp;&amp; n&lt;=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.</p>
  
  <p>The method</p>
  
  <p>public String boardToString();</p>
  
  <p>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.</p>
  
  <p>The method</p>
  
  <p>public boolean flip(int i);</p>
  
  <p>plays card number i. If card i cannot be played because it’s 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.</p>
  
  <p>The method</p>
  
  <p>public boolean wasMatch();</p>
  
  <p>returns true if the previous pair was a match and returns false otherwise. This method should be
  called only after flip has been successfully called an even number of times and before flipMismatch
  is called. (A successful call of flip is a call that results in a flip and returns true.)</p>
  
  <p>The method</p>
  
  <p>public char previousFlipIdentity();</p>
  
  <p>returns the face of the previously flipped card as a char. This method should only be called after
  a card has been flipped.</p>
  
  <p>The method</p>
  
  <p>public void flipMismatch();</p>
  
  <p>reverts the mismatched pair to face-down position. This method should only be called after a 2
  calls of flip results in a mismatch.</p>
  
  <p>The method</p>
  
  <p>public boolean gameOver();</p>
  
  <p>returns true if all cards have been matched and the game is over and returns false otherwise.
  The method
  public int getFlips();</p>
  
  <p>returns the total number of card flips that have been performed so far.</p>
  
  <p>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.</p>
  
  <p>Remark. The problem specifies how these methods should be used. For example, the input n of
  MatchCardGame’s 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.</p>
  
  <p><strong>So far, I have the following:</strong></p>
  
  <pre><code>public class MatchCardGame {
  //Private classes
  private boolean facingup;
  private char[] board;
  private int flips = 0;
  private int matches = 0;
  private int currentflip;
  
  //Public classes
  public final int n;
  
  public MatchCardGame(int n) {
      for (int i = 0; i &lt; n; i++) {
          if (i % 4 == 0) {
              this.board[i] = 'a';
          }
          else if (i % 4 == 1) {
              this.board[i] = 'b';
          }
          else if (i % 4 == 2) {
              this.board[i] = 'c';
          }
          else {
              this.board[i] = 'd';
          }
      }
  }
  
  public String boardToString() {
      String stringboard = "";
      for (int i; i &lt; n; i++) {
          if (facingup){
              stringboard += stringboard + "[" + this.board[i] + "] ";
          }
          else {
              stringboard += "[-] ";
          }
      }
      return stringboard;
  }
  
  public boolean flip(int i) {
      flips++;
      if (i &lt; 0 || i &gt;= n || currentflip == ((int) this.board[i]) % 32) {
          return false;
      }
      else {
          if (flips % 2 != 0) {
              currentflip = ((int) board[i]) % 32;
              facingup = true;
              return true;
          }
          else if (board[i] == this.board[i]) {
              currentflip = -1;
              this.facingup = true;
              board [i] = '0';
              this.board[i] = '0';
              matches += 2;
              return true;
          }
      }
  }
  
  public boolean wasMatch() {
      if (currentflip == -1) {
          return true;
      }
      else {
          return false;
      }
  }
  
  public char previousFlipIdentity() {
      return board [i];
  }
  
  public void flipMismatch() {
      facingup = false;
      this.facingup = false;
  
  }
  
  public boolean gameOver() {
      if (matches == n) {
          return true;
      }
      else {
          return false;
      }
  }
  
  public int getFlips() {
      return flips;
  }}
  </code></pre>
  
  <p><strong>Any assistance would be appreciated.</strong></p>