Answer the question
In order to leave comments, you need to log in
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]);
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
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]);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question