Zeshin

OOP_06

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