Question: Implement a Connect 4 game. The program should allow two players to play each other. For each turn, have the user enter the column they
Implement a Connect 4 game. The program should allow two players to play each other. For each turn, have the user enter the column they want to play in, validate the user has made a good move, and then place the piece on the board. Then the program should check for a winner and a tie, and assuming there is neither, redisplay the board and get the next players move. Display the moves using print/println commands, and use an X for player 1 and an O for player 2. When the game ends, the program should display congratulations to the winner or announce there was a tie, and then allow the players to choose to play again or exit. You must use a 2D array for this lab.
Language : java
Shell:
import java.util.Scanner;
public class Lab6Shell
{
public static void main(String args[])
{
// variables
Scanner input = new Scanner(System.in);
char[][] board = new char[7][8];
boolean finished = false;
boolean gameOver = false;
char currentPlayer = 'X';
int numMoves = 0;
// loop until user wants to stop
do
{
// init game board
// initialize variables
// display the board
DisplayBoard(board);
// loop until this game is over
do
{
// get the next move for the current
player
int columnChosen = 0;
// place piece
// increment number of moves
// display the board
DisplayBoard(board);
// check for win
if (CheckForWin(board))
{
// if winner, display
congratulations and set gameOver true
}
else if (numMoves == 42)
{
// if tie, display result
and set gameOver true
}
else
{
// switch current player
}
} while (!gameOver);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
