欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

1、設(shè)計主框架,界面。

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設(shè)置顏色,利用Graphics類的fillOval方法設(shè)置形狀大小。

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

12、勝負已定的時候,能夠彈出相應(yīng)的信息。

二、功能代碼實現(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;//設(shè)置開始按鈕 
 private Button backButton;//設(shè)置悔棋按鈕 
 private Button exitButton;//設(shè)置退出按鈕 

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

public StartChessJFrame(){ 
 setTitle("單機版五子棋");//設(shè)置標題 
 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);//設(shè)置界面關(guān)閉事件 
 pack();//自適應(yīng)大小 
 } 

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é)束應(yīng)用程序 
 }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; 
 /*恢復游戲相關(guān)的變量值*/ 
 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ù)需要變大或變小時窗口應(yīng)隨之發(fā)生改變

//Dimension:矩形ChessBoard類內(nèi)部 
 public Dimension getPreferredSize(){ 
 return new Dimension(MARGIN*2+GRID_SPAN*COLS,MARGIN*2+GRID_SPAN*ROWS); 
 } 
pack();//自適應(yīng)大小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);//設(shè)置背景顏色為灰色 
 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));//設(shè)置成默認形狀 
 }else{ 
 setCursor(new Cursor(Cursor.HAND_CURSOR));//設(shè)置成手型 
 } 
 } 
 @Override 
 public void mouseDragged(MouseEvent e) { 
 } 
 }); 
 } 

3.3設(shè)置鼠標監(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));//設(shè)置成默認形狀 
 }else{ 
 setCursor(new Cursor(Cursor.HAND_CURSOR));//設(shè)置成手型 
 } 
 } 
 @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());//設(shè)置顏色 
 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彈出相應(yīng)消息框(在鼠標按壓函數(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;//設(shè)置開始按鈕 
 private Button backButton;//設(shè)置悔棋按鈕 
 private Button exitButton;//設(shè)置退出按鈕 
 
 
 public StartChessJFrame(){ 
 setTitle("單機版五子棋");//設(shè)置標題 
 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);//設(shè)置界面關(guān)閉事件 
 pack();//自適應(yīng)大小 
 } 
 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é)束應(yīng)用程序 
 }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);//設(shè)置背景顏色為黃色 
 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));//設(shè)置成默認形狀 
 }else{ 
 setCursor(new Cursor(Cursor.HAND_CURSOR));//設(shè)置成手型 
 } 
 } 
 
 @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());//設(shè)置顏色 
 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; 
 /*恢復游戲相關(guān)的變量值*/ 
 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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring事務(wù)失效場景的詳細整理

    Spring事務(wù)失效場景的詳細整理

    Spring 事務(wù)的傳播特性說的是,當多個事務(wù)同時存在的時候,Spring 如何處理這些事務(wù)的特性,下面這篇文章主要給大家介紹了關(guān)于Spring事務(wù)失效場景的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • java web在高并發(fā)和分布式下實現(xiàn)訂單號生成唯一的解決方案

    java web在高并發(fā)和分布式下實現(xiàn)訂單號生成唯一的解決方案

    這篇文章主要介紹了java web在高并發(fā)和分布式下實現(xiàn)訂單號生成唯一的解決方案,需要的朋友可以參考下
    2017-11-11
  • Java用POI導入導出Excel實例分析

    Java用POI導入導出Excel實例分析

    在本篇文章里小編給大家整理的是一篇關(guān)于Java用POI導入導出Excel實例分析內(nèi)容,有需要的朋友們可以跟著學習下。
    2021-11-11
  • eclipse安裝spring ide的步驟詳解

    eclipse安裝spring ide的步驟詳解

    這篇文章主要介紹了eclipse安裝spring ide的步驟詳解,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • 完美解決SpringCloud-OpenFeign使用okhttp替換不生效問題

    完美解決SpringCloud-OpenFeign使用okhttp替換不生效問題

    這篇文章主要介紹了完美解決SpringCloud-OpenFeign使用okhttp替換不生效問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • SpringBoot詳細講解視圖整合引擎thymeleaf

    SpringBoot詳細講解視圖整合引擎thymeleaf

    這篇文章主要分享了Spring Boot整合使用Thymeleaf,Thymeleaf是新一代的Java模板引擎,類似于Velocity、FreeMarker等傳統(tǒng)引擎,關(guān)于其更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下
    2022-06-06
  • java數(shù)字和中文算數(shù)驗證碼的實現(xiàn)

    java數(shù)字和中文算數(shù)驗證碼的實現(xiàn)

    這篇文章主要介紹了java數(shù)字和中文算數(shù)驗證碼的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • springboot整合ehcache 實現(xiàn)支付超時限制的方法

    springboot整合ehcache 實現(xiàn)支付超時限制的方法

    在線支付系統(tǒng)需要極高的穩(wěn)定性,在有限的系統(tǒng)資源下,穩(wěn)定性優(yōu)先級要高于系統(tǒng)并發(fā)以及用戶體驗,因此需要合理的控制用戶的支付請求。下面通過本文給大家介紹springboot整合ehcache 實現(xiàn)支付超時限制的方法,一起看看吧
    2018-01-01
  • Java?的訪問修飾符public,protected,private(封裝、繼承)

    Java?的訪問修飾符public,protected,private(封裝、繼承)

    這篇文章主要介紹了Java?的訪問修飾符public,protected,private(封裝、繼承),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Spring Boot中自動化配置的利弊以及解決方法

    Spring Boot中自動化配置的利弊以及解決方法

    這篇文章主要給大家介紹了關(guān)于Spring Boot中自動化配置的利弊以及解決方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起看看吧。
    2017-08-08

最新評論