當前位置:編程學習大全網 - 編程語言 - 求java圍棋提子算法

求java圍棋提子算法

import java.awt.*;import java.awt.event.*;

//創建棋盤的類:

class ChessPad extends Panel implements MouseListener,ActionListener

{ int x=-1,y=-1, 棋子顏色=1; //控制棋子顏色的成員變量。

Button button=new Button("重新開局"); //控制重新開局的按扭。

TextField text_1=new TextField("請黑棋下子"),

text_2=new TextField(); //提示下棋的兩個文本框。

ChessPad()

{setSize(440,440);

setLayout(null);setBackground(Color.pink);

addMouseListener(this);add(button);button.setBounds(10,5,60,26);

button.addActionListener(this);

add(text_1);text_1.setBounds(90,5,90,24);

add(text_2);text_2.setBounds(290,5,90,24);

text_1.setEditable(false);text_2.setEditable(false);

}

public void paint(Graphics g) //繪制圍棋棋盤的外觀。

{for(int i=40;i<=380;i=i+20)

{g.drawLine(40,i,400,i);}

g.drawLine(40,400,400,400);

for(int j=40;j<=380;j=j+20)

{g.drawLine(j,40,j,400);}

g.drawLine(400,40,400,400);

g.fillOval(97,97,6,6); g.fillOval(337,97,6,6);

g.fillOval(97,337,6,6);g.fillOval(337,337,6,6);

g.fillOval(217,217,6,6);

}

public void mousePressed(MouseEvent e) //當按下鼠標左鍵時下棋子。

{ if(e.getModifiers()==InputEvent.BUTTON1_MASK)

{ x=(int)e.getX();y=(int)e.getY(); //獲取按下鼠標時的坐標位置。

ChessPoint_black chesspoint_black=new ChessPoint_black(this);

ChessPoint_white chesspoint_white=new ChessPoint_white(this);

int a=(x+10)/20,b=(y+10)/20;

if(x/20<2||y/20<2||x/20>19||y/20>19) //棋盤以外不下棋子。

{}

else{ if(棋子顏色==1) //當棋子顏色是1時下黑棋子。

{this.add(chesspoint_black);

chesspoint_black.setBounds(a*20-7,b*20-7,16,16);

棋子顏色=棋子顏色*(-1);

text_2.setText("請白棋下子");

text_1.setText("");

}

else if(棋子顏色==-1) //當棋子顏色是-1時下白棋子。

{this.add(chesspoint_white);

chesspoint_white.setBounds(a*20-7,b*20-7,16,16);

棋子顏色=棋子顏色*(-1);

text_1.setText("請黑棋下子");

text_2.setText("");

}

}

}

}

public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e){}

public void actionPerformed(ActionEvent e)

{this.removeAll();棋子顏色=1;

add(button);button.setBounds(10,5,60,26);

add(text_1);text_1.setBounds(90,5,90,24);

text_2.setText("");text_1.setText("請黑棋下子");

add(text_2);text_2.setBounds(290,5,90,24);

}

}

//負責創建黑色棋子的類:

class ChessPoint_black extends Canvas implements MouseListener

{ ChessPad chesspad=null; //棋子所在的棋盤。

ChessPoint_black(ChessPad p)

{setSize(20,20);chesspad=p; addMouseListener(this);

}

public void paint(Graphics g) //繪制棋子的大小。

{ g.setColor(Color.black);g.fillOval(0,0,14,14);

}

public void mousePressed(MouseEvent e)

{ if(e.getModifiers()==InputEvent.BUTTON3_MASK)

{chesspad.remove(this);//當用鼠標右鍵點擊棋子時,從棋盤中去掉該棋子(悔棋)。

chesspad.棋子顏色=1;

chesspad.text_2.setText("");chesspad.text_1.setText("請黑棋下子");

}

}

public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e)

{ if(e.getClickCount()>=2)

chesspad.remove(this); //當用左鍵雙擊該棋子時,吃掉該棋子。

}

}

//負責創建白色棋子的類:

class ChessPoint_white extends Canvas implements MouseListener

{

ChessPad chesspad=null;

ChessPoint_white(ChessPad p)

{setSize(20,20);addMouseListener(this);

chesspad=p;

}

public void paint(Graphics g)

{ g.setColor(Color.white);g.fillOval(0,0,14,14);

}

public void mousePressed(MouseEvent e)

{ if(e.getModifiers()==InputEvent.BUTTON3_MASK)

{chesspad.remove(this);chesspad.棋子顏色=-1;

chesspad.text_2.setText("請白棋下子"); chesspad.text_1.setText("");

}

}

public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e)

{if(e.getClickCount()>=2)

chesspad.remove(this);

}

}

public class Chess extends Frame //添加棋盤的窗口。

{ChessPad chesspad=new ChessPad();

Chess()

{setSize(600,600);

setVisible(true);

setLayout(null);

Label label=

new Label("單擊下棋子,雙擊吃棋子,右擊棋子悔棋",Label.CENTER);

add(label);label.setBounds(70,55,440,26);

label.setBackground(Color.orange);

add(chesspad);chesspad.setBounds(70,90,440,440);

addWindowListener(new WindowAdapter()

{public void windowClosing(WindowEvent e){System.exit(0);}

});

}

public static void main(String args[])

{ Chess chess=new Chess();

}

}

  • 上一篇:JavaScript的WB括號裏的值是什麽意思?
  • 下一篇:程序員拿到阿裏工作想放棄:互聯網的壓力令人害怕,想去國企銀行
  • copyright 2024編程學習大全網