?? five.java
字號(hào):
package five;
import java.awt.*;
import java.awt.event.*;
public class Five extends Frame{
static Five mainfram; //當(dāng)前表單的控制碼
Bowl bowl = new Bowl();
ControlPanel cp = new ControlPanel();
static int cheese = 1;//cheese為棋子的顏色,1:黑,2:白
static int allCheese[][] = new int[13][13]; //用二維陣列來存儲(chǔ)棋盤上已經(jīng)下過的棋子
//初始化為0,然後1代表黑子,2代表白子
//陣列初始化為0(棋盤清空)
static{
for(int i=0;i<13;i++){
for(int j=0;j<13;j++){
allCheese[i][j] = 0;
}
}
}
public Five() {
setLayout(new BorderLayout());
add(bowl,BorderLayout.CENTER);
add(cp,BorderLayout.EAST);
addWindowListener(new FiveWindowLinstener());
setTitle("++WAYCE 五子棋++");
mainfram = this;
}
public static void main(String[] args) {
Five f = new Five();
f.setSize(500,500);
f.show();
}
public class FiveWindowLinstener extends WindowAdapter
{
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
}//end FiveWindowLinstener
}//end Five
/***************************棋盤********************************/
class Bowl extends Panel
{
FivePanel fp[][] = new FivePanel[13][13];//棋盤實(shí)際由169個(gè)儲(chǔ)存格面板所組成
Bowl()
{
setLayout(new GridLayout(13,13));
for(int i=0;i<13;i++){
for(int j=0;j<13;j++){
fp[i][j] = new FivePanel(i,j);
add(fp[i][j]); //按網(wǎng)格類型填加儲(chǔ)存格面板
}
}
}//end Bowl()
class FivePanel extends Panel
{
int row,column;
public FivePanel(int row,int column)
{
this.row = row;
this.column = column;
setBackground(Color.gray);
addMouseListener(new PutCheese());
}//end FivePanel
public void paint(Graphics g){
g.setColor(Color.white);
if(row == 0 && column == 0){//左上角
g.drawLine(getWidth()/2,getHeight()/2,getWidth(),getHeight()/2);
g.drawLine(getWidth()/2,getHeight()/2,getWidth()/2,getHeight());
}else if( row == 0 && column == 12){//右上角
g.drawLine(0,getHeight()/2,getWidth()/2,getHeight()/2);
g.drawLine(getWidth()/2,getHeight()/2,getWidth()/2,getHeight());
}else if(row == 12 && column == 0){//左下角
g.drawLine(getWidth()/2,getHeight()/2,getWidth(),getHeight()/2);
g.drawLine(getWidth()/2,0,getWidth()/2,getHeight()/2);
}else if(row ==12 && column ==12){//左下角
g.drawLine(getWidth()/2,0,getWidth()/2,getHeight()/2);
g.drawLine(0,getHeight()/2,getWidth()/2,getHeight()/2);
}else if(row == 0){
g.drawLine(0,getHeight()/2,getWidth(),getHeight()/2);
g.drawLine(getWidth()/2,getHeight()/2,getWidth()/2,getHeight());
}else if(row == 12){
g.drawLine(0,getHeight()/2,getWidth(),getHeight()/2);
g.drawLine(getWidth()/2,getHeight()/2,getWidth()/2,0);
}else if(column == 0){
g.drawLine(getWidth()/2,0,getWidth()/2,getHeight());
g.drawLine(getWidth()/2,getHeight()/2,getWidth(),getHeight()/2);
}else if(column == 12){
g.drawLine(getWidth()/2,0,getWidth()/2,getHeight());
g.drawLine(getWidth()/2,getHeight()/2,0,getHeight()/2);
}else{
g.drawLine(0,getHeight()/2,getWidth(),getHeight()/2);
g.drawLine(getWidth()/2,0,getWidth()/2,getHeight());
}
if(Five.allCheese[row][column]==1){
g.setColor(Color.orange);
g.fillOval(getWidth()/4,getHeight()/4,getWidth()/2,getHeight()/2);
}else if(Five.allCheese[row][column]==2){
g.setColor(Color.white);
g.fillOval(getWidth()/4,getHeight()/4,getWidth()/2,getHeight()/2);
}
}//end paint
public class PutCheese extends MouseAdapter{
public void mouseClicked(MouseEvent e){
Graphics g = getGraphics();
System.out.println(Five.cheese+"-------------------");
if(Five.allCheese[row][column] != 0) return; //此處已經(jīng)下過棋子了
if (Five.cheese == 1){
g.setColor(Color.orange);
g.fillOval(getWidth()/4,getHeight()/4,getWidth()/2,getHeight()/2);
Five.allCheese[row][column] = 1;
}else if(Five.cheese == 2){
g.setColor(Color.white);
g.fillOval(getWidth()/4,getHeight()/4,getWidth()/2,getHeight()/2);
Five.allCheese[row][column] = 2;
}
if(checkWin(Five.cheese,row,column)==1){
// System.out.println("win");
MessageBox dl = new MessageBox((Frame)Five.mainfram,"五子棋",true," 黃方勝! ");
}else if(checkWin(Five.cheese,row,column)==2){
MessageBox dl = new MessageBox((Frame)Five.mainfram,"五子棋",true," 白方勝! ");
}
if(Five.cheese == 1){
Five.cheese = 2;
}else {
Five.cheese = 1;
}
for(int i=0;i<13;i++){
for(int j=0;j<13;j++){
System.out.print(Five.allCheese[i][j]+" ");
}
System.out.println("");
}
}//end mouseClicked()
}//end PutCheese
//*******計(jì)算輸贏的函數(shù)******************
public int checkWin(int che,int row,int column){
int startRow=0;
int startColumn=0;
int line =0;
//左斜線
System.out.println("棋子為:"+che+",row="+row+",column="+column);
startRow = row; startColumn = column;
while(true){
if((startRow == 0)||(startColumn == 0)) {break;}
else {startRow--; startColumn--;}
}
while(true){
if( (startRow == 12)||(startColumn ==12) ||(line>=5)) break;//遍歷到最後結(jié)束
if( Five.allCheese[startRow][startColumn]==che){
line++;
System.out.print(line);
}else{ line = 0; }
startRow++; startColumn++;
}
System.out.println("左斜線:"+line);
if(line>=5) return che;//五子連線,贏!
else line = 0;
//右斜線
startRow = row; startColumn = column;
while(true){
if((startRow == 0)||(startColumn == 12)) {break;}
else {startRow--; startColumn++;}
}
while(true){
if( (startRow == 12)||(startColumn ==0) ||(line>=5))break;//遍歷到最後結(jié)束
if( Five.allCheese[startRow][startColumn]==che){
line++;
System.out.print(line);
}else{ line = 0; }
startRow++; startColumn--;
}
System.out.println("右斜線:"+line);
if(line>=5) return che;//五子連線,贏!
else line = 0;
//水平線
startRow = row; startColumn = 0;
while(true){
if((startColumn ==12) ||(line>=5)) break;//遍歷到最後結(jié)束
if( Five.allCheese[startRow][startColumn]==che){
line++;
System.out.print(Five.allCheese[startRow][startColumn]);
System.out.println("");
System.out.print(line);
}else{ line = 0; }
startColumn++;
}
System.out.println("水平線:"+line);
if(line>=5) return che;//五子連線,贏!
else line = 0;
//垂直線
startRow = 0; startColumn = column;
while(true){
if((startRow ==12) ||(line>=5))break;//遍歷到最後結(jié)束
if( Five.allCheese[startRow][startColumn]==che){
line++;System.out.print(line);
}else{ line = 0; }
startRow++;
}
System.out.println("垂直線:"+line);
if(line>=5) return che;//五子連線,贏!
else line = 0;
return 0;
}//end checkWin()
class MessageBox extends Dialog
{
Label message;
Button confirm = new Button("確定");
MessageBox(Frame owner,String title,boolean modal,String msg){
super(owner,title,modal);
setLayout(new FlowLayout(FlowLayout.CENTER));
message = new Label(msg);
add(message);
add(confirm);
confirm.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
setSize(130,100);
setResizable(false) ;
setBounds(200,200,130,100);
show();
}
}
}//end FivePanel
}//end Bowl;
/*******************控制臺(tái)****************************/
class ControlPanel extends Panel
{
CheckboxGroup cbg = new CheckboxGroup();
Checkbox cb1 = new Checkbox("黃方先",cbg,true);
Checkbox cb2 = new Checkbox("白方先",cbg,false);
Button b1 = new Button("開始");
Button b2 = new Button("重新開始");
Button b3 = new Button("幫助");
Button b4 = new Button("退出");
ControlPanel()
{
setLayout(new GridLayout(14,1,10,5));
add(cb1);
add(cb2);
add(new Label());
add(new Label());
add(new Label());
add(b1);
add(b2);
add(b3);
add(b4);
setBounds(0,0,200,500);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -