当前位置:   article > 正文

单机版五子棋【JAVA】_java五子棋内容

java五子棋内容
单机版五子棋

这个小游戏是我和我姐们儿的JAVA课程设计,也是我做的第一个JAVA项目,适合初学者,希望能帮到那些被JAVA课设所困扰的孩纸们~~~

一、该游戏需要的实现的:

1、设计主框架,界面。

2、利用ActionListener接口实现按钮事件的监听。

3、重新开始功能的实现。

4、悔棋功能的实现。

5、退出功能的实现。

6、棋盘中棋子点类的定义。

7,利用MouseListener接口实现事件监听,并实现接口里的所有方法。

8,当鼠标移动到棋盘上的交点上,且该点上无棋子时能够变成小手形状。

9,点击棋盘时,利用if语句判断该点是否点在交点上,并利用foreach语句和棋子类中的getX(),getY()方法遍历每一个棋子的位置判断该点是否有棋子。

10,当判断到可以在所点击的点上下子时,画棋子时利用for循环遍历已有的每一个点并利用Graphics类的setColor设置颜色,利用Graphics类的fillOval方法设置形状大小。

11,当画完棋子时要及时判断输赢,用棋子所在索引和for循环遍历最后一个棋子的各个方向,如果有在同一条直线上的棋子个数大于等于五的即当前棋子所代表的那方赢。

12,胜负已定的时候,能够弹出相应的信息。

二、功能代码实现

2.1进入游戏

  1. public static void main(String[] args) {
  2. StartChessJFrame f=new StartChessJFrame();//创建主框架
  3. f.setVisible(true);//显示主框架
  4. }

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

  1. private ChessBoard chessBoard;//对战面板
  2. private Panel toolbar;//工具条面板
  3. private Button startButton;//设置开始按钮
  4. private Button backButton;//设置悔棋按钮
  5. private Button exitButton;//设置退出按钮

2.3界面的构造方法(游戏的框架)

  1. public StartChessJFrame(){
  2. setTitle("单机版五子棋");//设置标题
  3. chessBoard=new ChessBoard();//初始化面板对象,创建和添加菜单
  4. MyItemListener lis=new MyItemListener();//初始化按钮事件监听器内部类
  5. toolbar=new Panel();//工具面板栏实例化
  6. startButton=new Button("重新开始");
  7. backButton=new Button("悔棋");
  8. exitButton=new Button("退出");//三个按钮初始化
  9. toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));//将工具面板按钮用FlowLayout布局
  10. toolbar.add(backButton);
  11. toolbar.add(startButton);
  12. toolbar.add(exitButton);//将三个按钮添加到工具面板上
  13. startButton.addActionListener(lis);
  14. backButton.addActionListener(lis);
  15. exitButton.addActionListener(lis);//将三个按钮事件注册监听事件
  16. add(toolbar,BorderLayout.SOUTH);//将工具面板布局到界面南方也就是下面
  17. add(chessBoard);//将面板对象添加到窗体上
  18. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置界面关闭事件
  19. pack();//自适应大小
  20. }

2.4按钮的实现与监听(构造方法内部)

  1. MyItemListener lis=new MyItemListener();//初始化按钮事件监听器内部类
  2. toolbar=new Panel();//工具面板栏实例化
  3. startButton=new Button("重新开始");
  4. backButton=new Button("悔棋");
  5. exitButton=new Button("退出");//三个按钮初始化
  6. toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));//将工具面板按钮用FlowLayout布局
  7. toolbar.add(backButton);
  8. toolbar.add(startButton);
  9. toolbar.add(exitButton);//将三个按钮添加到工具面板上
  10. startButton.addActionListener(lis);
  11. backButton.addActionListener(lis);
  12. exitButton.addActionListener(lis);//将三个按钮事件注册监听事件

2.5按钮事件的监听

  1. private class MyItemListener implements ActionListener{
  2. public void actionPerformed(ActionEvent e) {
  3. Object obj=e.getSource();//获取事件源
  4. if(obj==startButton){
  5. System.out.println("重新开始...");//重新开始
  6. //JFiveFrame.this内部类引用外部类
  7. chessBoard.restartGame();
  8. }else if(obj==exitButton){
  9. System.exit(0);//结束应用程序
  10. }else if(obj==backButton){
  11. System.out.println("悔棋...");//悔棋
  12. chessBoard.goback();
  13. }
  14. }
  15. }

2.6重新开始按钮的功能实现

  1. public void restartGame(){//清除棋子
  2. for(int i=0;i<chessList.length;i++)
  3. chessList[i]=null;
  4. /*恢复游戏相关的变量值*/
  5. isBack=true;
  6. gameOver=false;//游戏是否结束
  7. chessCount=0;//当前棋盘的棋子个数
  8. repaint();
  9. }

2.7悔棋按钮的功能实现

  1. public void goback(){
  2. if(chessCount==0)
  3. return ;
  4. chessList[chessCount-1]=null;
  5. chessCount--;
  6. if(chessCount>0){
  7. xIndex=chessList[chessCount-1].getX();
  8. yIndex=chessList[chessCount-1].getY();
  9. }
  10. isBack=!isBack;
  11. repaint();
  12. }

2.8当棋盘根据需要变大或变小时窗口应随之发生改变

  1. //Dimension:矩形ChessBoard类内部
  2. public Dimension getPreferredSize(){
  3. return new Dimension(MARGIN*2+GRID_SPAN*COLS,MARGIN*2+GRID_SPAN*ROWS);
  4. }
  5. pack();//自适应大小StartChessBoard类内部

2.9定义棋子类

  1. import java.awt.*;
  2. public class Point {
  3. private int x;//棋子在棋盘中的x索引值
  4. private int y;//棋子在棋盘中的y索引值
  5. private Color color;//颜色
  6. public static int DIAMETER=30;//直径
  7. public Point(int x,int y,Color color){
  8. this.x=x;
  9. this.y=y;
  10. this.color=color;
  11. }
  12. //得到棋子在棋盘中的x索引值
  13. public int getX(){
  14. return x;
  15. }
  16. //得到棋子在棋盘中的y索引值
  17. public int getY(){
  18. return y;
  19. }
  20. //得到棋子颜色
  21. public Color getColor(){
  22. return color;
  23. }
  24. }

三、功能部分代码实现

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

  1. public static int MARGIN=30;//边距
  2. public static int GRID_SPAN=35;//网格间距
  3. public static int ROWS=18;//棋盘行数
  4. public static int COLS=18;//棋盘列数
  5. Point[] chessList=new Point[(ROWS+1)*(COLS+1)];//初始化每个数组元素为null
  6. boolean isBack=true;//默认开始是黑棋先下
  7. boolean gameOver=false;//游戏是否结束
  8. int chessCount;//当前棋盘的棋子个数
  9. int xIndex,yIndex;//当前刚下棋子的索引

3.2棋盘对象的构造方法

  1. public ChessBoard(){
  2. setBackground(Color.LIGHT_GRAY);//设置背景颜色为灰色
  3. addMouseListener(this);//添加事件监听器
  4. addMouseMotionListener(new MouseMotionListener() {//匿名内部类
  5. @Override
  6. public void mouseMoved(MouseEvent e) {
  7. int x1=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
  8. int y1=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//将鼠标单击的坐标位置转化为网格索引
  9. if(x1<0||x1>ROWS||y1<0||y1>COLS||gameOver||findChess(x1,y1)){//游戏已经结束,不能下;落在棋盘外,不能下;x,y位置已经有棋子存在,不能下
  10. setCursor(new Cursor(Cursor.DEFAULT_CURSOR));//设置成默认形状
  11. }else{
  12. setCursor(new Cursor(Cursor.HAND_CURSOR));//设置成手型
  13. }
  14. }
  15. @Override
  16. public void mouseDragged(MouseEvent e) {
  17. }
  18. });
  19. }

3.3设置鼠标监听器,变小手(在构造方法内部)

  1. addMouseMotionListener(new MouseMotionListener() {//匿名内部类
  2. @Override
  3. public void mouseMoved(MouseEvent e) {
  4. int x1=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
  5. int y1=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//将鼠标单击的坐标位置转化为网格索引
  6. if(x1<0||x1>ROWS||y1<0||y1>COLS||gameOver||findChess(x1,y1)){//游戏已经结束,不能下;落在棋盘外,不能下;x,y位置已经有棋子存在,不能下
  7. setCursor(new Cursor(Cursor.DEFAULT_CURSOR));//设置成默认形状
  8. }else{
  9. setCursor(new Cursor(Cursor.HAND_CURSOR));//设置成手型
  10. }
  11. }
  12. @Override
  13. public void mouseDragged(MouseEvent e) {
  14. }
  15. });

3.4点击棋盘时的鼠标按压事件

  1. public void mousePressed(MouseEvent e) {//鼠标按键在组件上按下时调用
  2. if(gameOver)//游戏已经结束,不能下
  3. return ;
  4. String colorName=isBack ? "黑棋" : "白棋";
  5. xIndex=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
  6. yIndex=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//将鼠标单击的坐标位置转化为网格索引
  7. if(xIndex<0||xIndex>ROWS||yIndex<0||yIndex>COLS)//棋子落在棋盘外,不能下
  8. return ;
  9. if(findChess(xIndex,yIndex))//x,y位置已经有棋子存在,不能下
  10. return ;
  11. Point ch=new Point(xIndex,yIndex,isBack ? Color.black : Color.white);
  12. chessList[chessCount++]=ch;
  13. repaint();//通知系统重新绘制
  14. if(isWin()){
  15. String msg=String.format("恭喜,%s赢啦~", colorName);
  16. JOptionPane.showMessageDialog(this, msg);
  17. gameOver=true;
  18. }
  19. else if(chessCount==(COLS+1)*(ROWS+1))
  20. {
  21. String msg=String.format("棋鼓相当,棒棒哒~");
  22. JOptionPane.showMessageDialog(this,msg);
  23. gameOver=true;
  24. }
  25. isBack=!isBack;
  26. }

3.5绘制棋盘,棋子还有红框框

  1. public void paintComponent(Graphics g){
  2. super.paintComponent(g);//画棋盘
  3. for(int i=0;i<=ROWS;i++){//画横线
  4. g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN);
  5. }
  6. for(int i=0;i<=COLS;i++){//画直线
  7. g.drawLine(MARGIN+i*GRID_SPAN, MARGIN, MARGIN+i*GRID_SPAN,MARGIN+ROWS*GRID_SPAN);
  8. }
  9. /*画棋子*/
  10. for(int i=0;i<chessCount;i++){
  11. int xPos=chessList[i].getX()*GRID_SPAN+MARGIN;//网格交叉的x坐标
  12. int yPos=chessList[i].getY()*GRID_SPAN+MARGIN;//网格交叉的y坐标
  13. g.setColor(chessList[i].getColor());//设置颜色
  14. g.fillOval(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER);
  15. if(i==chessCount-1){
  16. g.setColor(Color.red);//标记最后一个棋子为红色
  17. g.drawRect(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER);
  18. }
  19. }
  20. }

3.6判断输赢

  1. /*判断哪方赢*/
  2. private boolean isWin(){
  3. int continueCount=1;//连续棋子的个数
  4. for(int x=xIndex-1;x>=0;x--){//横向向左寻找
  5. Color c=isBack ? Color.black : Color.white;
  6. if(getChess(x,yIndex,c)!=null){
  7. continueCount++;
  8. }else
  9. break;
  10. }
  11. for(int x=xIndex+1;x<=ROWS;x++){//横向向右寻找
  12. Color c=isBack ? Color.black : Color.white;
  13. if(getChess(x,yIndex,c)!=null){
  14. continueCount++;
  15. }else
  16. break;
  17. }
  18. if(continueCount>=5){//判断记录数大于等于五,即表示此方获胜
  19. return true;
  20. }else
  21. continueCount=1;
  22. //
  23. for(int y=yIndex-1;y>=0;y--){//纵向向上寻找
  24. Color c=isBack ? Color.black : Color.white;
  25. if(getChess(xIndex,y,c)!=null){
  26. continueCount++;
  27. }else
  28. break;
  29. }
  30. for(int y=yIndex+1;y<=ROWS;y++){//纵向向下寻找
  31. Color c=isBack ? Color.black : Color.white;
  32. if(getChess(xIndex,y,c)!=null){
  33. continueCount++;
  34. }else
  35. break;
  36. }
  37. if(continueCount>=5){//判断记录数大于等于五,即表示此方获胜
  38. return true;
  39. }else
  40. continueCount=1;
  41. //
  42. for(int x=xIndex+1,y=yIndex-1;y>=0&&x<=COLS;x++,y--){//右下寻找
  43. Color c=isBack ? Color.black : Color.white;
  44. if(getChess(x,y,c)!=null){
  45. continueCount++;
  46. }else
  47. break;
  48. }
  49. for(int x=xIndex-1,y=yIndex+1;y<=ROWS&&x>=0;x--,y++){//左上寻找
  50. Color c=isBack ? Color.black : Color.white;
  51. if(getChess(x,y,c)!=null){
  52. continueCount++;
  53. }else
  54. break;
  55. }
  56. if(continueCount>=5){//判断记录数大于等于五,即表示此方获胜
  57. return true;
  58. }else
  59. continueCount=1;
  60. //
  61. for(int x=xIndex-1,y=yIndex-1;y>=0&&x>=0;x--,y--){//左下寻找
  62. Color c=isBack ? Color.black : Color.white;
  63. if(getChess(x,y,c)!=null){
  64. continueCount++;
  65. }else
  66. break;
  67. }
  68. for(int x=xIndex+1,y=yIndex+1;y<=ROWS&&x<=COLS;x++,y++){//右上寻找
  69. Color c=isBack ? Color.black : Color.white;
  70. if(getChess(x,y,c)!=null){
  71. continueCount++;
  72. }else
  73. break;
  74. }
  75. if(continueCount>=5){//判断记录数大于等于五,即表示此方获胜
  76. return true;
  77. }else
  78. continueCount=1;
  79. return false;
  80. }

3.7弹出相应消息框(在鼠标按压函数内部)

  1. if(isWin()){
  2. String msg=String.format("恭喜,%s赢啦~", colorName);
  3. JOptionPane.showMessageDialog(this, msg);
  4. gameOver=true;
  5. }
  6. else if(chessCount==(COLS+1)*(ROWS+1))//平局
  7. {
  8. String msg=String.format("棋鼓相当,棒棒哒~");
  9. JOptionPane.showMessageDialog(this,msg);
  10. gameOver=true;
  11. }

3.8上面用到的一个判断某点是否有棋子的函数

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

3.9因为该棋盘类实现了鼠标监听接口MonseListener,所以要重写该接口内的所有方法,其它方法如下

  1. @Override
  2. public void mouseClicked(MouseEvent e) {//鼠标按键在组件上单击(按下并释放)时调用
  3. }
  4. @Override
  5. public void mouseReleased(MouseEvent e) {鼠标按键在组件上释放时调用
  6. }
  7. @Override
  8. public void mouseEntered(MouseEvent e) {//鼠标进入组件时调用
  9. }
  10. @Override
  11. public void mouseExited(MouseEvent e){//鼠标离开组件时调用
  12. }

四、运行结果





五、代码汇总

该游戏总共建了三个类,一个是界面StartChessJFrame,一个是棋盘类ChessBoard,一个是棋子类Point

5.1StartChessJFrame类

  1. package chess.lcc.com;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.awt.*;
  5. /*
  6. * 五子棋的主框架,程序启动类
  7. */
  8. public class StartChessJFrame extends JFrame {
  9. private ChessBoard chessBoard;//对战面板
  10. private Panel toolbar;//工具条面板
  11. private Button startButton;//设置开始按钮
  12. private Button backButton;//设置悔棋按钮
  13. private Button exitButton;//设置退出按钮
  14. public StartChessJFrame(){
  15. setTitle("单机版五子棋");//设置标题
  16. chessBoard=new ChessBoard();//初始化面板对象,创建和添加菜单
  17. MyItemListener lis=new MyItemListener();//初始化按钮事件监听器内部类
  18. toolbar=new Panel();//工具面板栏实例化
  19. startButton=new Button("重新开始");
  20. backButton=new Button("悔棋");
  21. exitButton=new Button("退出");//三个按钮初始化
  22. toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));//将工具面板按钮用FlowLayout布局
  23. toolbar.add(backButton);
  24. toolbar.add(startButton);
  25. toolbar.add(exitButton);//将三个按钮添加到工具面板上
  26. startButton.addActionListener(lis);
  27. backButton.addActionListener(lis);
  28. exitButton.addActionListener(lis);//将三个按钮事件注册监听事件
  29. add(toolbar,BorderLayout.SOUTH);//将工具面板布局到界面南方也就是下面
  30. add(chessBoard);//将面板对象添加到窗体上
  31. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置界面关闭事件
  32. pack();//自适应大小
  33. }
  34. private class MyItemListener implements ActionListener{
  35. public void actionPerformed(ActionEvent e) {
  36. Object obj=e.getSource();//获取事件源
  37. if(obj==startButton){
  38. System.out.println("重新开始...");//重新开始
  39. //JFiveFrame.this内部类引用外部类
  40. chessBoard.restartGame();
  41. }else if(obj==exitButton){
  42. System.exit(0);//结束应用程序
  43. }else if(obj==backButton){
  44. System.out.println("悔棋...");//悔棋
  45. chessBoard.goback();
  46. }
  47. }
  48. }
  49. public static void main(String[] args) {
  50. StartChessJFrame f=new StartChessJFrame();//创建主框架
  51. f.setVisible(true);//显示主框架
  52. }
  53. }
5.2ChessBoard类
  1. package chess.lcc.com;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.MouseListener;
  5. import java.awt.event.MouseMotionListener;
  6. import java.awt.event.MouseEvent;
  7. /*五子棋-棋盘类*/
  8. public class ChessBoard extends JPanel implements MouseListener{
  9. public static int MARGIN=30;//边距
  10. public static int GRID_SPAN=35;//网格间距
  11. public static int ROWS=15;//棋盘行数
  12. public static int COLS=15;//棋盘列数
  13. Point[] chessList=new Point[(ROWS+1)*(COLS+1)];//初始化每个数组元素为null
  14. boolean isBack=true;//默认开始是黑棋先下
  15. boolean gameOver=false;//游戏是否结束
  16. int chessCount;//当前棋盘的棋子个数
  17. int xIndex,yIndex;//当前刚下棋子的索引
  18. public ChessBoard(){
  19. setBackground(Color.LIGHT_GRAY);//设置背景颜色为黄色
  20. addMouseListener(this);//添加事件监听器
  21. addMouseMotionListener(new MouseMotionListener() {//匿名内部类
  22. @Override
  23. public void mouseMoved(MouseEvent e) {
  24. int x1=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
  25. int y1=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//将鼠标单击的坐标位置转化为网格索引
  26. if(x1<0||x1>ROWS||y1<0||y1>COLS||gameOver||findChess(x1,y1)){//游戏已经结束,不能下;落在棋盘外,不能下;x,y位置已经有棋子存在,不能下
  27. setCursor(new Cursor(Cursor.DEFAULT_CURSOR));//设置成默认形状
  28. }else{
  29. setCursor(new Cursor(Cursor.HAND_CURSOR));//设置成手型
  30. }
  31. }
  32. @Override
  33. public void mouseDragged(MouseEvent e) {
  34. }
  35. });
  36. }
  37. /*绘制*/
  38. public void paintComponent(Graphics g){
  39. super.paintComponent(g);//画棋盘
  40. for(int i=0;i<=ROWS;i++){//画横线
  41. g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN);
  42. }
  43. for(int i=0;i<=COLS;i++){//画直线
  44. g.drawLine(MARGIN+i*GRID_SPAN, MARGIN, MARGIN+i*GRID_SPAN,MARGIN+ROWS*GRID_SPAN);
  45. }
  46. /*画棋子*/
  47. for(int i=0;i<chessCount;i++){
  48. int xPos=chessList[i].getX()*GRID_SPAN+MARGIN;//网格交叉的x坐标
  49. int yPos=chessList[i].getY()*GRID_SPAN+MARGIN;//网格交叉的y坐标
  50. g.setColor(chessList[i].getColor());//设置颜色
  51. g.fillOval(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER);
  52. if(i==chessCount-1){
  53. g.setColor(Color.red);//标记最后一个棋子为红色
  54. g.drawRect(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER);
  55. }
  56. }
  57. }
  58. @Override
  59. public void mousePressed(MouseEvent e) {//鼠标按键在组件上按下时调用
  60. if(gameOver)//游戏已经结束,不能下
  61. return ;
  62. String colorName=isBack ? "黑棋" : "白棋";
  63. xIndex=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
  64. yIndex=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//将鼠标单击的坐标位置转化为网格索引
  65. if(xIndex<0||xIndex>ROWS||yIndex<0||yIndex>COLS)//棋子落在棋盘外,不能下
  66. return ;
  67. if(findChess(xIndex,yIndex))//x,y位置已经有棋子存在,不能下
  68. return ;
  69. Point ch=new Point(xIndex,yIndex,isBack ? Color.black : Color.white);
  70. chessList[chessCount++]=ch;
  71. repaint();//通知系统重新绘制
  72. if(isWin()){
  73. String msg=String.format("恭喜,%s赢啦~", colorName);
  74. JOptionPane.showMessageDialog(this, msg);
  75. gameOver=true;
  76. }
  77. else if(chessCount==(COLS+1)*(ROWS+1))
  78. {
  79. String msg=String.format("棋鼓相当,棒棒哒~");
  80. JOptionPane.showMessageDialog(this,msg);
  81. gameOver=true;
  82. }
  83. isBack=!isBack;
  84. }
  85. @Override
  86. public void mouseClicked(MouseEvent e) {//鼠标按键在组件上单击(按下并释放)时调用
  87. }
  88. @Override
  89. public void mouseReleased(MouseEvent e) {鼠标按键在组件上释放时调用
  90. }
  91. @Override
  92. public void mouseEntered(MouseEvent e) {//鼠标进入组件时调用
  93. }
  94. @Override
  95. public void mouseExited(MouseEvent e){//鼠标离开组件时调用
  96. }
  97. private boolean findChess(int x,int y){
  98. for(Point c:chessList){
  99. if(c!=null&&c.getX()==x&&c.getY()==y)
  100. return true;
  101. }
  102. return false;
  103. }
  104. /*判断那方赢*/
  105. private boolean isWin(){
  106. int continueCount=1;//连续棋子的个数
  107. for(int x=xIndex-1;x>=0;x--){//横向向左寻找
  108. Color c=isBack ? Color.black : Color.white;
  109. if(getChess(x,yIndex,c)!=null){
  110. continueCount++;
  111. }else
  112. break;
  113. }
  114. for(int x=xIndex+1;x<=ROWS;x++){//横向向右寻找
  115. Color c=isBack ? Color.black : Color.white;
  116. if(getChess(x,yIndex,c)!=null){
  117. continueCount++;
  118. }else
  119. break;
  120. }
  121. if(continueCount>=5){//判断记录数大于等于五,即表示此方获胜
  122. return true;
  123. }else
  124. continueCount=1;
  125. //
  126. for(int y=yIndex-1;y>=0;y--){//纵向向上寻找
  127. Color c=isBack ? Color.black : Color.white;
  128. if(getChess(xIndex,y,c)!=null){
  129. continueCount++;
  130. }else
  131. break;
  132. }
  133. for(int y=yIndex+1;y<=ROWS;y++){//纵向向下寻找
  134. Color c=isBack ? Color.black : Color.white;
  135. if(getChess(xIndex,y,c)!=null){
  136. continueCount++;
  137. }else
  138. break;
  139. }
  140. if(continueCount>=5){//判断记录数大于等于五,即表示此方获胜
  141. return true;
  142. }else
  143. continueCount=1;
  144. //
  145. for(int x=xIndex+1,y=yIndex-1;y>=0&&x<=COLS;x++,y--){//右下寻找
  146. Color c=isBack ? Color.black : Color.white;
  147. if(getChess(x,y,c)!=null){
  148. continueCount++;
  149. }else
  150. break;
  151. }
  152. for(int x=xIndex-1,y=yIndex+1;y<=ROWS&&x>=0;x--,y++){//左上寻找
  153. Color c=isBack ? Color.black : Color.white;
  154. if(getChess(x,y,c)!=null){
  155. continueCount++;
  156. }else
  157. break;
  158. }
  159. if(continueCount>=5){//判断记录数大于等于五,即表示此方获胜
  160. return true;
  161. }else
  162. continueCount=1;
  163. //
  164. for(int x=xIndex-1,y=yIndex-1;y>=0&&x>=0;x--,y--){//左下寻找
  165. Color c=isBack ? Color.black : Color.white;
  166. if(getChess(x,y,c)!=null){
  167. continueCount++;
  168. }else
  169. break;
  170. }
  171. for(int x=xIndex+1,y=yIndex+1;y<=ROWS&&x<=COLS;x++,y++){//右上寻找
  172. Color c=isBack ? Color.black : Color.white;
  173. if(getChess(x,y,c)!=null){
  174. continueCount++;
  175. }else
  176. break;
  177. }
  178. if(continueCount>=5){//判断记录数大于等于五,即表示此方获胜
  179. return true;
  180. }else
  181. continueCount=1;
  182. return false;
  183. }
  184. private Point getChess(int xIndex,int yIndex,Color color){
  185. for(Point c:chessList){
  186. if(c!=null&&c.getX()==xIndex&&c.getY()==yIndex&&c.getColor()==color)
  187. return c;
  188. }
  189. return null;
  190. }
  191. public void restartGame(){//清除棋子
  192. for(int i=0;i<chessList.length;i++)
  193. chessList[i]=null;
  194. /*恢复游戏相关的变量值*/
  195. isBack=true;
  196. gameOver=false;//游戏是否结束
  197. chessCount=0;//当前棋盘的棋子个数
  198. repaint();
  199. }
  200. public void goback(){
  201. if(chessCount==0)
  202. return ;
  203. chessList[chessCount-1]=null;
  204. chessCount--;
  205. if(chessCount>0){
  206. xIndex=chessList[chessCount-1].getX();
  207. yIndex=chessList[chessCount-1].getY();
  208. }
  209. isBack=!isBack;
  210. repaint();
  211. }
  212. //Dimension:矩形
  213. public Dimension getPreferredSize(){
  214. return new Dimension(MARGIN*2+GRID_SPAN*COLS,MARGIN*2+GRID_SPAN*ROWS);
  215. }
  216. }

5.3Point类

  1. package chess.lcc.com;
  2. import java.awt.*;
  3. public class Point {
  4. private int x;//棋子在棋盘中的x索引值
  5. private int y;//棋子在棋盘中的y索引值
  6. private Color color;//颜色
  7. public static int DIAMETER=30;//直径
  8. public Point(int x,int y,Color color){
  9. this.x=x;
  10. this.y=y;
  11. this.color=color;
  12. }
  13. //得到棋子在棋盘中的x索引值
  14. public int getX(){
  15. return x;
  16. }
  17. //得到棋子在棋盘中的y索引值
  18. public int getY(){
  19. return y;
  20. }
  21. //得到棋子颜色
  22. public Color getColor(){
  23. return color;
  24. }
  25. }








声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/326351
推荐阅读
相关标签
  

闽ICP备14008679号