
/*
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;

interface playConnectFour
{
    public void insertChip();
    public boolean checkAvailability(int input, int colour);
    public boolean checkConnectFour(int colour);
    public void viewBoard();
}

class connectFour implements playConnectFour 
{
    int [][] board;
    int rows = 6;
    int columns = 7;
    int j;
    int input;
    
    board.length();
    
    Player1 p1 = new Player1(this, board);  // not sure why this object ref needs to be passed down
    //Player2 p2 = new Player2(this,board);
    
    
    public connectFour(int[][] board)
    {
        this.board=board;
        
    }
    
    public void insertChip()
    {
        
    }
    
    /*
    public void viewBoard()
    {
        for (int p=0;p<b
        
    }
    */
    
    public boolean checkConnectFour(int colour)
    {
        //this will have to check if 4 in a row.
        
        // this will check first in the vertical direction
        
        //int rowsBelow = j;
        //int rowsAbove = (rows-1) -j;   // 1 has been subtracted due to z index in variable j
        int vertical=0;
        int horizontal=0;
        
        // CHECKING VERTICALLY
        
        // need logic here that if there is not a vertical yellow, it will set count back to 0
        // not so easy since it needs to count upwards and downwards
        
        for (int k = j; k>=0; k--)  // this is checking all rows below (same column) for yellows
        {
            if (board[k][input]==colour)  //there is also a yellow in that position
            {
                vertical++;
                
            }
            
            if (board[k][input]==0)
            {
                vertical=0;
                
            }
        }
        
         for (int k = j; k<=7; k++)  // this is checking all rows below (same column) for yellows
        {
            if (board[k][input]==colour)  //there is also a yellow in that position
            {
                vertical++;
                
            }
            
            if (board[k][input]==0)
            {
                vertical=0;
                
            }
            
        }
        
        if (vertical==3)
        {
            System.out.println("connect 4");
            //perhaps need a function here to display the board array.
            return true;
        }
        
        //return false;
        
        
        // CHECKING HORIZONTALLY
        // WITH HORIZONTAL CHECK, NEED TO CHECK EACH LAYER
        // SO NEED ANOTHER FOR LOOP
        // to the right
        
        for (int j=0; j<rows;j++)
        {
        for (int m = input; m<=columns; m++)  // this is checking the same row to the right for yellows..
        // it is adding 1 to colsRight to ensure 5 cols are checked
        {
            if (board[j][m]==colour)  //there is also a yellow in that position
            {
                horizontal++;
                
            }
            
            if (board[j][m]==0)
            {
                horizontal=0;
                
            }
            
        }
        }
        
        
        // to the left
         
         for (int j=0; j<rows;j++)
        {
         
         for (int m = input; m>=0; m--)  // this is checking all rows below (same column) for yellows
        {
            if (board[j][m]==colour)  //there is also a yellow in that position
            {
                horizontal++;
                
            }
            
            if (board[j][m]==0)
            {
                vertical=0;
                
            }
            
        }
        }
        
        
        if (horizontal==3)
        {
            System.out.println("connect 4");
            //perhaps need a function here to display the board array.
            return true;
        }
        
        return false;
    
}



// CHECKING DIAGONALLY
        
        for (int m = input; m<=columns-1; m++)   // this has to reduce columns by 1 to ensure no overrun diagonal check
        {
            if (board[j+1][m+1]==colour)  //there is also a yellow in that position
            {
                diagnonal++;
                
            }
            
            if (board[j+1][m+1]==0)
            {
                diagnonal=0;
                
            }
            
            
            if (board[j-1][m+1]==colour)  //there is also a yellow in that position
            {
                diagnonal++;
                
            }
            
            if (board[j-1][m+1]==0)
            {
                diagnonal=0;
                
            }
            
            
            
        }
        
         
         for (int m = input; m>0; m--)  // m is set greater than 0 to allow diagnonal to function
        {
            if (board[j-1][m-1]==colour)  //there is also a yellow in that position
            {
                horizontal++;
                
            }
            
            if (board[j][m]==0)
            {
                vertical=0;
                
            }
            
        }
        
        
        
        if (horizontal==3)
        {
            System.out.println("connect 4");
            //perhaps need a function here to display the board array.
            return true;
        }
        
        return false;
    
}



    
    
    public boolean checkAvailability(int input, int colour)
    {
        this.input=input;
        boolean availability=false;
        //yellow is being assigned value 1
        
        System.out.println("This is the input:" + input);
        
        for (j=0; j<7;j++)
        {
            System.out.println("value of j: " + j);
            
            //System.out.println(board[0][1]);
            System.out.println(board[j][input]);
            
            
            // the coin will drop to lowest position on the grid
            
            if (board[j][input]==0)   // all default values in declared array is 0
            {
                board[j][input]=1;  // this will input a 1 to denote yellow if available
                availability=true;
                checkConnectFour(1);
                break;
                
            }
            
        }
        
        
        
        if (availability)
        {
            return true;
            
        }
        
        p1.insertYellowChip();
        
        return false;
        
    }
    
}


public class Main
{
    public static void main(String[] args) {
    System.out.println("Welcome to Online IDE!! Happy Coding :)");
    
    int [][] board = new int [5][6];   // this ensures  7 columns and 6 rows
    
    connectFour cf = new connectFour(board);
    
    }
    
}

class Player1
{
    int[][] currentBoard;
    connectFour cf;
    chips yellow;
    
    Player2 p2;
    
    enum chips
    {
        YELLOW, RED;
    }
    
    
    public Player1(connectFour cf, int[][] currentBoard)
    {
        this.currentBoard=currentBoard;
        this.cf=cf;
        chips yellow = chips.YELLOW;
        this.yellow=yellow;
        
        
        insertYellowChip(yellow);
        p2=new Player2(currentBoard);
        
    }
    
   
    public void insertYellowChip(chips yellow)
    {
        System.out.println("Which column would you like to insert the coin");
        Scanner scan = new Scanner(System.in);
        int selection = scan.nextInt();
        
        cf.checkAvailability(selection,1);
        
        
    }
    
    public void insertYellowChip()
    {
        insertYellowChip(yellow);
    }
    
    
}


class Player2
{
    int[][]currentBoard;
    
    enum chips
    {
        YELLOW, RED;
    }
    
    
    public Player2(int[][] currentBoard)
    {
        this.currentBoard=currentBoard;
        //chips yellow = chips.YELLOW;
        chips red = chips.RED;
        insertRedChip(red);
    }
    
    public void insertRedChip(chips red)
    {
        
    }
    
    
}