public
Last active

Connect4K with AI. Run this as applet.

  • Download Gist
N.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
import java.applet.Applet;
import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.Arrays;
 
 
public class N extends Applet implements Runnable{
private static final int CELL = 50, COL = 6, ROW = 8, WIDTH = CELL * COL, HEIGHT = CELL * ROW + CELL;
private static final int[] blank_0 = {-1, -1, -1, -1, -1, -1};
private int x = -1, y = -1, vm = -1, lastX = -1;
private boolean m = false, human = true, first = true, q = false;
private int[] up = new int[COL];
private int[][] v;
public void start(){
new Thread(this).start();
}
 
@Override
public void run() {
// TODO Auto-generated method stub
final int dy = 300;
setSize(new Dimension(WIDTH, HEIGHT));
enableEvents(AWTEvent.KEY_EVENT_MASK);
requestFocus();
while(true){
x = y = vm = lastX = -1;
m = q = false;
human = first = q = true;
v = new int[COL][ROW];
Arrays.fill(up, ROW - 1);
long a =0;
int t = 0, fps = 0, i = 0, r = 0, win_v = -1;
boolean win = false;
first = true;
BufferedImage b = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)b.getGraphics();
Graphics p = getGraphics();
for (long d = 0, start = System.nanoTime(); q; d = System.nanoTime() - start, start = System.nanoTime()){
a += d;
t++;
if (a >= 1000000000L) {
a -= 1000000000L;
fps = t;
t = 0;
}
if (!win){
if (m){
y += d * dy / 1000000000L;
if (y >= up[x] * CELL){
if (vm == 1) lastX = x;
m = false;
v[x][up[x]] = vm;
up[x]--;
if (checkWin(vm)){
win_v = vm;
win = true;
}
x = y = vm = -1;
human = !human;
if (Arrays.equals(up, blank_0)){
win = true;
win_v = 1;
}
}
}else{
if (!human){
m = true;
if (lastX == COL / 2 - 1){ //2 (3)
if (first){
x = COL / 2;
first = false;
}else{
x = lastX;
}
}else if (lastX == COL / 2){ //3 (4)
if (first){
x = COL / 2 - 1;
first = false;
}else{
x = lastX;
}
}else{ // follow-up
x = lastX;
}
if (up[x] < 0){
for (i=0; i<up.length; i++){
if (up[i] % 2 == 0){ //ganjil
x = i;
break;
}
}
}
y = 0;
vm = 2;
}
}
}
g.setColor(Color.black);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.DARK_GRAY); //background
g.fillRect(0, 0, WIDTH, HEIGHT - CELL);
for (i=1; i<=COL; i++)
g.drawString(String.valueOf(i), i * CELL - 30, HEIGHT - 20);
g.setColor(Color.black);
for (i=1; i<ROW; i++)
g.drawLine(0, i * CELL, WIDTH, i * CELL);
for (i=1; i<COL; i++)
g.drawLine(i * CELL, 0, i * CELL, HEIGHT);
for (i=0; i<COL; i++){
for (r=0; r<ROW; r++){
if (v[i][r] != 0){
g.setColor((v[i][r]==2)? Color.black : Color.white);
g.fillOval(i * CELL, r * CELL, CELL, CELL);
}
}
}
if (m){
g.setColor((vm == 2)? Color.black : Color.white);
g.fillOval(x * CELL, y, CELL, CELL);
}
g.setColor(Color.red);
g.drawString("FPS " + String.valueOf(fps), 10, HEIGHT - 5);
g.drawString("press 9 to restart", 200, HEIGHT - 5);
if (win){
g.setColor(Color.green);
if (win_v == 1){
g.drawString("DRAW!", WIDTH /2 - 50, HEIGHT - 5);
}else{
g.drawString(((win_v == 2)? "BLACK" : "WIN").concat(" HAS WIN!"), WIDTH /2 - 50, HEIGHT - 5);
}
}
p.drawImage(b, 0, 0, null);
try {
Thread.sleep(2L);
} catch (Exception e) {}
if (!isActive())
return;
}
}
}
private boolean checkWin(int v1){
boolean ret = false;
for (int r=0; r<2; r++)
for (int i=0; i<5; i++)
if (v[r][i] == v1 && v[r+1][i+1] == v1 && v[r+2][i+2] == v1 && v[r+3][i+3] == v1) ret = true;
if (!ret)
for (int r=COL-1; r>COL-3; r--)
for (int i=0; i<5; i++)
if (v[r][i] == v1 && v[r-1][i+1] == v1 && v[r-2][i+2] == v1 && v[r-3][i+3] == v1) ret = true;
if (!ret)
for (int r=0; r<ROW; r++)
for (int i=0; i<COL/2; i++)
if (v[i][r] == v1 && v[i+1][r] == v1 && v[i+2][r] == v1 && v[i+3][r] == v1) ret = true;
if (!ret)
for (int r=0; r<COL; r++)
for (int i=0; i<=ROW-4; i++)
if (v[r][i] == v1 && v[r][i+1] == v1 && v[r][i+2] == v1 && v[r][i+3] == v1) ret = true;
return ret;
}
public void processEvent(AWTEvent e){
if (e.getID() == KeyEvent.KEY_RELEASED){
int k = ((KeyEvent) e).getKeyCode();
if (k == 57)
q = false;
if (k >= 49 && k<=54 && !m){
x = (k - 49);
if (up[x] >= 0){
m = true;
y = 0;
vm = 1;
}
}
}
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.