📄 othello.java
字号:
}
if(grid[k1] == k)
break;
}
}
flag = false;
for(int l5 = j - 1; l5 >= 0; l5--)
{
int l1 = i * DIM + l5;
if(grid[l1] == l)
continue;
if(grid[l1] == k)
flag = true;
break;
}
if(flag)
{
for(int i6 = j - 1; i6 >= 0; i6--)
{
int i2 = i * DIM + i6;
if(grid[i2] == l)
{
grid[i2] = i1;
continue;
}
if(grid[i2] == k)
break;
}
}
flag = false;
for(int j6 = i + 1; j6 < DIM; j6++)
{
int j2 = j6 * DIM + j;
if(grid[j2] == l)
continue;
if(grid[j2] == k)
flag = true;
break;
}
if(flag)
{
for(int k6 = i + 1; k6 < DIM; k6++)
{
int k2 = k6 * DIM + j;
if(grid[k2] == l)
{
grid[k2] = i1;
continue;
}
if(grid[k2] == k)
break;
}
}
flag = false;
for(int l6 = i - 1; l6 >= 0; l6--)
{
int l2 = l6 * DIM + j;
if(grid[l2] == l)
continue;
if(grid[l2] == k)
flag = true;
break;
}
if(flag)
{
for(int i7 = i - 1; i7 >= 0; i7--)
{
int i3 = i7 * DIM + j;
if(grid[i3] == l)
grid[i3] = i1;
if(grid[i3] == k)
break;
}
}
flag = false;
int j7 = j + 1;
for(int k7 = i - 1; j7 < DIM && k7 >= 0; k7--)
{
int j3 = k7 * DIM + j7;
if(grid[j3] != l)
{
if(grid[j3] == k)
flag = true;
break;
}
j7++;
}
if(flag)
{
int l7 = j + 1;
for(int j8 = i - 1; l7 < DIM && j8 >= 0; j8--)
{
int k3 = j8 * DIM + l7;
if(grid[k3] == l)
grid[k3] = i1;
if(grid[k3] == k)
break;
l7++;
}
}
flag = false;
int i8 = j + 1;
for(int k8 = i + 1; i8 < DIM && k8 < DIM; k8++)
{
int l3 = k8 * DIM + i8;
if(grid[l3] != l)
{
if(grid[l3] == k)
flag = true;
break;
}
i8++;
}
if(flag)
{
int l8 = j + 1;
for(int j9 = i + 1; l8 < DIM && j9 < DIM; j9++)
{
int i4 = j9 * DIM + l8;
if(grid[i4] == l)
grid[i4] = i1;
if(grid[i4] == k)
break;
l8++;
}
}
flag = false;
int i9 = j - 1;
for(int k9 = i - 1; i9 >= 0 && k9 >= 0; k9--)
{
int j4 = k9 * DIM + i9;
if(grid[j4] != l)
{
if(grid[j4] == k)
flag = true;
break;
}
i9--;
}
if(flag)
{
int l9 = j - 1;
for(int j10 = i - 1; l9 >= 0 && j10 >= 0; j10--)
{
int k4 = j10 * DIM + l9;
if(grid[k4] == l)
grid[k4] = i1;
if(grid[k4] == k)
break;
l9--;
}
}
flag = false;
int i10 = j - 1;
for(int k10 = i + 1; i10 >= 0 && k10 < DIM; k10++)
{
int l4 = k10 * DIM + i10;
if(grid[l4] != l)
{
if(grid[l4] == k)
flag = true;
break;
}
i10--;
}
if(flag)
{
int l10 = j - 1;
for(int i11 = i + 1; l10 >= 0 && i11 < DIM; i11++)
{
int i5 = i11 * DIM + l10;
if(grid[i5] == l)
grid[i5] = i1;
if(grid[i5] == k)
break;
l10--;
}
}
animating = true;
}
public void checkScore()
{
whiteScore = blackScore = 0;
for(int j = 0; j < DIM; j++)
{
for(int k = 0; k < DIM; k++)
{
int i = j * DIM + k;
if(grid[i] == 2 || grid[i] == 5)
whiteScore++;
else
if(grid[i] == 3 || grid[i] == 4)
blackScore++;
}
}
}
public synchronized void paint(Graphics g)
{
if(!imageTracker.checkAll())
{
g.setColor(getForeground());
g.drawString("Loading images", 50, 50);
return;
}
buttonHeight = button.size().height + button.location().y * 2;
getFontMetrics(f);
int i = size().width - fontHeight * 3 - buttonHeight;
int j = size().height - fontHeight * 3 - buttonHeight;
if(i != boardSize)
if(i > j)
boardSize = (j / DIM) * DIM;
else
boardSize = (i / DIM) * DIM;
if(boardSize > 400)
boardSize = 400;
offScrGraphics.setColor(getBackground());
offScrGraphics.fillRect(0, 0, size().width, size().height);
offScrGraphics.setColor(Color.white);
offScrGraphics.fillRect(0, 0, boardSize, boardSize);
int k = boardSize / DIM;
offScrGraphics.setColor(Color.red);
for(int l = 0; l < DIM; l++)
{
offScrGraphics.drawLine(0, l * k, boardSize - 1, l * k);
for(int i1 = 0; i1 < DIM; i1++)
{
offScrGraphics.drawLine(i1 * k, 0, i1 * k, boardSize - 1);
if(grid[l * DIM + i1] == 2)
offScrGraphics.drawImage(whiteImage, i1 * k + 1, l * k + 1, k - 2, k - 2, this);
else
if(grid[l * DIM + i1] == 3)
offScrGraphics.drawImage(blackImage, i1 * k + 1, l * k + 1, k - 2, k - 2, this);
}
}
offScrGraphics.drawLine(boardSize - 1, 0, boardSize - 1, boardSize - 1);
offScrGraphics.drawLine(0, boardSize - 1, boardSize - 1, boardSize - 1);
offScrGraphics.setColor(getForeground());
if(illegalMove)
offScrGraphics.drawString("Illegal Move", button.location().x, boardSize + fontAscent);
else
if(blackNotMoving)
offScrGraphics.drawString("Computer Pass", button.location().x, boardSize + fontAscent);
offScrGraphics.drawImage(whiteImage, button.location().x, (boardSize + fontHeight * 2) - fontAscent, fontAscent, fontAscent, this);
offScrGraphics.drawString(" Player: " + String.valueOf(whiteScore), button.location().x + fontAscent, boardSize + fontHeight * 2);
offScrGraphics.drawImage(blackImage, button.location().x, (boardSize + fontHeight * 3) - fontAscent, fontAscent, fontAscent, this);
offScrGraphics.drawString(" Computer: " + String.valueOf(blackScore), button.location().x + fontAscent, boardSize + fontHeight * 3);
offScrGraphics.setColor(Color.white);
boolean flag = false;
if(animating)
{
for(int j1 = k - 2; j1 > 0;)
{
for(int k1 = 0; k1 < DIM; k1++)
{
for(int l1 = 0; l1 < DIM; l1++)
if(grid[k1 * DIM + l1] == 5)
{
offScrGraphics.fillRect(l1 * k + 1, k1 * k + 1, k - 1, k - 1);
offScrGraphics.drawImage(blackImage, (l1 * k + 1 + k / 2) - j1 / 2, (k1 * k + 1 + k / 2) - j1 / 2, j1, j1, this);
if(j1 <= 2)
grid[k1 * DIM + l1] = 2;
} else
if(grid[k1 * DIM + l1] == 4)
{
offScrGraphics.fillRect(l1 * k + 1, k1 * k + 1, k - 1, k - 1);
offScrGraphics.drawImage(whiteImage, (l1 * k + 1 + k / 2) - j1 / 2, (k1 * k + 1 + k / 2) - j1 / 2, j1, j1, this);
if(j1 <= 2)
grid[k1 * DIM + l1] = 3;
}
}
g.drawImage(offScrImage, button.location().x, buttonHeight, this);
j1 -= 3;
try
{
Thread.sleep(50L);
}
catch(Exception _ex) { }
}
animating = false;
paint(g);
}
g.drawImage(offScrImage, button.location().x, buttonHeight, this);
}
public synchronized void update(Graphics g)
{
paint(g);
}
public synchronized boolean mouseUp(Event event, int i, int j)
{
if(!imageTracker.checkID(0, true))
return true;
int k = ((i - button.location().x) * DIM) / boardSize;
int l = ((j - buttonHeight) * DIM) / boardSize;
if(l < DIM && k < DIM && grid[l * DIM + k] == 0)
{
illegalMove = false;
if(howGood(l, k, 2, 3) <= 0)
{
illegalMove = true;
paint(getGraphics());
return true;
}
grid[l * DIM + k] = 2;
turnPlys(l, k, 2, 3, 5);
checkScore();
paint(getGraphics());
try
{
Thread.sleep(1000L);
}
catch(Exception _ex) { }
blackMove();
checkScore();
paint(getGraphics());
} else
if(l < DIM && k < DIM && grid[l * DIM + k] == 3)
{
illegalMove = true;
paint(getGraphics());
return true;
}
return true;
}
public String getAppletInfo()
{
return "Othello-Lite by F. Permadi";
}
public Othello()
{
boardSize = 400;
grid = new int[400];
}
int boardSize;
static int DIM = 10;
int grid[];
MediaTracker imageTracker;
Image whiteImage;
Image blackImage;
Image offScrImage;
Graphics offScrGraphics;
Font f;
int fontHeight;
int fontAscent;
Button button;
int buttonHeight;
Choice choice;
Choice moveFirst;
boolean whiteMoveFirst;
boolean illegalMove;
boolean blackNotMoving;
boolean animating;
int whiteScore;
int blackScore;
Thread aThread;
static final int EMPTY = 0;
static final int WHITE = 2;
static final int BLACK = 3;
static final int BLACK_TEMP = 4;
static final int WHITE_TEMP = 5;
static final int VERYGOOD = 9999;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -