• 五子棋——Java语言


    在《疯狂Java讲义》一书看到一部分“五子棋”的代码,一时兴起就自己尝试动手写了一个。

    我写和书上的有很大的不同。不过当然是我写的太臃肿(烂,乱)了。

    7月中旬左右写过俄罗斯方块,到后面,不过程序不按自己预期的运行,找不到Bug。于是先放弃了。

    因此,这段时间还是心情比较低落的,不过还和生活不太顺利有关。

    这次,应该说今天,写出了个五子棋。又找回了一点信心和编程的激情。

    我写的这个游戏有无漏洞还不确定,暂时还没发现。

    效果图

    源代码

    wuziqi.java

     1 public class wuziqi
     2 {
     3     public static void main(String[] args){
     4         showGameName();
     5         board board1 = new board();
     6         
     7         board1.newBoard();
     8         
     9         startgame();
    10         
    11     }
    12 
    13     public static boolean startgame(){
    14         while(true){
    15             //提示信息
    16             System.out.println("white turn:");
    17             /*玩家下一步旗*/
    18             board.printChessman('○');
    19             /*检查一次是否结束*/
    20             if(tools.chekGame('○')){System.out.println("Game Over
    white have Win");return false;}
    21             //提示信息
    22             System.out.println("black turn:");
    23             /*电脑下一步旗*/
    24             board.printChessman('●');
    25             /*检查一次是否结束*/
    26             if(tools.chekGame('●')){System.out.println("Game Over
    black have Win");return false;}
    27         }
    28         
    29     }
    30     public static void showGameName(){
    31         System.out.println("***********");
    32         System.out.println("  五子棋");
    33         System.out.println("***********");
    34     }
    35 
    36 
    37 }

    board.java

     1 import java.util.Scanner;
     2 import java.util.Date;
     3 public class board
     4 {
     5     /*这是一个15x15的棋盘*/
     6     public static int Board_Size = 15;
     7     /*用俩个二维数组定义棋盘*/
     8     public static char elements[][]=new char[Board_Size][Board_Size];
     9     public static int boards[][]=new int[Board_Size][Board_Size];
    10     /*初始化棋盘 并 绘制*/
    11     public static void newBoard(){
    12         for(int i=0;i<Board_Size;i++){//初始化
    13             for(int j=0;j<Board_Size;j++){
    14                 elements[i][j]='╂';
    15             }
    16         }
    17         /*绘制*/
    18         System.out.println("   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4");//打印标尺 列数
    19         for(int i=0;i<Board_Size;i++){
    20             System.out.print(i);if(i<10){System.out.print(" ");}////打印标尺 行数
    21             for(int j=0;j<Board_Size;j++){
    22                 if(tools.tool1(i,j)&&tools.tool2(i,j))//检查4个角和4条边
    23                 System.out.print(elements[i][j]);
    24             }System.out.println();
    25         }
    26     }
    27     /*绘制棋盘的方法*/
    28     public static void paintBoard(){
    29         System.out.println("   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4");//打印标尺 列数
    30         for(int i=0;i<Board_Size;i++){
    31             System.out.print(i);if(i<10){System.out.print(" ");}////打印标尺 行数
    32             for(int j=0;j<Board_Size;j++){
    33                 System.out.print(elements[i][j]);
    34             }System.out.println();
    35         }
    36     }
    37     /*根据用户输入的坐标打印旗子*/
    38     public static void printChessman(char C){
    39         while(true){
    40         Scanner scanner = new Scanner(System.in);
    41         System.out.println("请输入x:");
    42         int x = scanner.nextInt();
    43         System.out.println("请输入y:");
    44         int y = scanner.nextInt();
    45         if(x<=14&&x>=0&&y<=14&&y>=0&&(elements[x][y]!='○'&&elements[x][y]!='●')){
    46             elements[x][y]=C;break;
    47         }else{System.out.println("输入坐标无效!");}
    48         }
    49         
    50         paintBoard();
    51     }
    52         /*根据用户输入的坐标打印旗子2*/
    53     public static void printChessman2(){
    54         while(true){
    55         Scanner scanner = new Scanner(System.in);
    56         System.out.println("请输入x:");
    57         int x = scanner.nextInt();
    58         System.out.println("请输入y:");
    59         int y = scanner.nextInt();
    60         if(x<=14&&x>=0&&y<=14&&y>=0&&(elements[x][y]!='○'&&elements[x][y]!='●')){
    61             elements[x][y]='●';break;
    62         }else{System.out.println("输入坐标无效!");}
    63         }
    64         
    65         paintBoard();
    66     }
    67 }

    tools.java

     1 public class tools 
     2 {
     3     public static boolean tool1(int x,int y){//检查棋盘四个角
     4         if(x==0&&y==0){board.elements[x][y]='┎';return true;}
     5         else if(x==0&&y==board.Board_Size-1){board.elements[x][y]='┒';return true;}
     6         else if(x==board.Board_Size-1&&y==0){board.elements[x][y]='┖';return true;}
     7         else if(x==board.Board_Size-1&&y==board.Board_Size-1){board.elements[x][y]='┚';return true;}
     8         return true;
     9     }
    10     public static boolean tool2(int x,int y){//检查棋盘四条边缘
    11         if(x==0&&y!=board.Board_Size-1&&y!=0){board.elements[x][y]='┰';return true;}
    12         else if(x==board.Board_Size-1&&y!=board.Board_Size-1&&y!=0){board.elements[x][y]='┸';return true;}
    13         else if(y==0&&x!=board.Board_Size-1&&x!=0){board.elements[x][y]='┠';return true;}
    14         else if(y==board.Board_Size-1&&x!=board.Board_Size-1&&x!=0){board.elements[x][y]='┨';return true;}
    15         return true;
    16     }
    17     public static boolean chekGame(char C){/*检查是否有同色的旗子连着5个*/
    18         for(int i=0;i<board.Board_Size;i++){
    19             for(int j=0;j<board.Board_Size;j++){
    20                 int count1=0;
    21                 int count2=0;
    22                 int count3=0;
    23                 int count4=0;
    24                 /*检查是否有5个竖着连着*/
    25                 if(board.elements[i][j]==C){
    26                 for(int x=1;x<=4;x++){
    27                     if(i-x>=0){if(board.elements[i-x][j]==C){count1++;}else{break;}}
    28                     else{break;}
    29                 }
    30                 for(int x=1;x<=4;x++){
    31                     if(i+x<=14){if(board.elements[i+x][j]==C){count1++;}else{break;}}
    32                     else{break;}
    33                 }System.out.println("棋子的纵向:"+count1);}
    34                 
    35                 if(count1>=4){return true;}//退出
    36                 ////////////////////////////////////////////////////////////////////////
    37                 /*检查是否有5个横着连着*/
    38                 if(board.elements[i][j]==C){
    39                 for(int x=1;x<=4;x++){
    40                     if(j-x>=0){if(board.elements[i][j-x]==C){count2++;}else{break;}}
    41                     else{break;}
    42                 }
    43                 for(int x=1;x<=4;x++){
    44                     if(j+x<=14){if(board.elements[i][j+x]==C){count2++;}else{break;}}
    45                     else{break;}
    46                 }
    47                 System.out.println("棋子的横向:"+count2);}
    48                 
    49                 if(count2>=4){return true;}//退出
    50                 ////////////////////////////////////////////////////////////////////////
    51                 /*检查是否有5个左斜个连着*/
    52                 if(board.elements[i][j]==C){
    53                 for(int x=1;x<=4;x++){
    54                     if(j-x>=0&&i-x>=0){if(board.elements[i-x][j-x]==C){count3++;}else{break;}}
    55                     else{break;}
    56                 }
    57                 for(int x=1;x<=4;x++){
    58                     if(j+x<=14&&i+x<=14){if(board.elements[i+x][j+x]==C){count3++;}else{break;}}
    59                     else{break;}
    60                 }
    61                 System.out.println("棋子的左斜:"+count3);}
    62                 
    63                 if(count3>=4){return true;}//退出
    64                 ////////////////////////////////////////////////////////////////////////
    65                 /*检查是否有5个右斜个连着*/
    66                 if(board.elements[i][j]==C){
    67                 for(int x=1;x<=4;x++){
    68                     if(j+x<=14&&i-x>=0){if(board.elements[i-x][j+x]==C){count4++;}else{break;}}
    69                     else{break;}
    70                 }
    71                 for(int x=1;x<=4;x++){
    72                     if(j-x>=0&&i+x<=14){if(board.elements[i+x][j-x]==C){count4++;}else{break;}}
    73                     else{break;}
    74                 }
    75                 System.out.println("棋子的右斜:"+count4);}
    76                 
    77                 if(count4>=4){return true;}//退出
    78                 
    79             }
    80         }
    81         return false;
    82     }
    83 }

    写于大一暑假

    2020-8-7 04:27:09

  • 相关阅读:
    整理了一份FAQ,新手看一下
    分享:pythonbitstring 3.1.2 发布
    分享:TokuDB v7 发布,并宣布全面开源
    在美国学CS能挣多少钱?美国IT公司标准 offer package详细数字及绿卡政策 | 美国留学申请与就业找工作咨询博客|Warald|一亩三分地论坛
    写的split带改进
    分享:一个多进程并发执行程序ps命令 ls命令
    分享:vi/vim使用进阶: 指随意动,移动如飞 (一)
    waning rm i rm rvfi
    分享:C++中头文件、源文件之间的区别与联系
    分享:神奇的动归状态转移方程——最优子序列
  • 原文地址:https://www.cnblogs.com/flyingpenguins/p/13450168.html
Copyright © 2020-2023  润新知