Skip to content

@drabiter /N.java
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Connect4K with AI. Run this as applet.
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;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.