最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實現(xiàn)單機版五子棋

 更新時間:2020年04月08日 10:18:27   作者:lin14543  
這篇文章主要為大家詳細介紹了java實現(xiàn)單機版五子棋源碼,以及五子棋游戲需要的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下

這個小游戲是我和我姐們兒的JAVA課程設計,也是我做的第一個JAVA項目,適合初學者,希望能幫到那些被JAVA課設所困擾的孩紙們~~~

一、該游戲需要實現(xiàn)

1、設計主框架,界面。

2、利用ActionListener接口實現(xiàn)按鈕事件的監(jiān)聽。

3、重新開始功能的實現(xiàn)。

4、悔棋功能的實現(xiàn)。

5、退出功能的實現(xiàn)。

6、棋盤中棋子點類的定義。

7、利用MouseListener接口實現(xiàn)事件監(jiān)聽,并實現(xiàn)接口里的所有方法。

8、當鼠標移動到棋盤上的交點上,且該點上無棋子時能夠變成小手形狀。

9、點擊棋盤時,利用if語句判斷該點是否點在交點上,并利用foreach語句和棋子類中的getX(),getY()方法遍歷每一個棋子的位置判斷該點是否有棋子。

10、當判斷到可以在所點擊的點上下子時,畫棋子時利用for循環(huán)遍歷已有的每一個點并利用Graphics類的setColor設置顏色,利用Graphics類的fillOval方法設置形狀大小。

11、當畫完棋子時要及時判斷輸贏,用棋子所在索引和for循環(huán)遍歷最后一個棋子的各個方向,如果有在同一條直線上的棋子個數(shù)大于等于五的即當前棋子所代表的那方贏。

12、勝負已定的時候,能夠彈出相應的信息。

二、功能代碼實現(xiàn)

2.1進入游戲

public static void main(String[] args) { 
 StartChessJFrame f=new StartChessJFrame();//創(chuàng)建主框架 
 f.setVisible(true);//顯示主框架 
 } 

2.2初始化,定義一些要用到的量。

private ChessBoard chessBoard;//對戰(zhàn)面板 
 private Panel toolbar;//工具條面板 
 private Button startButton;//設置開始按鈕 
 private Button backButton;//設置悔棋按鈕 
 private Button exitButton;//設置退出按鈕 

2.3界面的構(gòu)造方法(游戲的框架)

public StartChessJFrame(){ 
 setTitle("單機版五子棋");//設置標題 
 chessBoard=new ChessBoard();//初始化面板對象,創(chuàng)建和添加菜單 
 MyItemListener lis=new MyItemListener();//初始化按鈕事件監(jiān)聽器內(nèi)部類 
 toolbar=new Panel();//工具面板欄實例化 
 startButton=new Button("重新開始"); 
 backButton=new Button("悔棋"); 
 exitButton=new Button("退出");//三個按鈕初始化 
 toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));//將工具面板按鈕用FlowLayout布局 
 toolbar.add(backButton); 
 toolbar.add(startButton); 
 toolbar.add(exitButton);//將三個按鈕添加到工具面板上 
 startButton.addActionListener(lis); 
 backButton.addActionListener(lis); 
 exitButton.addActionListener(lis);//將三個按鈕事件注冊監(jiān)聽事件 
 add(toolbar,BorderLayout.SOUTH);//將工具面板布局到界面南方也就是下面 
 add(chessBoard);//將面板對象添加到窗體上 
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置界面關閉事件 
 pack();//自適應大小 
 } 

2.4按鈕的實現(xiàn)與監(jiān)聽(構(gòu)造方法內(nèi)部)

MyItemListener lis=new MyItemListener();//初始化按鈕事件監(jiān)聽器內(nèi)部類 
 toolbar=new Panel();//工具面板欄實例化 
 startButton=new Button("重新開始"); 
 backButton=new Button("悔棋"); 
 exitButton=new Button("退出");//三個按鈕初始化 
 toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));//將工具面板按鈕用FlowLayout布局 
 toolbar.add(backButton); 
 toolbar.add(startButton); 
 toolbar.add(exitButton);//將三個按鈕添加到工具面板上 
 startButton.addActionListener(lis); 
 backButton.addActionListener(lis); 
 exitButton.addActionListener(lis);//將三個按鈕事件注冊監(jiān)聽事件 

2.5按鈕事件的監(jiān)聽

private class MyItemListener implements ActionListener{ 
 public void actionPerformed(ActionEvent e) { 
 Object obj=e.getSource();//獲取事件源 
 if(obj==startButton){ 
 System.out.println("重新開始...");//重新開始 
 //JFiveFrame.this內(nèi)部類引用外部類 
 chessBoard.restartGame(); 
 }else if(obj==exitButton){ 
 System.exit(0);//結(jié)束應用程序 
 }else if(obj==backButton){ 
 System.out.println("悔棋...");//悔棋 
 chessBoard.goback(); 
 } 
 } 
 } 

2.6重新開始按鈕的功能實現(xiàn)

public void restartGame(){//清除棋子 
 for(int i=0;i<chessList.length;i++) 
 chessList[i]=null; 
 /*恢復游戲相關的變量值*/ 
 isBack=true; 
 gameOver=false;//游戲是否結(jié)束 
 chessCount=0;//當前棋盤的棋子個數(shù) 
 repaint(); 
 } 

2.7悔棋按鈕的功能實現(xiàn)

public void goback(){ 
 if(chessCount==0) 
 return ; 
 chessList[chessCount-1]=null; 
 chessCount--; 
 if(chessCount>0){ 
 xIndex=chessList[chessCount-1].getX(); 
 yIndex=chessList[chessCount-1].getY(); 
 } 
 isBack=!isBack; 
 repaint(); 
 } 

2.8當棋盤根據(jù)需要變大或變小時窗口應隨之發(fā)生改變

//Dimension:矩形ChessBoard類內(nèi)部 
 public Dimension getPreferredSize(){ 
 return new Dimension(MARGIN*2+GRID_SPAN*COLS,MARGIN*2+GRID_SPAN*ROWS); 
 } 
pack();//自適應大小StartChessBoard類內(nèi)部 

2.9定義棋子類

import java.awt.*; 
public class Point { 
 private int x;//棋子在棋盤中的x索引值 
 private int y;//棋子在棋盤中的y索引值 
 private Color color;//顏色 
 public static int DIAMETER=30;//直徑 
 public Point(int x,int y,Color color){ 
 this.x=x; 
 this.y=y; 
 this.color=color; 
 } 
 //得到棋子在棋盤中的x索引值 
 public int getX(){ 
 return x; 
 } 
 //得到棋子在棋盤中的y索引值 
 public int getY(){ 
 return y; 
 } 
 //得到棋子顏色 
 public Color getColor(){ 
 return color; 
 } 
} 

三、功能部分代碼實現(xiàn)

3.1初始化,定義一些要用到的量。

public static int MARGIN=30;//邊距 
 public static int GRID_SPAN=35;//網(wǎng)格間距 
 public static int ROWS=18;//棋盤行數(shù) 
 public static int COLS=18;//棋盤列數(shù) 
 Point[] chessList=new Point[(ROWS+1)*(COLS+1)];//初始化每個數(shù)組元素為null 
 boolean isBack=true;//默認開始是黑棋先下 
 boolean gameOver=false;//游戲是否結(jié)束 
 int chessCount;//當前棋盤的棋子個數(shù) 
 int xIndex,yIndex;//當前剛下棋子的索引 

3.2棋盤對象的構(gòu)造方法

public ChessBoard(){ 
 setBackground(Color.LIGHT_GRAY);//設置背景顏色為灰色 
 addMouseListener(this);//添加事件監(jiān)聽器 
 addMouseMotionListener(new MouseMotionListener() {//匿名內(nèi)部類 
 
 @Override 
 public void mouseMoved(MouseEvent e) { 
 int x1=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN; 
 int y1=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//將鼠標單擊的坐標位置轉(zhuǎn)化為網(wǎng)格索引 
 if(x1<0||x1>ROWS||y1<0||y1>COLS||gameOver||findChess(x1,y1)){//游戲已經(jīng)結(jié)束,不能下;落在棋盤外,不能下;x,y位置已經(jīng)有棋子存在,不能下 
 setCursor(new Cursor(Cursor.DEFAULT_CURSOR));//設置成默認形狀 
 }else{ 
 setCursor(new Cursor(Cursor.HAND_CURSOR));//設置成手型 
 } 
 } 
 @Override 
 public void mouseDragged(MouseEvent e) { 
 } 
 }); 
 } 

3.3設置鼠標監(jiān)聽器,變小手(在構(gòu)造方法內(nèi)部)

addMouseMotionListener(new MouseMotionListener() {//匿名內(nèi)部類 
 
 @Override 
 public void mouseMoved(MouseEvent e) { 
 int x1=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN; 
 int y1=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//將鼠標單擊的坐標位置轉(zhuǎn)化為網(wǎng)格索引 
 if(x1<0||x1>ROWS||y1<0||y1>COLS||gameOver||findChess(x1,y1)){//游戲已經(jīng)結(jié)束,不能下;落在棋盤外,不能下;x,y位置已經(jīng)有棋子存在,不能下 
 setCursor(new Cursor(Cursor.DEFAULT_CURSOR));//設置成默認形狀 
 }else{ 
 setCursor(new Cursor(Cursor.HAND_CURSOR));//設置成手型 
 } 
 } 
 @Override 
 public void mouseDragged(MouseEvent e) { 
 } 
 }); 

3.4點擊棋盤時的鼠標按壓事件

public void mousePressed(MouseEvent e) {//鼠標按鍵在組件上按下時調(diào)用 
 if(gameOver)//游戲已經(jīng)結(jié)束,不能下 
 return ; 
 String colorName=isBack ? "黑棋" : "白棋"; 
 xIndex=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN; 
 yIndex=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//將鼠標單擊的坐標位置轉(zhuǎn)化為網(wǎng)格索引 
 if(xIndex<0||xIndex>ROWS||yIndex<0||yIndex>COLS)//棋子落在棋盤外,不能下 
 return ; 
 if(findChess(xIndex,yIndex))//x,y位置已經(jīng)有棋子存在,不能下 
 return ; 
 Point ch=new Point(xIndex,yIndex,isBack ? Color.black : Color.white); 
 chessList[chessCount++]=ch; 
 repaint();//通知系統(tǒng)重新繪制 
 if(isWin()){ 
 String msg=String.format("恭喜,%s贏啦~", colorName); 
 JOptionPane.showMessageDialog(this, msg); 
 gameOver=true; 
 } 
 else if(chessCount==(COLS+1)*(ROWS+1)) 
 { 
 String msg=String.format("棋鼓相當,棒棒噠~"); 
 JOptionPane.showMessageDialog(this,msg); 
 gameOver=true; 
 } 
 isBack=!isBack; 
 } 

3.5繪制棋盤,棋子還有紅框框

public void paintComponent(Graphics g){ 
 super.paintComponent(g);//畫棋盤 
 for(int i=0;i<=ROWS;i++){//畫橫線 
 g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN); 
 } 
 for(int i=0;i<=COLS;i++){//畫直線 
 g.drawLine(MARGIN+i*GRID_SPAN, MARGIN, MARGIN+i*GRID_SPAN,MARGIN+ROWS*GRID_SPAN); 
 } 
 /*畫棋子*/ 
 for(int i=0;i<chessCount;i++){ 
 int xPos=chessList[i].getX()*GRID_SPAN+MARGIN;//網(wǎng)格交叉的x坐標 
 int yPos=chessList[i].getY()*GRID_SPAN+MARGIN;//網(wǎng)格交叉的y坐標 
 g.setColor(chessList[i].getColor());//設置顏色 
 g.fillOval(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER); 
 if(i==chessCount-1){ 
 g.setColor(Color.red);//標記最后一個棋子為紅色 
 g.drawRect(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER); 
 } 
 } 
 } 

3.6判斷輸贏

/*判斷哪方贏*/ 
 private boolean isWin(){ 
 int continueCount=1;//連續(xù)棋子的個數(shù) 
 for(int x=xIndex-1;x>=0;x--){//橫向向左尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(x,yIndex,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 for(int x=xIndex+1;x<=ROWS;x++){//橫向向右尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(x,yIndex,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 if(continueCount>=5){//判斷記錄數(shù)大于等于五,即表示此方獲勝 
 return true; 
 }else 
 continueCount=1; 
 // 
 for(int y=yIndex-1;y>=0;y--){//縱向向上尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(xIndex,y,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 for(int y=yIndex+1;y<=ROWS;y++){//縱向向下尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(xIndex,y,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 if(continueCount>=5){//判斷記錄數(shù)大于等于五,即表示此方獲勝 
 return true; 
 }else 
 continueCount=1; 
 // 
 for(int x=xIndex+1,y=yIndex-1;y>=0&&x<=COLS;x++,y--){//右下尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(x,y,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 for(int x=xIndex-1,y=yIndex+1;y<=ROWS&&x>=0;x--,y++){//左上尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(x,y,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 if(continueCount>=5){//判斷記錄數(shù)大于等于五,即表示此方獲勝 
 return true; 
 }else 
 continueCount=1; 
 // 
 for(int x=xIndex-1,y=yIndex-1;y>=0&&x>=0;x--,y--){//左下尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(x,y,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 for(int x=xIndex+1,y=yIndex+1;y<=ROWS&&x<=COLS;x++,y++){//右上尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(x,y,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 if(continueCount>=5){//判斷記錄數(shù)大于等于五,即表示此方獲勝 
 return true; 
 }else 
 continueCount=1; 
 return false; 
 } 

3.7彈出相應消息框(在鼠標按壓函數(shù)內(nèi)部)

if(isWin()){ 
 String msg=String.format("恭喜,%s贏啦~", colorName); 
 JOptionPane.showMessageDialog(this, msg); 
 gameOver=true; 
 } 
 else if(chessCount==(COLS+1)*(ROWS+1))//平局 
 { 
 String msg=String.format("棋鼓相當,棒棒噠~"); 
 JOptionPane.showMessageDialog(this,msg); 
 gameOver=true; 
 } 

3.8上面用到的一個判斷某點是否有棋子的函數(shù)

private boolean findChess(int x,int y){ 
 for(Point c:chessList){ 
 if(c!=null&&c.getX()==x&&c.getY()==y) 
 return true; 
 } 
 return false; 
 } 

3.9因為該棋盤類實現(xiàn)了鼠標監(jiān)聽接口MonseListener,所以要重寫該接口內(nèi)的所有方法,其它方法如下

@Override 
 public void mouseClicked(MouseEvent e) {//鼠標按鍵在組件上單擊(按下并釋放)時調(diào)用 
 } 
 @Override 
 public void mouseReleased(MouseEvent e) {////鼠標按鍵在組件上釋放時調(diào)用 
 } 
 @Override 
 public void mouseEntered(MouseEvent e) {//鼠標進入組件時調(diào)用 
 } 
 @Override 
 public void mouseExited(MouseEvent e){//鼠標離開組件時調(diào)用 
 } 

四、運行結(jié)果

五、代碼匯總

該游戲總共建了三個類,一個是界面StartChessJFrame,一個是棋盤類ChessBoard,一個是棋子類Point

5.1StartChessJFrame類

package chess.lcc.com; 
import javax.swing.*; 
 
import java.awt.event.*; 
import java.awt.*; 
/* 
 * 五子棋的主框架,程序啟動類 
 */ 
public class StartChessJFrame extends JFrame { 
 private ChessBoard chessBoard;//對戰(zhàn)面板 
 private Panel toolbar;//工具條面板 
 private Button startButton;//設置開始按鈕 
 private Button backButton;//設置悔棋按鈕 
 private Button exitButton;//設置退出按鈕 
 
 
 public StartChessJFrame(){ 
 setTitle("單機版五子棋");//設置標題 
 chessBoard=new ChessBoard();//初始化面板對象,創(chuàng)建和添加菜單 
 MyItemListener lis=new MyItemListener();//初始化按鈕事件監(jiān)聽器內(nèi)部類 
 toolbar=new Panel();//工具面板欄實例化 
 startButton=new Button("重新開始"); 
 backButton=new Button("悔棋"); 
 exitButton=new Button("退出");//三個按鈕初始化 
 toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));//將工具面板按鈕用FlowLayout布局 
 toolbar.add(backButton); 
 toolbar.add(startButton); 
 toolbar.add(exitButton);//將三個按鈕添加到工具面板上 
 startButton.addActionListener(lis); 
 backButton.addActionListener(lis); 
 exitButton.addActionListener(lis);//將三個按鈕事件注冊監(jiān)聽事件 
 add(toolbar,BorderLayout.SOUTH);//將工具面板布局到界面南方也就是下面 
 add(chessBoard);//將面板對象添加到窗體上 
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置界面關閉事件 
 pack();//自適應大小 
 } 
 private class MyItemListener implements ActionListener{ 
 public void actionPerformed(ActionEvent e) { 
 Object obj=e.getSource();//獲取事件源 
 if(obj==startButton){ 
 System.out.println("重新開始...");//重新開始 
 //JFiveFrame.this內(nèi)部類引用外部類 
 chessBoard.restartGame(); 
 }else if(obj==exitButton){ 
 System.exit(0);//結(jié)束應用程序 
 }else if(obj==backButton){ 
 System.out.println("悔棋...");//悔棋 
 chessBoard.goback(); 
 } 
 } 
 } 
 public static void main(String[] args) { 
 StartChessJFrame f=new StartChessJFrame();//創(chuàng)建主框架 
 f.setVisible(true);//顯示主框架 
 } 
} 

5.2ChessBoard類

package chess.lcc.com; 
 
import javax.swing.*; 
 
import java.awt.*; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseMotionListener; 
import java.awt.event.MouseEvent; 
/*五子棋-棋盤類*/ 
public class ChessBoard extends JPanel implements MouseListener{ 
 public static int MARGIN=30;//邊距 
 public static int GRID_SPAN=35;//網(wǎng)格間距 
 public static int ROWS=15;//棋盤行數(shù) 
 public static int COLS=15;//棋盤列數(shù) 
 Point[] chessList=new Point[(ROWS+1)*(COLS+1)];//初始化每個數(shù)組元素為null 
 boolean isBack=true;//默認開始是黑棋先下 
 boolean gameOver=false;//游戲是否結(jié)束 
 int chessCount;//當前棋盤的棋子個數(shù) 
 int xIndex,yIndex;//當前剛下棋子的索引 
 public ChessBoard(){ 
 setBackground(Color.LIGHT_GRAY);//設置背景顏色為黃色 
 addMouseListener(this);//添加事件監(jiān)聽器 
 addMouseMotionListener(new MouseMotionListener() {//匿名內(nèi)部類 
 
 @Override 
 public void mouseMoved(MouseEvent e) { 
 int x1=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN; 
 int y1=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//將鼠標單擊的坐標位置轉(zhuǎn)化為網(wǎng)格索引 
 if(x1<0||x1>ROWS||y1<0||y1>COLS||gameOver||findChess(x1,y1)){//游戲已經(jīng)結(jié)束,不能下;落在棋盤外,不能下;x,y位置已經(jīng)有棋子存在,不能下 
 setCursor(new Cursor(Cursor.DEFAULT_CURSOR));//設置成默認形狀 
 }else{ 
 setCursor(new Cursor(Cursor.HAND_CURSOR));//設置成手型 
 } 
 } 
 
 @Override 
 public void mouseDragged(MouseEvent e) { 
 } 
 }); 
 } 
 /*繪制*/ 
 public void paintComponent(Graphics g){ 
 super.paintComponent(g);//畫棋盤 
 for(int i=0;i<=ROWS;i++){//畫橫線 
 g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN); 
 } 
 for(int i=0;i<=COLS;i++){//畫直線 
 g.drawLine(MARGIN+i*GRID_SPAN, MARGIN, MARGIN+i*GRID_SPAN,MARGIN+ROWS*GRID_SPAN); 
 } 
 /*畫棋子*/ 
 for(int i=0;i<chessCount;i++){ 
 int xPos=chessList[i].getX()*GRID_SPAN+MARGIN;//網(wǎng)格交叉的x坐標 
 int yPos=chessList[i].getY()*GRID_SPAN+MARGIN;//網(wǎng)格交叉的y坐標 
 g.setColor(chessList[i].getColor());//設置顏色 
 g.fillOval(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER); 
 if(i==chessCount-1){ 
 g.setColor(Color.red);//標記最后一個棋子為紅色 
 g.drawRect(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER); 
 } 
 } 
 } 

 
 @Override 
 public void mousePressed(MouseEvent e) {//鼠標按鍵在組件上按下時調(diào)用 
 if(gameOver)//游戲已經(jīng)結(jié)束,不能下 
 return ; 
 String colorName=isBack ? "黑棋" : "白棋"; 
 xIndex=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN; 
 yIndex=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//將鼠標單擊的坐標位置轉(zhuǎn)化為網(wǎng)格索引 
 if(xIndex<0||xIndex>ROWS||yIndex<0||yIndex>COLS)//棋子落在棋盤外,不能下 
 return ; 
 if(findChess(xIndex,yIndex))//x,y位置已經(jīng)有棋子存在,不能下 
 return ; 
 
 Point ch=new Point(xIndex,yIndex,isBack ? Color.black : Color.white); 
 chessList[chessCount++]=ch; 
 repaint();//通知系統(tǒng)重新繪制 
 if(isWin()){ 
 String msg=String.format("恭喜,%s贏啦~", colorName); 
 JOptionPane.showMessageDialog(this, msg); 
 gameOver=true; 
 } 
 else if(chessCount==(COLS+1)*(ROWS+1)) 
 { 
 String msg=String.format("棋鼓相當,棒棒噠~"); 
 JOptionPane.showMessageDialog(this,msg); 
 gameOver=true; 
 } 
 isBack=!isBack; 
 } 
 
 @Override 
 public void mouseClicked(MouseEvent e) {//鼠標按鍵在組件上單擊(按下并釋放)時調(diào)用 
 } 
 
 @Override 
 public void mouseReleased(MouseEvent e) {////鼠標按鍵在組件上釋放時調(diào)用 
 } 
 
 @Override 
 public void mouseEntered(MouseEvent e) {//鼠標進入組件時調(diào)用 
 } 
 
 @Override 
 public void mouseExited(MouseEvent e){//鼠標離開組件時調(diào)用 
 } 
 
 private boolean findChess(int x,int y){ 
 for(Point c:chessList){ 
 if(c!=null&&c.getX()==x&&c.getY()==y) 
 return true; 
 } 
 return false; 
 } 
 
 /*判斷那方贏*/ 
 private boolean isWin(){ 
 int continueCount=1;//連續(xù)棋子的個數(shù) 
 for(int x=xIndex-1;x>=0;x--){//橫向向左尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(x,yIndex,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 for(int x=xIndex+1;x<=ROWS;x++){//橫向向右尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(x,yIndex,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 if(continueCount>=5){//判斷記錄數(shù)大于等于五,即表示此方獲勝 
 return true; 
 }else 
 continueCount=1; 
 // 
 for(int y=yIndex-1;y>=0;y--){//縱向向上尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(xIndex,y,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 for(int y=yIndex+1;y<=ROWS;y++){//縱向向下尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(xIndex,y,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 if(continueCount>=5){//判斷記錄數(shù)大于等于五,即表示此方獲勝 
 return true; 
 }else 
 continueCount=1; 
 // 
 for(int x=xIndex+1,y=yIndex-1;y>=0&&x<=COLS;x++,y--){//右下尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(x,y,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 for(int x=xIndex-1,y=yIndex+1;y<=ROWS&&x>=0;x--,y++){//左上尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(x,y,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 if(continueCount>=5){//判斷記錄數(shù)大于等于五,即表示此方獲勝 
 return true; 
 }else 
 continueCount=1; 
 // 
 for(int x=xIndex-1,y=yIndex-1;y>=0&&x>=0;x--,y--){//左下尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(x,y,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 for(int x=xIndex+1,y=yIndex+1;y<=ROWS&&x<=COLS;x++,y++){//右上尋找 
 Color c=isBack ? Color.black : Color.white; 
 if(getChess(x,y,c)!=null){ 
 continueCount++; 
 }else 
 break; 
 } 
 if(continueCount>=5){//判斷記錄數(shù)大于等于五,即表示此方獲勝 
 return true; 
 }else 
 continueCount=1; 
 return false; 
 } 
 private Point getChess(int xIndex,int yIndex,Color color){ 
 for(Point c:chessList){ 
 if(c!=null&&c.getX()==xIndex&&c.getY()==yIndex&&c.getColor()==color) 
 return c; 
 } 
 return null; 
 } 
 public void restartGame(){//清除棋子 
 for(int i=0;i<chessList.length;i++) 
 chessList[i]=null; 
 /*恢復游戲相關的變量值*/ 
 isBack=true; 
 gameOver=false;//游戲是否結(jié)束 
 chessCount=0;//當前棋盤的棋子個數(shù) 
 repaint(); 
 } 
 public void goback(){ 
 if(chessCount==0) 
 return ; 
 chessList[chessCount-1]=null; 
 chessCount--; 
 if(chessCount>0){ 
 xIndex=chessList[chessCount-1].getX(); 
 yIndex=chessList[chessCount-1].getY(); 
 } 
 isBack=!isBack; 
 repaint(); 
 } 
 //Dimension:矩形 
 public Dimension getPreferredSize(){ 
 return new Dimension(MARGIN*2+GRID_SPAN*COLS,MARGIN*2+GRID_SPAN*ROWS); 
 } 
 
} 

5.3Point類

package chess.lcc.com; 
 
import java.awt.*; 
 
public class Point { 
 private int x;//棋子在棋盤中的x索引值 
 private int y;//棋子在棋盤中的y索引值 
 private Color color;//顏色 
 public static int DIAMETER=30;//直徑 
 public Point(int x,int y,Color color){ 
 this.x=x; 
 this.y=y; 
 this.color=color; 
 } 
 //得到棋子在棋盤中的x索引值 
 public int getX(){ 
 return x; 
 } 
 //得到棋子在棋盤中的y索引值 
 public int getY(){ 
 return y; 
 } 
 //得到棋子顏色 
 public Color getColor(){ 
 return color; 
 } 
} 

更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:

C++經(jīng)典小游戲匯總

python經(jīng)典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經(jīng)典游戲 玩不停

java經(jīng)典小游戲匯總

javascript經(jīng)典小游戲匯總

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 史上最全的IDEA快捷鍵總結(jié)

    史上最全的IDEA快捷鍵總結(jié)

    這篇文章主要介紹了史上最全的IDEA快捷鍵總結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • java?讀寫鎖的使用及它的優(yōu)點

    java?讀寫鎖的使用及它的優(yōu)點

    這篇文章主要介紹了java?讀寫鎖的使用及它的優(yōu)點,讀寫鎖的特點就是是讀讀不互斥、讀寫互斥、寫寫互斥,下面具體使用分享需要的小伙伴可以參考一下
    2022-05-05
  • 使用java的注解(用在java類的方法上的注解)方法

    使用java的注解(用在java類的方法上的注解)方法

    這篇文章主要介紹了使用java的注解(用在java類的方法上的注解)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • Spring Boot Maven插件使用詳解

    Spring Boot Maven插件使用詳解

    這篇文章主要為大家詳細介紹了Spring Boot Maven插件使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 解決JavaMail附件名字過長導致的亂碼問題

    解決JavaMail附件名字過長導致的亂碼問題

    這篇文章主要介紹了解決JavaMail附件名字過長導致的亂碼問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Spring?Mvc中CommonsMultipartFile的特性實例詳解

    Spring?Mvc中CommonsMultipartFile的特性實例詳解

    這篇文章主要給大家介紹了關于Spring?Mvc中CommonsMultipartFile特性的相關資料,SpringMVC擁有強大的靈活性,非侵入性和可配置性,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • RocketMQ事務消息保證消息的可靠性和一致性

    RocketMQ事務消息保證消息的可靠性和一致性

    RocketMQ事務消息是一種能夠保證消息傳遞的可靠性和一致性的消息傳遞模式。它通過引入“半消息”和“事務狀態(tài)”機制,實現(xiàn)了消息發(fā)送和本地事務執(zhí)行的原子性,從而確保了消息的可靠性和一致性
    2023-04-04
  • SpringBoot如何使用feign實現(xiàn)遠程接口調(diào)用和錯誤熔斷

    SpringBoot如何使用feign實現(xiàn)遠程接口調(diào)用和錯誤熔斷

    這篇文章主要介紹了SpringBoot如何使用feign實現(xiàn)遠程接口調(diào)用和錯誤熔斷,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • JavaWeb之會話技術案例詳解

    JavaWeb之會話技術案例詳解

    這篇文章主要介紹了JavaWeb之會話技術案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • springboot之Validation參數(shù)校驗詳細解讀

    springboot之Validation參數(shù)校驗詳細解讀

    這篇文章主要介紹了springboot之Validation參數(shù)校驗詳細解讀,本篇是關于springboot的參數(shù)校驗知識,當然也適用其它java應用,讀完本篇將學會基本的參數(shù)校驗,自定義參數(shù)校驗和分組參數(shù)校驗,需要的朋友可以參考下
    2023-10-10

最新評論

建德市| 东兰县| 堆龙德庆县| 商水县| 大丰市| 乌拉特后旗| 靖西县| 江川县| 建德市| 麻阳| 开原市| 怀化市| 方山县| 英超| 梁平县| 唐河县| 泽普县| 梁山县| 中山市| 饶阳县| 安顺市| 怀来县| 通山县| 阳东县| 沙田区| 开化县| 台山市| 深泽县| 桦川县| 攀枝花市| 永兴县| 镇康县| 马山县| 临沧市| 宁海县| 济南市| 嘉黎县| 清流县| 那曲县| 新源县| 玉门市|