Search code examples
javaarraysconsole-application

Why is my Connect 4 recreation not counting consecutive pieces correctly?


I'm coding in an IDE for my final Computer Science 1 assignment of the year. I can't figure out why when a player puts pieces next to each other, the horizontalLeft and horizontalRight variables aren't increasing?

//Sam Stevens
import java.util.*;
public class MyProgram
{
    public static void main(String[] args)
    {
        String[][] gameboard = new String[7][7];
        //first value "i" is the height, second "j" is the width.
        for(int i = 0; i< gameboard.length; i++){
        for(int j = 0; j< gameboard[0].length; j++){
        gameboard[i][j] = "_";
        }
        }
        //Initialize Gameboard
        print(gameboard);
        for(int i = 0; i < 10; i++){
        play(gameboard);
        }
        //this for loop makes the game last 10 turns
    }
        
        public static void print(String[][] gb){
            System.out.println("\n");
        for(int i = 0; i< gb.length; i++){
        for(int j = 0; j< gb[0].length; j++){
        System.out.printf("%-3s ", gb[i][j]);
        }
        System.out.println();
        }
        }
        //print out the gameboard
    
        public static void play(String[][] gb){
            Scanner input = new Scanner(System.in);
            boolean cont = true;
            do{
                System.out.println("Player 1, where would you like to go? (1,2,3,4,5,6,7)");
                int p1choice = input.nextInt() - 1;
                boolean moving = true;
                int i = 6;
                int j = p1choice;
                int yIndex = 0;
                do{
                    if(i>=0){
                    if(gb[i][j] == "_"){
                        gb[i][j] = "1";
                        moving = false;
                        yIndex = i;
                    }
                    i--;
                    }
                    else{
                        System.out.println("Invalid move. Sucks to be you.");
                        moving = false;
                    }
                }while(moving);
                print(gb);
                checkWin(gb, "1", p1choice, yIndex);
                
                System.out.println("Player 2, where would you like to go? (1,2,3,4,5,6,7)");
                int p2choice = input.nextInt() - 1;
                moving = true;
                i = 6;
                j = p2choice;
                int yIndexP2 = 0;
                do{
                    if(i>=0){
                    if(gb[i][j] == "_"){
                        gb[i][j] = "2";
                        moving = false;
                        yIndexP2 = i;
                    }
                    i--;
                    }
                    else{
                        System.out.println("Invalid move. Sucks to be you.");
                        moving = false;
                    }
                }while(moving);
                print(gb);
                checkWin(gb, "2", p2choice, yIndexP2);
            }while(!cont);
        }
        
        public static void checkWin(String[][] gb, String p, int heightIndex, int widthIndex){
            //p tells us which player we are checking for and we can use it as which piece we're looking for.
            int horizontalLeft = 0;
            int horizontalRight = 0;
            int verticalUp = 0;
            int verticalDown = 0;
            
            //check pieces to the left
            boolean leftContinue = true;
            if(widthIndex != 0){
                int leftCount = 1;
                do{
                    if(gb[heightIndex][widthIndex - leftCount] == p){
                        horizontalLeft ++;
                        leftCount ++;
                    }
                    else{
                        leftContinue = false;
                    }
                }while(leftContinue && (widthIndex - leftCount >= 0));
            }
            
            //check pieces to the right
            boolean rightContinue = true;
            if(widthIndex != 6){
                int rightCount = 1;
                do{
                    if(gb[heightIndex][widthIndex + rightCount] == p){
                        horizontalRight ++;
                        rightCount ++;
                    }
                    else{
                        rightContinue = false;
                    }
                }while(rightContinue && (widthIndex + rightCount <= 6));
            }
            
            System.out.println(p + " "+ horizontalLeft + " "+ horizontalRight);
            //BELOW HERE SHOULD BE ONLY CHECK WIN
            if(horizontalLeft + horizontalRight >= 3){
                System.out.println("Game over. Player " + p + " wins!");
            }
        }
}

I've tried moving the "leftCount/rightCount ++;" lines, but when I do that, the game mysteriously hangs when a player puts two of their own pieces one on top of another strictly on the far right side. I'm stumped. Apologies if I didn't format this question correctly, this is my first post.


Solution

  • The error occurs with the checkWin method parameters.

    The heightIndex and widthIndex should be swapped.

    void checkWin(String[][] gb, String p, int widthIndex, int heightIndex)
    
    _   _   _   _   _   _   _   
    _   _   _   _   _   _   _   
    _   _   _   _   _   _   _   
    _   _   _   _   _   _   _   
    _   _   _   _   _   _   2   
    _   _   _   _   _   _   2   
    1   1   1   1   _   _   2   
    1 3 0
    Game over. Player 1 wins!