/*
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(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 checkTicTacToe(int pos, int columnChipPlacedZeroIndex, Character chipValue, String checkConnectFour);
    public void viewBoard();
}

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

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(TicTacToe noughtsCrosses, 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;
        noughtsCrosses.assignChip(playerOneName, yellowValue);    
        noughtsCrosses.selectChipPosition(playerOneName,yellowValue);
        noughtsCrosses.checkAvailability(noughtsCrosses.inputAsIntegerZeroIndex,yellowValue, this, plTwo, (playerOneName+"(Player 1)"));
        noughtsCrosses.checkTicTacToe(noughtsCrosses.inputAsIntegerZeroIndex, noughtsCrosses.rowChipPlacedZeroIndex, yellowValue, playerOneName);
        
        plTwo = new PlayerTwo (noughtsCrosses, 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(TicTacToe noughtsCrosses, String playerOne, String playerTwo, PlayerOne plOne)
    {
        this.board=board;
        Chips red = Chips.RED;
        Character redValue = red.value;
        this.playerTwoName=playerTwo;
        this.playerOneName=playerOne;
        noughtsCrosses.assignChip(playerTwoName, redValue); 
        noughtsCrosses.selectChipPosition(playerTwoName,redValue);
        noughtsCrosses.checkAvailability(noughtsCrosses.inputAsIntegerZeroIndex,redValue, plOne, this, (playerTwoName+"(Player 2)"));
        noughtsCrosses.checkTicTacToe(noughtsCrosses.inputAsIntegerZeroIndex, noughtsCrosses.rowChipPlacedZeroIndex, redValue, playerTwoName);
        plOne = new PlayerOne (noughtsCrosses, playerOneName,playerTwoName, this);
    }
}

class TicTacToe implements Playable
{
    boolean hasConfigureConsecutiveSymbols;
    boolean hasConfigurePlayers;
    boolean hasConfigureBoardSize;
    int numberConsecutiveForWin;
    String regexOptions;
    Pattern patternCols;
    Pattern patternRows;
    Scanner scanner;
    String[][] board;
    Matcher matcherRows;
    Matcher matcherCols;
    Integer inputInteger;
    Character inputCharColumn;
    Character inputCharRow;
    int inputAsIntegerZeroIndex;
    int columnChipPlacedZeroIndex;
    int rowChipPlacedZeroIndex;
    int rowChipPlaced;
    int columnChipPlaced;
    int boardHeightZeroIndex=0;
    int boardBaseLevelZeroIndex;
    int boardWidth;
    int boardWidthZeroIndex = boardWidth-1;
    int boardFirstColumnZeroIndex = 0;
    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("***CHECKING AVAILABILITY****" + "\tBoard height: " +boardHeight + "\tboard Width: " + boardWidth + "\tnumber consecutive for win: " +  numberConsecutiveForWin);
        
        for (int k=boardBaseLevelZeroIndex; k>=boardHeightZeroIndex; k--)
        {
            if (board[k][input].equals("-"))
            {
                board[k][input]= String.valueOf(chipValue);
                
                rowChipPlacedZeroIndex=k;      
                rowChipPlaced=Math.abs((rowChipPlacedZeroIndex-boardHeight));
                
                System.out.println("Chip: " + chipValue + " will be placed into column: " + (columnChipPlaced) + " row: " + rowChipPlaced);
                lastChipName = name;
                lastChipValue = chipValue;
                
                if (rowChipPlaced==boardHeight)
                {
                    isAvailable=false;
                }
                else
                {
                    isAvailable=true;
                    break;
                }
            }
        }
        
        if (!isAvailable)
        {
            if (columnsFilled==boardWidth)
            {
                System.out.println("GAME OVER - NO WINNER");
                System.exit(0);
            }
            
            if(!columnFull[columnChipPlacedZeroIndex])
            {
                columnFull[columnChipPlacedZeroIndex]=true;
                System.out.println("Column: " + columnChipPlaced + " is NOW full.");
                columnsFilled++;
                System.out.println("AVAILABILITY: Total of  " + columnsFilled + " columns filled");
            }
               
            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****");
    }
    
    public boolean checkTicTacToe(int pos, int row, Character chipValue, String playerName)
    {
        System.out.println("***CHECK CONNECT FOUR****");
        
        playerNameChipValue = playerName + "("+chipValue+")";
        
        System.out.println("VERTICAL CHECK => DOWNWARDS*****");
       
        if (rowChipPlacedZeroIndex!=boardBaseLevelZeroIndex)
        {
            for (int count = rowChipPlacedZeroIndex; count<=boardBaseLevelZeroIndex; count++)
            {
                if (!board[count][columnChipPlacedZeroIndex].equals(String.valueOf(chipValue)))
                {
                    sameColourTotal=0;
                    break;
                }
                
                if (board[count][columnChipPlacedZeroIndex].equals(String.valueOf(chipValue)))
                {
                    sameColourTotal++;
                    
                    if (sameColourTotal==4)
                    {
                        System.out.println("***1Congratulations " + playerNameChipValue + " Connect 4 in column:" + columnChipPlaced);
                        isGameOver=true;
                    }
                }
            }
            sameColourTotal=0;
        }
        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=0;
                    break;
                }
                
                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;
                    }
                }
            }
            
            runningTotalSameColour=sameColourTotal;
            sameColourTotal=0;
        }
        
        System.out.println("HORIZONTAL CHECK => LEFT*****");
        System.out.println(sameColourTotal);
        System.out.println(runningTotalSameColour);
        
        if (columnChipPlacedZeroIndex!=boardFirstColumnZeroIndex)
        {
            for (int count = columnChipPlacedZeroIndex; count>=boardFirstColumnZeroIndex; count--)
            {
                if (!board[rowChipPlacedZeroIndex][count].equals(String.valueOf(chipValue)))
                {
                    sameColourTotal=0;
                    break;
                }
                
                if (board[rowChipPlacedZeroIndex][count].equals(String.valueOf(chipValue)))
                {
                    sameColourTotal++;
                    
                    if (rightColour)
                    {
                        runningTotalSameColour=runningTotalSameColour-1;
                    }
                    
                    if (sameColourTotal+runningTotalSameColour==4)
                    {
                        System.out.println("***3Congratulations " + playerNameChipValue + "  Connect 4 in row:" + rowChipPlaced);
                        isGameOver=true;
                        return true;
                    }
                }
            }
            offset=0;
            sameColourTotal=0;
            runningTotalSameColour=0;
            rightColour=false;
        }
        System.out.println("DIAGONAL CHECK 1");  
    
        if (columnChipPlacedZeroIndex!=boardWidthZeroIndex && rowChipPlacedZeroIndex!=boardHeightZeroIndex)
        {
            System.out.println("INSIDE D1 - Diagonal north east check");
            
            for (int count = rowChipPlacedZeroIndex; count>=boardHeightZeroIndex; count--)
            {
                if (columnChipPlacedZeroIndex+offset<=boardWidthZeroIndex)
                {
                    if (!board[count][columnChipPlacedZeroIndex+offset].equals(String.valueOf(chipValue)))
                    {
                        sameColourTotal=0;
                        break;
                    }
                
                    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;
        }
        
        System.out.println("DIAGONAL CHECK 2"); 
                                                                         
        if (columnChipPlacedZeroIndex!=boardFirstColumnZeroIndex && rowChipPlacedZeroIndex!=boardBaseLevelZeroIndex)
        {
            System.out.println("INSIDE D2 - Diagonal south west check");
            
            for (int count = rowChipPlacedZeroIndex; count<=boardBaseLevelZeroIndex; count++)
            {
                if (columnChipPlacedZeroIndex-offset>=boardFirstColumnZeroIndex)
                {
                    if (!board[count][columnChipPlacedZeroIndex-offset].equals(String.valueOf(chipValue)))
                    {
                        sameColourTotal=0;
                        break;
                    }
                    
                    if (board[count][columnChipPlacedZeroIndex-offset].equals(String.valueOf(chipValue)))
                    {
                        sameColourTotal++;
                        offset++;
                        
                        if (rightColour)
                        {
                            runningTotalSameColour=runningTotalSameColour-1;
                        }
                        
                        if (sameColourTotal+runningTotalSameColour==4)
                        {
                            System.out.println("***6Congratulations " + playerNameChipValue + "  Connect 4 in diagonal");
                            isGameOver=true;
                            return true;
                        }
                    }
                }
            }
            runningTotalSameColour=0;
            rightColour=false;
            sameColourTotal=0;
            offset=0;
        }
        System.out.println("DIAGONAL CHECK 3"); 
                                      
        if (columnChipPlacedZeroIndex!=boardWidthZeroIndex && rowChipPlacedZeroIndex!=boardBaseLevelZeroIndex)
        {
            System.out.println("INSIDE D3 - Diagonal south east check");
                                                        
            for (int count = rowChipPlacedZeroIndex; count<=boardBaseLevelZeroIndex; count++)
            {
                if (columnChipPlacedZeroIndex+offset<=boardWidthZeroIndex)               
                {
                    if (!board[count][columnChipPlacedZeroIndex+offset].equals(String.valueOf(chipValue)))
                    {
                        sameColourTotal=0;
                        break;
                    }
                    
                    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;
        }
        
        System.out.println("DIAGONAL CHECK 4"); 
        
        if (columnChipPlacedZeroIndex!=boardFirstColumnZeroIndex && rowChipPlacedZeroIndex!=boardHeightZeroIndex)
        {
            System.out.println("INSIDE D4 - Diagonal north west check");
                
            for (int count = rowChipPlacedZeroIndex; count>=boardFirstColumnZeroIndex; count--)
            {
                if (columnChipPlacedZeroIndex-offset>=boardFirstColumnZeroIndex)
                {
                    if (!board[count][columnChipPlacedZeroIndex-offset].equals(String.valueOf(chipValue)))
                    {
                        sameColourTotal=0;
                        break;
                    }
                    
                    if (board[count][columnChipPlacedZeroIndex-offset].equals(String.valueOf(chipValue)))
                    {
                        sameColourTotal++;
                        offset++;
                        
                        if (rightColour)
                        {
                            runningTotalSameColour=runningTotalSameColour-1;
                        }
                    
                        if (sameColourTotal+runningTotalSameColour==4)
                        {
                            System.out.println("***9Congratulations " + playerNameChipValue + "  Connect 4 in diagonal");
                            isGameOver=true;
                            return true;
                        }
                    }
                }
            }
            rightColour=false;
            runningTotalSameColour=0;
            sameColourTotal=0;
            offset=0;
        }
        return false;
    }
    
    public void menu()
    {
        regexOptions="[12345]";
        
        do
        {
        
        scanner = new Scanner(System.in);
        patternRows = Pattern.compile(regexOptions, Pattern.CASE_INSENSITIVE);
        
        System.out.println("***MENU****");
        System.out.println("1. Configure Players");
        System.out.println("2. Configure board size");
        System.out.println("3. Configure consecutive symbols for win");
        System.out.println("4. Start game");
        System.out.println("5. Exit");
        System.out.println("Please make a choice");
        
        inputInteger = scanner.nextInt();
        matcherRows = patternRows.matcher(String.valueOf(inputInteger));
        
        }while (!matcherRows.find());
        
        System.out.println("SELECTED: " + inputInteger);
        
        switch(inputInteger)
        {
            case 1:
                configurePlayers();
                menu();
                break;
                
                case 2:
                    System.out.println("HERE");
                    configureBoardSize();
                    menu();
                    break;
                    
                    case 3:
                        if (hasConfigureBoardSize)
                        {
                            configureConsecutiveSymbols();
                        }
                        else
                        {
                            System.out.println("Please complete\t\t2.Configure board size");
                            
                        }
                        menu();
                        break;
                        
                        case 4:
                            if (hasConfigureBoardSize && hasConfigurePlayers && hasConfigureConsecutiveSymbols)
                            {
                                System.out.println("WELCOME: " + playerOneName + " , " + playerTwoName+"\n");
                                PlayerOne plOne = new PlayerOne (this, playerOneName,playerTwoName, plTwo);
                            }
                            else
                            {
                                System.out.println("Please complete 1,2,3");
                                menu();
                            }
                            
                            break;
                            
                            case 5:
                            break;
                            default:
                            System.exit(0);
        }
    }
    
    public void configureBoardSize()
    {
        do 
        {
        scanner = new Scanner(System.in);
        System.out.println("Enter width:");
        boardWidth = scanner.nextInt();
        System.out.println("Enter height:");
        boardHeight = scanner.nextInt();
        }while (((boardHeight==3 && boardWidth==1) || (boardHeight==1 && boardWidth==3))); 
        
        //need to fill board now
        board = new String[boardHeight][boardWidth];
        
        System.out.println(boardHeight);
        System.out.println(boardWidth);
        
        for (int i=0; i<boardHeight;i++)
        {
            for (int j=0; j<boardWidth;j++)
            {
                board[i][j]="-";
            }
        }
        
        /*
        //Need to move this away
        //We can define customise board sizes here as required. All other areas will fall into place
        board = new String [][]   { {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"}
                                };
                                
                                */
        
        boardBaseLevelZeroIndex = board.length-1;
        boardHeight=board.length;
        boardWidth=board[0].length;
        hasConfigureBoardSize=true;
    }
    
    public void configureConsecutiveSymbols()
    {
        scanner = new Scanner(System.in);
        
        do
        {
            System.out.println("Number consecutive symbols in row/diagonal constituing a win (greater than 2):");
            numberConsecutiveForWin = scanner.nextInt();
            System.out.println("board width: " + boardWidth);
            System.out.println("numberConsecutiveForWin: " + numberConsecutiveForWin);
            
        }while (numberConsecutiveForWin==2);
              
        
        hasConfigureConsecutiveSymbols = true;
    }
    
    public void configurePlayers()
    {
        scanner = new Scanner(System.in);
        System.out.println("Enter name for Player 1:");
        playerOneName = scanner.next();
        System.out.println("Enter name for Player 2:");
        playerTwoName = scanner.next();
        
        hasConfigurePlayers=true;
    }
    
    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 = new Scanner(System.in);
        
        viewBoard();
        
        do
        {
            System.out.println(playerName + "("+chip+")" +  ",  Which column would you like to insert the chip?");
            inputCharColumn = scanner.next().charAt(0);
            
            System.out.println(playerName + "("+chip+")" +  ",  Which row would you like to insert the chip?");
            inputCharRow = scanner.next().charAt(0);
            
            //need to adjust this
            
            StringBuilder regex=new StringBuilder();
            int numColumnRegex=1;
            
            regex.append("[");
            do
            {
                regex.append(Integer.toString(numColumnRegex));
                numColumnRegex++;
                
            }while (numColumnRegex<=boardWidth);
            
            regex.append("]");
            regexOptions=regex.toString();
            System.out.println("AVAILABLE COLUMNS: " + regexOptions);
            
            patternCols = Pattern.compile(regexOptions, Pattern.CASE_INSENSITIVE);
            matcherCols = patternCols.matcher(Character.toString(inputCharColumn));
            
            regex=new StringBuilder();
            int numRowRegex=1;
            
            
            regex.append("[");
            do
            {
                regex.append(Integer.toString(numRowRegex));
                numRowRegex++;
                
            }while (numRowRegex<=boardHeight);
            
            regex.append("]");
            regexOptions=regex.toString();
            System.out.println("AVAILABLE COLUMNS: " + regexOptions);
            
            patternRows = Pattern.compile(regexOptions, Pattern.CASE_INSENSITIVE);
            matcherRows = patternRows.matcher(Character.toString(inputCharColumn));
            
        
        }while (!matcherRows.find() && !matcherCols.find());
        
        inputAsIntegerZeroIndex = Character.getNumericValue(inputCharColumn-1);
        columnChipPlacedZeroIndex = Character.getNumericValue(inputCharColumn-1);
        
        columnChipPlaced = Character.getNumericValue(inputCharColumn);
    }
    
    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****");
        }
        
        StringBuilder [] sb  = new StringBuilder[((board.length*2)+1)];
        
        for (int i=0; i<sb.length;i++)
        {
            if (i%2==0)
            {
                for (int f=0; f<board[0].length;f++)
                {
                    if (f==0)
                    {
                        sb[i+j]=new StringBuilder("|---|");
                    }
                    else
                    {
                        sb[i+j].append("---|");
                    }
                }
            }
            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 TicTacToe() 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        System.out.println("*********Welcome to TICTACTOE**************");
        System.out.println("NOTE: Code will not terminate on a win, see onscreen messages");
        
        
        menu();
        
        if (hasConfigureBoardSize)
        {
            viewBoard();
        }
    }
}