/*
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.Scanner;
import java.io.*;
import java.util.*;
import java.util.Arrays;
import java.util.regex.Pattern;
import java.util.regex.Matcher;


interface Playable
{
    //public void assignChip();    
    public void selectChipPosition();
    public boolean checkAvailability(int inputAsIntegerZeroIndex, Character chipValue);
    public void insertChip(int pos); 
    public boolean checkConnectFour(int pos, int columnChipPlacedZeroIndex, Character chipValue);
    public void viewBoard();
}


public class Main
{
    
    
    public static void main (String[] args)
    {
        
        
        ConnectFour cf = new ConnectFour();
        
    }
}


class PlayerTwo
{
    public PlayerTwo(ConnectFour cf)
    {
        
    }
}


class PlayerOne
{
    String[][] board;
    PlayerTwo plTwo;
    
     enum Chips
    {
        RED('X'),
        YELLOW('O');
            
        final Character value;
        
        Chips(Character value)
        {
            this.value=value;
        }
    }
    
    public PlayerOne(ConnectFour cf)
    {
        this.board=board;
        Chips yellow = Chips.YELLOW;
        Character yellowValue = yellow.value;
        
        System.out.println("PLAYER1 has been assigned: " + yellowValue + " chip");
        cf.selectChipPosition();
        cf.checkAvailability(cf.inputAsIntegerZeroIndex,yellowValue);
        cf.checkConnectFour(cf.inputAsIntegerZeroIndex, cf.rowChipPlacedZeroIndex, yellowValue);
        plTwo = new PlayerTwo (cf);
        
    }
}

class ConnectFour implements Playable
{
    String[][] board;
    Matcher matcher;
    Character input;
    int inputAsIntegerZeroIndex;
    int columnChipPlacedZeroIndex;
    int rowChipPlacedZeroIndex;
    int rowChipPlaced;
    int columnChipPlaced;
    
    public boolean checkAvailability(int input, Character chipValue)
    {
        System.out.println("***CHECK AVAILABILITY****");
        
        for (int k=(board.length-1); k>=0; k--)
        {
            System.out.println("value of k:" + k);
            
            if (board[k][input].equals("-"))
            {
                board[k][input]= String.valueOf(chipValue);
                rowChipPlacedZeroIndex=k;
                rowChipPlaced=k+1;
                
                System.out.println("Chip: " + chipValue + "will be placed into column: " + (input+1) + " row: " + rowChipPlaced);
                
                break;
           }
        }
        viewBoard();
        
        return true;
    };
    
    public void insertChip(int pos)
    {
        System.out.println("***INSERT CHIP****");
    }
    
    //pos is the column,  row is the row
    public boolean checkConnectFour(int pos, int row, Character chipValue)
    {
        int boardBaseLevel=0;
        int boardHeightLevel = board.length-1;
        int sameColour=0;
        
        System.out.println("***CHECK CONNECT FOUR****");
        
        //need to check vertically first above and below current position
        //in order to check vertically
        
       // rowChipPlacedZeroIndex
        
        //can only check below if rowChipPlacedZeroIndex!=0
        //can only check above if rowChipPlacedZeroIndex!=(board.length-1)
        
        //System.out.println("VERTICAL CHECK******");
        
        if (rowChipPlacedZeroIndex!=0)
        {
            for (int count = rowChipPlacedZeroIndex-1; count>=0; count--)
            {
                if (board[rowChipPlacedZeroIndex][columnChipPlacedZeroIndex]==String.valueOf(chipValue))
                {
                    sameColour++;
                }
                else
                {
                    sameColour=0;
                    break;
                }
                
            }
            
            for (int count = rowChipPlacedZeroIndex+1; count>=0; count--)
            {
                if (board[rowChipPlacedZeroIndex][columnChipPlacedZeroIndex]==String.valueOf(chipValue))
                {
                    sameColour++;
                }
                else
                {
                    sameColour=0;
                    break;
                }
            }
        }
        System.out.println(sameColour);

        if (sameColour+1==4)
        {
            System.out.println("Connect 4");
        }
            
        
        
        return true;
    };
    
    public void assignChip()
    {
        System.out.println("***ASSIGN CHIP****");
    }
    
    public void selectChipPosition()
    {
        System.out.println("***SELECT CHIP POSITION****");
        Scanner in = new Scanner(System.in);
        
        //we want to accept input as a String
        //this will mean that whilst validating input,
        //it needs to keep while loop while 
        //!length==1 && indexOf(numbers between 1 - 7)
        
        viewBoard();
        //currentAvailability();
        do
        {
        System.out.println("Which column would you like to insert the chip?");
        input = in.next().charAt(0);
        
        String regexColumns="[1234567]";
        Pattern pattern = Pattern.compile(regexColumns, Pattern.CASE_INSENSITIVE);
        matcher = pattern.matcher(Character.toString(input));
        
        }while (!matcher.find());
        
        //this is to stay inline with zero indexing
        inputAsIntegerZeroIndex = Character.getNumericValue(input-1);
        columnChipPlacedZeroIndex = Character.getNumericValue(input-1);
        columnChipPlaced = Character.getNumericValue(input);
    }
    
    public void viewBoard()
    {
        System.out.println("****CURRENT BOARD****");
        for (String[]row:board)
        {
                System.out.println(Arrays.deepToString(row));
        }
    };
    
    public ConnectFour() 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        board = new String [][]   { {"-","-","-","-","-","-","-"},
                                    {"-","-","-","-","-","-","-"},
                                    {"-","-","-","-","-","-","-"},
                                    {"-","O","-","-","-","-","-"},
                                    {"-","O","-","-","-","-","-"},
                                    {"-","O","-","-","-","-","-"},
                                };
        viewBoard();
        
        PlayerOne plOne = new PlayerOne (this);
    }
    
   
   
    
}