Connect Four game using java

It’s time to create your first game nammed connect four. we will implement this game using java swing. and it’s up to you to do it with your favorite language like c ++ using the QT library for the GUI.

Connect Four (also known as Captain’s Mistress, Four Up, Plot Four, Find Four, Four in a Row, Four in a Line, Drop Four, and Gravitrips (in Soviet Union)) is a two-player connection game in which the players first choose a color and then take turns dropping one colored disc from the top into a seven-column, six-row vertically suspended grid. The pieces fall straight down, occupying the lowest available space within the column. The objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of one’s own discs. Connect Four is a solved game. The first player can always win by playing the right moves.
The game was first sold under the Connect Four trademark[3] by Milton Bradley in February 1974.

wikipedia

Requirement

  • JAVA
  • POO

Gameplay

A gameplay example (bellow), shows the first player starting Connect Four by dropping one of his/her yellow discs into the center column of an empty game board. The two players then alternate turns dropping one of their discs at a time into an unfilled column, until the second player, with red discs, achieves a diagonal four in a row, and wins the game. For games where the board fills up before either player achieves four in a row, then the games are a draw.

connect four gameplay

Code overview

package algorithmepuissance;

import java.awt.Color;

public class joueur {
	
	String pseudo;
	int score;
	boolean gagne;
        Color c;
	
	public joueur(){
		this.pseudo="iconnu";
		this.score=0;
		this.gagne=false;
		this.c=Color.white;
		
	}
	public joueur(String nom, int score,Color c) {
		this.pseudo = nom;
		this.score = score;
		this.gagne=false;
		this.c=c;
		
	}

A simple Model with some informations about the gamer

public static void verification_diagonale_bas_gauche_vers_haut_droite(joueur a){
	for(int i=5;i>=3;i--){
		
		for(int j=0;j<=3;j++){
			if(tab[i][j].getC()==a.getColor() && tab[i-1][j+1].getC()==a.getColor() &&tab[i-2][j+2].getC()==a.getColor() && tab[i-3][j+3].getC()==a.getColor()){ 
				try {
					Thread.sleep(200);
					tab[i][j].setBackground(Color.WHITE);
					Thread.sleep(200);
					tab[i-1][j+1].setBackground(Color.WHITE);
					Thread.sleep(200);
					tab[i-2][j+2].setBackground(Color.WHITE);
					Thread.sleep(200);
					tab[i-3][j+3].setBackground(Color.WHITE);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
				a.setGagne(true);
				a.setScore(a.getScore()+1);

			}
		}
	}
	
}

A snippet from the management logic of the game, this method try verify if a diagonal combination left-right is true . you can see the other method that try to make verification horizontal and vertical at each game pass.

Project

The Source code is available in github , check it here

inferom
web developer passionate about new technologies