Here is a small Java Connect 4 Game I Created
The main part of the program is here:
/**
* The ConnectFour class.
* CREATE BY http://electronicentertainment.wordpress.com/
* This class represents a Connect Four (TM)
* game, which allows two players to drop
* checkers into a grid until one achieves
* four checkers in a straight line.
*/
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.io.*;
import java.lang.Object;
public class ConnectFour {
final int Empty = -1; // empty space on the game board
final int Blank = 2; // “null” not playable space
final int NUMPLAYER;
final int MAXGAME; // number of players
final int NUMROW; // number of rows on the game board
final int NUMCOL; // number of columns on the game board
int CONNECT = 4;
ConnectFourGUI gui; // the gui for the game
boolean win = false;
int numMove; // number of moves that has been made in the game
int CurrentPlayer; // identifies the current player
int grid[][]; // represents the grid of the game board
int score[]; // represents the scores of the players
boolean playermove; // checks whether the CurrentPlayer has made a move
int connect4; // checks if there are four pieces in a row
int latestpiece; // saves the last piece checked on the board
int i; // variables for coordinate calculation
int j;
int x; // the coordinates of the last piece placed
int y;
/**
* Constructor: ConnectFour
*/
public ConnectFour(ConnectFourGUI gui) {
this.gui = gui;
NUMPLAYER = gui.NUMPLAYER;
NUMROW = gui.NUMROW;
NUMCOL = gui.NUMCOL;
MAXGAME = gui.MAXGAME;
numMove = 0;
CurrentPlayer = 0;
grid = new int [NUMROW][NUMCOL];
EmptyGrid(grid);
score = new int [NUMPLAYER];
for(i = 0; i < NUMPLAYER; i ++){
score[i] = 0;
}
}
// TO DO: creation of arrays, and initialization of variables should be added here
//This changes the current player and updates the “NEXT” section
public void ChangePlayer(){
if(CurrentPlayer == 0){
CurrentPlayer = 1;
gui.setNextPlayer(CurrentPlayer);
}
else if(CurrentPlayer == 1){
CurrentPlayer = 0;
gui.setNextPlayer(CurrentPlayer);
}
}
//This checks for the available spaces in the column selected
public void placePiece(int column){
i = NUMROW – 1;
do //moves through the rows in a column
{
//checks if spot is available
if(grid[i][column] == Empty){
gui.setPiece(i, column, CurrentPlayer);
grid[i][column] = CurrentPlayer;
playermove = true;
numMove += 1;
x = i;
y = column;
}
i–;
}while(playermove == false && i >= 0);
}
//This method checks whether there are any winners. It updates the scoreboard
//and displays the winning message. If one of the players reach 3 wins, it
//displays the final winning message. If there is no winner, it displays the
//tie came message.
public void checkWin(int[][]board){
win = false;
checkHorizontal(NUMROW, NUMCOL);
checkVertical(NUMROW, NUMCOL);
checkBackDiag(NUMROW, NUMCOL);
checkDiag(NUMROW, NUMCOL);
//checks for final winner
if(win == true && score[CurrentPlayer] == MAXGAME-1){
score[CurrentPlayer]++;
gui.setPlayerScore(CurrentPlayer,score[CurrentPlayer]);
gui.MatchWinner(CurrentPlayer);
}
//checks for round winner
else if(win == true){
score[CurrentPlayer]++;
gui.setPlayerScore(CurrentPlayer,score[CurrentPlayer]);
gui.showWinnerMessage(CurrentPlayer);
gui.resetGameBoard();
EmptyGrid(board);
numMove = 0;
}
//checks for tie game
else if(win == false && numMove == NUMCOL*NUMROW){
gui.showTieGameMessage();
gui.resetGameBoard();
EmptyGrid(board);
numMove = 0;
}
}
//checks for a horizantal win
public void checkHorizontal (int row, int column){ // checks for a horizontal win
int count = 0;
for (int i = column; i < NUMCOL && grid[row][i] == CurrentPlayer; i++){ // checks each box to the right of the piece that has just been set, until the box doesn’t have a piece from the current player
count++; // increase count for every continuous piece from the same player
}
for (int i = column; i >= 0 && grid[row][i] == CurrentPlayer; i–){ // checks each box to the left of the piece that has just been set, until the box doesn’t have a piece from the current player
count++;
}
if (count >= CONNECT){ // if count is greater then the number of continuous pieces required to win, the current player is declared the winner
win = true; //declares a win
}
}
//checks for a vertical win
public void checkVertical (int row, int column){
int count = 0;
for (int i = row; i < NUMROW && grid[column][i] == CurrentPlayer; i++){ // checks each box to the right of the piece that has just been set, until the box doesn’t have a piece from the current player
count++; // increase count for every continuous piece from the same player
}
for (int i = row; i < NUMROW && grid[column][i] == CurrentPlayer; i–){ // checks each box to the left of the piece that has just been set, until the box doesn’t have a piece from the current player
count++;
}
if (count >= CONNECT){ // if count is greater then the number of continuous pieces required to win, the current player is declared the winner
win = true; //declares a win
}
}
public void checkDiag (int row, int column){
int count = 0;
for (i = row, j = column;i >= 0 && j < NUMCOL && grid[i][j]==CurrentPlayer;i–,j++){
count++;
}
for (i = row, j = column;i >= 0 && j < NUMCOL && grid[i][j]==CurrentPlayer;i++,j–){
count++;
}
if (count >=4){
win = true;
}
}
public void checkBackDiag(int row, int column){
int count = 0;
for (i = row, j = column;i >= 0 && j < NUMCOL && grid[i][j]==CurrentPlayer;i++,j++){
count++;
}
for (i = row, j = column;i >= 0 && j < NUMCOL && grid[i][j]==CurrentPlayer;i–,j–){
count++;
}
if (count >= 4){
win = true;
}
}
public void EmptyGrid(int[][]board){
for(int i = 0; i < NUMROW; i++){
for(int j = 0; j < NUMCOL; j++){
board[i][j] = Empty;
}
}
}
// This method will be called when a column is clicked. “column” is
// the number of the column that is clicked by the user
public void play (int column) {
playermove = false;
placePiece(column);
if(playermove == true){
checkWin(grid);
ChangePlayer();
}
}
}
GUI:
/**
* ConnectFourGUI
* Provide the GUI for the Connect Four game
*/
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.io.*;
import java.lang.Object;
public class ConnectFourGUI {
private final String CONFIGFILE = “config.txt”;
private JLabel[][] slots;
private JFrame Framen;
private JTextField[] playerScore;
private ImageIcon[] playerIcon;
private ImageIcon[] scoreIcon;
private ImageIcon blank;
private JLabel nextPlayerIcon;
private Color scorelines = new Color(128, 64, 0);
private Color background = new Color(220, 128, 5);
private String logoIcon;
private String blankfile;
private String[] iconFile;
private String[] scoreIconFile;
public final int NUMPLAYER = 2;
public final int NUMROW = 6;
public final int NUMCOL = 7;
public int MAXGAME; //number of games needed to win the series
private final int PieceSize = 70;
private final int GridW = NUMCOL * PieceSize;
private final int GridH = NUMROW * PieceSize;
private final int Scoreboardw = 2 * PieceSize;
private final int ScoreboardH = GridH;
private final int LogoH = 2 * PieceSize;
private final int logow = GridW + Scoreboardw;
private final int FrameW = (int)(logow * 1.02);
private final int FrameH = (int)((LogoH + GridH) * 1.1);
// Constructor: ConnectFourGUI
// – intialize variables from config files
// – initialize the imageIcon array
// – initialize the slots array
// – create the main frame
public ConnectFourGUI () {
initConfig();
InitilizeImageIcons();
initSlots();
createMainFrame();
}
private void initConfig() {
/* TO DO: initialize the following variables with information read from the config file
* – MAXGAME
* – logoIcon
* – iconFile
*/
iconFile = new String[NUMPLAYER];
scoreIconFile = new String[NUMPLAYER];
try{
BufferedReader read = new BufferedReader(new FileReader(CONFIGFILE));
MAXGAME = Integer.parseInt(read.readLine());
logoIcon = read.readLine();
iconFile[0] = read.readLine();
iconFile[1] = read.readLine();
scoreIconFile[0] = read.readLine();
scoreIconFile[1] = read.readLine();
blankfile = read.readLine();
}
catch(IOException io){
System.out.println(“Not Working”);
}
}
// InitilizeImageIcons
// Initialize playerIcon arrays with graphic files
private void InitilizeImageIcons() {
playerIcon = new ImageIcon[NUMPLAYER];
scoreIcon = new ImageIcon[NUMPLAYER];
for (int i = 0; i < NUMPLAYER; i++) {
playerIcon[i] = new ImageIcon(iconFile[i]);
scoreIcon[i] = new ImageIcon(scoreIconFile[i]);
}
blank = new ImageIcon(blankfile);
}
// initSlots
// initialize the array of JLabels
private void initSlots() {
slots = new JLabel[NUMROW][NUMCOL];
for (int i = 0; i < NUMROW; i++) {
for (int j = 0; j < NUMCOL; j++) {
slots [i] [j] = new JLabel ();
slots[i][j].setPreferredSize(new Dimension(PieceSize, PieceSize));
slots [i] [j].setHorizontalAlignment (SwingConstants.CENTER);
slots [i] [j].setBorder (new LineBorder (scorelines));
}
}
}
// Create a the grid
private JPanel CreateGrid() {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(GridW, GridH));
panel.setBackground(background);
panel.setLayout(new GridLayout(NUMROW, NUMCOL));
for (int i = 0; i < NUMROW; i++) {
for (int j = 0; j < NUMCOL; j++) {
panel.add(slots[i][j]);
}
}
return panel;
}
// createInfoPanel
private JPanel createInfoPanel() {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(Scoreboardw, ScoreboardH));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBackground (scorelines);
Font HeaderFont = new Font (“Serif”, Font.PLAIN, 18);
Font regularFont = new Font (“Serif”, Font.BOLD, 16);
// Create a panel for the scoreboard
JPanel scorePanel = new JPanel();
scorePanel.setBackground(scorelines);
// Create the label to display “SCOREBOARD” heading
JLabel scoreLabel = new JLabel (“SCOREBOARD”, JLabel.CENTER);
scoreLabel.setFont(HeaderFont);
scoreLabel.setAlignmentX (Component.CENTER_ALIGNMENT);
//scoreLabel.setForeground(Color.white);
// Create JLabels for players
JLabel[] playerLabel = new JLabel[NUMPLAYER];
for (int i = 0; i < NUMPLAYER; i++) {
playerLabel[i] = new JLabel(scoreIcon[i]);
}
// Create the array of textfield for players’ score
playerScore = new JTextField[NUMPLAYER];
for (int i = 0; i < NUMPLAYER; i++) {
playerScore[i] = new JTextField();
playerScore[i].setFont(regularFont);
playerScore[i].setText(“0″);
playerScore[i].setEditable(false);
playerScore[i].setHorizontalAlignment (JTextField.CENTER);
playerScore[i].setPreferredSize (new Dimension (Scoreboardw – PieceSize – 10, 30));
//playerScore[i].setBackground(background);
playerScore[i].setBackground(scorelines);
}
scorePanel.add(scoreLabel);
for (int i = 0; i < NUMPLAYER; i++) {
scorePanel.add(playerLabel[i]);
scorePanel.add(playerScore[i]);
}
JPanel nextPanel = new JPanel();
//nextPanel.setBackground(background);
nextPanel.setBackground(scorelines);
// Create the label to display “NEXT TURN” heading
JLabel nextLabel = new JLabel (“NEXT TURN”, JLabel.CENTER);
nextLabel.setFont(HeaderFont);
nextLabel.setAlignmentX (Component.CENTER_ALIGNMENT);
//nextLabel.setForeground(Color.white);
// Create the JLabel for the nextPlayer
nextPlayerIcon = new JLabel();
System.out.println(nextPlayerIcon.getAlignmentX());
nextPlayerIcon.setAlignmentX(JLabel.CENTER_ALIGNMENT);
nextPlayerIcon.setIcon(scoreIcon[0]);
nextPanel.add(nextLabel);
nextPanel.add(nextPlayerIcon);
panel.add(scorePanel);
panel.add(nextPanel);
return panel;
}
// createMainFrame
private void createMainFrame() {
// Create the frame
Framen = new JFrame (“Connect Four”);
JPanel panel = (JPanel)Framen.getContentPane();
panel.setLayout (new BoxLayout(panel,BoxLayout.Y_AXIS));
// Create the panel for the logo
JPanel logoPane = new JPanel();
logoPane.setPreferredSize(new Dimension (logow, LogoH));
JLabel logo = new JLabel();
logo.setIcon(new ImageIcon(logoIcon));
logoPane.add(logo);
// Create the bottom Panel which contains the play panel and info Panel
JPanel bottomPane = new JPanel();
bottomPane.setLayout(new BoxLayout(bottomPane,BoxLayout.X_AXIS));
bottomPane.setPreferredSize(new Dimension(GridW + Scoreboardw, GridH));
bottomPane.add(CreateGrid());
bottomPane.add(createInfoPanel());
// Add the logo and bottom panel to the main frame
panel.add(logoPane);
panel.add(bottomPane);
Framen.setContentPane(panel);
Framen.setSize(FrameW, FrameH);
Framen.setVisible(true);
}
// Returns the column number of where the given JLabel is on
public int getColumn(JLabel label) {
int result = -1;
for (int i = 0; i < NUMROW && result == -1; i++) {
for (int j = 0; j < NUMCOL && result == -1; j++) {
if (slots[i][j] == label) {
result = j;
}
}
}
return result;
}
public void addListener (ConnectFourListener listener) {
for (int i = 0; i < NUMROW; i++) {
for (int j = 0; j < NUMCOL; j++) {
slots [i] [j].addMouseListener (listener);
}
}
}
// Display the specified player icon on the specified slot
public void setPiece(int row, int col, int player) {
int i = -1;
//Animation
try{
//Moves pieces down a column
while(i < row){
if(i != -1){//”clears” boxes
slots[i][col].setIcon(blank);
slots[i][col].paint(slots[i][col].getGraphics());
}
i++;
//Draws Icon
slots[i][col].setIcon(playerIcon[player]);
slots[i][col].paint(slots[i][col].getGraphics());
Thread.sleep(1);//delays
}
}
catch(InterruptedException ie){
}
}
// Display the score on the textfield of the corresponding player
public void setPlayerScore(int player, int score) {
playerScore[player].setText(score+”");
}
// Display the appropriate player icon under”Next Turn”
public void setNextPlayer(int player) {
nextPlayerIcon.setIcon(scoreIcon[player]);
}
// Reset the game board (clear all the pieces on the game board)
public void resetGameBoard() {
for (int i = 0; i < NUMROW; i++) {
for (int j = 0; j < NUMCOL; j++) {
slots[i][j].setIcon(null);
}
}
}
// Display a pop up window displaying the message about a tie game
public void showTieGameMessage(){
JOptionPane.showMessageDialog(null, “Stalemate”, “Tie Game”, JOptionPane.PLAIN_MESSAGE, null);
}
// Display a window showing the winner of this game
public void showWinnerMessage(int player){
JOptionPane.showMessageDialog(null, ” has won this game!”, “Winner!”, JOptionPane.PLAIN_MESSAGE, playerIcon[player]);
}
// Display a window showing the winner of this match
public void MatchWinner(int player){
JOptionPane.showMessageDialog(null, ” has won the match with ” + MAXGAME + ” wins”, “Overall Winner!”, JOptionPane.PLAIN_MESSAGE, playerIcon[player]);
System.exit (0);
}
public static void main (String[] args) {
ConnectFourGUI gui = new ConnectFourGUI ();
ConnectFour game = new ConnectFour (gui);
ConnectFourListener listener = new ConnectFourListener (game, gui);
}
}
and the Listner:
import javax.swing.*;
import java.awt.event.*;
public class ConnectFourListener implements MouseListener
{
private ConnectFourGUI gui;
private ConnectFour game;
public ConnectFourListener (ConnectFour game, ConnectFourGUI gui) {
this.game = game;
this.gui = gui;
gui.addListener (this);
}
public void mouseClicked (MouseEvent event) {
JLabel label = (JLabel) event.getComponent ();
int column = gui.getColumn (label);
game.play(column);
}
public void mousePressed (MouseEvent event) {
}
public void mouseReleased (MouseEvent event) {
}
public void mouseEntered (MouseEvent event) {
}
public void mouseExited (MouseEvent event) {
}
}
-
Recent
- Why Steam Was Created
- Rocks. . .
- Top 5 Party Games
- BEST GIF EVER
- Top 5 Game Developers
- 5 Worst Games this Generation
- Famitsu Review of Final Fantasy 13
- Tony Hawk Says Ride is Fun
- Quantum Theory Going Multi-Plat!!
- A Few Graph Comics
- And you thought that the MAG Beta was big
- Here is a small Java Connect 4 Game I Created
-
Links
-
Archives
- August 2010 (3)
- March 2010 (1)
- February 2010 (2)
- December 2009 (5)
- November 2009 (1)
- August 2009 (3)
- March 2009 (1)
- February 2009 (1)
- January 2009 (1)
- October 2008 (3)
- September 2008 (1)
- August 2008 (3)
-
Categories
-
RSS
Entries RSS
Comments RSS