Skip to content

Instantly share code, notes, and snippets.

@CSchoel
Created January 27, 2015 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CSchoel/1c407c7cb5f365620212 to your computer and use it in GitHub Desktop.
Save CSchoel/1c407c7cb5f365620212 to your computer and use it in GitHub Desktop.
Connect four game
//Autor: Christopher Schölzel, Nadja Krümmel
int TYPE_EMPTY = 0;
int TYPE_BLUE = 1;
int TYPE_RED = -1;
class ConnectFour {
float brickWidth = 50;
float margin = 50;
int[][] field;
// 0 = leer, +1 = blau, -1 = rot
ConnectFour(int cols, int rows) {
field = new int[cols][rows];
}
int findWinner() {
int rowType = TYPE_BLUE;
int rowCount = 0;
for (int row = 0; row < field[0].length; row++) {
for (int col = 0; col < field.length; col++) {
int type = field[col][row];
if (type == TYPE_EMPTY) continue;
if (isSeries(col, row, 0, 1) || isSeries(col, row, 1, 0)
|| isSeries(col, row, 1, 1) || isSeries(col, row, -1, 1))
return type;
}
}
return TYPE_EMPTY;
}
boolean isSeries(int col, int row, int dc, int dr) {
int type = field[col][row];
if (!isLegalIndex(col+3*dc, row+3*dr)) return false;
for (int i = 1; i < 4; i++) {
if (field[col+i*dc][row+i*dr] != type) return false;
}
return true;
}
boolean isLegalIndex(int col, int row) {
return col >= 0 && col < field.length && row >= 0 && row < field[col].length;
}
int count(int type) {
int value = 0;
for (int i = 0; i < field.length; i++) {
for (int j = 0; j< field[0].length; j++) {
if (field[i][j] == type) {
value++;
}
}
}
return value;
}
void display() {
int rows = field[0].length;
for (int i = 0; i < field.length; i++) {
for (int j = 0; j< field[0].length; j++) {
if (field[i][j] == 0) fill(255);
if (field[i][j] == 1) fill(0, 0, 255);
if (field[i][j] == -1) fill(255, 0, 0);
ellipse(
margin+(i+0.5)*brickWidth,
margin+(rows-j-0.5)*brickWidth,
brickWidth-2,
brickWidth-2
);
}
}
}
void placeBrick(int col, int brick) {
int i = 0;
boolean notPlaced = true;
while (notPlaced && i < field[col].length) {
if (field[col][i] == 0) {
field[col][i] = brick;
notPlaced = false;
return;
}
i++;
}
}
int col(float x) {
return (int)((x-margin)/brickWidth);
}
int row(float y) {
return (int)((y-margin)/brickWidth);
}
int getColumns() {
return field.length;
}
}
ConnectFour cf;
int currentPlayer = TYPE_BLUE;
void setup() {
size(500, 400);
cf = new ConnectFour(7, 6);
}
void draw() {
background(0);
cf.display();
}
void gameOver(int playerWon) {
draw();
textAlign(CENTER);
textSize(30);
fill(255,0,255);
if(playerWon == TYPE_EMPTY) {
text("Gleichstand!",width/2.0,height/2.0);
} else if (playerWon == TYPE_BLUE) {
text("Blau gewinnt!",width/2.0,height/2.0);
} else if (playerWon == TYPE_RED) {
text("Rot gewinnt!",width/2.0,height/2.0);
}
noLoop();
}
void mousePressed() {
int col = cf.col(mouseX);
if (col < 0 || col >= cf.getColumns()) return;
cf.placeBrick(cf.col(mouseX),currentPlayer);
currentPlayer *= -1;
int winner = cf.findWinner();
if(cf.count(TYPE_EMPTY) == 0) gameOver(TYPE_EMPTY);
else if (winner != TYPE_EMPTY) gameOver(winner);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment