java實(shí)現(xiàn)單機(jī)版五子棋
這個(gè)小游戲是我和我姐們兒的JAVA課程設(shè)計(jì),也是我做的第一個(gè)JAVA項(xiàng)目,適合初學(xué)者,希望能幫到那些被JAVA課設(shè)所困擾的孩紙們~~~
一、該游戲需要實(shí)現(xiàn)
1、設(shè)計(jì)主框架,界面。
2、利用ActionListener接口實(shí)現(xiàn)按鈕事件的監(jiān)聽(tīng)。
3、重新開(kāi)始功能的實(shí)現(xiàn)。
4、悔棋功能的實(shí)現(xiàn)。
5、退出功能的實(shí)現(xiàn)。
6、棋盤(pán)中棋子點(diǎn)類(lèi)的定義。
7、利用MouseListener接口實(shí)現(xiàn)事件監(jiān)聽(tīng),并實(shí)現(xiàn)接口里的所有方法。
8、當(dāng)鼠標(biāo)移動(dòng)到棋盤(pán)上的交點(diǎn)上,且該點(diǎn)上無(wú)棋子時(shí)能夠變成小手形狀。
9、點(diǎn)擊棋盤(pán)時(shí),利用if語(yǔ)句判斷該點(diǎn)是否點(diǎn)在交點(diǎn)上,并利用foreach語(yǔ)句和棋子類(lèi)中的getX(),getY()方法遍歷每一個(gè)棋子的位置判斷該點(diǎn)是否有棋子。
10、當(dāng)判斷到可以在所點(diǎn)擊的點(diǎn)上下子時(shí),畫(huà)棋子時(shí)利用for循環(huán)遍歷已有的每一個(gè)點(diǎn)并利用Graphics類(lèi)的setColor設(shè)置顏色,利用Graphics類(lèi)的fillOval方法設(shè)置形狀大小。
11、當(dāng)畫(huà)完棋子時(shí)要及時(shí)判斷輸贏,用棋子所在索引和for循環(huán)遍歷最后一個(gè)棋子的各個(gè)方向,如果有在同一條直線上的棋子個(gè)數(shù)大于等于五的即當(dāng)前棋子所代表的那方贏。
12、勝負(fù)已定的時(shí)候,能夠彈出相應(yīng)的信息。
二、功能代碼實(shí)現(xiàn)
2.1進(jìn)入游戲
public static void main(String[] args) {
StartChessJFrame f=new StartChessJFrame();//創(chuàng)建主框架
f.setVisible(true);//顯示主框架
}
2.2初始化,定義一些要用到的量。
private ChessBoard chessBoard;//對(duì)戰(zhàn)面板 private Panel toolbar;//工具條面板 private Button startButton;//設(shè)置開(kāi)始按鈕 private Button backButton;//設(shè)置悔棋按鈕 private Button exitButton;//設(shè)置退出按鈕
2.3界面的構(gòu)造方法(游戲的框架)
public StartChessJFrame(){
setTitle("單機(jī)版五子棋");//設(shè)置標(biāo)題
chessBoard=new ChessBoard();//初始化面板對(duì)象,創(chuàng)建和添加菜單
MyItemListener lis=new MyItemListener();//初始化按鈕事件監(jiān)聽(tīng)器內(nèi)部類(lèi)
toolbar=new Panel();//工具面板欄實(shí)例化
startButton=new Button("重新開(kāi)始");
backButton=new Button("悔棋");
exitButton=new Button("退出");//三個(gè)按鈕初始化
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));//將工具面板按鈕用FlowLayout布局
toolbar.add(backButton);
toolbar.add(startButton);
toolbar.add(exitButton);//將三個(gè)按鈕添加到工具面板上
startButton.addActionListener(lis);
backButton.addActionListener(lis);
exitButton.addActionListener(lis);//將三個(gè)按鈕事件注冊(cè)監(jiān)聽(tīng)事件
add(toolbar,BorderLayout.SOUTH);//將工具面板布局到界面南方也就是下面
add(chessBoard);//將面板對(duì)象添加到窗體上
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設(shè)置界面關(guān)閉事件
pack();//自適應(yīng)大小
}
2.4按鈕的實(shí)現(xiàn)與監(jiān)聽(tīng)(構(gòu)造方法內(nèi)部)
MyItemListener lis=new MyItemListener();//初始化按鈕事件監(jiān)聽(tīng)器內(nèi)部類(lèi)
toolbar=new Panel();//工具面板欄實(shí)例化
startButton=new Button("重新開(kāi)始");
backButton=new Button("悔棋");
exitButton=new Button("退出");//三個(gè)按鈕初始化
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));//將工具面板按鈕用FlowLayout布局
toolbar.add(backButton);
toolbar.add(startButton);
toolbar.add(exitButton);//將三個(gè)按鈕添加到工具面板上
startButton.addActionListener(lis);
backButton.addActionListener(lis);
exitButton.addActionListener(lis);//將三個(gè)按鈕事件注冊(cè)監(jiān)聽(tīng)事件
2.5按鈕事件的監(jiān)聽(tīng)
private class MyItemListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
Object obj=e.getSource();//獲取事件源
if(obj==startButton){
System.out.println("重新開(kāi)始...");//重新開(kāi)始
//JFiveFrame.this內(nèi)部類(lèi)引用外部類(lèi)
chessBoard.restartGame();
}else if(obj==exitButton){
System.exit(0);//結(jié)束應(yīng)用程序
}else if(obj==backButton){
System.out.println("悔棋...");//悔棋
chessBoard.goback();
}
}
}
2.6重新開(kāi)始按鈕的功能實(shí)現(xiàn)
public void restartGame(){//清除棋子
for(int i=0;i<chessList.length;i++)
chessList[i]=null;
/*恢復(fù)游戲相關(guān)的變量值*/
isBack=true;
gameOver=false;//游戲是否結(jié)束
chessCount=0;//當(dāng)前棋盤(pán)的棋子個(gè)數(shù)
repaint();
}
2.7悔棋按鈕的功能實(shí)現(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當(dāng)棋盤(pán)根據(jù)需要變大或變小時(shí)窗口應(yīng)隨之發(fā)生改變
//Dimension:矩形ChessBoard類(lèi)內(nèi)部
public Dimension getPreferredSize(){
return new Dimension(MARGIN*2+GRID_SPAN*COLS,MARGIN*2+GRID_SPAN*ROWS);
}
pack();//自適應(yīng)大小StartChessBoard類(lèi)內(nèi)部
2.9定義棋子類(lèi)
import java.awt.*;
public class Point {
private int x;//棋子在棋盤(pán)中的x索引值
private int y;//棋子在棋盤(pán)中的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;
}
//得到棋子在棋盤(pán)中的x索引值
public int getX(){
return x;
}
//得到棋子在棋盤(pán)中的y索引值
public int getY(){
return y;
}
//得到棋子顏色
public Color getColor(){
return color;
}
}
三、功能部分代碼實(shí)現(xiàn)
3.1初始化,定義一些要用到的量。
public static int MARGIN=30;//邊距 public static int GRID_SPAN=35;//網(wǎng)格間距 public static int ROWS=18;//棋盤(pán)行數(shù) public static int COLS=18;//棋盤(pán)列數(shù) Point[] chessList=new Point[(ROWS+1)*(COLS+1)];//初始化每個(gè)數(shù)組元素為null boolean isBack=true;//默認(rèn)開(kāi)始是黑棋先下 boolean gameOver=false;//游戲是否結(jié)束 int chessCount;//當(dāng)前棋盤(pán)的棋子個(gè)數(shù) int xIndex,yIndex;//當(dāng)前剛下棋子的索引
3.2棋盤(pán)對(duì)象的構(gòu)造方法
public ChessBoard(){
setBackground(Color.LIGHT_GRAY);//設(shè)置背景顏色為灰色
addMouseListener(this);//添加事件監(jiān)聽(tīng)器
addMouseMotionListener(new MouseMotionListener() {//匿名內(nèi)部類(lè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;//將鼠標(biāo)單擊的坐標(biāo)位置轉(zhuǎn)化為網(wǎng)格索引
if(x1<0||x1>ROWS||y1<0||y1>COLS||gameOver||findChess(x1,y1)){//游戲已經(jīng)結(jié)束,不能下;落在棋盤(pán)外,不能下;x,y位置已經(jīng)有棋子存在,不能下
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));//設(shè)置成默認(rèn)形狀
}else{
setCursor(new Cursor(Cursor.HAND_CURSOR));//設(shè)置成手型
}
}
@Override
public void mouseDragged(MouseEvent e) {
}
});
}
3.3設(shè)置鼠標(biāo)監(jiān)聽(tīng)器,變小手(在構(gòu)造方法內(nèi)部)
addMouseMotionListener(new MouseMotionListener() {//匿名內(nèi)部類(lè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;//將鼠標(biāo)單擊的坐標(biāo)位置轉(zhuǎn)化為網(wǎng)格索引
if(x1<0||x1>ROWS||y1<0||y1>COLS||gameOver||findChess(x1,y1)){//游戲已經(jīng)結(jié)束,不能下;落在棋盤(pán)外,不能下;x,y位置已經(jīng)有棋子存在,不能下
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));//設(shè)置成默認(rèn)形狀
}else{
setCursor(new Cursor(Cursor.HAND_CURSOR));//設(shè)置成手型
}
}
@Override
public void mouseDragged(MouseEvent e) {
}
});
3.4點(diǎn)擊棋盤(pán)時(shí)的鼠標(biāo)按壓事件
public void mousePressed(MouseEvent e) {//鼠標(biāo)按鍵在組件上按下時(shí)調(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;//將鼠標(biāo)單擊的坐標(biāo)位置轉(zhuǎn)化為網(wǎng)格索引
if(xIndex<0||xIndex>ROWS||yIndex<0||yIndex>COLS)//棋子落在棋盤(pán)外,不能下
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("棋鼓相當(dāng),棒棒噠~");
JOptionPane.showMessageDialog(this,msg);
gameOver=true;
}
isBack=!isBack;
}
3.5繪制棋盤(pán),棋子還有紅框框
public void paintComponent(Graphics g){
super.paintComponent(g);//畫(huà)棋盤(pán)
for(int i=0;i<=ROWS;i++){//畫(huà)橫線
g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN);
}
for(int i=0;i<=COLS;i++){//畫(huà)直線
g.drawLine(MARGIN+i*GRID_SPAN, MARGIN, MARGIN+i*GRID_SPAN,MARGIN+ROWS*GRID_SPAN);
}
/*畫(huà)棋子*/
for(int i=0;i<chessCount;i++){
int xPos=chessList[i].getX()*GRID_SPAN+MARGIN;//網(wǎng)格交叉的x坐標(biāo)
int yPos=chessList[i].getY()*GRID_SPAN+MARGIN;//網(wǎng)格交叉的y坐標(biāo)
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);//標(biāo)記最后一個(gè)棋子為紅色
g.drawRect(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER);
}
}
}
3.6判斷輸贏
/*判斷哪方贏*/
private boolean isWin(){
int continueCount=1;//連續(xù)棋子的個(gè)數(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)消息框(在鼠標(biāo)按壓函數(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("棋鼓相當(dāng),棒棒噠~");
JOptionPane.showMessageDialog(this,msg);
gameOver=true;
}
3.8上面用到的一個(gè)判斷某點(diǎn)是否有棋子的函數(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因?yàn)樵撈灞P(pán)類(lèi)實(shí)現(xiàn)了鼠標(biāo)監(jiān)聽(tīng)接口MonseListener,所以要重寫(xiě)該接口內(nèi)的所有方法,其它方法如下
@Override
public void mouseClicked(MouseEvent e) {//鼠標(biāo)按鍵在組件上單擊(按下并釋放)時(shí)調(diào)用
}
@Override
public void mouseReleased(MouseEvent e) {////鼠標(biāo)按鍵在組件上釋放時(shí)調(diào)用
}
@Override
public void mouseEntered(MouseEvent e) {//鼠標(biāo)進(jìn)入組件時(shí)調(diào)用
}
@Override
public void mouseExited(MouseEvent e){//鼠標(biāo)離開(kāi)組件時(shí)調(diào)用
}
四、運(yùn)行結(jié)果


五、代碼匯總
該游戲總共建了三個(gè)類(lèi),一個(gè)是界面StartChessJFrame,一個(gè)是棋盤(pán)類(lèi)ChessBoard,一個(gè)是棋子類(lèi)Point
5.1StartChessJFrame類(lèi)
package chess.lcc.com;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/*
* 五子棋的主框架,程序啟動(dòng)類(lèi)
*/
public class StartChessJFrame extends JFrame {
private ChessBoard chessBoard;//對(duì)戰(zhàn)面板
private Panel toolbar;//工具條面板
private Button startButton;//設(shè)置開(kāi)始按鈕
private Button backButton;//設(shè)置悔棋按鈕
private Button exitButton;//設(shè)置退出按鈕
public StartChessJFrame(){
setTitle("單機(jī)版五子棋");//設(shè)置標(biāo)題
chessBoard=new ChessBoard();//初始化面板對(duì)象,創(chuàng)建和添加菜單
MyItemListener lis=new MyItemListener();//初始化按鈕事件監(jiān)聽(tīng)器內(nèi)部類(lèi)
toolbar=new Panel();//工具面板欄實(shí)例化
startButton=new Button("重新開(kāi)始");
backButton=new Button("悔棋");
exitButton=new Button("退出");//三個(gè)按鈕初始化
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));//將工具面板按鈕用FlowLayout布局
toolbar.add(backButton);
toolbar.add(startButton);
toolbar.add(exitButton);//將三個(gè)按鈕添加到工具面板上
startButton.addActionListener(lis);
backButton.addActionListener(lis);
exitButton.addActionListener(lis);//將三個(gè)按鈕事件注冊(cè)監(jiān)聽(tīng)事件
add(toolbar,BorderLayout.SOUTH);//將工具面板布局到界面南方也就是下面
add(chessBoard);//將面板對(duì)象添加到窗體上
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("重新開(kāi)始...");//重新開(kāi)始
//JFiveFrame.this內(nèi)部類(lèi)引用外部類(lè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類(lèi)
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;
/*五子棋-棋盤(pán)類(lèi)*/
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;//棋盤(pán)行數(shù)
public static int COLS=15;//棋盤(pán)列數(shù)
Point[] chessList=new Point[(ROWS+1)*(COLS+1)];//初始化每個(gè)數(shù)組元素為null
boolean isBack=true;//默認(rèn)開(kāi)始是黑棋先下
boolean gameOver=false;//游戲是否結(jié)束
int chessCount;//當(dāng)前棋盤(pán)的棋子個(gè)數(shù)
int xIndex,yIndex;//當(dāng)前剛下棋子的索引
public ChessBoard(){
setBackground(Color.LIGHT_GRAY);//設(shè)置背景顏色為黃色
addMouseListener(this);//添加事件監(jiān)聽(tīng)器
addMouseMotionListener(new MouseMotionListener() {//匿名內(nèi)部類(lè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;//將鼠標(biāo)單擊的坐標(biāo)位置轉(zhuǎn)化為網(wǎng)格索引
if(x1<0||x1>ROWS||y1<0||y1>COLS||gameOver||findChess(x1,y1)){//游戲已經(jīng)結(jié)束,不能下;落在棋盤(pán)外,不能下;x,y位置已經(jīng)有棋子存在,不能下
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));//設(shè)置成默認(rèn)形狀
}else{
setCursor(new Cursor(Cursor.HAND_CURSOR));//設(shè)置成手型
}
}
@Override
public void mouseDragged(MouseEvent e) {
}
});
}
/*繪制*/
public void paintComponent(Graphics g){
super.paintComponent(g);//畫(huà)棋盤(pán)
for(int i=0;i<=ROWS;i++){//畫(huà)橫線
g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN);
}
for(int i=0;i<=COLS;i++){//畫(huà)直線
g.drawLine(MARGIN+i*GRID_SPAN, MARGIN, MARGIN+i*GRID_SPAN,MARGIN+ROWS*GRID_SPAN);
}
/*畫(huà)棋子*/
for(int i=0;i<chessCount;i++){
int xPos=chessList[i].getX()*GRID_SPAN+MARGIN;//網(wǎng)格交叉的x坐標(biāo)
int yPos=chessList[i].getY()*GRID_SPAN+MARGIN;//網(wǎng)格交叉的y坐標(biāo)
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);//標(biāo)記最后一個(gè)棋子為紅色
g.drawRect(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER);
}
}
}
@Override
public void mousePressed(MouseEvent e) {//鼠標(biāo)按鍵在組件上按下時(shí)調(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;//將鼠標(biāo)單擊的坐標(biāo)位置轉(zhuǎn)化為網(wǎng)格索引
if(xIndex<0||xIndex>ROWS||yIndex<0||yIndex>COLS)//棋子落在棋盤(pán)外,不能下
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("棋鼓相當(dāng),棒棒噠~");
JOptionPane.showMessageDialog(this,msg);
gameOver=true;
}
isBack=!isBack;
}
@Override
public void mouseClicked(MouseEvent e) {//鼠標(biāo)按鍵在組件上單擊(按下并釋放)時(shí)調(diào)用
}
@Override
public void mouseReleased(MouseEvent e) {////鼠標(biāo)按鍵在組件上釋放時(shí)調(diào)用
}
@Override
public void mouseEntered(MouseEvent e) {//鼠標(biāo)進(jìn)入組件時(shí)調(diào)用
}
@Override
public void mouseExited(MouseEvent e){//鼠標(biāo)離開(kāi)組件時(shí)調(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ù)棋子的個(gè)數(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;
/*恢復(fù)游戲相關(guān)的變量值*/
isBack=true;
gameOver=false;//游戲是否結(jié)束
chessCount=0;//當(dāng)前棋盤(pán)的棋子個(gè)數(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類(lèi)
package chess.lcc.com;
import java.awt.*;
public class Point {
private int x;//棋子在棋盤(pán)中的x索引值
private int y;//棋子在棋盤(pán)中的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;
}
//得到棋子在棋盤(pán)中的x索引值
public int getX(){
return x;
}
//得到棋子在棋盤(pán)中的y索引值
public int getY(){
return y;
}
//得到棋子顏色
public Color getColor(){
return color;
}
}
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java實(shí)現(xiàn)五子棋小游戲
- java基于swing實(shí)現(xiàn)的五子棋游戲代碼
- Java實(shí)現(xiàn)五子棋網(wǎng)絡(luò)版
- Java實(shí)現(xiàn)兩人五子棋游戲(二) 畫(huà)出棋盤(pán)
- java實(shí)現(xiàn)單人版五子棋游戲
- Java實(shí)現(xiàn)五子棋游戲的完整代碼
- Java實(shí)現(xiàn)五子棋(附詳細(xì)源碼)
- Java編程實(shí)現(xiàn)五子棋人人對(duì)戰(zhàn)代碼示例
- Java實(shí)現(xiàn)五子棋AI算法
- java實(shí)現(xiàn)五子棋程序
相關(guān)文章
java?讀寫(xiě)鎖的使用及它的優(yōu)點(diǎn)
這篇文章主要介紹了java?讀寫(xiě)鎖的使用及它的優(yōu)點(diǎn),讀寫(xiě)鎖的特點(diǎn)就是是讀讀不互斥、讀寫(xiě)互斥、寫(xiě)寫(xiě)互斥,下面具體使用分享需要的小伙伴可以參考一下2022-05-05
使用java的注解(用在java類(lèi)的方法上的注解)方法
這篇文章主要介紹了使用java的注解(用在java類(lèi)的方法上的注解)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
解決JavaMail附件名字過(guò)長(zhǎng)導(dǎo)致的亂碼問(wèn)題
這篇文章主要介紹了解決JavaMail附件名字過(guò)長(zhǎng)導(dǎo)致的亂碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
Spring?Mvc中CommonsMultipartFile的特性實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于Spring?Mvc中CommonsMultipartFile特性的相關(guān)資料,SpringMVC擁有強(qiáng)大的靈活性,非侵入性和可配置性,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
SpringBoot如何使用feign實(shí)現(xiàn)遠(yuǎn)程接口調(diào)用和錯(cuò)誤熔斷
這篇文章主要介紹了SpringBoot如何使用feign實(shí)現(xiàn)遠(yuǎn)程接口調(diào)用和錯(cuò)誤熔斷,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
springboot之Validation參數(shù)校驗(yàn)詳細(xì)解讀
這篇文章主要介紹了springboot之Validation參數(shù)校驗(yàn)詳細(xì)解讀,本篇是關(guān)于springboot的參數(shù)校驗(yàn)知識(shí),當(dāng)然也適用其它java應(yīng)用,讀完本篇將學(xué)會(huì)基本的參數(shù)校驗(yàn),自定義參數(shù)校驗(yàn)和分組參數(shù)校驗(yàn),需要的朋友可以參考下2023-10-10

