Unfortunately, as an AI language model, I am unable to generate entire working code for a game without any specific requirements and details. However, here is a brief outline of how to approach building a Connect Four (Lig4) game in Java:
-
Create a board: The Connect Four board consists of a grid of 6 rows and 7 columns. You can represent the board using a 2D array or a 1D array of size 42 (6 * 7).
-
Create players: The game requires two players to take turns dropping their colored discs into the board until a player wins or the game ends in a draw.
-
Play the game: Each player takes turns dropping a disc into one of the columns to form a horizontal, vertical, or diagonal line of 4 discs of their own color. The game will continue until a player wins or the board is completely filled.
-
Determine the winner: You should check the board after each player's move to see if they have formed a line of 4 discs. If a player forms a winning line, the game should end and the winner is declared. If the board fills up with no winner, the game ends in a tie.
-
Allow players to start a new game: After a game ends, ask the players if they want to start a new game. If yes, reset the board and let the players take turns again. If no, end the game.
Here is a sample code snippet of the game initialization:
import java.util.Scanner;
public class Lig4Game {
private static final int ROWS = 6;
private static final int COLUMNS = 7;
private static final char EMPTY_SLOT = ' ';
private static final char RED_SLOT = 'R';
private static final char YELLOW_SLOT = 'Y';
private char[][] board;
private boolean redTurn;
private boolean gameEnded;
private int movesCount;
public Lig4Game() {
board = new char[ROWS][COLUMNS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
board[i][j] = EMPTY_SLOT;
}
}
redTurn = true;
gameEnded = false;
movesCount = 0;
}
public void play() {
Scanner scanner = new Scanner(System.in);
while (!gameEnded) {
System.out.println("Current board:");
printBoard();
System.out.println(redTurn ? "Player 1's turn (RED)" : "Player 2's turn (YELLOW)");
System.out.print("Select a column (1-7): ");
int column = scanner.nextInt() - 1;
boolean isValidMove = dropDisc(column);
if (!isValidMove) {
System.out.println("Invalid move. Please select a different column.");
continue;
}
checkWinner();
togglePlayer();
}
scanner.close();
}
private boolean dropDisc(int column) {
if (column < 0 || column >= COLUMNS || board[0][column] != EMPTY_SLOT) {
return false;
}
int i;
for (i = ROWS-1; i >= 0; i--) {
if (board[i][column] == EMPTY_SLOT) {
board[i][column] = redTurn ? RED_SLOT : YELLOW_SLOT;
movesCount++;
return true;
}
}
return false;
}
private void checkWinner() {
if (movesCount < 7) {
return; // Not possible to form a winning line with less than 7 moves
}
int i, j;
// Check horizontal lines
for (i = 0; i < ROWS; i++) {
for (j = 0; j <= COLUMNS-4; j++) {
if (board[i][j] == EMPTY_SLOT) {
continue;
}
if (board[i][j] == board[i][j+1] && board[i][j] == board[i][j+2] && board[i][j] == board[i][j+3]) {
gameEnded = true;
System.out.println("Player " + (redTurn ? 1 : 2) + " wins!");
return;
}
}
}
// Check vertical lines
for (i = 0; i <= ROWS-4; i++) {
for (j = 0; j < COLUMNS; j++) {
if (board[i][j] == EMPTY_SLOT) {
continue;
}
if (board[i][j] == board[i+1][j] && board[i][j] == board[i+2][j] && board[i][j] == board[i+3][j]) {
gameEnded = true;
System.out.println("Player " + (redTurn ? 1 : 2) + " wins!");
return;
}
}
}
// Check diagonal lines
for (i = 0; i <= ROWS-4; i++) {
for (j = 0; j <= COLUMNS-4; j++) {
if (board[i][j] == EMPTY_SLOT) {
continue;
}
if (board[i][j] == board[i+1][j+1] && board[i][j] == board[i+2][j+2] && board[i][j] == board[i+3][j+3]) {
gameEnded = true;
System.out.println("Player " + (redTurn ? 1 : 2) + " wins!");
return;
}
if (board[i][j+3] == EMPTY_SLOT) {
continue;
}
if (board[i][j+3] == board[i+1][j+2] && board[i][j+3] == board[i+2][j+1] && board[i][j+3] == board[i+3][j]) {
gameEnded = true;
System.out.println("Player " + (redTurn ? 1 : 2) + " wins!");
return;
}
}
}
// Check for a tie
if (movesCount == ROWS * COLUMNS) {
gameEnded = true;
System.out.println("It's a tie!");
}
}
private void togglePlayer() {
redTurn = !redTurn;
}
private void printBoard() {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
System.out.print("| " + board[i][j] + " ");
}
System.out.println("|");
}
System.out.println(" 1 2 3 4 5 6 7");
}
public static void main(String[] args) {
Lig4Game game = new Lig4Game();
game.play();
}
}
Note: The sample code above is just for reference and does not have all the implementation details. You may need to modify it to fit your specific requirements.