package wuziqi;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JOptionPane;
public class MainPanel extends Panel implements MouseListener {
private static final int COLUMN = 16;
private static final int ROW = 16;
private static final int GAP = 40;
private static boolean isBlack = true;
private static int click_X;
private static int click_Y;
private char[][] allChess = new char[ROW][COLUMN];
public MainPanel() {
super();
for (int i = 0; i < allChess.length; i++) {
for (int j = 0; j < allChess[i].length; j++) {
allChess[i][j] = '*';
}
}
}
public void paint(Graphics g) {
for (int i = 0; i < ROW; i++) {
g.setColor(Color.BLACK);
g.drawLine(20, 20 + i * GAP, 640 - 20, 20 + i * GAP);
}
for (int i = 0; i < COLUMN; i++) {
g.setColor(Color.BLACK);
g.drawLine(20 + i * GAP, 20, 20 + i * GAP, 640 - 20); // 棋盘间隔分布
}
for (int i = 0; i < allChess.length; i++) {
for (int j = 0; j < allChess[i].length; j++) {
if (allChess[i][j] == 'w') {
g.setColor(Color.WHITE);
g.fillOval(5 + i * 40, 5 + j * 40, 30, 30);
}
if (allChess[i][j] == 'b') {
g.setColor(Color.BLACK);
g.fillOval(5 + i * 40, 5 + j * 40, 30, 30);
}
}
}
}
public void isCon(int x,int y){
char ch = allChess[x][y];
int RLastX = x;
while (RLastX >= 0 && allChess[RLastX][y] == ch) {
RLastX--;
}
int LRLastX=RLastX;
int RNum = 0;
RLastX++;
while (RLastX < allChess.length&& allChess[RLastX][y] == ch) {
RNum++;
RLastX++;
}
int LLastY = y;
while (LLastY >= 0 && allChess[x][LLastY] == ch) {
LLastY--;
}
int LLLastY=LLastY;
int LNum = 0;
LLastY++;
while ( LLastY < allChess[x].length&& allChess[x][LLastY] == ch) {
LLastY++;
LNum++;
}
int LDLastX = x;
int RULastY = y;
while (LDLastX >= 0 && RULastY < allChess[x].length&& allChess[LDLastX][RULastY] == ch) {
LDLastX--;
RULastY++;
}
int LLDLastX=LDLastX;
int DRULastY=RULastY;
int LDNum = 0;
LDLastX++;
RULastY--;
while (RULastY >= 0&& LDLastX < allChess.length
&& allChess[LDLastX][RULastY] == ch) {
LDNum++;
LDLastX++;
RULastY--;
}
int RULastX = x;
int LDLastY = y;
while (RULastX >= 0 && LDLastY >= 0 && allChess[RULastX][LDLastY] == ch) {
RULastX--;
LDLastY--;
}
int LRULastX=RULastX;
int ULDLastY=LDLastY;
int RUNum = 0;
RULastX++;
LDLastY++;
while (RULastX >= 0 && RULastX < allChess.length&& LDLastY < allChess[x].length
&& allChess[RULastX][LDLastY] == ch) {
RULastX++;
LDLastY++;
RUNum++;
}
int max=0;
if(max<RNum) max=RNum;
if(max<LNum) max=LNum;
if(max<LDNum) max=LDNum;
if(max<RUNum) max=RUNum;
if(max==RNum){
if(max==1){
if(x>0){
allChess[x-1][y]='w';
isBlack=true;
return;
}
else{
if(allChess[x+1][y]=='*'){
allChess[x+1][y]='w';
isBlack=true;
return;
}
else{
do {
x = (int) (Math.round((float) (Math.random() * 15)));
y = (int) (Math.round((float) (Math.random() * 15)));
} while(allChess[x][y] != '*');
allChess[x][y] = 'w';// 令棋盘(x,y)设置为白棋 isBlack = true;
isBlack=true;
return;
}
}
}
else{
if(allChess[LRLastX][y]=='*'){
allChess[LRLastX][y]='w';
isBlack=true;
return;
}
else if(allChess[RLastX][y]=='*'){
allChess[RLastX][y]='w';
isBlack=true;
return;
}
}
}
else if(max==LNum){
if(allChess[x][LLLastY]=='*'){
allChess[x][LLLastY]='w';
isBlack=true;
return;
}
else if(allChess[x][LLastY]=='*'){
allChess[x][LLastY]='w';
isBlack=true;
return;
}
}
else if(max==LDNum){
if(allChess[LLDLastX][DRULastY]=='*'){
allChess[LLDLastX][DRULastY]='w';
isBlack=true;
return;
}
else if(allChess[LDLastX][RULastY]=='*'){
allChess[LDLastX][RULastY]='w';
isBlack=true;
return;
}
}
else if(max==RUNum){
if(allChess[LRULastX][ULDLastY]=='*'){
allChess[LRULastX][ULDLastY]='w';
isBlack=true;
return;
}
else if(allChess[RULastX][LDLastY]=='*'){
allChess[RULastX][LDLastY]='w';
isBlack=true;
return;
}
}
}
public boolean isWin(int x, int y, boolean isColor) {
char ch = allChess[x][y];
int RLastX = x;
while (RLastX >= 0 && allChess[RLastX][y] == ch) {
RLastX--;
}
int RNum = 0;
RLastX++;
int itemRLastx = RLastX + 5;
while (RLastX < itemRLastx && RLastX < allChess.length
&& allChess[RLastX][y] == ch) {
RNum++;
RLastX++;
}
int LLastY = y;
while (LLastY >= 0 && allChess[x][LLastY] == ch) {
LLastY--;
}
int LNum = 0;
LLastY++;
int itemLLastY = LLastY + 5;
while (LLastY < itemLLastY && LLastY < allChess[x].length
&& allChess[x][LLastY] == ch) {
LLastY++;
LNum++;
}
int LDLastX = x;
int RULastY = y;
while (LDLastX >= 0 && RULastY < allChess[x].length
&& allChess[LDLastX][RULastY] == ch) {
LDLastX--;
RULastY++;
}
int LDNum = 0;
LDLastX++;
RULastY--;
int itemLDLastX = LDLastX + 5;
while (LDLastX < itemLDLastX && RULastY >= 0
&& LDLastX < allChess.length
&& allChess[LDLastX][RULastY] == ch) {
LDNum++;
LDLastX++;
RULastY--;
}
int RULastX = x;
int LDLastY = y;
while (RULastX >= 0 && LDLastY >= 0 && allChess[RULastX][LDLastY] == ch) {
RULastX--;
LDLastY--;
}
int RUNum = 0;
RULastX++;
LDLastY++;
int itemLDLastY = LDLastY + 5;
while (RULastX >= 0 && RULastX < allChess.length
&& LDLastY < itemLDLastY && LDLastY < allChess[x].length
&& allChess[RULastX][LDLastY] == ch) {
RULastX++;
LDLastY++;
RUNum++;
}
if (RNum == 5 || LNum == 5 || RUNum == 5 || LDNum == 5) {
return true;
}
return false;
}
@Override
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
int click_x = e.getX();
int click_y = e.getY();
int chess_x = Math.round((float) (click_x - 20) / GAP);
int chess_y = Math.round((float) (click_y - 20) / GAP);
click_X = chess_x;
click_Y = chess_y;
if (isBlack == true && allChess[chess_x][chess_y] == '*') {
allChess[chess_x][chess_y] = 'b';
isBlack = false;
}
repaint();
if (isWin(chess_x, chess_y, isBlack)) {
if (isBlack) {
JOptionPane.showMessageDialog(null, "白棋赢了");
System.exit(0);
} else {
JOptionPane.showMessageDialog(null, "黑棋赢了");
System.exit(0);
}
}
if (isBlack == false) {
isCon(chess_x, chess_y);
/*
* do { chess_x = (int) (Math.round((float) (Math.random() * 15)));
* chess_y = (int) (Math.round((float) (Math.random() * 15)));
* System.out.println(chess_x + " " + chess_y); } while
* (allChess[chess_x][chess_y] != '*');
* System.out.println(allChess[chess_x][chess_y]);
* allChess[chess_x][chess_y] = 'w';
*
* /* if(isBlack==false&&allChess[chess_x][chess_y]=='*'){
* allChess[chess_x][chess_y] = 'w'; isBlack = true;
* }
*/
repaint();
if (isWin(chess_x, chess_y, isBlack)) {
if (isBlack) {
JOptionPane.showMessageDialog(null, "白棋赢了");
} else {
JOptionPane.showMessageDialog(null, "黑棋赢了");
}
}
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
评论0