Artificial Intelligence can generate reasonable code; it is up to you to make it shine. I gave ChatGPT two assignments, the first to write a Connect Four program. This one has extensive instructions. The second was to create a Checker game. where I needed only two sentences. Both games work well.
Caveat
People are starting to realize that Artificial Intelligence is a tool. Use it as a tool to improve productivity. Students need to learn how to code and use AI to help. If you notice the detail I use on the Connect 4 instructions, this told the code created what I wanted. It was not perfect at first. Maybe knowing the Scrum methodology of good story telling would help. Take a look at this video and let me know what you think.
Connect Four
I developed my Connect 4 Code in two steps. The first was to create a description of what I thought I wanted. ChatGPT made a decent program, but then had an offer to add the features describe in my description but not implemented on the first try.
Instructions
- Create a Java program using Java Swing for the Graphical User Interface.
- Create a Player class with private variables String name, Color color, int wins, losses, and ties.
- Player has a constructor with two parameters: Player(String name, Color colorID). From the user input, set name and colorID, Set the variables wins, losses, and ties to 0.
- The Player constructor checks to see if there is a file <name>.txt. If there is a file by that name, the first line of the file is a name. It should match the name passed into the constructor. The second line of the file contains the wins, the next line is the losses, and the last line contains the ties. Update these variables.
- Method closeFile() makes sure the file <name>.txt is written with the player’s name, wins, losses, and ties each written on its own line.
- The destructor for Player should call closeFile().
- Create a Connect4Board class with 6 rows of 7 empty circles. Give each empty circle depth using shadows. Have a light blue background. The disks that drop need to look like 3D checkers.
- Have three private variables of class Player called player1, player2, and currentPlayer.
- Have a choosePlayers() function that asks the users to type in their names and allows them to select their favorite color from a color list. There needs to be verification that the two chosen colors do not match.
- In the Connect4Board constructor, call choosePlayers().
- Create a two-dimensional array with 6 rows and 7 columns which matches the display rows and columns.
- Have a Menu bar across the top with a Game menu and a Help menu.
- Based on currentPlayer. Have a line under the menu displaying who goes next.
- Under the Game menu, have a Choose Players Button. Under that is a Scores button that will open a dialog box with each user’s name and their wins, losses, and ties. Under the Scores button is a Reset button that calls the reset() method.
- Under the Reset button is an Exit button which calls each players’ closeFile().It then deallocates any memory for that player.
- The Choose Players Button calla each players’ closeFile() method. It then calls the choosePlayers() method.
- Have the help button bring up a dialog box with the rules for Connect 4.
- Have a reset() method that will empty all the circles on the board and empty the array also. It randomly chooses whether player1 or player2 starts the game.
- Have a method called drop(int column). It would place the proper disk at the top of the selected column and animate the drop until it sits in the position just above any precious drops in that column. Update the two-dimensional array to match the display board for each player in each position. In this routine, after the repaint, call checkWinner().
- Allow a user to click on the desired column and call drop(column).
- The method checkWinner() checks if either player has four in a row, either vertical, horizontal, or diagonal. Check for the winner, put the winning checker in each of the four winning locations with a darker version of the user color. Highlight the squares around the winning four checkers with a dark blue. Increment the appropriate win and loss counts. Check for any empty cells in the array. If none, there is no winner, increment the tie variable for each player. Pop up a dialog box and say which player won or with both players’ names and declaring a tie.
Code
I copied the above instructions into ChatGPT. I then allowed ChatGPT to put in the 3D features I had requested. This is the generated c ode.
Msin.jsva
// Program to start Connect 4
// AI (ChatGPT) generated with modifications by Wayne Cook
// 5 April 2025
// Purpose: Start up the Connect 4 program.
public class Connect4Game {
public static void main(String[] args) {
new Connect4Board();
}
}
Player.java
// Player.java
// AI (ChatGPT) generated with modifications by Wayne Cook
// 5 April 2025
// Purpose: Store information needed to define one player, save and restore all information.
import java.awt.Color;
import java.io.*;
public class Player {
private String name;
private Color color;
private int wins, losses, ties;
public Player(String name, Color color) {
this.name = name;
this.color = color;
wins = 0;
losses = 0;
ties = 0;
loadFromFile();
}
private void loadFromFile() {
File file = new File(name + ".txt");
if (file.exists()) {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String fileName = br.readLine();
if (fileName.equals(name)) {
wins = Integer.parseInt(br.readLine());
losses = Integer.parseInt(br.readLine());
ties = Integer.parseInt(br.readLine());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void closeFile() {
try (PrintWriter pw = new PrintWriter(new FileWriter(name + ".txt"))) {
pw.println(name);
pw.println(wins);
pw.println(losses);
pw.println(ties);
} catch (Exception e) {
e.printStackTrace();
}
}
// No real destructor in Java → use finalize (not recommended, but per assignment)
@Override
protected void finalize() throws Throwable {
closeFile();
super.finalize();
}
public String getName() { return name; }
public Color getColor() { return color; }
public int getWins() { return wins; }
public int getLosses() { return losses; }
public int getTies() { return ties; }
public void addWin() { wins++; }
public void addLoss() { losses++; }
public void addTie() { ties++; }
}
Connect4Board.java
// Connect4Board.java
// AI (ChatGPT) generated with modifications by Wayne Cook
// 5 April 2025
// Purpose: Manage board play, gather and update players information.
// Fully upgraded Connect 4 with 3D graphics, gradients, animation, and highlights
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
public class Connect4Board extends JFrame {
private Player player1, player2, currentPlayer;
private int[][] board = new int[6][7];
private JLabel statusLabel;
private BoardPanel boardPanel;
private java.util.List<Point> winningCells = new ArrayList<>();
public Connect4Board() {
setTitle("Connect 4 3D");
setSize(800, 650);
setDefaultCloseOperation(EXIT_ON_CLOSE);
choosePlayers();
boardPanel = new BoardPanel();
add(boardPanel, BorderLayout.CENTER);
statusLabel = new JLabel("", JLabel.CENTER);
statusLabel.setFont(new Font("Arial", Font.BOLD, 16));
add(statusLabel, BorderLayout.NORTH);
createMenu();
reset();
setVisible(true);
}
private void choosePlayers() {
String name1 = JOptionPane.showInputDialog("Enter Player 1 Name:");
Color color1 = JColorChooser.showDialog(this, "Choose Player 1 Color", Color.RED);
String name2;
Color color2;
while (true) {
name2 = JOptionPane.showInputDialog("Enter Player 2 Name:");
color2 = JColorChooser.showDialog(this, "Choose Player 2 Color", Color.YELLOW);
if (!color1.equals(color2)) break;
JOptionPane.showMessageDialog(this, "Colors must be different!");
}
if (player1 != null) player1.closeFile();
if (player2 != null) player2.closeFile();
player1 = new Player(name1, color1);
player2 = new Player(name2, color2);
}
private void createMenu() {
JMenuBar bar = new JMenuBar();
JMenu game = new JMenu("Game");
JMenu help = new JMenu("Help");
JMenuItem choose = new JMenuItem("Choose Players");
JMenuItem scores = new JMenuItem("Scores");
JMenuItem reset = new JMenuItem("Reset");
JMenuItem exit = new JMenuItem("Exit");
choose.addActionListener(e -> {
player1.closeFile();
player2.closeFile();
choosePlayers();
reset();
});
scores.addActionListener(e -> JOptionPane.showMessageDialog(this,
player1.getName() + " W:" + player1.getWins() + " L:" + player1.getLosses() + " T:" + player1.getTies() + "\n" +
player2.getName() + " W:" + player2.getWins() + " L:" + player2.getLosses() + " T:" + player2.getTies()));
reset.addActionListener(e -> reset());
exit.addActionListener(e -> {
player1.closeFile();
player2.closeFile();
System.exit(0);
});
game.add(choose);
game.add(scores);
game.add(reset);
game.add(exit);
JMenuItem rules = new JMenuItem("Rules");
rules.addActionListener(e -> JOptionPane.showMessageDialog(this,
"Connect 4: Get 4 in a row horizontally, vertically, or diagonally."));
help.add(rules);
bar.add(game);
bar.add(help);
setJMenuBar(bar);
}
public void reset() {
winningCells.clear();
for (int[] row : board)
Arrays.fill(row, 0);
currentPlayer = new Random().nextBoolean() ? player1 : player2;
updateStatus();
repaint();
}
private void updateStatus() {
statusLabel.setText("Turn: " + currentPlayer.getName());
}
public void drop(int col) {
new Thread(() -> {
for (int row = 5; row >= 0; row--) {
if (board[row][col] == 0) {
for (int anim = 0; anim <= row; anim++) {
repaint();
try { Thread.sleep(40); } catch (Exception ignored) {}
}
board[row][col] = currentPlayer == player1 ? 1 : 2;
repaint();
if (checkWinner()) return;
currentPlayer = currentPlayer == player1 ? player2 : player1;
updateStatus();
return;
}
}
}).start();
}
private boolean checkWinner() {
for (int r = 0; r < 6; r++) {
for (int c = 0; c < 7; c++) {
int p = board[r][c];
if (p == 0) continue;
if (checkDir(r, c, 0, 1) || checkDir(r, c, 1, 0) || checkDir(r, c, 1, 1) || checkDir(r, c, 1, -1)) {
Player winner = p == 1 ? player1 : player2;
Player loser = p == 1 ? player2 : player1;
winner.addWin();
loser.addLoss();
JOptionPane.showMessageDialog(this, winner.getName() + " wins!");
return true;
}
}
}
boolean full = true;
for (int[] row : board)
for (int cell : row)
if (cell == 0) full = false;
if (full) {
player1.addTie();
player2.addTie();
JOptionPane.showMessageDialog(this, "Tie game!");
reset();
return true;
}
return false;
}
private boolean checkDir(int r, int c, int dr, int dc) {
int p = board[r][c];
winningCells.clear();
winningCells.add(new Point(r, c));
for (int i = 1; i < 4; i++) {
int nr = r + dr * i;
int nc = c + dc * i;
if (nr < 0 || nr >= 6 || nc < 0 || nc >= 7 || board[nr][nc] != p) {
winningCells.clear();
return false;
}
winningCells.add(new Point(nr, nc));
}
return true;
}
class BoardPanel extends JPanel {
public BoardPanel() {
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int col = e.getX() / (getWidth() / 7);
drop(col);
}
});
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint bg = new GradientPaint(0, 0, new Color(120,180,255), 0, getHeight(), new Color(30,90,200));
g2.setPaint(bg);
g2.fillRect(0,0,getWidth(),getHeight());
int w = getWidth()/7;
int h = getHeight()/6;
for(int r=0;r<6;r++){
for(int c=0;c<7;c++){
int x=c*w;
int y=r*h;
g2.setColor(new Color(0,0,50,80));
g2.fillOval(x+8,y+8,w-10,h-10);
Color diskColor = Color.WHITE;
if(board[r][c]==1) diskColor = player1.getColor();
if(board[r][c]==2) diskColor = player2.getColor();
RadialGradientPaint rg = new RadialGradientPaint(
new Point2D.Double(x+w/2,y+h/2),
w/2,
new float[]{0f,1f},
new Color[]{Color.WHITE,diskColor.darker()});
g2.setPaint(rg);
g2.fillOval(x+5,y+5,w-10,h-10);
if (winningCells.contains(new Point(r,c))) {
g2.setStroke(new BasicStroke(4));
g2.setColor(Color.BLUE);
g2.drawOval(x+5,y+5,w-10,h-10);
}
}
}
}
}
}
Game Board
This is what the gameboard looks like after one player wins.

Checkers
I wanted to create a game with as few instructions as possible. Checkers is a well-known game, and should be easy to generate.
Instructions
Using Java Swing, create a checker game. Have red and black checker pieces with a contrasting board. Allow Kings to jump backwards. Look for multiple jumps and allow them.
ChatGPT Rely
Creating a simple checker game in Java Swing involves several steps, including building the game board, implementing the rules for checker movement (including jumping and multiple jumps), and providing an interface for the user to interact with the game. Below is a basic outline of how you can implement a checker game with the requested features:
- Board Setup: A checkerboard with alternating red and black pieces.
- Movement: Regular pieces can move forward, while Kings can move both forward and backward.
- Jumping: Pieces can jump over opponent pieces, and multiple jumps are allowed in one turn.
- Kings: When a piece reaches the opponent’s back row, it becomes a King and can move both forward and backward.
- User Interaction: Mouse events to select and move pieces.
Here’s a simple version of a Checkerboard game using Java Swing. This implementation includes the basic features, but it’s not a fully polished game. You can further enhance it with more sophisticated rule validation, GUI enhancements, and game-over logic.
Code
// CheckerGame.java
// AI (ChatGPT) generated with no modifications by Wayne Cook
// 5 April 2025
// Purpose: Allow two people to play checkers. There is no winner declared or
// score kept.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CheckerGame extends JPanel {
private static final int BOARD_SIZE = 8;
private static final int TILE_SIZE = 80;
private static final Color DARK_SQUARE = new Color(139, 69, 19); // Brownish for dark squares
private static final Color LIGHT_SQUARE = new Color(255, 222, 185); // Light brown
private static final Color RED_PIECE = new Color(255, 0, 0);
private static final Color BLACK_PIECE = new Color(0, 0, 0);
private CheckerPiece[][] board;
private CheckerPiece selectedPiece;
private boolean redTurn = true; // Red goes first
public CheckerGame() {
this.setPreferredSize(new Dimension(BOARD_SIZE * TILE_SIZE, BOARD_SIZE * TILE_SIZE));
board = new CheckerPiece[BOARD_SIZE][BOARD_SIZE];
initializeBoard();
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int row = e.getY() / TILE_SIZE;
int col = e.getX() / TILE_SIZE;
if (selectedPiece == null) {
selectPiece(row, col);
} else {
movePiece(row, col);
}
repaint();
}
});
}
private void initializeBoard() {
// Set up initial pieces
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
if ((row + col) % 2 == 1) { // Only dark squares get pieces
if (row < 3) {
board[row][col] = new CheckerPiece(row, col, BLACK_PIECE, false); // Black pieces
} else if (row > 4) {
board[row][col] = new CheckerPiece(row, col, RED_PIECE, false); // Red pieces
}
}
}
}
}
private void selectPiece(int row, int col) {
CheckerPiece piece = board[row][col];
if (piece != null && (piece.isRed() == redTurn)) {
selectedPiece = piece;
}
}
private void movePiece(int row, int col) {
if (selectedPiece != null) {
if (isValidMove(selectedPiece, row, col)) {
// Move the piece
board[row][col] = selectedPiece;
board[selectedPiece.getRow()][selectedPiece.getCol()] = null;
selectedPiece.setRow(row);
selectedPiece.setCol(col);
// Check if it's a king
if ((selectedPiece.isRed() && row == 0) || (!selectedPiece.isRed() && row == BOARD_SIZE - 1)) {
selectedPiece.setKing(true);
}
// After a valid move, it's the other player's turn
redTurn = !redTurn;
}
}
selectedPiece = null;
}
private boolean isValidMove(CheckerPiece piece, int newRow, int newCol) {
// Ensure move is on the board and the target square is empty
if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE) {
return false;
}
if (board[newRow][newCol] != null) {
return false; // The square is occupied
}
// Determine the direction of movement
int rowDiff = Math.abs(piece.getRow() - newRow);
int colDiff = Math.abs(piece.getCol() - newCol);
// Check for normal move or jump
if (rowDiff == 1 && colDiff == 1) {
return true; // Normal move
} else if (rowDiff == 2 && colDiff == 2) {
// Check for a valid jump (over a piece of the opposite color)
int midRow = (piece.getRow() + newRow) / 2;
int midCol = (piece.getCol() + newCol) / 2;
CheckerPiece middlePiece = board[midRow][midCol];
if (middlePiece != null && middlePiece.isRed() != piece.isRed()) {
// Can jump over the opponent's piece
board[midRow][midCol] = null; // Remove the jumped piece
return true;
}
}
return false;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw the board
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
Color color = (row + col) % 2 == 0 ? LIGHT_SQUARE : DARK_SQUARE;
g.setColor(color);
g.fillRect(col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE, TILE_SIZE);
// Draw the piece (if any)
CheckerPiece piece = board[row][col];
if (piece != null) {
g.setColor(piece.getColor());
g.fillOval(col * TILE_SIZE + 10, row * TILE_SIZE + 10, TILE_SIZE - 20, TILE_SIZE - 20);
if (piece.isKing()) {
g.setColor(Color.YELLOW);
g.drawOval(col * TILE_SIZE + 10, row * TILE_SIZE + 10, TILE_SIZE - 20, TILE_SIZE - 20);
}
}
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Checker Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new CheckerGame());
frame.pack();
frame.setVisible(true);
}
private static class CheckerPiece {
private int row, col;
private final Color color;
private boolean isKing;
public CheckerPiece(int row, int col, Color color, boolean isKing) {
this.row = row;
this.col = col;
this.color = color;
this.isKing = isKing;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public void setRow(int row) {
this.row = row;
}
public void setCol(int col) {
this.col = col;
}
public Color getColor() {
return color;
}
public boolean isKing() {
return isKing;
}
public void setKing(boolean isKing) {
this.isKing = isKing;
}
public boolean isRed() {
return color.equals(RED_PIECE);
}
}
}
Checker Board
The checkerboard looks good. I would like to get it to do double jumps and to keep score over multiple plays. It is not bad for such a brief description.

Conclusion
These were fun games to create. I spent about three hours on the Connect4 game, as compared to three weeks when I used JavaFX and did all the coding without AI help. The Checker game took me less than an hour.
Artificial Intelligence is helpful, but you must know how to program the code so you can check what it is doing and improve the code.




