Code:
import java.applet.Applet;
import java.awt.*;
public class Connect4 extends Applet
{
public void init()
{
c4k = new Connect4Kernel(this);
bestMove[0] = c4k.Field(0, 0);
value[0] = 0;
level = 4;
levelEverDecreased = false;
changeCnt = 0;
lastHumanArrowColumn = -1;
lastCompArrowColumn = -1;
lastHumanArrowEmpty = true;
currStatus = new StringBuffer("Start your game!");
if(g == null)
g = getGraphics();
SetMode(0);
}
public void SetMode(int i)
{
mode = i;
}
public boolean IsMode(int i)
{
return mode == i;
}
public void DrawStatus(String s)
{
Dimension dimension = size();
int i = (dimension.width - 10) / 7;
int j = dimension.height / 8;
g.setColor(Color.white);
g.fillRect(5, 7 * j + 1, 7 * i, j - 5 - 1);
if(currFontSize != j)
{
currFontSize = j;
font = new Font("Dialog", 0, (j * 5) / 10);
}
g.setFont(font);
currStatus = new StringBuffer(s);
g.setColor(Color.black);
g.drawString(currStatus.toString(), 15, 7 * j + (j * 5) / 8);
for(int k = 4; k < 9 && k <= level; k++)
{
g.setColor(Color.green);
g.fillRect(7 * i - 5 - 20, 8 * j - 5 - (k - 3) * 4 - 1, 22, 3);
}
}
public void DrawChip(int i, int j)
{
Dimension dimension = size();
int k = (dimension.width - 10) / 7;
int l = dimension.height / 8;
int i1 = 5 + k * i + k / 8;
int j1 = (dimension.height - (j + 2) * l) + l / 8;
int k1 = (k * 3) / 4;
int l1 = (l * 3) / 4;
Color color;
switch(c4k.FieldState(j, i))
{
case 1: // '\001'
color = Color.green;
break;
case -1:
color = Color.red;
break;
case 0: // '\0'
default:
color = Color.white;
break;
}
g.setColor(color);
g.fillOval(i1, j1, k1, l1);
}
public void paint(Graphics g1)
{
Dimension dimension = size();
int i = (dimension.width - 10) / 7;
int j = dimension.height / 8;
g1.setColor(Color.white);
g1.fillRect(0, 0, (7 * i + 10) - 1, j * 8 - 1);
g1.setColor(Color.blue.darker().darker());
g1.fillRect(5, j, i * 7, j * 6);
for(int k = 0; k < 6; k++)
{
for(int j1 = 0; j1 < 7; j1++)
DrawChip(j1, k);
}
DrawStatus(currStatus.toString());
g1.setColor(Color.gray);
for(int l = 0; l < 5; l++)
{
g1.drawLine(l, l, l, j * 8 - l - 1);
g1.drawLine(l + 1, l, (i * 7 + 10) - 1, l);
}
g1.setColor(Color.darkGray);
for(int i1 = 0; i1 < 5; i1++)
{
g1.drawLine(((i * 7 - i1) + 10) - 1, i1 + 1, ((i * 7 - i1) + 10) - 1, j * 8 - i1 - 1);
g1.drawLine(i1, j * 8 - i1 - 1, (i * 7 + 10) - 1, j * 8 - i1 - 1);
}
}
public int DoMove(int i, int j)
{
int k = c4k.DoMove(i, j);
DrawChip(j, c4k.Line(k));
if(i == -1)
lastCompArrowColumn = -1;
else
lastHumanArrowColumn = -1;
DrawArrows(j, i);
return k;
}
private void AdaptLevel()
{
if(c4k.Cnt() < 6000)
{
if(++changeCnt == 2)
{
level++;
changeCnt = 0;
return;
}
} else
{
if(c4k.Cnt() > 40000)
{
level--;
changeCnt = 0;
levelEverDecreased = true;
return;
}
changeCnt = 0;
}
}
public boolean mouseUp(Event event, int i, int j)
{
if(IsMode(2))
{
init();
repaint();
return true;
}
SetMode(1);
if(c4k.GameOver(bestMove[0], value) == 0)
{
Dimension dimension = size();
int k = ((i - 5) * 7) / dimension.width;
if(k >= 0 && k <= 6 && c4k.IsEmpty(k))
{
currField = DoMove(-1, k);
label0:
switch(res = c4k.GameOver(currField, value))
{
default:
break;
case -1:
DrawStatus("It ends in a tie! (click to restart)");
SetMode(2);
break;
case 1: // '\001'
DrawStatus("You won! (click to restart)");
SetMode(2);
break;
case 0: // '\0'
res = c4k.BestMove(1, bestMove, level);
currField = DoMove(1, bestMove[0]);
AdaptLevel();
switch(res)
{
case -1:
DrawStatus("Your move!");
break label0;
case 1: // '\001'
if(c4k.GameOver(currField, value) != 0)
{
DrawStatus("I won! (click to restart)");
SetMode(2);
break label0;
}
if(levelEverDecreased)
DrawStatus("Your move!");
else
DrawStatus("I'm going to win!");
break;
case 0: // '\0'
default:
DrawStatus("Your move!");
break;
}
break;
}
}
}
if(IsMode(1))
SetMode(0);
return true;
}
void DrawArrow(Graphics g1, int i, int j, int k, Color color, boolean flag)
{
int ai[] = new int[8];
int ai1[] = new int[8];
g1.setColor(color);
ai[0] = 5 + k * i + i / 2;
ai1[0] = (j * 3) / 4;
ai[1] = ai[0] + i / 5;
ai1[1] = ai1[0] - j / 4;
ai[2] = ai[0] + i / 10;
ai1[2] = ai1[1];
ai[3] = ai[2];
ai1[3] = ai1[0] - j / 2;
ai[4] = ai[0] - i / 10;
ai1[4] = ai1[3];
ai[5] = ai[4];
ai1[5] = ai1[1];
ai[6] = ai[0] - i / 5;
ai1[6] = ai1[1];
ai[7] = ai[0];
ai1[7] = ai1[0];
if(flag)
{
g1.fillPolygon(ai, ai1, 8);
return;
} else
{
g1.drawPolygon(ai, ai1, 8);
return;
}
}
void DrawArrows(int i, int j)
{
if(i >= 0 && i < 7)
{
boolean flag = c4k.IsEmpty(i);
Dimension dimension = size();
int k = (dimension.width - 10) / 7;
int l = dimension.height / 8;
g.setColor(Color.white);
g.fillRect(5, 5, 7 * k, l - 5);
if(j == -1)
{
lastHumanArrowColumn = i;
lastHumanArrowEmpty = flag;
} else
if(j == 1)
{
lastCompArrowColumn = i;
} else
{
lastHumanArrowColumn = -1;
lastCompArrowColumn = -1;
}
if(lastHumanArrowColumn != -1)
{
Color color;
if(lastHumanArrowEmpty)
color = Color.red;
else
color = Color.gray;
DrawArrow(g, k, l, lastHumanArrowColumn, color, true);
}
if(lastCompArrowColumn != -1)
{
Color color1 = Color.green;
DrawArrow(g, k, l, lastCompArrowColumn, color1, lastCompArrowColumn != lastHumanArrowColumn);
}
}
}
public boolean mouseMove(Event event, int i, int j)
{
if(IsMode(0))
{
Dimension dimension = size();
int k = ((i - 5) * 7) / dimension.width;
if(k != lastHumanArrowColumn)
DrawArrows(k, -1);
}
return true;
}
public String getAppletInfo()
{
return "Connect4 - Java (beta) Applet by Sven Wiebus, Dec. 1995";
}
public Connect4()
{
bestMove = new int[1];
value = new int[1];
levelEverDecreased = false;
lastCompArrowColumn = -1;
lastHumanArrowColumn = -1;
lastHumanArrowEmpty = false;
mode = -1;
}
public static final int WAITING_FOR_MOVE = 0;
public static final int ANALYZING = 1;
public static final int RESTART = 2;
public static final int BORDER_WIDTH = 5;
Connect4Kernel c4k;
int currField;
int level;
int bestMove[];
int res;
int value[];
int changeCnt;
boolean levelEverDecreased;
int lastCompArrowColumn;
int lastHumanArrowColumn;
boolean lastHumanArrowEmpty;
int currFontSize;
Font font;
StringBuffer currStatus;
Graphics g;
Panel panel;
int mode;
}