Connect Four
Connect Four© is a game in which two players, Black and Green, alternate placing chips in one of seven columns. Each column can contain up to six chips. The chips, inserted at the top of a column, slide down the column and come to rest above the last chip placed in that column. A player wins the game when there are four chips in a row vertically, horizontally, or diagonally, of his color.
a. A configuration of the game is a 6 7 integer array, board, such that
i. board[i][j] 1 indicates that a green chip occupies position ( i,j );
ii. board[i][j] 2 indicate that a black chip occupies position ( i,j ); and
iii. board[i][j] 0 indicates that position( i,j ) is empty.
The confi guration that models the picture of Figure 7.44 is
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 2 0 0 0 0
0 2 2 1 0 2 0
0 1 2 1 1 1 0
Write a method that accepts a game configuration and determines whether or not a player has won, returning ‘G’ for green, ‘B’ for black, or ‘N’ for neither. Test your method in a program that reads a board configuration and reports the winner, if there is one.
b. Write a method
such that confi guration is a board confi guration, column is an integer in the range 0 to 6, and color is either ‘G’ or ‘B’. The method makeMove(...) updates the board confi guration by placing a chip of the specifi ed color in the appropriate column. If a column is full, then the method simply returns 0, otherwise the method performs the update and returns 1, indicating a successful move.
c. Write a method that plays the game interactively against the computer. Your method must check for illegal moves and report when the game is over. The computer can play by choosing a random column and retrying if the move is illegal. However, to make the game more interesting, you might devise a “strategy” for the computer’s move. You need to check if the game ends in a tie. Include your methods in a program that plays the game.