/*
Online Java - IDE, Code Editor, Compiler

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

//I have made slightly harder work since I kept
//variables to use in zero index world such as navigating the board
//and also non zero index, which I feel would be better to end user
//when providing useful information.. and also terminating non-iterative scenarios.

//Also although there is no direct communication between Player1 and Player2,
//I have set scenario to ensure that instance variable is available between them
//should future need arise.
//However due to the alternating nature of turns, it will still use same instance variable
//for re-instantation.. So the scope is limited to any communication between their turn..
//Also when it reaches the endpoint, if one player is repeatedly repeatedly placing
//chip in same full column, he will still mantain his turn and their state will be reset (instantiate player again).
//These are sort of areas I wish to improve in my further OOP design.

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(String playerName, Character chipValue);    
    public void selectChipPosition(String playerName, Character chipValue);
    public boolean checkAvailability(int inputAsIntegerZeroIndex, Character chipValue, PlayerOne plOne, PlayerTwo plTwo, String name);
    public void insertChip(int pos); 
    public boolean checkConnectFour(int pos, int columnChipPlacedZeroIndex, Character chipValue, String checkConnectFour);
    public void viewBoard();
}

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

class PlayerOne
{
    String[][] board;
    String playerOneName;
    String playerTwoName;
    PlayerTwo plTwo;
    
    enum Chips
    {
        RED('O'),
        YELLOW('X');
            
        final Character value;
        
        Chips(Character value)
        {
            this.value=value;
        }
    }
    
    public PlayerOne(ConnectFour cf, String playerOne, String playerTwo, PlayerTwo plTwo)
    {
        this.board=board;
        this.plTwo = plTwo;
        Chips yellow = Chips.YELLOW;
        Character yellowValue = yellow.value;
        this.playerOneName=playerOne;
        this.playerTwoName=playerTwo;
        cf.assignChip(playerOneName, yellowValue);    
        cf.selectChipPosition(playerOneName,yellowValue);
        cf.checkAvailability(cf.inputAsIntegerZeroIndex,yellowValue, this, plTwo, (playerOneName+"(Player 1)"));
        cf.checkConnectFour(cf.inputAsIntegerZeroIndex, cf.rowChipPlacedZeroIndex, yellowValue, playerOneName);
        
        //Note players do not need instance variables of each other,
        //no specific variables are being written in their respective classes
        //but I have chosen in event I wish to add class specific implementations...
        plTwo = new PlayerTwo (cf, playerOneName, playerTwoName, this);
    }
}

class PlayerTwo
{
    String[][] board;
    PlayerOne plOne;
    String playerTwoName;
    String playerOneName;
    
     enum Chips
    {
        RED('O'),
        YELLOW('X');
            
        final Character value;
        
        Chips(Character value)
        {
            this.value=value;
        }
    }
    
    public PlayerTwo(ConnectFour cf, String playerOne, String playerTwo, PlayerOne plOne)
    {
        this.board=board;
        Chips red = Chips.RED;
        Character redValue = red.value;
        this.playerTwoName=playerTwo;
        this.playerOneName=playerOne;
        cf.assignChip(playerTwoName, redValue); 
        cf.selectChipPosition(playerTwoName,redValue);
        cf.checkAvailability(cf.inputAsIntegerZeroIndex,redValue, plOne, this, (playerTwoName+"(Player 2)"));
        cf.checkConnectFour(cf.inputAsIntegerZeroIndex, cf.rowChipPlacedZeroIndex, redValue, playerTwoName);
        plOne = new PlayerOne (cf, playerOneName,playerTwoName, this);
    }
}

class ConnectFour implements Playable
{
    String[][] board;
    Matcher matcher;
    Character input;
    int inputAsIntegerZeroIndex;
    int columnChipPlacedZeroIndex;
    int rowChipPlacedZeroIndex;
    int rowChipPlaced;
    int columnChipPlaced;
    int boardBaseLevel=0;
    int boardHeightZeroIndex;
    int boardWidth=7;
    int boardWidthZeroIndex = boardWidth-1;
    int sameColourTotal=0;
    int columnsFilled;
    boolean columnFull[] = new boolean[boardWidth];
    boolean isAvailable;
    boolean rightColour=false;
    int offset;
    int runningTotalSameColour;
    String playerOneName;
    String playerTwoName;
    String playerNameChipValue;
    int boardHeight;
    PlayerOne plOne;
    PlayerTwo plTwo;
    String lastChipName;
    Character lastChipValue;
    boolean isGameOver = false;
    
    public boolean checkAvailability(int input, Character chipValue, PlayerOne plOne, PlayerTwo plTwo, String name)
    {
        this.plOne=plOne;
        this.plTwo=plTwo;
        System.out.println("***CHECK AVAILABILITY****" + "Board height: " +boardHeight + " board Width:" + boardWidth);
        
        //this is going through height of the board
        for (int k=(boardHeightZeroIndex); k>=0; k--)
        {
            //this will check the column end user specified for a - (vacant spot)   
            if (board[k][input].equals("-"))
            {
                board[k][input]= String.valueOf(chipValue);
                rowChipPlacedZeroIndex=k;
                rowChipPlaced=rowChipPlacedZeroIndex+1;
                
                System.out.println("Chip: " + chipValue + " will be placed into column: " + (columnChipPlaced) + " row: " + rowChipPlaced);
                lastChipName = name;
                lastChipValue = chipValue;
                
                //we know that rowChipPlaced reaching the boardHeight (both non zero index) is when
                //the column is full
                //rows count from 1 to 6 (top to bottom)
                
                if (rowChipPlaced==1)
                {
                    //System.out.println("COLUMN: " + columnChipPlaced + "is now full");
                    isAvailable=false;
                }
                else
                {
                    isAvailable=true;
                    break;
                }
            }
         }
        
        //if no availability
        //there are lots of possibilities of completing this.
        //I prefer keeping track at the column level
        
        if (!isAvailable)
        {
             if (columnsFilled==boardWidth)
            {
                System.out.println("GAME OVER - NO WINNER");
                System.exit(0);
            }
            
            //if it is currently not marked as full
            //it sets it and increases columnsFilled
            //otherwise columnsFilled will become invalidated
                    
            if(!columnFull[columnChipPlacedZeroIndex])
            {
                columnFull[columnChipPlacedZeroIndex]=true;
                System.out.println("Column: " + columnChipPlaced + " is NOW full.");
                columnsFilled++;
                System.out.println("AVAILABILITY: Total of  " + columnsFilled + " columns filled");
            }
               
            //we know from enum that Player 1 is assigned Yellow (X) and Player 2 is assigned (O)
            //This decision making is required to call the right constructor in line with their turn..
               
            System.out.println("Last chip inserted " + lastChipValue + " by: " + lastChipName);
               
            if (lastChipValue.equals('O'))
            {
                  plOne = new PlayerOne (this,playerOneName,playerTwoName, plTwo);
            }
            else
            {
                System.out.println("again one");
                plTwo = new PlayerTwo (this, playerOneName, playerTwoName, plOne);
            }
        }
        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, String playerName)
    {
        System.out.println("***CHECK CONNECT FOUR****");
        
        playerNameChipValue = playerName + "("+chipValue+")";
        
        //need to check vertically first above and below current position
        //in order to check vertically
        //can only check below if rowChipPlacedZeroIndex!=5
        
        System.out.println("VERTICAL CHECK => DOWNWARDS*****");
        
        //will not perform if on bottom row
	    //i.e   (if rowChipPlacedZeroIndex!=5 && rowChipPlacedZeroIndex

        if (rowChipPlacedZeroIndex!=5)
        {
            for (int count = rowChipPlacedZeroIndex; count<=boardHeightZeroIndex; count++)
            {
                if (board[count][columnChipPlacedZeroIndex].equals(String.valueOf(chipValue)))
                {
                    sameColourTotal++;
                    
                    if (sameColourTotal==4)
                    {
                        System.out.println("***1Congratulations " + playerNameChipValue + " Connect 4 in column:" + columnChipPlaced);
                        isGameOver=true;
                        //break;
                    }
                }
                
            }
            sameColourTotal=0;
        }
        //checking horizontally to right, can not perform if on last column
                                           //!=6
        System.out.println("HORIZONTAL CHECK => RIGHT*****");
        
        if (columnChipPlacedZeroIndex!=boardWidthZeroIndex)
        {
            for (int count = columnChipPlacedZeroIndex; count<=boardWidthZeroIndex; count++)
            {
                if (board[rowChipPlacedZeroIndex][count].equals(String.valueOf(chipValue)))
                {
                    sameColourTotal++;
                    rightColour=true;
                    
                    if (sameColourTotal==4)
                    {
                        System.out.println("***2Congratulations " +  playerNameChipValue + "  Connect 4 in row:" + rowChipPlaced);
                        isGameOver=true;
                        return true;
                    }
                }
            }
            
            //need be careful here since opposite player chip can
            //appear on right horizontal, but there might be sufficient
            //player colours on left horizontal to win (net total 4)
            //note these loops are doing counts relative to the chip placed.
            //in the vertical situation, this does not come into play
            //since opposition colour in the column would disrupt the linearity
            //to win the game
            //so it is important to keep track of the total stored in 
            //sameColour in a new variable (runningTotalSameColour)
            //and once it has finished the left examination, it can remove contents of
            //runningTotalSameColour
            runningTotalSameColour=sameColourTotal;
            sameColourTotal=0;
        }
        //checking horizontally to left, can not perform if on first column
        //also need to ensure that the chip placed is not counted again!
        System.out.println("HORIZONTAL CHECK => LEFT*****");
        
        if (columnChipPlacedZeroIndex!=0)
        {
            for (int count = columnChipPlacedZeroIndex; count>=0; count--)
            {
                if (board[rowChipPlacedZeroIndex][count].equals(String.valueOf(chipValue)))
                {
                    sameColourTotal++;
                    
                    //all chips appearing horizontally left relative to the chip inserted
                    if (sameColourTotal+runningTotalSameColour==4 & !rightColour)
                    {
                        System.out.println("***3Congratulations " + playerNameChipValue + "  Connect 4 in row:" + rowChipPlaced);
                        isGameOver=true;
                        return true;
                    }
                    
                    //need to get an additional since it has counted both left
                    //and right and includes the chip placed in both totals
                    if (sameColourTotal+runningTotalSameColour==5 & rightColour)
                    {
                        System.out.println("***4Congratulations " + playerNameChipValue + "  Connect 4 in row:" + rowChipPlaced);
                        isGameOver=true;
                        return true;
                    }
                }
                
            }
            //all set back to defaults
            offset=0;
            sameColourTotal=0;
            runningTotalSameColour=0;
            rightColour=false;
        }
        System.out.println("DIAGONAL CHECK 1");  
            
        //most trickiest, it has to examine 4 diagonals.
        //concept similar to horizontal
        
        //diagonal NE (coordinates) - row would decrease, column increase
        //it can not perform if on top row or last column board
                                          //!=6                                        //!=0
        if (columnChipPlacedZeroIndex!=boardWidthZeroIndex && rowChipPlacedZeroIndex!=boardBaseLevel)
        {
            System.out.println("INSIDE D1 - Diagonal north east check");
            
            //it has to scan one column further and one row upward (going toward 0 row index)
            for (int count = rowChipPlacedZeroIndex; count>=0; count--)
            {
                if (columnChipPlacedZeroIndex+offset<=boardWidthZeroIndex)
                {
                    if (board[count][columnChipPlacedZeroIndex+offset].equals(String.valueOf(chipValue)))
                    {
                        rightColour=true;
                        sameColourTotal++;
                        offset++;
                        
                        if (sameColourTotal==4)
                        {
                            System.out.println("***5Congratulations " + playerNameChipValue + "  Connect 4 in diagonal");
                            isGameOver=true;
                            return true;
                        }
                    }
                    
                }
            }
            runningTotalSameColour=sameColourTotal;
            sameColourTotal=0;
            offset=0;
        }
        //it has to scan one column left and and one row downward
        //not possible from bottom row or first column
        //SW coordinates,  row would increase, column decrease
        System.out.println("DIAGONAL CHECK 2"); 
                                                                          //!=6
        if (columnChipPlacedZeroIndex!=0 && rowChipPlacedZeroIndex!=boardHeightZeroIndex)
        {
            System.out.println("INSIDE D2 - Diagonal south west check");
                
                                                          //<=6
            for (int count = rowChipPlacedZeroIndex; count<=boardHeightZeroIndex; count++)
            {
                if (columnChipPlacedZeroIndex-offset>=0)
                {
                    if (board[count][columnChipPlacedZeroIndex-offset].equals(String.valueOf(chipValue)))
                    {
                        sameColourTotal++;
                        offset++;
                        
                        if (sameColourTotal+runningTotalSameColour==4 & !rightColour)
                        {
                            System.out.println("***6Congratulations " + playerNameChipValue + "  Connect 4 in diagonal");
                            isGameOver=true;
                            return true;
                        }
                        //need to get an additional since it has counted both left
                        //and right and includes the chip placed in both totals
                        if (sameColourTotal+runningTotalSameColour==5 & rightColour)
                        {
                            System.out.println("***7Congratulations" + playerNameChipValue + "  Connect 4 in diagonal");
                            isGameOver=true;
                            return true;
                        }
                    }
                    
                }
            }
            runningTotalSameColour=0;
            rightColour=false;
            sameColourTotal=0;
            offset=0;
        }
        
        //it now has to scan SE  (right movement on board)
        //column increase, increase
        //it can not perform from following locations
        //column 7 and row 6
        
        System.out.println("DIAGONAL CHECK 3"); 
                                        //!=6                      //!=5
        if (columnChipPlacedZeroIndex!=boardWidthZeroIndex && rowChipPlacedZeroIndex!=boardHeightZeroIndex)
        {
            System.out.println("INSIDE D3 - Diagonal south east check");
                                                         //<=6
            for (int count = rowChipPlacedZeroIndex; count<=boardHeightZeroIndex; count++)
            {
                if (columnChipPlacedZeroIndex+offset<=boardWidthZeroIndex)               
                {
                    if (board[count][columnChipPlacedZeroIndex+offset].equals(String.valueOf(chipValue)))
                    {
                        rightColour=true;
                        sameColourTotal++;
                        offset++;
                        System.out.println("value of offset: " + offset);
                    
                        if (sameColourTotal==4)
                        {
                            System.out.println("***8Congratulations " + playerNameChipValue + "  Connect 4 in diagonal");
                            isGameOver=true;
                            return true;
                        }
                    }
                    
                }
            }
            runningTotalSameColour=sameColourTotal;
            sameColourTotal=0;
            offset=0;
        }
        
        //it now has to scan NW   column decrease row decrease
        //it can not perform from following locations
        //column 0 and row 0
        System.out.println("DIAGONAL CHECK 4"); 
        
        if (columnChipPlacedZeroIndex!=0 && rowChipPlacedZeroIndex!=0)
        {
            System.out.println("INSIDE D4 - Diagonal north west check");
            //System.out.println("This is the offset:" + offset);
            //it has to scan one column further and one row upward (going toward 0 row index)
                
            for (int count = rowChipPlacedZeroIndex; count>=0; count--)
            {
                if (columnChipPlacedZeroIndex-offset>=0)
                {
                    if (board[count][columnChipPlacedZeroIndex-offset].equals(String.valueOf(chipValue)))
                    {
                        sameColourTotal++;
                        offset++;
                            
                        if (sameColourTotal+runningTotalSameColour==4 && !rightColour)
                        {
                            System.out.println("***9Congratulations " + playerNameChipValue + "  Connect 4 in diagonal");
                            isGameOver=true;
                            return true;
                        }
                        
                        //as previously, need to get 5 since it has counted both left
                        //and right and includes the chip placed in both totals
                        if (sameColourTotal+runningTotalSameColour==5 && rightColour)
                        {
                            System.out.println("***10Congratulations" + playerNameChipValue + "  Connect 4 in diagonal");
                            return true;
                        }
                    }
                    
                }
            }
            rightColour=false;
            runningTotalSameColour=0;
            sameColourTotal=0;
            offset=0;
        }
        return false;
    }
    
    public void configurePlayers()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter name for Player 1:");
        playerOneName = input.next();
        System.out.println("Enter name for Player 2:");
        playerTwoName = input.next();
    }
    
    public void assignChip(String playerName, Character chip)
    {
        System.out.println("***ASSIGN CHIP****");
        System.out.println(playerName + " has been assigned: " + chip + " chip");
    }
    
    public void selectChipPosition(String playerName, Character chip)
    {
        System.out.println("***SELECT CHIP POSITION****");
        Scanner in = new Scanner(System.in);
        
        viewBoard();
        
        //if there is a valid number in the first position of the Scanner(between 1-7), 
        //it will take first character
        
        do
        {
            System.out.println(playerName + "("+chip+")" +  ",  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()
    {
        int j=0;
        int rowOnBoard=0;
        
        if (isGameOver)
        {
            System.out.println("\n****CURRENT BOARD****: " + "***Congratulations " + playerNameChipValue);
        }
        else
        {
            System.out.println("\n****CURRENT BOARD****");
        }
        
        //it might be better viewing the board using StringBuilder
        //this will ensure that the board larger and visible given that its a pivotal user experience
        StringBuilder [] sb  = new StringBuilder[13];
        
        for (int i=0; i<sb.length;i++)
        {
            if (i%2==0)
            {
                sb[i+j] =   new StringBuilder("|---|---|---|---|---|---|---|");
            }
            else
            {
                sb[i]=new StringBuilder("");
                
                if (rowOnBoard!=board.length)
                {
                    for (int m=0; m<board[0].length;m++)
                    {
                        if (m==0)
                        {
                            sb[i].append("|"+" " +board[rowOnBoard][m]+ " |");
                        }
                        else
                        {
                            sb[i].append(""+" "+board[rowOnBoard][m]+ " |");
                        }
                    }
                    rowOnBoard++;
                }
            }
        }
        
        for (StringBuilder gg: sb)
        {
            System.out.println(gg.toString());
        }
    }
    
    public ConnectFour() 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        System.out.println("*********Welcome to CONNECT 4**************");
        System.out.println("NOTE: Code will not terminate on a win, see onscreen messages");
        
        board = new String [][]   { {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"},
                                };
        
        boardHeightZeroIndex = board.length-1;
        boardHeight=board.length;
        
        viewBoard();
        configurePlayers();
        PlayerOne plOne = new PlayerOne (this, playerOneName,playerTwoName, plTwo);
    }
}