package com.itjob;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import sun.java2d.loops.DrawLine;
/**
* 棋盘类
*/
public class Board extends Canvas implements MouseListener,Runnable,ActionListener
{
private Huiqi hq;
private Huiqi jbhq;
Stack<Huiqi> stack=new Stack<Huiqi>();
//计算栈存的对象数
int h=0;
public JButton jb=new JButton("悔棋");
private boolean hqFlag=false;
private int r0,g0,b0;
private int jop=0;
int count=0;
//定义棋盘中需要控制的点
private int[][] point=new int[10][9];
//定义一个棋子类
private Man man=new Man();
//定义一个提示的类,棋子闪烁
// private Tishi ts=new Tishi();
// private Thread t=new Thread(ts);
//定义当前选中的子
private int currrow;
private int currcol;
//定义是该那个颜色的棋子走 false是黑棋走,true是红棋走
private boolean flag=false;
//定义是否选中子
private boolean selected=false;
public int getJop() {
return jop;
}
public void setJop(int jop) {
this.jop = jop;
}
public Board()
{
//this.setBackground(c);改棋盘的背景
this.addMouseListener(this);
jb.addActionListener(this);
this.initPoint();
new Thread(this).start();
// System.out.println("Board----"+jb);
}
/**
* 每一颗棋子的移动规则
*/
public void guize(int currrow,int currcol,int row,int col)
{
System.out.println("----走子位子是"+"("+row+","+col+")__"+point[row][col]+"\n");
//将下棋的位子保存到栈中
hq.setX(currcol);
hq.setY(currrow);
hq.setX1(col);
hq.setY1(row);
hq.setType1(point[this.currrow][this.currcol]);
hq.setType2(point[row][col]);
this.point[row][col]=this.point[this.currrow][this.currcol];
this.point[this.currrow][this.currcol]=0;
this.selected=false;
}
//初始化棋子
public void initPoint()
{
for(int pr=0;pr<10;pr++){
for(int pc=0;pc<9;pc++)
{
point[pr][pc]=0;
}
}
//黑棋
this.point[0][0]=1;
this.point[0][1]=2;
this.point[0][2]=3;
this.point[0][3]=4;
this.point[0][4]=5;
this.point[0][5]=4;
this.point[0][6]=3;
this.point[0][7]=2;
this.point[0][8]=1;
this.point[2][1]=6;
this.point[2][7]=6;
this.point[3][0]=7;
this.point[3][2]=7;
this.point[3][4]=7;
this.point[3][6]=7;
this.point[3][8]=7;
this.point[6][0]=14;
this.point[6][2]=14;
this.point[6][4]=14;
this.point[6][6]=14;
this.point[6][8]=14;
this.point[7][1]=13;
this.point[7][7]=13;
this.point[9][0]=8;
this.point[9][1]=9;
this.point[9][2]=10;
this.point[9][3]=11;
this.point[9][4]=12;
this.point[9][5]=11;
this.point[9][6]=10;
this.point[9][7]=9;
this.point[9][8]=8;
}
public void run() {
System.out.println("run"+Thread.currentThread().getName());
while(true){
repaint(this.currcol*50+10, this.currrow*50+10, 50, 50);
r0=(int)(Math.random()*255);
g0=(int)(Math.random()*255);
b0=(int)(Math.random()*255);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g)
{
//画10条横线
g.setColor(new Color(0,85,229));
for(int y=30;y<=480;y+=50)
g.drawLine(30,y,430,y);
//画10条竖线
for(int x =30;x<=430;x+=50)
g.drawLine(x,30,x,480);
//==============白炮===============
g.drawLine(65, 125, 75, 125);
g.drawLine(75, 115, 75, 125);
g.drawLine(85, 125, 95, 125);
g.drawLine(85, 115, 85, 125);
g.drawLine(65, 135, 75, 135);
g.drawLine(75, 135, 75, 145);
g.drawLine(85, 135, 95, 135);
g.drawLine(85, 135, 85, 145);
//-----------------------------
g.drawLine(365, 125, 375, 125);
g.drawLine(375, 115, 375, 125);
g.drawLine(385, 125, 395, 125);
g.drawLine(385, 115, 385, 125);
g.drawLine(365, 135, 375, 135);
g.drawLine(375, 135, 375, 145);
g.drawLine(385, 135, 395, 135);
g.drawLine(385, 135, 385, 145);
//===============红炮===============
g.drawLine(65, 125+250, 75, 125+250);
g.drawLine(75, 115+250, 75, 125+250);
g.drawLine(85, 125+250, 95, 125+250);
g.drawLine(85, 115+250, 85, 125+250);
g.drawLine(65, 135+250, 75, 135+250);
g.drawLine(75, 135+250, 75, 145+250);
g.drawLine(85, 135+250, 95, 135+250);
g.drawLine(85, 135+250, 85, 145+250);
//-----------------------------
g.drawLine(365, 125+250, 375, 125+250);
g.drawLine(375, 115+250, 375, 125+250);
g.drawLine(385, 125+250, 395, 125+250);
g.drawLine(385, 115+250, 385, 125+250);
g.drawLine(365, 135+250, 375, 135+250);
g.drawLine(375, 135+250, 375, 145+250);
g.drawLine(385, 135+250, 395, 135+250);
g.drawLine(385, 135+250, 385, 145+250);
//==============白卒子===============
for(int i=50;i<=250;i+=100){
g.drawLine(65+i, 125+50, 75+i, 125+50);
g.drawLine(75+i, 115+50, 75+i, 125+50);
g.drawLine(85+i, 125+50, 95+i, 125+50);
g.drawLine(85+i, 115+50, 85+i, 125+50);
g.drawLine(65+i, 135+50, 75+i, 135+50);
g.drawLine(75+i, 135+50, 75+i, 145+50);
g.drawLine(85+i, 135+50, 95+i, 135+50);
g.drawLine(85+i, 135+50, 85+i, 145+50);
}
//------------------------------
g.drawLine(85-50, 125+50, 95-50, 125+50);
g.drawLine(85-50, 115+50, 85-50, 125+50);
g.drawLine(85-50, 135+50, 95-50, 135+50);
g.drawLine(85-50, 135+50, 85-50, 145+50);
//------------------------------
g.drawLine(65+350, 125+50, 75+350, 125+50);
g.drawLine(75+350, 115+50, 75+350, 125+50);
g.drawLine(65+350, 135+50, 75+350, 135+50);
g.drawLine(75+350, 135+50, 75+350, 145+50);
//==============红卒子===============
for(int i=50;i<=250;i+=100){
g.drawLine(65+i, 125+200, 75+i, 125+200);
g.drawLine(75+i, 115+200, 75+i, 125+200);
g.drawLine(85+i, 125+200, 95+i, 125+200);
g.drawLine(85+i, 115+200, 85+i, 125+200);
g.drawLine(65+i, 135+200, 75+i, 135+200);
g.drawLine(75+i, 135+200, 75+i, 145+200);
g.drawLine(85+i, 135+200, 95+i, 135+200);
g.drawLine(85+i, 135+200, 85+i, 145+200);
}
//------------------------------
g.drawLine(85-50, 125+200, 95-50, 125+200);
g.drawLine(85-50, 115+200, 85-50, 125+200);
g.drawLine(85-50, 135+200, 95-50, 135+200);
g.drawLine(85-50, 135+200, 85-50, 145+200);
//------------------------------
g.drawLine(65+350, 125+200, 75+350, 125+200);
g.drawLine(75+350, 115+200, 75+350, 125+200);
g.drawLine(65+350, 135+200, 75+350, 135+200);
g.drawLine(75+350, 135+200, 75+350, 145+200);
g.drawLine(180, 30,280, 130);
g.drawLine(180, 130, 280, 30);
g.drawLine(180, 380, 280, 480);
g.drawLine(180, 480, 280, 380);
g.setColor(new Color(238,238,238));
g.fillRect(31, 231, 399, 49);
g.setColor(Color.BLACK);
g.setFont(new Font("华文行楷",Font.BOLD,30));
g.drawString("楚 河 汉 界", 80, 262);
//遍历数据结构,画棋子
for(int row=0;row<this.point.length;row++)
for(int col=0;col<this.point[row].length;col++)
{
if(this.point[row][col]==0)
continue;
//设置棋子的类型
man.setType(this.point[row][col]);
//告诉棋子,下标是多少
man.setRow(row);
man.setCol(col);
//调用画棋子的方法
if(this.selected==true)
{
g.setColor(new Color(r0,g0,b0));
g.drawOval(currcol*50+10, currrow*50+10, 40, 40);//表示当前选中的位子
g.drawOval(currcol*50+11, currrow*50+11, 38, 38);//表示当前选中的位子
g.drawOval(currcol*50+12, currrow*50+12, 36, 36);//表示当�