Java Edition Easy Monopoly

//game class:
package com.fs.DaFuWen;

import java.util.*;

public class Game {
    // Declare the map
    Map map;
    //declare the current position of player 1 in the battle
    int playerPos1;
    //declare the current position of player 2 in the match
    int playerPos2;
    // Declare go or stop flag setting
    String[] goAndStop = new String[2];
    //Declare the battle role
    String[] playerName = new String[4];

    /**
     * Initialize a round of the game
     */
    public void init() {
        //Create a Map object
        map = new Map();
        //Generate the map
        map. createMap();
        //Set the starting position of player 1
        playerPos1 = 0;
        //set player 2 start position
        playerPos2 = 0;
        //Record player 1's next walk or stop
        goAndStop[0] = "on";
        //Set player 2 to go or stop next time
        goAndStop[1] = "on";
    }


    /**
     * start the game
     */
    public void start() {
        // call initialization method
        init();
        //Display the game interface
        System.out.println("~~~~~~two-player battle~~~~~~");
        System.out.println();
        System.out.println();
        //role settings
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please select a character: 1, Mrs. Qian 2, Daddy 3, Atubo 4, Beige");
        for (int no = 1; no < 3; no ++ ) {
            System.out.println("Please player" + no + "Enter a number to select a character:");
            int role = scanner. nextInt();
            setRole(no, role);
        }
        // start the game
        play();

    }

    /**
     * Set the battle role
     *
     * @param no player order 1: player 1 2: player 2
     * @param role role code
     */
    public void setRole(int no, int role) {
        switch (role) {
            case 1:
                playerName[no - 1] = "Madam Qian";
                break;
            case 2:
                //Set the player's name to "Big Brother"
                playerName[no - 1] = "Trickety";
                break;

            case 3:
                //Set the player name to "Atubo"
                playerName[no - 1] = "Atubo";
                break;
            case 4:
                //Set the player's name to "Bego"
                playerName[no - 1] = "Bego";
                break;
            default:
                break;
        }
    }


    /**
     * Two-player game
     */
    public void play() {
        System.out.println("\
\
\
\
");

        System.out.print("\
\
**************************************** *************\
");
        System.out.print(" Game Start \
");
        System.out.print("******************************************** **********\
\
");

        //Display the styles of soldiers on both sides
        System.out.println("^_^" + playerName[0] + ", your code name is: A");
        System.out.println("^_^" + playerName[1] + ", your code name is: B\
");

        //Display the battle map
        System.out.println("\
Legend: " + " Pause ¤ Lucky Wheel  Landmine 〓 Time Tunnel ∷ Normal\
");

        map.showMap(playerPos1, playerPos2);


        //Games start
        int step; // store the number of dice
        while (playerPos1 < 99 & amp; & amp; playerPos2 < 99) { //If any party reaches the end, jump out of the loop

            // take turns rolling the dice
            if (goAndStop[0].equals("on")) {
                //Player 1 rolls the dice
                step = throwShifter(1); // throw the dice
                System.out.println("\
--------------"); //Display the result information
                System.out.println("Dice number: " + step);
                playerPos1 = getCurPos(1, playerPos1, step); // Calculate the current position after this move
                System.out.println(playerName[0] + "\
Your current position: " + (playerPos1 + 1));
                System.out.println(playerName[1] + "current position:" + (playerPos2 + 1));
                System.out.println("-----------------\
");
                map.showMap(playerPos1, playerPos2); //Display the current map
                if (playerPos1 == 99) { //If you reach the end
                    break; //Exit
                }
            } else {
                System.out.println("\
" + playerName[0] + "Pause once!\
"); //Display the pause information
                goAndStop[0] = "on"; //Set the next throwable state
            }


            System.out.println("\
\
\
\
");

            if (goAndStop[1].equals("on")) {
                //Player 2 rolls the dice
                step = throwShifter(2); // throw the dice
                System.out.println("\
--------------"); //Display the result information
                System.out.println("Dice number: " + step);
                playerPos2 = getCurPos(2, playerPos2, step); // Calculate the current position after this move
                System.out.println(playerName[1] + "\
Your current position: " + (playerPos2 + 1));
                System.out.println(playerName[0] + "current position:" + (playerPos1 + 1));
                System.out.println("-----------------\
");
                map.showMap(playerPos1, playerPos2);
                if (playerPos2 == 99) { //If you reach the end
                    break; //exit
                }
            } else {
                System.out.println("\
" + playerName[1] + "Pause once!\
"); //Display the pause information
                goAndStop[1] = "on"; //Set the next throwable state
            }

            System.out.println("\
\
\
\
");
        }

        //game over
        System.out.println("\
\
\
\
");
        System.out.print("******************************************** **********\
");
        System.out.print(" Game Over \
");
        System.out.print("******************************************** **********\
\
");
        judge();
    }


    /**
     * roll the dice
     *
     * @param no player order
     * @return step The number of dice rolled
     */
    public int throwShifter(int no) {
        //Define variables to store the number of dice
        int step = 0;

        // Prompt the player to start rolling the dice
        Scanner sc = new Scanner(System.in);
        Random rd = new Random();
        System.out.println();
        System.out.println(playerName[no - 1] + "Please press any letter to start rolling the dice");
        sc. nextLine();

        //Simulate rolling dice: generate a number from 1 to 6 as the number of dice thrown by the player
        step = rd.nextInt(6) + 1;
        return step;
    }


    /**
     * Calculate the current position of the player after this move
     *
     * @param no player order
     * @param position the position before moving
     * @param step number of dice rolled
     * @return position the moved position
     */
    public int getCurPos(int no, int position, int step) {
        position = position + step; //The position after the first move
        if (position >= 99) {
            return 99;
        }
        Scanner input = new Scanner(System.in);
        switch (map.map[position]) { //judgment based on the level code in the map
            case 0: //go to normal grid
                if (position == playerPos2) { // Add condition: Player 1 meets the opponent's cavalry
                    //Add code to achieve: step on the opponent, the opponent returns to the starting point
                    playerPos2 = 0;
                    System.out.println(":-D hahahahaha...step on it!");
                }
                if (position == playerPos1) { //Add condition: Player 2 meets the opponent's cavalry
                    //Add code to achieve: step on the opponent, the opponent returns to the starting point
                    playerPos1 = 0;
                    System.out.println(":-D hahahahaha...step on it!");
                }
                break;
            case 1: //lucky roulette
                System.out.println("\
◆◇◆◇◆Welcome to Lucky Roulette◆◇◆◇◆");
                System.out.println("Please choose a luck:");
                System.out.println("1. Swap position 2. Bomb");
                System.out.println("================================\
");
                int choice = input. nextInt();
                int temp; //Temporary variable during exchange
                switch (choice) {
                    case 1: //exchange position
                        if (no == 1) {
                            //Add code to achieve exchange: position and playerPos2 value exchange
                            temp = position;
                            position = playerPos2;
                            playerPos2 = temp;

                        } else if (no == 2) {
                            //Add code to achieve exchange: position and playPos1 value exchange
                            temp = position;
                            position = playerPos1;
                            playerPos1 = temp;
                        }
                        break;
                    case 2: //bombing
                        if (no == 1 & amp; & amp; playerPos2 < 6) { //no is 1 and player 2 position is less than 6
                            //Add code to achieve: calculate the current position of player 2
                            playerPos2 = 0;
                            break;
                        } else {
                            //Add code to achieve: calculate the current position of player 2
                            playerPos2 = 0;
                        }
                        if (no == 2 & amp; & amp; playerPos1 < 6) { //no is 2 and player 1 position is less than 6
                            //Add code to achieve: calculate the current position of player 1
                            playerPos1 = 0;
                            break;
                        } else {
                            //Add code to achieve: calculate the current position of player 1
                            playerPos1 = 0;
                        }
                        break;
                }
                break;
            case 2: //step on a mine
                //Add code to achieve: 6 steps back when stepping on a landmine
                position = position - 6;
                System.out.println("~:-( " + "Stepped on a landmine, so mad...");
                break;
            case 3: // Pause once next time
                //Add code to achieve: set the next pause to roll the dice
                goAndStop[no - 1] = "step";
                System.out.println("~~>_<~~ It's time for a truce.");
                break;
            case 4: //Time-space tunnel
                //Add code to achieve: enter the space-time tunnel, add 10 steps
                position = position + 10;
                System.out.println("|-P " + "Entering the space-time tunnel, really cool!");
                break;
        }

        //Return the coordinates of the player's position after rolling the dice
        if (position < 0) {
            return 0;
        } else if (position > 99) {
            return 99;
        } else {
            return position;
        }
    }

    /**
     * Display the result of the battle
     */
    public void judge() {
        //add code
        if (playerPos1==99){
            System.out.println("gameOver! The winner is " + playerName[0]);
        }
        if (playerPos2==99){
            System.out.println("gameOver! The winner is" + playerName[1]);
        }



    }
}
//Map creation, and current location acquisition class:
package com.fs.DaFuWen;


public class Map {
    int[] map = new int[100]; // battle map
    int[] luckyTurn = {6, 23, 40, 55, 69, 83}; // Lucky roulette ¤
    int[] landMine = {5, 13, 17, 33, 38, 50, 64, 80, 94}; //landmine position 
    int[] pause = {9, 27, 60, 93}; // Pause 
    int[] timeTunnel = {20, 25, 45, 63, 72, 88, 90}; //time-space tunnel 〓


    /**
     * Generate map:
     * Level codes are: 1: Lucky Roulette 2: Landmines 3: Pause 4: Space-Time Tunnel 0: Normal ∷
     */
    public void createMap() {
        int i = 0;

        //Set the lucky roulette on the battle map
        for (i = 0; i < luckyTurn. length; i ++ ) {
            map[luckyTurn[i]] = 1;
        }
        //Add code to set mines on the battle map
        for (i = 0; i < landMine. length; i ++ ) {
            map[landMine[i]] = 2;
        }
        //Add code to set pause on the battle map
        for (i = 0; i < pause. length; i ++ ) {
            map[pause[i]] = 3;
        }
        //Add code to set up a space-time tunnel on the battle map
        for (i = 0; i < timeTunnel. length; i ++ ) {
            map[timeTunnel[i]] = 4;
        }
    }

    /**
     * Display the graphics corresponding to the map level
     *
     * @param i The level code of the current location of the map
     * @param index current map location number
     * @param playerPos1 the current position of player 1
     * @param playerPos2 current position of player 2
     * @return The image corresponding to the current location of the map
     * The display rules are as follows: @@ When two people overlap
     * A player 1
     * B Player 2
     * ¤ Wheel of Fortune
     *  mines
     *  pause
     * 〓 Space-time tunnel
     * ∷ ordinary case
     * Implementation logic: first judge whether playerPos1, playerPos2 and index are at the same position, if so, overlap and display @@
     * If not, judge whether playerPos1 coincides with index, if yes, display A
     * If not, judge whether playerPos2 coincides with index, if yes, display B
     * If not, judge what the value is and display the corresponding level graphics.
     */
    public String getGraph(int i, int index, int playerPos1, int playerPos2) {
        String graph = "";

        //add code
        if (playerPos1 == index & amp; & amp; playerPos2 == index) {
            graph = "@@";
        } else if (playerPos1 == index) {
            graph = "A";
        } else if (playerPos2 == index) {
            graph = "B";
        } else {
            for (int j = 0; j < map. length; j ++ ) {
                //i = map[j];
                switch (i) {
                    case 1:
                        graph = "¤";
                        break;
                    case 2:
                        graph = "";
                        break;
                    case 3:
                        graph = "";
                        break;
                    case 4:
                        graph = "〓";
                        break;
                    default:
                        graph = "::";
                        break;

                }
            }
        }


        return graph;
    }


    /**
     * Output the even-numbered lines of the map (line 2)
     *
     * @param start The position of the output starting point on the map
     * @param end The position of the output end point on the map
     * @param playerPos1 the current position of player 1
     * @param playerPos2 current position of player 2
     */
    public void showLine2(int start, int end, int playerPos1, int playerPos2) {
        for (int i = end - 1; i >= start; i--) {
            System.out.print(getGraph(map[i], i, playerPos1, playerPos2));
        }
    }

    /**
     * Output the right vertical column of the map
     *
     * @param start The position of the output starting point on the map
     * @param end The position of the output end point on the map
     * @param playerPos1 the current position of player 1
     * @param playerPos2 current position of player 2
     */
    public void showRLine(int start, int end, int playerPos1, int playerPos2) {
        for (int i = start; i < end; i ++ ) {
            for (int j = 28; j > 0; j--) { // output 29 spaces
                System.out.print(" ");
            }
            System.out.print(getGraph(map[i], i, playerPos1, playerPos2));
            System.out.println();
        }
    }


    /**
     * Odd-numbered lines of the output map (lines 1 and 3)
     *
     * @param start The position of the output starting point on the map
     * @param end The position of the output end point on the map
     * @param playerPos1 the current position of player 1
     * @param playerPos2 current position of player 2
     */
    public void showLine1(int start, int end, int playerPos1, int playerPos2) {
        //add code
        for (int i = start; i < end; i + + ) {
            System.out.print(getGraph(map[i], i, playerPos1, playerPos2));
        }

    }


    /**
     * Output the left column of the map
     *
     * @param start The position of the output starting point on the map
     * @param end The position of the output end point on the map
     * @param playerPos1 the current position of player 1
     * @param playerPos2 current position of player 2
     */
    public void showLLine(int start, int end, int playerPos1, int playerPos2) {
        //add code
        for (int i = start; i < end; i ++ ) {
            System.out.println(getGraph(map[i], i, playerPos1, playerPos2));
        }
    }

    /**
     * Show battle map
     *
     * @param playerPos1 the current position of player 1
     * @param playerPos2 current position of player 2
     */
    public void showMap(int playerPos1, int playerPos2) {
        //display the first line of the map
        showLine1(0,30,playerPos1,playerPos2);
        //new line
        System.out.println();
        //Display the right vertical row of the map
        showRLine(30,35,playerPos1,playerPos2);
        //Display the second line of the map
        showLine2(35,65,playerPos1,playerPos2);
        //new line
        System.out.println();
        //Display the left vertical line of the map
        showLLine(65,70,playerPos1,playerPos2);
        //Display the 3rd line of the map
        showLine1(70,100,playerPos1,playerPos2);
    }
}
//Test class:
package com.fs.DaFuWen;

public class Test {
    public static void main(String[] args) {

        Game game = new Game();
        game.init();
        game. start();
        game.play();
    }
}

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge Java skill treeHomepageOverview 125204 people are studying systematically