zoukankan      html  css  js  c++  java
  • Java简易撞鬼游戏demo

    9*9方格内两点随机行走,相遇则停止。

     1 public class 撞鬼 {
     2 
     3     public static int length = 9;
     4     public static char[][] matrix = new char[length][length];
     5     public static int firstx = length - length;
     6     public static int firsty = length - length;
     7     public static int secondx = length - 1;
     8     public static int secondy = length - 1;
     9     
    10     public static void main(String[] args) {
    11         
    12         final int timeInterval = 333;
    13         
    14         for(int i=length - length;i<length;i++){
    15             for(int j=length - length;j<length;j++){
    16                 matrix[i][j] = '○';
    17                 if((i==j&&i==(length - length))||(i==j&&i==(length-1))){
    18                     matrix[i][j] = '●';
    19                 }
    20             }
    21         }
    22         
    23         Runnable runnable = new Runnable() {
    24             public void run() {
    25                 while (true) {
    26                     try {
    27                         for(int i=length - length;i<length;i++){
    28                             for(int j=length - length;j<length;j++){
    29                                 System.out.print(matrix[i][j]);
    30                                 System.out.print(' ');
    31                             }
    32                             System.out.println();
    33                         }
    34                         if(firstx==secondx&&firsty==secondy){
    35                             break;
    36                         }
    37                         changePosition();
    38                         Thread.sleep(timeInterval);
    39                     } catch (InterruptedException e) {
    40                         e.printStackTrace();
    41                     }
    42                 }
    43             }
    44         };
    45         Thread thread = new Thread(runnable);
    46         thread.start();
    47         
    48     }
    49     
    50     public static void changePosition(){
    51         double randomfirst = Math.random();
    52         double randomsecond = Math.random();
    53         if(randomfirst<0.25){
    54             if(firstx>length - length){
    55                 matrix[firstx--][firsty] = '○';
    56                 matrix[firstx][firsty] = '●';
    57             }
    58         }else if(randomfirst<0.50){
    59             if(firstx<length - 1){
    60                 matrix[firstx++][firsty] = '○';
    61                 matrix[firstx][firsty] = '●';
    62             }
    63         }else if(randomfirst<0.75){
    64             if(firsty>length - length){
    65                 matrix[firstx][firsty--] = '○';
    66                 matrix[firstx][firsty] = '●';
    67             }
    68         }else{
    69             if(firsty<length - 1){
    70                 matrix[firstx][firsty++] = '○';
    71                 matrix[firstx][firsty] = '●';
    72             } 
    73         }
    74         if(randomsecond<0.25){
    75             if(secondx>length - length){
    76                 matrix[secondx--][secondy] = '○';
    77                 matrix[secondx][secondy] = '●';
    78             }
    79         }else if(randomsecond<0.50){
    80             if(secondx<length - 1){
    81                 matrix[secondx++][secondy] = '○';
    82                 matrix[secondx][secondy] = '●';
    83             }
    84         }else if(randomsecond<0.75){
    85             if(secondy>length - length){
    86                 matrix[secondx][secondy--] = '○';
    87                 matrix[secondx][secondy] = '●';
    88             }
    89         }else{
    90             if(secondy<length - 1){
    91                 matrix[secondx][secondy++] = '○';
    92                 matrix[secondx][secondy] = '●';
    93             } 
    94         }
    95     }
    96         
    97 }
  • 相关阅读:
    枚举类型或运算
    设置字体同时为粗体、斜体
    批量处理任务进度条控制—基于BackgroundWorker
    PyCharm终端执行python脚本报错:ModuleNotFoundError: No module named 'lib.apilib'
    PyCharm用pytest模式执行python脚本不生成allure测试报告
    Python3+Appium基于安卓平台的app自动化
    JMeter上一个接口返回数据作为下一个接口的参数应用
    JMeter安装及语言切换
    Python之序列化和反序列化
    Requests库发送post请求,传入接口参数后报JSON parse error
  • 原文地址:https://www.cnblogs.com/VRGamer-006/p/8433220.html
Copyright © 2011-2022 走看看