#18 X in row (flexible 4inRow) Java mode
A flexible 4 in row program! Features:
- how many in row to win (default 4)
- customizable width and height (default 7*6)
- current turn display: outline (red if red is next, blue if blue is next)
- auto check winner: displays a green outline if someone wins and draws a line to show
Code
55 lines!
int w = 7, h = 6, turn = 1, scl=100, inRow = 4;
int grid[][]= new int[w][h];
color clr[] = {color(255, 0, 0), color(0, 0, 255), color(0, 255, 0)}; //first is p1, second p2, third winClr
void setup() {
size(700, 600);
}
void draw() {
background(0);
noFill();
stroke( ((checkWin() != 0)? clr[2] : ((turn == -1)? clr[1] : clr[0])));
strokeWeight(5);
rect(0, 0, width, height);
strokeWeight(0);
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
fill( ((grid[i][j] == 1)? clr[0] : clr[1]));
if (grid[i][j] != 0) circle((i+0.5)*scl, (j+0.5)*scl, scl);
}
if (checkWin() != 0) noLoop();
}
void mousePressed() {
int mx = constrain(floor(map(mouseX, 0, width, 0, w)), 0, w);
for (int i = h-1; i >= 0; i--) if (grid[mx][i] == 0) {
grid[mx][i] = turn;
turn *= -1;
break;
}
}
int checkWin() {
for (int i = 0; i < w+1-inRow; i++) for (int j = 0; j < h; j++) if (equal(i, j, 1, 0, inRow) && grid[i][j] != 0) {
drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i+inRow-0.5)*scl, (j+0.5)*scl);
return grid[i][j];
}
for (int i = 0; i < w; i++) for (int j = 0; j < h+1-inRow; j++) if (equal(i, j, 0, 1, inRow) && grid[i][j] != 0) {
drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i+0.5)*scl, (j+inRow-0.5)*scl);
return grid[i][j];
}
for (int i = 0; i < w+1-inRow; i++) for (int j = 0; j < h+1-inRow; j++) if (equal(i, j, 1, 1, inRow) && grid[i][j] != 0) {
drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i+inRow-0.5)*scl, (j+inRow-0.5)*scl);
return grid[i][j];
}
for (int i = inRow-1; i < w; i++) for (int j = 0; j < h+1-inRow; j++) if (equal(i, j, -1, 1, inRow) && grid[i][j] != 0) {
drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i-(inRow-1.5))*scl, (j+inRow-0.5)*scl);
return grid[i][j];
}
return 0;
}
boolean equal(int x, int y, int xs, int ys, int inrow) {
for (int i = 0; i < inrow; i++) if (grid[x][y] != grid[x+xs*i][y+ys*i]) return false;
return true;
}
void drawWinLine(float x, float y, float x2, float y2) {
stroke(0, 255, 0);
strokeWeight(scl/4);
line(x, y, x2, y2);
}
VERSION 2 Java mode
Can work as tictactoe, just disable gravity (boolean gravity = false
) and set w,h and inRow to 3
(settings are set to tictactoe)
Version 2
int w = 3, h = 3, turn = 1, scl=100, inRow = 3;
int grid[][]= new int[w][h];
color clr[] = {color(255, 0, 0), color(0, 0, 255), color(0, 255, 0)}; //first is p1, second p2, third winClr
boolean gravity = !false;
void setup() {
size(300, 300);
}
void draw() {
background(0);
noFill();
stroke( ((checkWin() != 0)? clr[2] : ((turn == -1)? clr[1] : clr[0])));
strokeWeight(5);
rect(0, 0, width, height);
strokeWeight(0);
for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) {
fill( ((grid[i][j] == 1)? clr[0] : clr[1]));
if (grid[i][j] != 0) circle((i+0.5)*scl, (j+0.5)*scl, scl);
}
if (checkWin() != 0) noLoop();
}
void mousePressed() {
int mx = constrain(floor(map(mouseX, 0, width, 0, w)), 0, w), my = constrain(floor(map(mouseY, 0, height, 0, h)), 0, h);
for (int i = h-1; i >= 0 && !gravity; i--) if (grid[mx][i] == 0) {
grid[mx][i] = turn;
turn *= -1;
break;
}
if(gravity && grid[mx][my] == 0) {
grid[mx][my] = turn;
turn *= -1;
}
}
int checkWin() {
for (int i = 0; i < w+1-inRow; i++) for (int j = 0; j < h; j++) if (equal(i, j, 1, 0, inRow) && grid[i][j] != 0) {
drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i+inRow-0.5)*scl, (j+0.5)*scl);
return grid[i][j];
}
for (int i = 0; i < w; i++) for (int j = 0; j < h+1-inRow; j++) if (equal(i, j, 0, 1, inRow) && grid[i][j] != 0) {
drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i+0.5)*scl, (j+inRow-0.5)*scl);
return grid[i][j];
}
for (int i = 0; i < w+1-inRow; i++) for (int j = 0; j < h+1-inRow; j++) if (equal(i, j, 1, 1, inRow) && grid[i][j] != 0) {
drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i+inRow-0.5)*scl, (j+inRow-0.5)*scl);
return grid[i][j];
}
for (int i = inRow-1; i < w; i++) for (int j = 0; j < h+1-inRow; j++) if (equal(i, j, -1, 1, inRow) && grid[i][j] != 0) {
drawWinLine((i+0.5)*scl, (j+0.5)*scl, (i-(inRow-1.5))*scl, (j+inRow-0.5)*scl);
return grid[i][j];
}
return 0;
}
boolean equal(int x, int y, int xs, int ys, int inrow) {
for (int i = 0; i < inrow; i++) if (grid[x][y] != grid[x+xs*i][y+ys*i]) return false;
return true;
}
void drawWinLine(float x, float y, float x2, float y2) {
stroke(0, 255, 0);
strokeWeight(scl/4);
line(x, y, x2, y2);
}