Title: Matching Game
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Matching Game

This game is similar to a TV show that you might have watched where contestants needed to match pairs in a rectangular grid. By default all of the items are covered, but as the contestants pick items, they get a chance to see one item at a time.

The Solution that is developed in this video is as follows:


package matching;

import java.util.Scanner;

public class MatchingGame {

    static final int  ROWS = 4; // Assume ROWS is even
    static final int  COLS = 3;

    int[][] items = new int[ROWS][COLS];
    boolean[][] visible = new boolean[ROWS][COLS];
    Scanner keyboard = new Scanner(System.in);

    MatchingGame()
    {
        for (int i=0; i < ROWS; i++)
            for (int j=0; j < COLS; j++)
                visible[i][j] = false;

        int totalPairs = ROWS * COLS / 2;
        int count =1;
        for (int i= 0; i < ROWS/2; i++)
        {
            for (int j=0; j < COLS; j++)
            {
                items[i][j] = count;
                items[i + ROWS/2][j] = count;
                count += 1;
            }
        }

        shuffle(items);


    }

    void display()
    {
        for (int i=0; i < ROWS; i++)
        {
            for (int j=0; j < COLS; j++)
            {
                if (visible[i][j])
                    System.out.printf(" %2d ", items[i][j]);
                else
                    System.out.printf(" ** ");
            }
            System.out.println();
        }
    }
    void playGame()
    {
        boolean continueFlag;
        do
        {
            display();
            System.out.println("Enter row and col of first item:");
            int row1 = keyboard.nextInt();
            int col1 = keyboard.nextInt();

            System.out.println("Enter row and col of second item:");
            int row2 = keyboard.nextInt();
            int col2 = keyboard.nextInt();

            visible[row1][col1] = true;
            visible[row2][col2] = true;
            display();
            System.out.println(" Do you want to continue Game(y/n");
            String yes = keyboard.next();
            continueFlag = false;
            if ("y".equals(yes))
                continueFlag = true;

            if (items[row1][col1] != items[row2][col2])
            {
                visible[row1][col1] = false;
                visible[row2][col2] = false;
            }

        } while (continueFlag);
    }

    void shuffle(int[][] arr)
    {
        for (int i=0; i < arr.length; i++)
        {
            for (int j=0; j < arr[i].length; j++)
            {
                int row = (int)(arr.length * Math.random());
                int col = (int)(arr[i].length * Math.random());

                int temp = arr[i][j];
                arr[i][j] = arr[row][col];
                arr[row][col] = temp;

            }
        }
    }


    public static void main(String[] args) {
        MatchingGame mg = new MatchingGame();
        mg.playGame();


    }

}


Last Modified: September 30, 2013 4:27 AM