0

So this program works pretty well except for one part. When I put a checker down. say I place Red into 1 and then try to place Blue into 1, it should stack on top of the Red but instead it replaces it the Red one. I will post the entire code. Anything helps!

package com.company; import java.util.Scanner;

public class ConnectFour {
    public static String[][] makeboard() {
        String[][] CJ = new String[7][15];

        for (int i = 0; i < CJ.length; i++) {

            for (int c = 0; c < CJ[i].length; c++) {
                if (c % 2 == 0)
                    CJ[i][c] = "|";
                else CJ[i][c] = " ";
                if (i == 6) CJ[i][c] = "-";
            }
        }
        return CJ;

    }

    public static void printboard(String[][] CJ) {

        for (int i = 0; i < CJ.length; i++) {
            for (int c = 0; c < CJ[i].length; c++) {
                System.out.print(CJ[i][c]);
            }
            System.out.println();
        }
    }

//this part does not work

    public static void Red(String[][] CJ,String player1) {

        System.out.println(player1+ " enter 0-6 to place checker");
        Scanner scnr = new Scanner(System.in);

        int g = (2 * (scnr.nextInt()) + 1);
        for (int i = 5; i >= 0; i--) {
            if (CJ[i][g] == " ") ;
            {
                CJ[i][g] = "R";
                break;
            }
        }
    }

//this part does not work

    public static void Blue(String[][] CJ, String player2) {

        System.out.println(player2 +" enter 0-6 to place checker");
        Scanner scnr = new Scanner(System.in);

        int g = (2 * (scnr.nextInt()) + 1);
        for (int i = 5; i >= 0; i--) {
            if (CJ[i][g] == " ") ;
            {
                CJ[i][g] = "B";
                break;

            }
        }
    }

//

    public static String checkforWinner(String[][]CJ){


        for( int i=0; i<6; i++){
            for(int j=0; j<7; j++){
                if      ((CJ[i][j+1] != " ")
                        && (CJ[i][j+3] != " ")
                        && (CJ[i][j+5] != " ")
                        && (CJ[i][j+7] != " ")
                        && ((CJ[i][j+1] == CJ[i][j+3])
                        && (CJ[i][j+3] == CJ[i][j+5])
                        && (CJ[i][j+5] == CJ[i][j+7])))
                    return CJ[i][j+1];
            }
        }
        for (int j=1; j<15; j+=2){


            for (int i =0; i<3; i++){
                if      ((CJ[i][j] != " ")
                        && (CJ[i+1][j] != " ")
                        && (CJ[i+2][j] != " ")
                        && (CJ[i+3][j] != " ")
                        && ((CJ[i][j] == CJ[i+1][j])
                        && (CJ[i+1][j] == CJ[i+2][j])
                        && (CJ[i+2][j] == CJ[i+3][j])))
                    return CJ[i][j];


            }
        }
        for (int i=0;i<3;i++){

            for (int k=7; k<15; k+=2){
                if ((CJ[i][k] != " ")
                        &&(CJ[i+1][k-2] != " ")
                        &&(CJ[i+2][k-4] != " ")
                        &&(CJ[i+3][k-6] != " ")
                        &&((CJ[i][k] == CJ[i+1][k-2])
                        &&(CJ[i+1][k-2]==CJ[i+2][k-4])
                        &&(CJ[i+2][k-4]==CJ[i+3][k-6])))
                    return CJ[i][k];
            }
        }
        return null;
    }
    public static void main(String[] args){
        Scanner scnr = new Scanner(System.in);
        System.out.println("Player one enter your name.");
        String player1= scnr.nextLine();
        System.out.println("Player two enter your name.");
        String player2 = scnr.nextLine();
        System.out.println(player1 + " is Red and " + player2 + " is Blue");
        String[][] CJ= makeboard();
        boolean playing = true;
        int count = 0;
        printboard(CJ);
        while(playing){
            if(count %2==0) Red(CJ,player1);
            else  Blue(CJ,player2);
            count++;
            printboard(CJ);}
            if (checkforWinner(CJ) != null){
                if (checkforWinner(CJ) =="R")
                    System.out.println(player1 + " wins!");
                else if (checkforWinner(CJ) == "B")
                    System.out.println(player2 + " wins!");

            }
        }


    }
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • the problem is at this line.. if (CJ[i][g] == " ") ;... remove the ; at the end.. the ";" is the same as writing if(CJ[i][g] == " ") {//do nothing} – telefunken Nov 12 '15 at 23:46
  • That was it! Thank you! – user5499053 Nov 12 '15 at 23:48
  • I was going to post a more complete answer, but it seems this is marked as duplicate... Although, the link referenced above does not seem like an answer to this question (nor does the question seem similar) – telefunken Nov 12 '15 at 23:48

0 Answers0