patternjavaMinor
Connect Four applet
Viewed 0 times
fourconnectapplet
Problem
I am making a Connect Four type of game in Java. I want to make sure that all the code seems nice before I decide to add more.
This is what the applet game looks like:
Player input:
Victory screen:
I have three classes:
Make4:
Board
```
import
This is what the applet game looks like:
Player input:
Victory screen:
I have three classes:
Make4:
import java.util.Arrays;
import java.awt.*;
import java.applet.Applet;
import javax.swing.JOptionPane;
/**
* @since 0.1.0
*/
public class Make4 extends Applet {
Board gb = new Board();
boolean hasFirstRun;
public void start(Graphics g) {
hasFirstRun = false;
}
public void paint(Graphics g) {
paintBoard(g);
if(!hasFirstRun) {
hasFirstRun = true;
startGame(2,g);
}
}
public void paintBoard(Graphics g) {
Expo.setColor(g,Expo.black);
for(int y = 0; y=0; x--) {
if(gb.board[x][y] != 0) {
switch(gb.board[x][y]) {
case 1:
Expo.setColor(g, Expo.red);
break;
case 2:
Expo.setColor(g,Expo.black);
break;
case 3:
Expo.setColor(g,Expo.green);
break;
case 4:
Expo.setColor(g,Expo.purple);
break;
}
Expo.fillCircle(g,x*100+50,y*100+50,45);
}
}
}
}
public void startGame(int np, Graphics g) {
int player = 0;
int numPlayer = np;
int wonType = 0;
while(wonType == 0) {
if(playergb.WIDTH) {
throw new Exception();
}
gb.dropPiece(playerColumn, player);
} catch(ArrayIndexOutOfBoundsException ex) {
JOptionPane.showMessageDialog(null, "That collum appears to be full. please try a diffrent one");
getPlayerInput(player);
} catch(Exception e) {
JOptionPane.showMessageDialog(null, "Please input a valid collum between 1 and "+gb.WIDTH);
getPlayerInput(player);
}
}
}Board
```
import
Solution
You seem to have a number of names that are very nondescript. What is
I notice that you have some magic numbers floating around.
You have also misspelled column as:
Java, for whatever reason, has not yet deprecated the lowercase colors. They do not conform to the convention of uppercase constants. I would change:
to
You should also change the names of the constant variables to uppercase.
See here: https://stackoverflow.com/questions/7281180
I'm also noticing some minor formatting inconsistencies that hurt readability a little.
And this code could use some spacing:
Spacing added:
There are a few more spots that need similar treatment, but this could be automatically done by an IDE.
The other thing is that you have some unnecessary code duplication here:
You could just have:
Expo? And hasFirstRun in what marathon? You at least need to include some comments.I notice that you have some magic numbers floating around.
2 doesn't exactly scream "draw", so you should use an enum.You have also misspelled column as:
collum, in two different output statements. It's not a code problem, but I'm mentioning it anyway.Java, for whatever reason, has not yet deprecated the lowercase colors. They do not conform to the convention of uppercase constants. I would change:
Color.red;to
Color.RED;You should also change the names of the constant variables to uppercase.
See here: https://stackoverflow.com/questions/7281180
I'm also noticing some minor formatting inconsistencies that hurt readability a little.
Expo lacks a layer of indentation.And this code could use some spacing:
for(int y = 0; y=0; x--) {Spacing added:
for(int y = 0; y = 0; x--) {There are a few more spots that need similar treatment, but this could be automatically done by an IDE.
The other thing is that you have some unnecessary code duplication here:
public Board() {
board = new int[7][6];
WIDTH = board.length;
HEIGHT = board[0].length;
}
public Board(int w, int h) {
board = new int[w][h];
WIDTH = board.length;
HEIGHT = board[0].length;
}You could just have:
public Board() {
this(7,6);
}
public Board(int w, int h) {
board = new int[w][h];
WIDTH = board.length;
HEIGHT = board[0].length;
}Code Snippets
for(int y = 0; y<gb.HEIGHT; y++) {
for(int x = gb.WIDTH-1; x>=0; x--) {for(int y = 0; y < gb.HEIGHT; y++) {
for(int x = gb.WIDTH - 1; x >= 0; x--) {public Board() {
board = new int[7][6];
WIDTH = board.length;
HEIGHT = board[0].length;
}
public Board(int w, int h) {
board = new int[w][h];
WIDTH = board.length;
HEIGHT = board[0].length;
}public Board() {
this(7,6);
}
public Board(int w, int h) {
board = new int[w][h];
WIDTH = board.length;
HEIGHT = board[0].length;
}Context
StackExchange Code Review Q#128239, answer score: 3
Revisions (0)
No revisions yet.