分享
 
 
 

简易黑白棋--我的第一个程序

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

/**

* @author FOAS@bbs.xjtu.edu.cn On Oct.23,2004 12:00

*

* TODO To change the template for this generated type comment go to Window -

* Preferences - Java - Code Generation - Code and Comments

*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class First extends JFrame {

public First() {

DrawPane drawPane = new DrawPane(getBackground());

Container pane = getContentPane();

pane.add(drawPane, BorderLayout.CENTER);

setSize(600, 580);

//f.setExtendedState(JFrame.MAXIMIZED_BOTH);

//f.setUndecorated(true);

//f.setAlwaysOnTop(true);

//f.setLocationByPlatform(true);

setLocation(208, 104);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//setBackground(Color.PINK);

setResizable(false);

setVisible(true);

}

public static void main(String[] args) {

new First();

}

}

class DrawPane extends JPanel {

private Insets insets;

final static int BLACK = -1;

final static int WHITE = 1;

private Point point = null;

private int colorStatus;

private int[][] status = null;

private int count;

private int chessNum;

private JTextArea infoText = null;

private int[][] statusW = null;

private int level=1 ;

private int whiteCount=0;

private int blackCount=0;

private Color bgColor=null;

public DrawPane(Color c) {

initComponent();

initChess();

bgColor=c;

addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

response(e.getPoint());

}

});

setFocusable(true);

addKeyListener(new myKeyListener());

}

public void paintComponent(Graphics g) {

super.paintComponent(g);

if (insets == null) {

insets = getInsets();

}

//super.paintComponents(g);

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(Color.orange);

g2d.fill3DRect(50, 50, 400, 400, true);

g2d.setColor(Color.RED);

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

g2d.drawString(i + "", 50 * i + 75, 45);

g2d.drawString(j + "", 40, 50 * j + 75);

g2d.drawRect(50 + 50 * i, 50 + 50 * j, 50, 50);

if (status[i][j] == BLACK) {

g2d.setColor(Color.BLACK);

g2d.fillOval(i * 50 + 53, j * 50 + 53, 44, 44);

}

if (status[i][j] == WHITE) {

g2d.setColor(Color.WHITE);

g2d.fillOval(i * 50 + 53, j * 50 + 53, 44, 44);

}

if (status[i][j] > 1) {

// g2d.setColor(Color.BLUE);

// g2d.drawString("" + (status[i][j] - 1), i * 50 + 75,

// j * 50 + 75);

g2d.drawString("X", i * 50 + 75, j * 50 + 75);

}

g2d.setColor(Color.BLACK);

g2d.fillOval(200, 470, 30, 30);

g2d.setColor(Color.WHITE);

g2d.fillOval(300, 470, 30, 30);

g2d.setColor(bgColor);

g2d.fillRect(240, 470, 60, 30);

g2d.fillRect(340, 470, 60, 30);

g2d.setColor(Color.RED);

g2d.drawString("" + blackCount, 240, 490);

g2d.drawString("" + whiteCount, 340, 490);

}

}

//jug(colorStatus);

}

public void initComponent() {

infoText = new JTextArea();

setLayout(new BorderLayout());

infoText.setEditable(false);

//infoText.setBackground(Color.PINK);

infoText.setForeground(Color.BLACK);

JScrollPane jsp = new JScrollPane(infoText);

JPanel infoPanel = new JPanel(new BorderLayout());

//infoPanel.setBackground(Color.PINK);

infoPanel.add(jsp, BorderLayout.CENTER);

infoPanel.add(new JLabel(" "), BorderLayout.NORTH);

infoPanel.add(new JLabel(" "), BorderLayout.SOUTH);

add(infoPanel, BorderLayout.EAST);

}

public void initChess() {

chessNum = 4;

count = 0;

colorStatus = BLACK;

status = new int[8][8];

status[3][3] = BLACK;

status[4][4] = BLACK;

status[3][4] = WHITE;

status[4][3] = WHITE;

statusW = new int[8][8];

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

statusW[i][j] = 2;

}

}

for (int i = 0; i < 4; i++) {

statusW[i + 2][0] = 3;

statusW[i + 2][1] = 1;

statusW[i + 2][7] = 3;

statusW[i + 2][6] = 1;

statusW[0][i + 2] = 3;

statusW[1][i + 2] = 1;

statusW[7][i + 2] = 3;

statusW[6][i + 2] = 1;

}

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++) {

statusW[i][j] = 0;

statusW[7 - i][j] = 0;

statusW[i][7 - j] = 0;

statusW[7 - i][7 - j] = 0;

}

}

statusW[0][0] = 4;

statusW[0][7] = 4;

statusW[7][0] = 4;

statusW[7][7] = 4;

jug(colorStatus);

String msg=" F1: 级别1\n F2: 级别2\n F3: 重新开始";

infoText.setText(msg+" \n");

repaint();

}

public void response(Point p) {

boolean interrupt = false;

point = setPoint(p);

if (point != null) {

int x = point.x / 50 - 1;

int y = point.y / 50 - 1;

if (check(x, y, colorStatus) > 0) {

manulChess(x, y, BLACK);

if (level == 1) {

autoChess(WHITE);

} else {

autoChess(WHITE, 2);

}

if (blPass()) {

if (!blPass()) {

repaint();

} else {

interrupt = true;

}

}

if (blOver(interrupt)) {

initChess();

}

}

}

}

public Point setPoint(Point p) {

if (p.x < 50 || p.x > 450 || p.y < 50 || p.y > 450) {

return null;

} else {

p.x = (p.x / 50) * 50;

p.y = (p.y / 50) * 50;

return p;

}

}

public boolean change(int i, int j, int sta) {

boolean flag = false;

//north

if (j != 0) {

if (sta != status[i][j - 1] && 1 == Math.abs(status[i][j - 1])) {

int m = j - 1;

while (status[i][j - 1] == status[i][m] && m > 0) {

m--;

// System.out.println(m);

}

if (status[i][m] == sta) {

flag = true;

for (int n = j - 1; n > m; n--) {

status[i][n] = sta;

// System.out.println("[" + i + "," + n + "]"

// + status[i][n]);

}

}

}

}

//south

if (j != 7) {

if (sta != status[i][j + 1] && 1 == Math.abs(status[i][j + 1])) {

int m = j + 1;

while (status[i][j + 1] == status[i][m] && m < 7) {

m++;

// System.out.println(m);

}

if (status[i][m] == sta) {

flag = true;

for (int n = j + 1; n < m; n++) {

status[i][n] = sta;

// System.out.println("[" + i + "," + n + "]"

// + status[i][n]);

}

}

}

}

//west

if (i != 0) {

if (sta != status[i - 1][j] && 1 == Math.abs(status[i - 1][j])) {

int m = i - 1;

while (status[i - 1][j] == status[m][j] && m > 0) {

m--;

// System.out.println(m);

}

if (status[m][j] == sta) {

flag = true;

for (int n = i - 1; n > m; n--) {

status[n][j] = sta;

// System.out.println("[" + n + "," + j + "]"

// + status[n][j]);

}

}

}

}

//east

if (i != 7) {

if (sta != status[i + 1][j] && 1 == Math.abs(status[i + 1][j])) {

int m = i + 1;

while (status[i + 1][j] == status[m][j] && m < 7) {

m++;

// System.out.println(m);

}

if (status[m][j] == sta) {

flag = true;

for (int n = i + 1; n < m; n++) {

status[n][j] = sta;

// System.out.println("[" + n + "," + j + "]"

// + status[n][j]);

}

}

}

}

//northwest

if (j != 0 && i != 0) {

if (sta != status[i - 1][j - 1]

&& 1 == Math.abs(status[i - 1][j - 1])) {

int m = i - 1;

int n = j - 1;

while (status[i - 1][j - 1] == status[m][n] && m > 0 && n > 0) {

m--;

n--;

// System.out.println("m=" + m + " n=" + n);

}

if (status[m][n] == sta) {

flag = true;

for (int x = i - 1, y = j - 1; x > m; x--, y--) {

// for (int y = j - 1; y > n; y--) {

status[x][y] = sta;

// System.out.println("[" + x + "," + y + "]"

// + status[x][y]);

// }

}

}

}

}

//southeast

if (j != 7 && i != 7) {

if (sta != status[i + 1][j + 1]

&& 1 == Math.abs(status[i + 1][j + 1])) {

int m = i + 1;

int n = j + 1;

while (status[i + 1][j + 1] == status[m][n] && m < 7 && n < 7) {

m++;

n++;

// System.out.println("m=" + m + " n=" + n);

}

if (status[m][n] == sta) {

flag = true;

for (int x = i + 1, y = j + 1; x < m; x++, y++) {

// for (int y = j + 1; y < n; y++) {

status[x][y] = sta;

// System.out.println("[" + x + "," + y + "]"

// + status[x][y]);

// }

}

}

}

}

//northeast

if (j != 0 && i != 7) {

if (sta != status[i + 1][j - 1]

&& 1 == Math.abs(status[i + 1][j - 1])) {

int m = i + 1;

int n = j - 1;

while (status[i + 1][j - 1] == status[m][n] && m < 7 && n > 0) {

m++;

n--;

// System.out.println("m=" + m + " n=" + n);

}

if (status[m][n] == sta) {

flag = true;

for (int x = i + 1, y = j - 1; x < m; x++, y--) {

// for (int y = j - 1; y > n; y--) {

status[x][y] = sta;

// System.out.println("[" + x + "," + y + "]"

// + status[x][y]);

// }

}

}

}

}

//southwest

if (j != 7 && i != 0) {

if (sta != status[i - 1][j + 1]

&& 1 == Math.abs(status[i - 1][j + 1])) {

int m = i - 1;

int n = j + 1;

while (status[i - 1][j + 1] == status[m][n] && m > 0 && n < 7) {

m--;

n++;

// System.out.println("m=" + m + " n=" + n);

}

if (status[m][n] == sta) {

flag = true;

for (int x = i - 1, y = j + 1; x > m; x--, y++) {

// for (int y = j + 1; y < n; y++) {

status[x][y] = sta;

// System.out.println("[" + x + "," + y + "]"

// + status[x][y]);

//}

}

}

}

}

return flag;

}

public int check(int i, int j, int sta) {

int flag = 0;

if (Math.abs(status[i][j]) == 1) {

return 0;

}

//north

if (j != 0) {

if (sta != status[i][j - 1] && 1 == Math.abs(status[i][j - 1])) {

int m = j - 1;

while (status[i][j - 1] == status[i][m] && m > 0) {

m--;

// System.out.println(m);

}

if (status[i][m] == sta) {

flag = flag + (j - 1 - m);

}

}

}

//south

if (j != 7) {

if (sta != status[i][j + 1] && 1 == Math.abs(status[i][j + 1])) {

int m = j + 1;

while (status[i][j + 1] == status[i][m] && m < 7) {

m++;

// System.out.println(m);

}

if (status[i][m] == sta) {

flag = flag + (m - j - 1);

}

}

}

//west

if (i != 0) {

if (sta != status[i - 1][j] && 1 == Math.abs(status[i - 1][j])) {

int m = i - 1;

while (status[i - 1][j] == status[m][j] && m > 0) {

m--;

// System.out.println(m);

}

if (status[m][j] == sta) {

flag = flag + (i - 1 - m);

}

}

}

//east

if (i != 7) {

if (sta != status[i + 1][j] && 1 == Math.abs(status[i + 1][j])) {

int m = i + 1;

while (status[i + 1][j] == status[m][j] && m < 7) {

m++;

// System.out.println(m);

}

if (status[m][j] == sta) {

flag = flag + (m - i - 1);

}

}

}

//northwest

if (j != 0 && i != 0) {

if (sta != status[i - 1][j - 1]

&& 1 == Math.abs(status[i - 1][j - 1])) {

int m = i - 1;

int n = j - 1;

while (status[i - 1][j - 1] == status[m][n] && m > 0 && n > 0) {

m--;

n--;

// System.out.println("m=" + m + " n=" + n);

}

if (status[m][n] == sta) {

flag = flag + (i - 1 - m);

}

}

}

//southeast

if (j != 7 && i != 7) {

if (sta != status[i + 1][j + 1]

&& 1 == Math.abs(status[i + 1][j + 1])) {

int m = i + 1;

int n = j + 1;

while (status[i + 1][j + 1] == status[m][n] && m < 7 && n < 7) {

m++;

n++;

// System.out.println("m=" + m + " n=" + n);

}

if (status[m][n] == sta) {

flag = flag + (m - i - 1);

}

}

}

//northeast

if (j != 0 && i != 7) {

if (sta != status[i + 1][j - 1]

&& 1 == Math.abs(status[i + 1][j - 1])) {

int m = i + 1;

int n = j - 1;

while (status[i + 1][j - 1] == status[m][n] && m < 7 && n > 0) {

m++;

n--;

// System.out.println("m=" + m + " n=" + n);

}

if (status[m][n] == sta) {

flag = flag + (j - 1 - n);

}

}

}

//southwest

if (j != 7 && i != 0) {

if (sta != status[i - 1][j + 1]

&& 1 == Math.abs(status[i - 1][j + 1])) {

int m = i - 1;

int n = j + 1;

while (status[i - 1][j + 1] == status[m][n] && m > 0 && n < 7) {

m--;

n++;

// System.out.println("m=" + m + " n=" + n);

}

if (status[m][n] == sta) {

flag = flag + (i - 1 - m);

}

}

}

return flag;

}

public void release() {

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

if (status[i][j] > 1) {

status[i][j] = 0;

}

}

}

}

public boolean jug(int color) {

//Hashtable table=new Hashtable();

int flag = 0;

int isPass = 0;

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

if (status[i][j] == 0) {

flag = check(i, j, color);

{

if (flag != 0) {

status[i][j] = flag + 1;

isPass++;

}

}

} else {

flag = 0;

}

}

}

if (isPass > 0) {

return true;

} else {

return false;

}

}

public boolean blOver(boolean interrupt) {

String msg = "";

whiteCount=0;

blackCount=0;

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

if (status[i][j] == BLACK) {

blackCount++;

}

if (status[i][j] == WHITE) {

whiteCount++;

}

}

}

if ((whiteCount + blackCount) == 64 || blackCount == 0

|| whiteCount == 0 || interrupt) {

if (whiteCount > blackCount) {

msg = "白方胜!白方:黑方=" + whiteCount + ":" + blackCount;

} else {

msg = "黑方胜!黑方:白方" + blackCount + ":" + whiteCount;

}

JOptionPane.showConfirmDialog(this, msg, "Game Over",

JOptionPane.CLOSED_OPTION);

return true;

} else {

return false;

}

}

public boolean blPass() {

if (chessNum >= 64) {

return false;

}

if (!jug(colorStatus)) {

JOptionPane.showMessageDialog(null, "pass");

colorStatus = -colorStatus;

count++;

infoText.append(count + ": --\n");

return true;

} else {

return false;

}

}

public void manulChess(int x, int y, int color) {

if (colorStatus == color) {

release();

if (status[x][y] == 0) {

status[x][y] = colorStatus;

}

change(x, y, colorStatus);

repaint();

count++;

chessNum++;

String value = "" + x + "*" + y;

infoText.append(count + ": " + value + "\n");

colorStatus = -colorStatus;

}

}

public void autoChess(int color) {

if (colorStatus == color) {

int flag = 0;

int x = 0;

int y = 0;

for (int i = 0; i < 8; i++) {

for (int j = 7; j >= 0; j--) {

int temp = check(i, j, colorStatus);

if (temp > flag) {

flag = temp;

x = i;

y = j;

}

}

}

if (flag > 0) {

if (status[x][y] == 0) {

status[x][y] = color;

}

change(x, y, colorStatus);

repaint();

colorStatus = -colorStatus;

count++;

chessNum++;

String value = "" + x + "*" + y;

infoText.append(count + ": " + value + "\n");

} else {

colorStatus = -colorStatus;

}

}

}

public void autoChess(int color, int level) {

if (colorStatus == color && level == 2) {

int flag = -1;

int x = -1;

int y = -1;

for (int i = 0; i < 8; i++) {

for (int j = 7; j >= 0; j--) {

if (status[i][j] == 0 && statusW[i][j] > flag) {

if (check(i, j, colorStatus) > 0) {

flag = statusW[i][j];

x = i;

y = j;

}

}

}

}

if (flag >= 0 && x > -1 && y > -1) {

if (status[x][y] == 0) {

status[x][y] = color;

}

change(x, y, colorStatus);

repaint();

colorStatus = -colorStatus;

count++;

chessNum++;

String value = "" + x + "*" + y;

infoText.append(count + ": " + value + "\n");

} else {

colorStatus = -colorStatus;

}

}

}

class myKeyListener extends KeyAdapter {

public void keyPressed(java.awt.event.KeyEvent e) {

System.out.println(e.getKeyChar());

if (e.getKeyCode() == KeyEvent.VK_F5) {

initChess();

}

if (e.getKeyCode() == KeyEvent.VK_F1) {

level = 1;

initChess();

}

if (e.getKeyCode() == KeyEvent.VK_F2) {

level = 2;

initChess();

}

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有