Advertisement
Boyan5

oop6

Jun 24th, 2021
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. import java.util.Scanner;
  2. public  class zad6 {
  3.     public static void main(String[] args) {
  4.         Scanner scan = new Scanner(System.in);
  5.         String[][] matrix = new String[3][3];
  6.         for (int i = 0; i < matrix.length; i++) {
  7.             String input = scan.nextLine();
  8.             matrix[i] = input.split(" ");
  9.         }
  10.         if (checkWinner(matrix).equals("No winner")) {
  11.             System.out.println("There is no winner");
  12.         } else {
  13.             System.out.println("The winner is: " + checkWinner(matrix));
  14.         }
  15.     }
  16.     public static String checkWinner(String[][] matrix) {
  17.         String line = "";
  18.         for (int i = 0; i < 8; i++) {
  19.             switch (i) {
  20.                 case 0:
  21.                     line = matrix[0][0] + matrix[0][1] + matrix[0][2];
  22.                     break;
  23.                 case 1:
  24.                     line = matrix[1][0] + matrix[1][1] + matrix[1][2];
  25.                     break;
  26.                 case 2:
  27.                     line = matrix[2][0] + matrix[2][1] + matrix[2][2];
  28.                     break;
  29.                 case 3:
  30.                     line = matrix[0][0] + matrix[1][0] + matrix[2][0];
  31.                     break;
  32.                 case 4:
  33.                     line = matrix[0][1] + matrix[1][1] + matrix[2][1];
  34.                     break;
  35.                 case 5:
  36.                     line = matrix[0][2] + matrix[1][2] + matrix[2][2];
  37.                     break;
  38.                 case 6:
  39.                     line = matrix[0][0] + matrix[1][1] + matrix[2][2];
  40.                     break;
  41.                 case 7:
  42.                     line = matrix[0][2] + matrix[1][1] + matrix[2][0];
  43.                     break;
  44.             }
  45.             if (line.equals("XXX")) {
  46.                 return "X";
  47.             }
  48.             if (line.equals("OOO")) {
  49.                 return "O";
  50.             }
  51.         }
  52.         return "No winner";
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement