Coding a game of Connect four with a 6 x 7 grid. It will use colored output to show where pieces have been put already.
Please let me know if you have any doubts or you want me to modify the code. And if you find this code useful then don't forget to rate my answer as thumps up. Thank you! :)
public class ConnectFourMain {
public static void main(String[] args) {
@SuppressWarnings("unused")
ConnectFourGui gui = new
ConnectFourGui();
}
}
-------------------------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class ConnectFourGui extends JFrame {
private static final long serialVersionUID =
1L;
private JLabel title;
private JButton[] buttons;
private JLabel[][] board;
private JButton reset;
private BlinkLabel blinker;
private ImageIcon arrow;
private ImageIcon emptySlot;
private ImageIcon redSlot;
private ImageIcon blackSlot;
private int player;
private boolean isWinner;
public ConnectFourGui() {
setTitle("Connect
Four");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new
JPanel();
panel.setLayout(new
GridLayout(7, 7));
panel.setBackground(Color.YELLOW);
JPanel panel2 = new
JPanel();
panel2.setBackground(Color.BLACK);
JPanel panelTop = new
JPanel();
panelTop.setLayout(new
BorderLayout());
this.title = new
JLabel("Connect Four");
title.setForeground(Color.RED);
title.setFont(new
Font("Sans-Serif", Font.BOLD, 50));
title.setHorizontalAlignment(SwingConstants.CENTER);
Board gameBoard = new
Board();
player = 1;
isWinner = false;
arrow = new
ImageIcon("arrowButton.png");
emptySlot = new
ImageIcon("emptySlot.png");
redSlot = new
ImageIcon("redSlot.png");
blackSlot = new
ImageIcon("blackSlot.png");
buttons = new
JButton[7];
board = new
JLabel[6][7];
reset = new
JButton("RESTART");
blinker = new
BlinkLabel("");
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
gameBoard.reset();
player = 1;
title.setText("Connect Four");
blinker.setBlinking(false);
blinker.setText("");
for (int i = 0; i < 6; i++){
for (int j = 0; j < 7; j++){
board[i][j].setIcon(emptySlot);
}
}
}
});
for (int i = 0; i
< 7; i++) {
int column = i;
this.buttons[i] = new JButton(arrow);
buttons[i].setBackground(Color.WHITE);
panel.add(buttons[i]);
buttons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
if (player == 1) {
player = 2;
try {
gameBoard.move(1, column);
for (int k = 5; k >= 0; k--) {
if (board[k][column].getIcon() == emptySlot) {
board[k][column].setIcon(redSlot);
isWinner = gameBoard.isWinner();
if (isWinner == true) {
title.setText("");
blinker.setText("RED IS WINNER!");
panel2.add(blinker, BorderLayout.CENTER);
blinker.setBlinking(true);
}
break;
}
}
} catch (NoEmptySpaceColumnException e) {
player = 1;
}
} else {
player = 1;
try {
gameBoard.move(2, column);
for (int k = 5; k >= 0; k--) {
if (board[k][column].getIcon() == emptySlot) {
board[k][column].setIcon(blackSlot);
isWinner = gameBoard.isWinner();
if (isWinner == true) {
title.setText("");
blinker.setText("BLACK IS WINNER!");
panel2.add(blinker, BorderLayout.CENTER);
blinker.setBlinking(true);
}
break;
}
}
} catch (NoEmptySpaceColumnException e) {
player = 2;
}
}
}
});
}
for (int i = 0; i <
6; i++){
for (int j = 0; j < 7; j++){
board[i][j] = new JLabel(emptySlot);
panel.add(board[i][j]);
}
}
panel2.add(reset,
BorderLayout.WEST);
panel2.add(title,
BorderLayout.CENTER);
panelTop.add(panel2,
BorderLayout.NORTH);
panelTop.add(panel,
BorderLayout.CENTER);
getContentPane().add(panelTop);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
------------------------------------------------------------------------------------
public class Board {
private int[][] gameBoard;
public Board() {
gameBoard = new
int[6][7];
}
public void move(int player, int column) throws NoEmptySpaceColumnException {
// check if column is
full
if (gameBoard[0][column]
!= 0) {
throw new NoEmptySpaceColumnException();
}
// if column is
partially full- put in bottom most
// available space
else {
for (int i = 5; i >= 0; i--) {
if (gameBoard[i][column] == 0) {
gameBoard[i][column] = player;
break;
}
}
}
}
public boolean isWinner() {
boolean winner =
false;
// check if four
values are consecutive horizontally
for (int row = 0; row
< gameBoard.length; row++) {
for (int column = 0; column < gameBoard[row].length - 3;
column++) {
if (gameBoard[row][column] == gameBoard[row][column + 1]
&& gameBoard[row][column + 1] == gameBoard[row][column +
2]
&& gameBoard[row][column + 2] == gameBoard[row][column + 3]
&& gameBoard[row][column] != 0) {
winner = true;
break;
}
}
}
// check if four
values are consecutive vertically
if (winner == false)
{
for (int row = 0; row < gameBoard.length - 3; row++) {
for (int column = 0; column < gameBoard[row].length; column++)
{
if (gameBoard[row][column] == gameBoard[row + 1][column]
&& gameBoard[row + 1][column] == gameBoard[row +
2][column]
&& gameBoard[row + 2][column] == gameBoard[row +
3][column]
&& gameBoard[row][column] != 0) {
winner = true;
break;
}
}
}
}
// check if four
values are diagonal
if (winner == false)
{
for (int row = 0; row < gameBoard.length - 3; row++) {
for (int column = 0; column < gameBoard[row].length - 3;
column++) {
if ((gameBoard[row][column] == gameBoard[row + 1][column + 1]
&& gameBoard[row + 1][column + 1] == gameBoard[row +
2][column + 2]
&& gameBoard[row + 2][column + 2] == gameBoard[row +
3][column + 3]
&& gameBoard[row][column] != 0)|| (gameBoard[row][column +
3] == gameBoard[row + 1][column + 2]
&& gameBoard[row + 1][column + 2] == gameBoard[row +
2][column + 1]
&& gameBoard[row + 2][column + 1] == gameBoard[row +
3][column]
&& gameBoard[row][column+3] != 0)) {
winner = true;
break;
}
}
}
}
return winner;
}
public void display() {
for (int i = 0; i <
6; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(gameBoard[i][j]);
}
System.out.print("\n");
}
}
public void reset() {
for (int i = 0; i <
6; i++) {
for (int j = 0; j < 7; j++) {
gameBoard[i][j] = 0;
}
}
}
}
-----------------------------------------------------------------------------------
public class NoEmptySpaceColumnException extends Exception {
private static final long serialVersionUID =
1L;
public NoEmptySpaceColumnException(){
super("All spots in
column filled.");
}
}
----------------------------------------------------------------------------------
import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
public class BlinkLabel extends JLabel {
private static final long serialVersionUID =
1L;
private final int BLINKING_RATE = 500;
private boolean blinkingOn = true;
public BlinkLabel(String text) {
super(text);
Timer timer = new Timer(
BLINKING_RATE , new TimerListener(this));
timer.setInitialDelay(0);
timer.start();
}
public void setBlinking(boolean flag) {
this.blinkingOn =
flag;
}
public boolean getBlinking(boolean flag) {
return
this.blinkingOn;
}
private class TimerListener implements
ActionListener {
private BlinkLabel
bl;
private Color bg;
private Color fg;
private boolean
isForeground = true;
public
TimerListener(BlinkLabel bl) {
this.bl = bl;
fg = Color.RED;
bg = Color.BLACK;
this.bl.setFont(new Font("Sans-Serif", Font.BOLD, 50));
}
public void
actionPerformed(ActionEvent e) {
if (bl.blinkingOn) {
if (isForeground) {
bl.setForeground(fg);
}
else {
bl.setForeground(bg);
}
isForeground = !isForeground;
}
else {
if (isForeground) {
bl.setForeground(fg);
isForeground = false;
}
}
}
}}
Coding a game of Connect four with a 6 x 7 grid. It will use colored...
Connect 4 is a 2 player game where each player has a set of colored tokens (red or yellow). Players take turns during which they place a single token into one of the columns of an n by m grid (where n is the number of rows and m is the number of columns. They place their token into a slot at the top of the column and it falls into the lowest unoccupied slot in that column. A player...
Vankin’s Mile is a solitaire game played on an
n x m grid. using assebly language
Vankin's Mile is a solitaire game played on an n x m grid. The player starts by placing a token on any square of the grid. Then on each turn, the player moves the token either one square to the right or one square down. The game ends when player moves the token off the edge of the board. Each square of the grid...
The game Battleship is played on a grid board. Each opponent has multiple ships that are placed on the grid where the other opponent cannot see them. In order to attack, each player takes turns calling out coordinates on a grid. If the attacker calls out a coordinate that hits their opponent's ship, they must call out, "Hit." You are going to be developing a computer program to mimic this game. Use the Gridlayout that is six columns by six...
Create a connect four board game in java. Use multiple methods and 2d arrays. The purpose of the game is to get four red "R" or yellow "Y" pucks in a row(vertically,horizontally, or diagonally)
Write a class named FBoard for playing a game...
PLEASE USE C++
PLEASE DO NOT USE "THIS -->". NOT
ALLOWED
PLEASE PROVIDE COMMENTS AND OUTPUT!
Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have: An 8x8 array of char for tracking the positions of the pieces. A data member...
The game Battleship is played on a grid board. Each opponent has multiple ships that are placed on the grid where the other opponent cannot see them. In order to attack, each player takes turns calling out coordinates on a grid. If the attacker calls out a coordinate that hits their opponent's ship, they must call out, "Hit." You are going to be developing a computer program to mimic this game. Use the Gridlayout that is six columns by six...
In C++. Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have: An 8x8 array of char for tracking the positions of the pieces. A data member called gameState that holds one of the following values: X_WON, O_WON, or UNFINISHED - use an enum type for this, not string (the...
Code in JAVA You are asked to implement “Connect 4” which is a two player game. The game will be demonstrated in class. The game is played on a 6x7 grid, and is similar to tic-tac-toe, except that instead of getting three in a row, you must get four in a row. To take your turn, you choose a column, and slide a checker of your color into the column that you choose. The checker drops into the slot, falling...
7) Implement a two-dimensional grid with a one dimensional array. a) Implement an empty array of four integers. D) Request four integers from the console and store them into the array. In this array, the index represents a column, the value a row. c) Implement an output function to display the array as a two-dimensional grid of X's and dots where x is an array coordinate. Example output (input is bold and italicized) : Enter 4. row values (from 0...
I need help writing a Java program for a game of TicTacToe. It is a two player game, and the rules are as follows: 1. The game begins with an empty, 3 × 3 grid. 2. The two players then take turns placing a mark in an empty grid cell. Player O will use the ‘O’ (letter ‘O’, not zero) mark and Player X will use the ‘X’ mark. Player O moves first. 3. The game is over in either...