/** * Matt Kretchmar * February 28, 2006 * ConnectFour.java * This program implements a version of the connect four board game. Two * players alternate placing chips in columns of a square board attempting to obtain * four in a row of their color. Player 1 (white chips) goes first while, * player 2 has black chips. A player must get four in a row horizontally * or vertically (diagonal does not count). When placing chips in a column, * the new chip must "fall" to the bottom of the column. */ import java.util.Scanner; class ConnectFour { final static int ROWS = 6; // board has 6 rows final static int COLS = 6; // and 6 columns public static void main ( String [] args ) { Scanner keyboard = new Scanner(System.in); int board[][] = new int[ROWS][COLS]; int i, j; int moves = 0; // Initialize the board for a new game for ( i = 0; i < ROWS; i++ ) { for ( j = 0; j < COLS; j++ ) { board[i][j] = 0; } } //============================================ // repeat until a player wins a game //============================================ boolean player1Wins = false; boolean player2Wins = false; int turn = 1; // player 1 goes first while ( !player1Wins && !player2Wins && moves < ROWS*COLS) { // Display the board System.out.print("+-"); for ( j = 0; j < COLS; j++ ) System.out.print((j+1) + "-"); System.out.println("+"); for ( i = ROWS-1; i >= 0; i-- ) { System.out.print("| "); for ( j = 0; j < COLS; j++ ) { char c = ' '; switch ( board[i][j] ) { case 0 : c = ' '; break; case 1 : c = 'w'; break; case 2 : c = 'b'; break; } System.out.print(c + " "); } System.out.println("|"); } System.out.print("+-"); for ( j = 0; j < COLS; j++ ) System.out.print("--"); System.out.println("+"); // Prompt for an input int column; do { System.out.println("Player " + turn + " enter column to play a move: "); column = keyboard.nextInt() - 1; if ( column < 0 || column >= COLS || board[ROWS-1][column] > 0 ) System.out.println("Invalid column or column already full, try another\n"); } while ( column < 0 || column >= COLS || board[ROWS-1][column] > 0 ); moves++; // put move into right row of indicated column i = 0; while ( board[i][column] > 0 ) i++; board[i][column] = turn; // next player's turn if ( turn == 1 ) turn = 2; else turn = 1; //================================= // now test for winner //================================= // test each row to see if there are four in a row for ( i = 0; i < ROWS; i++ ) { int current = board[i][0]; j = 1; int count = 1; while ( j < COLS ) { if ( board[i][j] == current && board[i][j] != 0 ) { count++; } else { current = board[i][j]; count = 1; } if ( count == 4 ) { if ( current == 1 ) player1Wins = true; else player2Wins = true; } j++; } } // test each col to see if there are four in a row for ( j = 0; j < COLS; j++ ) { int current = board[0][j]; i = 1; int count = 1; while ( i < ROWS ) { if ( board[i][j] == current && board[i][j] != 0 ) { count++; } else { current = board[i][j]; count = 1; } if ( count == 4 ) { if ( current == 1 ) player1Wins = true; else player2Wins = true; } i++; } } } // draw board once more System.out.print("+-"); for ( j = 0; j < COLS; j++ ) System.out.print((j+1) + "-"); System.out.println("+"); for ( i = ROWS-1; i >= 0; i-- ) { System.out.print("| "); for ( j = 0; j < COLS; j++ ) { char c = ' '; switch ( board[i][j] ) { case 0 : c = ' '; break; case 1 : c = 'w'; break; case 2 : c = 'b'; break; } System.out.print(c + " "); } System.out.println("|"); } System.out.print("+-"); for ( j = 0; j < COLS; j++ ) System.out.print("--"); System.out.println("+"); // Now declare a winner if ( player1Wins ) System.out.println("Player 1 wins!!!"); else if ( player2Wins ) System.out.println("Player 2 wins!!!"); else System.out.println("Tie game."); } }