第八章第二十题(游戏:四子连)(Game: Sizi company)

第八章第二十题(游戏:四子连)(Game: Sizi company)

  • ***8.29(游戏:四子连)四子连是一个两个人玩的棋盘游戏,在游戏中,玩家轮流将有颜色的妻子放在一个六行七列的垂直悬挂的网格中。
    这个游戏的目的是看谁先实现一行、一列或者一条对角线上有四个相同颜色的棋子。程序提示两个玩家交替地下红字Red或黄字Yellow。当放下一子时,程序在控制台重新显示这个棋盘,然后确定游戏的状态(赢、平局还是继续)。
    ***8.20(Game: Sizi company)Game: the game is a board game played by two people. In the game, players take turns to place colored wives in a vertical grid of six rows and seven columns.
    The purpose of this game is to see who first realizes a row, a column or a diagonal line with four pieces of the same color. The program prompts two players to alternate red or yellow. When a piece is put down, the program redisplays the chessboard on the console and determines the state of the game (win, draw or continue).

  • 参考代码:

    package chapter08;
    
    import java.util.Scanner;
    
    public class Code_20 {
        public static void main(String[] args) throws Exception {
            Scanner input = new Scanner(System.in);
            String[] rys = {"red","yellow"};
            int ry = 0;
            int[] ptrs = {5,5,5,5,5,5,5};
            int[][] map = new int[6][7];
            while(true){
                printMap(map);
                if(isConsecutiveFour(map,1)){
                    System.out.println("The red player won");
                    break;
                }
                if(isConsecutiveFour(map,2)){
                    System.out.println("The yellow player won");
                    break;
                }
                System.out.print("Drop a " + rys[ry % 2] + " disk at column (0-6):");
                int dick = input.nextInt();
                map[ptrs[dick]][dick]=ry % 2 + 1;
                ptrs[dick]--;
                ry++;
            }
        }
        public static boolean isConsecutiveFour(int[][] values,int ass){
            int rows = values.length;
            int columns = values[0].length;
            for(int i = 0;i < rows;i++)
                for(int j = 0;j <= columns - 4;j++)
                    if(values[i][j] == values[i][j + 1] && values[i][j] == values[i][j + 2] && values[i][j] == values[i][j + 3] && values[i][j] == ass)
                        return true;
            for(int i = 0;i < columns;i++)
                for(int j = 0;j <= rows - 4;j++)
                    if(values[j][i] == values[j + 1][i] && values[j][i] == values[j + 2][i] && values[j][i] == values[j + 3][i] && values[j][i] == ass)
                        return true;
            for(int i = 3;i < rows;i++)
                for(int j = 0;j < columns - 3;j++)
                    if(values[i][j] == values[i - 1][j + 1] && values[i][j] == values[i - 2][j + 2] && values[i][j] == values[i - 3][j + 3] && values[i][j] == ass)
                        return true;
            for(int i = 0;i < rows - 3;i++)
                for(int j = 0;j < columns - 3;j++)
                    if(values[i][j] == values[i + 1][j + 1] && values[i][j] == values[i + 2][j + 2] && values[i][j] == values[i + 3][j + 3] && values[i][j] == ass)
                        return true;
            return false;
        }
        public static void printMap(int[][] values){
            for(int i = 0;i < 6;i++){
                for(int j = 0;j < 7;j++){
                    System.out.print("|");
                    if(values[i][j] == 0)
                        System.out.print(" ");
                    else if(values[i][j] == 1)
                        System.out.print("R");
                    else if(values[i][j] == 2)
                        System.out.print("Y");
                }
                System.out.println("|");
            }
            System.out.println("---------------");
        }
    }
    
    
  • 结果显示:

    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    ---------------
    Drop a red disk at column (0-6):1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | |R| | | | | |
    ---------------
    Drop a yellow disk at column (0-6):2
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | |R|Y| | | | |
    ---------------
    Drop a red disk at column (0-6):1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | |R| | | | | |
    | |R|Y| | | | |
    ---------------
    Drop a yellow disk at column (0-6):2
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | |R|Y| | | | |
    | |R|Y| | | | |
    ---------------
    Drop a red disk at column (0-6):1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | |R| | | | | |
    | |R|Y| | | | |
    | |R|Y| | | | |
    ---------------
    Drop a yellow disk at column (0-6):2
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | |R|Y| | | | |
    | |R|Y| | | | |
    | |R|Y| | | | |
    ---------------
    Drop a red disk at column (0-6):1
    | | | | | | | |
    | | | | | | | |
    | |R| | | | | |
    | |R|Y| | | | |
    | |R|Y| | | | |
    | |R|Y| | | | |
    ---------------
    The red player won
    
    Process finished with exit code 0
    
    
上一篇:2020-12-01


下一篇:RabbitMQ中的cluster、mirrored queue,以及 warrens机制、RAM node、disk node及vhost介绍