Question: JAVA: Connect Four I need help adding these elements when it is ran: Determine how many human players, Alternate printing the board, getting player moves
JAVA: Connect Four
I need help adding these elements when it is ran: Determine how many human players, Alternate printing the board, getting player moves until someone wins the game or the game ends in a tie. Also, the winning condition is to be the first to form a horizontal, vertical, or diagonal line of four of one's own discs.
I have the following code:
import java.util.Scanner; public class ConnectFour { public static String[][] createPattern() { String[][] f = new String[7][15]; for (int i =0;ifor (int j =0;jif (j% 2 == 0){ f[i][j] ="|"; } else { f[i][j] = " "; } if (i==6) f[i][j]= "-"; } } return f; } public static void printPattern(String[][] f) { for (int i =0;ifor (int j=0;jpublic static void dropRedPattern(String[][] f) { System.out.print("Drop a red disk at column (06): "); Scanner scan = new Scanner (System.in); int c = 2*scan.nextInt()+1; for (int i =5;i>=0;i--) { if (f[i][c] == " ") { f[i][c] = "R"; break; } } } public static void dropBlackPattern(String[][] f) { System.out.print("Drop a black disk at column (06): "); Scanner scan = new Scanner (System.in); int c = 2*scan.nextInt()+1; for (int i =5;i>=0;i--) { if (f[i][c] == " ") { f[i][c] = "B"; break; } } } public static String checkWinner(String[][] f) { for (int i =0;i<6;i++) { for (int j=0;j<7;j+=2) { if ((f[i][j+1] != " ") && (f[i][j+3] != " ") && (f[i][j+5] != " ") && (f[i][j+7] != " ") && ((f[i][j+1] == f[i][j+3]) && (f[i][j+3] == f[i][j+5]) && (f[i][j+5] == f[i][j+7]))) return f[i][j+1]; } } for (int i=1;i<15;i+=2) { for (int j =0;j<3;j++) { if((f[j][i] != " ") && (f[j+1][i] != " ") && (f[j+2][i] != " ") && (f[j+3][i] != " ") && ((f[j][i] == f[j+1][i]) && (f[j+1][i] == f[j+2][i]) && (f[j+2][i] == f[j+3][i]))) return f[j][i]; } } for (int i=0;i<3;i++) { for (int j=1;j<9;j+=2) { if((f[i][j] != " ") && (f[i+1][j+2] != " ") && (f[i+2][j+4] != " ") && (f[i+3][j+6] != " ") && ((f[i][j] == f[i+1][j+2]) && (f[i+1][j+2] == f[i+2][j+4]) && (f[i+2][j+4] == f[i+3][j+6]))) return f[i][j]; } } for (int i=0;i<3;i++) { for (int j=7;j<15;j+=2) { if((f[i][j] != " ") && (f[i+1][j-2] != " ") && (f[i+2][j-4] != " ") && (f[i+3][j-6] != " ") && ((f[i][j] == f[i+1][j-2]) && (f[i+1][j-2] == f[i+2][j-4]) && (f[i+2][j-4] == f[i+3][j-6]))) return f[i][j]; } } return null; } public static void main (String[] args) { String[][] f = createPattern(); boolean loop = true; int count = 0; printPattern(f); while(loop) { if (count % 2 == 0) dropRedPattern(f); else dropBlackPattern(f); count++; printPattern(f); if (checkWinner(f) != null) { if (checkWinner(f) == "R"){ System.out.println("Red won."); } else if (checkWinner(f)== "B") { System.out.println("Black won."); } loop = false; } } } } It should run similar to this output:
Enter the number of Human players (1 or 2) 0 0 1 2 3 4 5 6 +-+-+-+-+-+-+-+ | | | | | | | | +-+-+-+-+-+-+-+ | | | | | | | | +-+-+-+-+-+-+-+ | | | | | | | | +-+-+-+-+-+-+-+ | | | | | | | | +-+-+-+-+-+-+-+ | | | | | | | | +-+-+-+-+-+-+-+ | | | | | | | | +-+-+-+-+-+-+-+ 0 1 2 3 4 5 6 Computer Player 1 chooses column 3 0 1 2 3 4 5 6 +-+-+-+-+-+-+-+ | | | | | | | | +-+-+-+-+-+-+-+ | | | | | | | | +-+-+-+-+-+-+-+ | | | | | | | | +-+-+-+-+-+-+-+ | | | | | | | | +-+-+-+-+-+-+-+ | | | | | | | | +-+-+-+-+-+-+-+ | | | |G| | | | +-+-+-+-+-+-+-+ 0 1 2 3 4 5 6
and so forth and then end by saying there is a winner like this:
Computer Player 2 chooses column 2 0 1 2 3 4 5 6 +-+-+-+-+-+-+-+ | | | | | | | | +-+-+-+-+-+-+-+ | | | | | | | | +-+-+-+-+-+-+-+ | |G| | | | | | +-+-+-+-+-+-+-+ |R|R|R|R| | | | +-+-+-+-+-+-+-+ |G|G|R|G| |G|R| +-+-+-+-+-+-+-+ |G|R|G|G|R|R|G| +-+-+-+-+-+-+-+ 0 1 2 3 4 5 6 Player 2 WINS!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
