
/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/


import java.util.*;

public class Main
{
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        int numberPairs=0;
        
        System.out.println("*** WELCOME TO MEMORY GAME *** - Please select size of matrix:");
        
        
        Scanner scanner = new Scanner (System.in);
        System.out.println("Enter width:");
        int length = scanner.nextInt();
        
        System.out.println("Enter length:");
        
        
        int width =  scanner.nextInt();
        numberPairs = (length * width)/2;
        
        System.out.println("Number of pairs: " + numberPairs);
        
        //int [] test = new int []{1,2,3};
        
        ArrayList <int[][]> board = new ArrayList<>();
        
        playMemoryGame mg = new playMemoryGame(numberPairs, board, width, length);
        mg.fillGrid();
        
    }
}

interface memoryGame
{
    void fillGrid();
    void flipCard();
    void pairFound();
    void noPair();
    void timeLeft();   // not sure how this will be implemented
    boolean fillNotComplete(int randomWidth, int randomLength);
    
}

class playMemoryGame implements memoryGame
{
    int numberPairs;
    ArrayList<int[][]> board;
    int width;
    int length;
    int [][] fillBoard = new int [width][length]; 
     
    
  
public void fillGrid()
  {
      int count=0;
        int number;
        int number1;
        int number2;
        
        int i=0;
        
        
        //since its default value is 0, another number is going to be written such as 99.
        // since this will be too high for a pair in matrix.
        
        System.out.println("this is width:" + width);
        System.out.println("this is length:" + length);
        
        
        for (int n=0; n<width; n++)
        {
            for (int p=0; p<length; p++)
            {
                fillBoard[n][p]=99;
            }
        }
        
        
        //Set<Integer> generatedNumbers = new HashSet<Integer>();
        List <Integer> myList = new ArrayList<Integer>();
        
        // to fill the grid, its best if 2d integer array is inserted into ArrayList
        //Another random number sequence can be performed of the width and length
        //At end, store entire 2d array into ArrayList
        
         // it has to complete process twice since the cards are in pairs
        // however on second execution it does not seem to generate new random numbers!!!
        // also need to create a board so that random numbers are
        
        Random rnd = new Random();
        
        do
        {
        
        
        
        number=rnd.nextInt((numberPairs));  // this ensures with 9 pairs.... it generates random number 0-8 and not 0-9
        
        System.out.println("Following number will be added into memory games:" + number );
        
        if (!myList.contains(number)) 
        { 
            myList.add(number); 
            
        }
        
        
        }while (myList.size()<numberPairs);
        
        
        Iterator it = myList.iterator();
        
        
        while (it.hasNext())   // this is now going through generated numbers of the Set
        {
            // it has to fill board in a random position as long as there is 99 in the cell
            
            
            number1=rnd.nextInt((width));
            number2=rnd.nextInt((numberPairs));
            
            
            if (fillNotComplete(number1, number2))
            {
                fillBoard[number1][number2]= (int)it.next();
            }
            
            
            if (fillNotComplete(number1, number2))
            {
                fillBoard[number1][number2]= (int)it.next();
            }
            
            
            System.out.println("This should be presented random order: " + fillBoard[number1][number2]);
            // for some instance, it is showing them in order even though Set should remain unordered.
            // hence a list has been used instead
           
           
            board.add(fillBoard);
            
        }
        
      
  }
  
    public playMemoryGame(int numberPairs, ArrayList<int[][]> board, int width, int length)
    {
     this.numberPairs=numberPairs;
     this.board=board;
     this.width=width;
     this.length=length;
    }
    
    public void timeLeft()
    {
    }
    
    public void noPair()
    {
    }
    
    public void pairFound()
    {
    }
    
    public void flipCard()
    {
    }
    
    
    public boolean fillNotComplete(int randomWidth, int randomLength)
    {
        if (fillBoard[randomWidth][randomLength]==99)   // this is checking that all values have been filled
        {        
                return true;
        }
        return false;
    }
 
    
}

/*
   enum cards
{
    
    ZERO(0), ONE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10);
    
    final int value;
    
    cards (int value)
    {
        this.value=value;
    }
}
*/



/*
System.out.println("Testing area");
        // this is checking to see if numbers store are returned correctly
        int []testing = new int[]{5,7,4};
        board.add(testing);
        
        
        for (int m: board.get(0))
        {
            System.out.println(m);
            
        }
*/