Advertisement
alxrWagner

Game

Dec 9th, 2022
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. import java.util.Formatter;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         Scanner read = new Scanner(System.in);
  8.  
  9.         game(read, 5, 5);
  10.     }
  11.  
  12.     public static StringBuilder drawField(char emptySector, char target, int colSize, int rowSize) {
  13.         StringBuilder targetCoords = new StringBuilder();
  14.         Formatter f = new Formatter();
  15.  
  16.         int targetX = new Random().nextInt(rowSize);
  17.         int targetY = new Random().nextInt(colSize);
  18.  
  19.         for (int i = 0; i < colSize; i++) {
  20.             for (int j = 0; j < rowSize; j++) {
  21.                 if (targetX == j && targetY == i) {
  22.                     System.out.print(target + " ");
  23.                 } else {
  24.                     System.out.print(emptySector + " ");
  25.                 }
  26.             }
  27.             System.out.println();
  28.         }
  29.         return targetCoords.append(f.format("%02d %02d", targetX, targetY));
  30.     }
  31.  
  32.     public static StringBuilder shot(Scanner scanner) {
  33.         Formatter f = new Formatter();
  34.         StringBuilder shotCoords = new StringBuilder();
  35.         System.out.println("Введите координаты выстрела: ");
  36.         int shotX = scanner.nextInt();
  37.         int shotY = scanner.nextInt();
  38.  
  39.         return shotCoords.append(f.format("%02d %02d", shotX, shotY));
  40.     }
  41.  
  42.     public static StringBuilder botShot(int colSize, int rowSize) {
  43.         Formatter f = new Formatter();
  44.  
  45.         StringBuilder shotCoords = new StringBuilder();
  46.         int shotX = new Random().nextInt(rowSize);
  47.         int shotY = new Random().nextInt(colSize);
  48.  
  49.         return shotCoords.append(f.format("%02d %02d", shotX, shotY));
  50.     }
  51.  
  52.     public static boolean checkShot(StringBuilder targetCoords, StringBuilder shot, String attacker) {
  53.  
  54.         System.out.printf("%S выстрелил по координатам %S%n", attacker, shot);
  55.         boolean isGameOver = false;
  56.         if (targetCoords.toString().equals(shot.toString())) {
  57.             System.out.printf("%S выиграл%n", attacker);
  58.  
  59.             isGameOver = true;
  60.         } else {
  61.             System.out.printf("%S не попал%n", attacker);
  62.         }
  63.         return isGameOver;
  64.     }
  65.  
  66.     public static void game(Scanner scanner, int colSize, int rowSize) {
  67.         StringBuilder targetCoords = new StringBuilder(drawField('*', '+', 5, 5));
  68.  
  69.         while (true) {
  70.             StringBuilder playerShot = shot(scanner);
  71.             StringBuilder botShot = botShot(colSize, rowSize);
  72.  
  73.             if (checkShot(targetCoords, playerShot, "Игрок")) {
  74.                 break;
  75.             }
  76.             if(checkShot(targetCoords, botShot, "Робот")){
  77.                 break;
  78.             }
  79.  
  80.         }
  81.         scanner.close();
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement