Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class GAME {
- public static void main(String[] args) {
- /* */
- Random rand = new Random();
- Scanner in = new Scanner(System.in);
- System.out.print("Введите имя игрока: ");
- String Name = in.nextLine();
- System.out.println(Name + ", Желаете увидеть правила?\n 1. да\n 2. нет");
- int rules = in.nextInt();
- if (rules == 1){
- System.out.println("ПРАВИЛА");
- }else {
- System.out.println("Желаем вам удачи, " + Name);
- }
- int sizemassiv = 3;
- System.out.println("Желаете ли начать первым?)\n 1. да\n 2. нет");
- int priority = in.nextInt();
- if (priority == 2) {
- //ходит система
- System.out.println("Игру начинает: СИСТЕМА, ее знак - X.");
- String[][] clearbord = clearboard(sizemassiv);
- System.out.println("Вот ваше поле: ");
- for (int i = 0; i < sizemassiv; i++) {
- int ch = 0;
- System.out.print(" ");
- for (int j = 0; j < sizemassiv; j++) {
- if (j < sizemassiv - 1) {
- System.out.print(clearbord[i][j] + " ");
- } else {
- System.out.print(clearbord[i][j]);
- }
- }
- System.out.println(" ");
- }
- System.out.println("СИСТЕМА делает ход...");
- System.out.println("СИСТЕМА сделала ход!!");
- System.out.println("Вот ваше поле: ");
- String[][] hod1 = hodmachine1(clearbord);
- for (int i = 0; i < sizemassiv; i++) {
- int ch = 0;
- System.out.print(" ");
- for (int j = 0; j < sizemassiv; j++) {
- if (j < sizemassiv - 1) {
- System.out.print(hod1[i][j] + " ");
- } else {
- System.out.print(hod1[i][j]);
- }
- }
- System.out.println(" ");
- }
- boolean win = false;
- if((hod1[0][0] == hod1[0][1] == hod1[0][2]) ||
- (hod1[1][0] == hod1[1][1] == hod1[1][2]) ||
- (hod1[2][0] == hod1[2][1] == hod1[2][2]) ||
- (hod1[0][0] == hod1[1][0] == hod1[2][0]) ||
- (hod1[0][1] == hod1[1][1] == hod1[2][1]) ||
- (hod1[0][2] == hod1[1][2] == hod1[2][2]) ||
- (hod1[0][0] == hod1[1][1] == hod1[2][2]) ||
- (hod1[0][2] == hod1[1][1] == hod1[2][0])) {
- win = true;
- }
- // ходит система
- } else {
- //ходит игрок
- System.out.println("Игру начинает: " + Name + ", ваш знак - O.");
- String[][] clearbord = clearboard(sizemassiv);
- System.out.println("Вот ваше поле: ");
- for (int i = 0; i < sizemassiv; i++) {
- int ch = 0;
- System.out.print(" ");
- for (int j = 0; j < sizemassiv; j++) {
- if (j < sizemassiv - 1) {
- System.out.print(clearbord[i][j] + " ");
- } else {
- System.out.print(clearbord[i][j]);
- }
- }
- System.out.println(" ");
- }
- String[][] hod1 = humanhod1(clearbord);
- System.out.println("Вот ваше поле: ");
- for (int i = 0; i < sizemassiv; i++) {
- int ch = 0;
- System.out.print(" ");
- for (int j = 0; j < sizemassiv; j++) {
- if (j < sizemassiv - 1) {
- System.out.print(hod1[i][j] + " ");
- } else {
- System.out.print(hod1[i][j]);
- }
- }
- System.out.println(" ");
- }
- boolean win = false;
- if((hod1[0][0] == hod1[0][1] == hod1[0][2]) ||
- (hod1[1][0] == hod1[1][1] == hod1[1][2]) ||
- (hod1[2][0] == hod1[2][1] == hod1[2][2]) ||
- (hod1[0][0] == hod1[1][0] == hod1[2][0]) ||
- (hod1[0][1] == hod1[1][1] == hod1[2][1]) ||
- (hod1[0][2] == hod1[1][2] == hod1[2][2]) ||
- (hod1[0][0] == hod1[1][1] == hod1[2][2]) ||
- (hod1[0][2] == hod1[1][1] == hod1[2][0])) {
- win = true;
- }
- //ходит игрок
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- }
- }
- public static String[][] clearboard (int x){
- Scanner in = new Scanner(System.in);
- String[][] a = new String[x][x];
- for (int i = 0; i < x; i++) {
- for (int j = 0; j < x; j++) {
- a[i][j] = "-";
- }
- }
- return a;
- }
- /*
- public static boolean isCellValid(int x, int y){
- boolean result = true;
- if (x < 0 || x >= 3 || y < 0 || y >= 3){
- result = false;
- } else if (a[y][x] != "-") {
- result = false;
- }
- return result;
- }*/
- public static String[][] hodmachine1(String[][] clearbord) {
- Random rand = new Random();
- boolean checkempty = true;
- String[][] a = clearbord;
- do {
- int n = rand.nextInt(2);
- int k = rand.nextInt(2);
- if (a[n][k] == "-") {
- checkempty = true;
- System.out.println("СИСТЕМА выбрала ячейку: " + (n+1) + " " + (k+ 1));
- a[n][k] = "X";
- } else if (a[n][k] != "-") {
- checkempty = false;
- }
- } while (checkempty = false);
- return a;
- }
- public static String[][] humanhod1(String[][] clearbord){
- Scanner in = new Scanner(System.in);
- boolean checkempty = true;
- String[][] a = clearbord;
- do {
- System.out.print("Выберите ячейку для хода: ");
- int n = in.nextInt() - 1;
- int k = in.nextInt() - 1;
- if (a[n][k] == "-") {
- checkempty = true;
- a[n][k] = "O";
- } else if (a[n][k] != "-") {
- checkempty = false;
- }
- } while (checkempty = false);
- return a;
- }
- }
Add Comment
Please, Sign In to add comment