V
V
Vladimir2018-03-06 21:14:11
Java
Vladimir, 2018-03-06 21:14:11

How to organize the given piece of code into a method?

Good evening everyone! Guys, I really apologize for my Hindu code. Unfortunately, I do not have a developer education and there is no one to ask corny.
I am developing an extremely simple version of a sapper. The goal is to find 2 bombs on 8 cells.
Each time after a successful step, there is a need to redraw the field.
I do this with the code below:

int[] pole = {0,1,2,3,4};
        String[] poleOne = {"A","X","X","X","X"};
        int [] poleOneInt = {0, 0, 0, 0, 0};
        String[] poleTwo = {"B","X","X","X","X"};
        int [] poleTwoInt = {0, 0, 0, 0, 0};
        System.out.println(pole[0] + "  " + pole[1] + "  " + pole[2] + "  " + pole [3] + "  " + pole[4]);
        System.out.println(poleOne[0] + "  " + poleOne[1] + "  " + poleOne[2] + "  " + poleOne[3] + "  " + poleOne[4]);
        System.out.println(poleTwo[0] + "  " + poleTwo[1] + "  " + poleTwo[2] + "  " + poleTwo[3] + "  " + poleTwo[4]);

From the condition it is logical that I will need to display this code 7 times (5 times with changes. If there is no bomb on the selected cell, I change the symbol "X" to " ").
The problem is that I can't organize it into a method. I did everything according to video tutorials and articles, but no matter how I changed it, I get an error. I would be very grateful if you could tell me how to do this. (I will also be happy with advice on how to simplify the program).
Full program code:
package com.company;
import java.lang.invoke.SwitchPoint;
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

        int[] pole = {0,1,2,3,4};
        String[] poleOne = {"A","X","X","X","X"};
        int [] poleOneInt = {0, 0, 0, 0, 0};
        String[] poleTwo = {"B","X","X","X","X"};
        int [] poleTwoInt = {0, 0, 0, 0, 0};
        System.out.println(pole[0] + "  " + pole[1] + "  " + pole[2] + "  " + pole [3] + "  " + pole[4]);
        System.out.println(poleOne[0] + "  " + poleOne[1] + "  " + poleOne[2] + "  " + poleOne[3] + "  " + poleOne[4]);
        System.out.println(poleTwo[0] + "  " + poleTwo[1] + "  " + poleTwo[2] + "  " + poleTwo[3] + "  " + poleTwo[4]);

        Random rbombOne = new Random();
        int bombOne = rbombOne.nextInt(4)+1;
        poleOneInt [bombOne] = bombOne;
        //проверка где бомба, для отладки
        System.out.println("бомба в " + bombOne);

        Random rbombTwo = new Random();
        int bombTwo = rbombTwo.nextInt(4)+1;
        poleTwoInt [bombTwo] = bombTwo;
        //проверка где бомба, для отладки
        System.out.println("бомба в " + bombTwo);

        System.out.println("Введите строку в формате А или B: ");

        Scanner sPole = new Scanner(System.in);
        String scanLetter = sPole.next();

        switch (scanLetter) {
            case "A":
                System.out.println("Введите номер столбца от 1 до 5: ");
                Scanner sNum = new Scanner(System.in);
                int scanNum = sNum.nextInt();
                if (poleOneInt[scanNum] > 0 ){
                   System.out.println("Booooooom! You Lose");}
                   poleOne[scanNum] = " ";
                    break;
            case "B":
                System.out.println("Введите номер столбца от 1 до 5: ");
                Scanner s2Num = new Scanner(System.in);
                int scan2Num = s2Num.nextInt();
                if (poleTwoInt[scan2Num] > 0 ){
                    System.out.println("Booooooom! You Lose");}
                    poleTwo[scan2Num] = " ";
                    break;
                }

        System.out.println(pole[0] + "  " + pole[1] + "  " + pole[2] + "  " + pole [3] + "  " + pole[4]);
        System.out.println(poleOne[0] + "  " + poleOne[1] + "  " + poleOne[2] + "  " + poleOne[3] + "  " + poleOne[4]);
        System.out.println(poleTwo[0] + "  " + poleTwo[1] + "  " + poleTwo[2] + "  " + poleTwo[3] + "  " + poleTwo[4]);

        }

    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2018-03-07
@Geoff

Looking at the source code, it is clear that the game lasts exactly one cycle and if the player does not stumble upon a bomb, there is no way to continue the game. If I understood everything correctly, then the current code can be modified like this:

int[] pole = {0,1,2,3,4};
        String[] poleOne = {"A","X","X","X","X"};
        int [] poleOneInt = {0, 0, 0, 0, 0};
        String[] poleTwo = {"B","X","X","X","X"};
        int [] poleTwoInt = {0, 0, 0, 0, 0};
        System.out.println(pole[0] + "  " + pole[1] + "  " + pole[2] + "  " + pole [3] + "  " + pole[4]);
        System.out.println(poleOne[0] + "  " + poleOne[1] + "  " + poleOne[2] + "  " + poleOne[3] + "  " + poleOne[4]);
        System.out.println(poleTwo[0] + "  " + poleTwo[1] + "  " + poleTwo[2] + "  " + poleTwo[3] + "  " + poleTwo[4]);

        Random rbombOne = new Random();
        int bombOne = rbombOne.nextInt(4)+1;
        poleOneInt [bombOne] = bombOne;
        //проверка где бомба, для отладки
        System.out.println("бомба в " + bombOne);

        Random rbombTwo = new Random();
        int bombTwo = rbombTwo.nextInt(4)+1;
        poleTwoInt [bombTwo] = bombTwo;
        //проверка где бомба, для отладки
        System.out.println("бомба в " + bombTwo);

        boolean runs = true;
        while(running) {
            System.out.println("Введите строку в формате А или B: ");

            Scanner sPole = new Scanner(System.in);
            String scanLetter = sPole.next();

            switch (scanLetter) {
                case "A":
                    System.out.println("Введите номер столбца от 1 до 5: ");
                    Scanner sNum = new Scanner(System.in);
                    int scanNum = sNum.nextInt();
                    if (poleOneInt[scanNum] > 0 ){
                        System.out.println("Booooooom! You Lose");
                        runs = false;
                    }
                    poleOne[scanNum] = " ";
                    break;
                case "B":
                    System.out.println("Введите номер столбца от 1 до 5: ");
                    Scanner s2Num = new Scanner(System.in);
                    int scan2Num = s2Num.nextInt();
                    if (poleTwoInt[scan2Num] > 0 ){
                        System.out.println("Booooooom! You Lose");
                        runs = false;
                    }
                    poleTwo[scan2Num] = " ";
                    break;
                default: break;
            }

            System.out.println(pole[0] + "  " + pole[1] + "  " + pole[2] + "  " + pole [3] + "  " + pole[4]);
            System.out.println(poleOne[0] + "  " + poleOne[1] + "  " + poleOne[2] + "  " + poleOne[3] + "  " + poleOne[4]);
            System.out.println(poleTwo[0] + "  " + poleTwo[1] + "  " + poleTwo[2] + "  " + poleTwo[3] + "  " + poleTwo[4]);

        }

If you look closely, we wrap the entire gameplay in a while loop , which will stop after the variable runs becomes false . In this particular case, runs will evaluate to false if the player enters the coordinates of the bomb field. This code is not optimal, and does not pretend to be the only possible solution. And it does not provide for the option when the player will reveal all the fields without bombs, i.e. will win. I will leave this to your discretion.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question