Vlad5080

попытка написать крестики нолики

May 1st, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.34 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4.   public class GAME {
  5.     public static void main(String[] args) {
  6.         /*  */
  7.         Random rand = new Random();
  8.         Scanner in = new Scanner(System.in);
  9.  
  10.         System.out.print("Введите имя игрока: ");
  11.         String Name = in.nextLine();
  12.         System.out.println(Name + ", Желаете увидеть правила?\n 1. да\n 2. нет");
  13.         int rules = in.nextInt();
  14.         if (rules == 1){
  15.             System.out.println("ПРАВИЛА");
  16.         }else {
  17.             System.out.println("Желаем вам удачи, " + Name);
  18.         }
  19.  
  20.         int sizemassiv = 3;
  21.         System.out.println("Желаете ли начать первым?)\n 1. да\n 2. нет");
  22.         int priority = in.nextInt();
  23.         if (priority == 2) {
  24.                 //ходит система
  25.                 System.out.println("Игру начинает: СИСТЕМА, ее знак - X.");
  26.  
  27.  
  28.                 String[][] clearbord = clearboard(sizemassiv);
  29.                 System.out.println("Вот ваше поле: ");
  30.  
  31.                 for (int i = 0; i < sizemassiv; i++) {
  32.                     int ch = 0;
  33.                     System.out.print(" ");
  34.                     for (int j = 0; j < sizemassiv; j++) {
  35.                         if (j < sizemassiv - 1) {
  36.                             System.out.print(clearbord[i][j] + "  ");
  37.                         } else {
  38.                             System.out.print(clearbord[i][j]);
  39.                         }
  40.  
  41.                     }
  42.                     System.out.println(" ");
  43.                 }
  44.                 System.out.println("СИСТЕМА делает ход...");
  45.                 System.out.println("СИСТЕМА сделала ход!!");
  46.                 System.out.println("Вот ваше поле: ");
  47.                 String[][] hod1 = hodmachine1(clearbord);
  48.                 for (int i = 0; i < sizemassiv; i++) {
  49.                     int ch = 0;
  50.                     System.out.print(" ");
  51.                     for (int j = 0; j < sizemassiv; j++) {
  52.                         if (j < sizemassiv - 1) {
  53.                             System.out.print(hod1[i][j] + "  ");
  54.                         } else {
  55.                             System.out.print(hod1[i][j]);
  56.                         }
  57.  
  58.                     }
  59.                     System.out.println(" ");
  60.                 }
  61.  
  62.                 boolean win = false;
  63.                 if((hod1[0][0] == hod1[0][1] == hod1[0][2]) ||
  64.                         (hod1[1][0] == hod1[1][1] == hod1[1][2]) ||
  65.                         (hod1[2][0] == hod1[2][1] == hod1[2][2]) ||
  66.                         (hod1[0][0] == hod1[1][0] == hod1[2][0]) ||
  67.                         (hod1[0][1] == hod1[1][1] == hod1[2][1]) ||
  68.                         (hod1[0][2] == hod1[1][2] == hod1[2][2]) ||
  69.                         (hod1[0][0] == hod1[1][1] == hod1[2][2]) ||
  70.                         (hod1[0][2] == hod1[1][1] == hod1[2][0])) {
  71.                 win = true;
  72.             }
  73.                 // ходит система
  74.  
  75.         } else {
  76.                 //ходит игрок
  77.                 System.out.println("Игру начинает: " + Name + ", ваш знак - O.");
  78.                 String[][] clearbord = clearboard(sizemassiv);
  79.                 System.out.println("Вот ваше поле: ");
  80.  
  81.                 for (int i = 0; i < sizemassiv; i++) {
  82.                     int ch = 0;
  83.                     System.out.print(" ");
  84.                     for (int j = 0; j < sizemassiv; j++) {
  85.                         if (j < sizemassiv - 1) {
  86.                             System.out.print(clearbord[i][j] + "  ");
  87.                         } else {
  88.                             System.out.print(clearbord[i][j]);
  89.                         }
  90.  
  91.                     }
  92.                     System.out.println(" ");
  93.                 }
  94.  
  95.  
  96.                 String[][] hod1 = humanhod1(clearbord);
  97.                 System.out.println("Вот ваше поле: ");
  98.  
  99.                 for (int i = 0; i < sizemassiv; i++) {
  100.                     int ch = 0;
  101.                     System.out.print(" ");
  102.                     for (int j = 0; j < sizemassiv; j++) {
  103.                         if (j < sizemassiv - 1) {
  104.                             System.out.print(hod1[i][j] + "  ");
  105.                         } else {
  106.                             System.out.print(hod1[i][j]);
  107.                         }
  108.  
  109.                     }
  110.                     System.out.println(" ");
  111.                 }
  112.  
  113.                 boolean win = false;
  114.             if((hod1[0][0] == hod1[0][1] == hod1[0][2]) ||
  115.                     (hod1[1][0] == hod1[1][1] == hod1[1][2]) ||
  116.                     (hod1[2][0] == hod1[2][1] == hod1[2][2]) ||
  117.                     (hod1[0][0] == hod1[1][0] == hod1[2][0]) ||
  118.                     (hod1[0][1] == hod1[1][1] == hod1[2][1]) ||
  119.                     (hod1[0][2] == hod1[1][2] == hod1[2][2]) ||
  120.                     (hod1[0][0] == hod1[1][1] == hod1[2][2]) ||
  121.                     (hod1[0][2] == hod1[1][1] == hod1[2][0])) {
  122.                 win = true;
  123.             }
  124.                 //ходит игрок
  125.             //////////////////////////////////////////////////////////////////////////////////////////////////////////
  126.             //////////////////////////////////////////////////////////////////////////////////////////////////////////
  127.  
  128.         }
  129.     }
  130.  
  131.  
  132.  
  133.  
  134.     public static String[][] clearboard (int x){
  135.             Scanner in = new Scanner(System.in);
  136.             String[][] a = new String[x][x];
  137.             for (int i = 0; i < x; i++) {
  138.                 for (int j = 0; j < x; j++) {
  139.                     a[i][j] = "-";
  140.                 }
  141.             }
  142.             return a;
  143.       }
  144.       /*
  145.       public static boolean isCellValid(int x, int y){
  146.           boolean result = true;
  147.  
  148.           if (x < 0 || x >= 3 || y < 0 || y >= 3){
  149.               result = false;
  150.           } else if (a[y][x] != "-") {
  151.               result = false;
  152.           }
  153.  
  154.           return result;
  155.       }*/
  156.  
  157.       public static String[][] hodmachine1(String[][] clearbord) {
  158.           Random rand = new Random();
  159.           boolean checkempty = true;
  160.  
  161.           String[][] a = clearbord;
  162.           do {
  163.               int n = rand.nextInt(2);
  164.               int k = rand.nextInt(2);
  165.               if (a[n][k] == "-") {
  166.                   checkempty = true;
  167.                   System.out.println("СИСТЕМА выбрала ячейку: " + (n+1) + " " + (k+ 1));
  168.                   a[n][k] = "X";
  169.               } else if (a[n][k] != "-") {
  170.                   checkempty = false;
  171.               }
  172.           } while (checkempty = false);
  173.           return a;
  174.       }
  175.       public static String[][] humanhod1(String[][] clearbord){
  176.           Scanner in = new Scanner(System.in);
  177.  
  178.  
  179.           boolean checkempty = true;
  180.           String[][] a = clearbord;
  181.           do {
  182.               System.out.print("Выберите ячейку для хода: ");
  183.  
  184.               int n = in.nextInt() - 1;
  185.               int k = in.nextInt() - 1;
  186.               if (a[n][k] == "-") {
  187.                   checkempty = true;
  188.                   a[n][k] = "O";
  189.               } else if (a[n][k] != "-") {
  190.                   checkempty = false;
  191.               }
  192.           } while (checkempty = false);
  193.           return a;
  194.       }
  195.  
  196.   }
Add Comment
Please, Sign In to add comment