• 【JAVA实例】五子棋小游戏


      1 /*
      2     (1)绘制棋盘 - 写一个成员方法实现
      3 
      4     (2)提示黑方和白方分别下棋并重新绘制棋盘 - 写一个成员方法实现。
      5 
      6     (3)每当一方下棋后判断是否获胜 - 写一个成员方法实现。
      7 
      8     (4)提示: 采用二维数组来模拟并描述棋盘,棋盘如下:
      9  */
     10 
     11 package stage1_module2.homework;
     12 
     13 import java.util.Scanner;
     14 import java.util.Arrays;
     15 
     16 public class FiveInARowGame {
     17 
     18     // 1. 绘制一个五子棋棋盘
     19     public String[][] drawChessBoard(){
     20 
     21         String[][] chessBoard = new String[17][17];
     22 
     23         for(int i = 0; i < chessBoard.length; i++) {
     24             for(int j = 0; j < chessBoard.length; j++) {
     25                 if(i == 0){
     26                     chessBoard[i][j] = "  "  + Long.toHexString(j-1) + "  ";
     27                 } else if(j == 0){
     28                     chessBoard[i][j] = Long.toHexString(i-1);
     29                 }
     30                 else {
     31                     chessBoard[i][j] = "  +  ";
     32                 }
     33                 chessBoard[0][0] = " ";
     34                 System.out.print(chessBoard[i][j] + " ");
     35             }
     36             System.out.println();
     37         }
     38         return chessBoard;
     39 
     40     }
     41 
     42     // 2. 提示黑方白方下棋
     43     public void playChess(){
     44 
     45         boolean flag = true;
     46         String side = "black";
     47 
     48         String[][] chessBoard = drawChessBoard();
     49 
     50         while ( flag ){
     51 
     52             System.out.println("请" + side + "落子(按照 行数 列数 的格式), eg: 7 8");
     53             System.out.println("PS:1 <= 行数 <= 16 / 1 <= 列数 <= 16 ");
     54             Scanner sc = new Scanner(System.in);
     55 
     56             int row = sc.nextInt();
     57             int column = sc.nextInt();
     58 
     59             if (chessBoard[row][column].equals("  +  ")) {
     60                 chessBoard[row][column] = side;
     61                 for (int i = 0; i < chessBoard.length; i++){
     62                     for (int j = 0; j < chessBoard[i].length; j++){
     63                         System.out.print
     64                                 (chessBoard[i][j]);
     65                     }
     66                     System.out.println();
     67                 }
     68 
     69                 if (judge(chessBoard, side).equals(side + " Winning")){
     70                     System.out.println("恭喜" + side + "方赢得胜利");
     71                     flag = false;
     72                 }else{
     73                     if (side.equals("black")){
     74                         side = " red ";
     75                     }else{
     76                         side = "black";
     77                     }
     78                 }
     79             } else{
     80                 System.out.println("请重新输入!");
     81             }
     82 
     83         }
     84     }
     85 
     86     // 3. 判断输赢
     87     public String judge(String[][] chessBoard, String side){
     88 
     89         // 1.判断横向是否连成五子
     90         for (int i = 1; i < chessBoard.length; i++) {
     91             int count = 0;
     92             for (int j = 1; j < chessBoard.length-1 ; j++) {
     93                 boolean horizontal = chessBoard[i][j].equals(chessBoard[i][j + 1])
     94                         && chessBoard[i][j].equals(side);
     95                 if ( horizontal ) {
     96                     count++;
     97                     if (count == 4) {
     98                         System.out.println(side + " Winning");
     99                         return side + " Winning";
    100                     }
    101                 } else {
    102                     count = 0;
    103                 }
    104             }
    105         }
    106 
    107         // 2.判断纵向是否连成五子
    108         for (int j = 1; j < chessBoard.length; j++) {
    109             int count = 0;
    110             for (int i = 1; i < chessBoard.length-1 ; i++) {
    111                 // 2.判断纵向是否连成五子
    112                 boolean vertical = chessBoard[i][j].equals(chessBoard[i + 1][j])
    113                         && chessBoard[i][j].equals(side);
    114                 if ( vertical ) {
    115                     count++;
    116                     if (count == 4) {
    117                         System.out.println(side + " Winning");
    118                         return side + " Winning";
    119                     }
    120                 } else {
    121                     count = 0;
    122                 }
    123             }
    124         }
    125 
    126         // 3.判断对角线是否连成五子(左上↖️️到右下↘️️)
    127         int cnt_1 = 2*(chessBoard.length-1)-9;
    128         for (int k = 0; k < cnt_1; k++){
    129             int count = 0;
    130             for ( int i = ((k <= (cnt_1/2)) ? 1 : (k-10)) ;
    131             i < ( (k <= cnt_1/2) ? k-(cnt_1/2)+chessBoard.length : (cnt_1/2)-k+chessBoard.length) - 1 ;
    132                  i++){
    133                 boolean diagLeftToRight =
    134                         chessBoard[i][chessBoard.length-k-6+i].equals(chessBoard[i+1][chessBoard.length-k-6+i+1])
    135                         && chessBoard[i][chessBoard.length-k-6+i].equals(side);
    136                 if ( diagLeftToRight ) {
    137                     count++;
    138                     if (count == 4) {
    139                         System.out.println(side + " Winning");
    140                         return side + " Winning";
    141                     }
    142                 } else {
    143                     count = 0;
    144                 }
    145 
    146             }
    147         }
    148 
    149         // 4.判断对角线是否连成五子(右上↗️到左下↙️)
    150         int cnt_2 = 2*(chessBoard.length-1)-9;
    151         for (int k = 0; k < cnt_2; k++){
    152             int count = 0;
    153             for ( int i = ((k <= (cnt_2/2)) ? 1 : (k-10)) ;
    154                   i < ( (k <= cnt_2/2) ? k-(cnt_2/2)+chessBoard.length : (cnt_2/2)-k+chessBoard.length) - 1;
    155                   i++){
    156                 boolean diagRightToLeft =
    157                         chessBoard[i][chessBoard.length+k-11-i].equals(chessBoard[i+1][chessBoard.length+k-11-i-1])
    158                                 && chessBoard[i][chessBoard.length+k-11-i].equals(side);
    159                 if ( diagRightToLeft ) {
    160                     count++;
    161 //                    System.out.print("inner count :" + count + " ");
    162                     if (count == 4) {
    163                         System.out.println(side + " Winning");
    164                         return side + " Winning";
    165                     }
    166                 } else {
    167                     count = 0;
    168 //                    System.out.print("count:" + count + " ");
    169                 }
    170             }
    171         }
    172 
    173         return "go on";
    174     }
    175 }
     1 package stage1_module2.homework;
     2 
     3 public class FiveARowGameTest {
     4 
     5     public static void main(String[] args){
     6 
     7         FiveInARowGame f1 = new FiveInARowGame();
     8         f1.drawChessBoard();
     9         f1.playChess();
    10     }
    11 }
  • 相关阅读:
    606. Construct String from Binary Tree
    557. Reverse Words in a String III
    551. Student Attendance Record I
    541. Reverse String II
    521. Longest Uncommon Subsequence I
    520. Detect Capital
    459. Repeated Substring Pattern
    人脸检测源码facedetection
    人脸检测的model类facemodel
    人脸检测解析json的工具类face_test
  • 原文地址:https://www.cnblogs.com/Tree0108/p/14107231.html
Copyright © 2020-2023  润新知