就給你一個吧,只不過沒有添加棋子,你自己搞吧。import java.applet.*;
創(chuàng)新互聯(lián)專注于惠濟企業(yè)網(wǎng)站建設,響應式網(wǎng)站,商城網(wǎng)站建設。惠濟網(wǎng)站建設公司,為惠濟等地區(qū)提供建站服務。全流程按需求定制開發(fā),專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務
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+=20)
{g.drawLine(40,i,400,i);
}
g.drawLine(40,400,400,400);
for(int j=40;j=380;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)/2,b=(y+10)/2;
if(x/202||y/202||x/2019||y/2019)
{}
else
{
if(棋子顏色==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)
{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()
{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);
}
});
pack();setSize(600,550);
}
public static void main (String args[])
{Chess chess=new Chess();
}
}
import java.awt.*;import java.awt.event.*;
//創(chuàng)建棋盤的類:
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/202||y/202||x/2019||y/2019) //棋盤以外不下棋子。
{}
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);
}
}
//負責創(chuàng)建黑色棋子的類:
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); //當用左鍵雙擊該棋子時,吃掉該棋子。
}
}
//負責創(chuàng)建白色棋子的類:
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();
}
}
這種題自己寫吧,題里狀態(tài)定義個變量,然后判斷鄰點這些都是方法,然后題里面if要怎么用都給你說清楚了 題了有如果的地方就是一個if語句然后跟著題里的如果把條件寫完了 結(jié)果也出來了
圍棋有幾種變化是一個老問題了,比較粗淺的說法是3的19乘19次方,意思就是棋盤上每個點有空、黑、白三種狀態(tài),總共有19*19個點,所以得出這個結(jié)果。但實際上并沒有那么多,因為在那么多狀態(tài)中,有很大一部分是不可能出現(xiàn)的狀態(tài),也就是盤面上有死棋的狀態(tài)。比如整個棋盤上布滿棋子的狀態(tài)都是不可能的,而這種狀態(tài)就有2的19*19次方之多。?xml:namespace?prefix?=?o?ns?=?"urn:schemas-microsoft-com:office:office"?/
密碼:exn4
按圍棋規(guī)則編寫算法計算每顆棋子的氣,可以根據(jù)棋盤經(jīng)緯度及周邊棋子的個數(shù)來判斷;
按圍棋規(guī)則連成片的棋子共享所有的氣,將連成片的棋子定義為一個棋子組的對象,以該片棋子經(jīng)緯度最小的那個棋子做為該對象的唯一標記,在每顆棋子落子的時候,就會發(fā)生棋子組屬性變化,包括棋子組的合并,棋子組氣數(shù)的變化等各類情況;
為了方便發(fā)現(xiàn)每次落子會影響到的棋子組,可以將每個棋子組的氣點經(jīng)緯度做為索引,該索引也需要隨著棋子組的變化而變化;
按圍棋規(guī)則氣數(shù)等于0的棋子組為死棋,當每顆棋子落子后,更新所有棋子組的信息后,遍歷將所有氣數(shù)為0的棋子組從棋盤移除??紤]到“打劫”規(guī)則,一方落子后,先判斷對方死棋,再判斷己方死棋,符合“打劫”情況還需要額外記錄次數(shù),防止棋盤陷入僵局;