daily pastebin goal
69%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Player {
  2.     private String coordinate;
  3.     private String name;
  4.  
  5.  
  6.     public Player(String coordinate, String name ) {
  7.         this.coordinate = coordinate;
  8.         this.name = name;
  9.  
  10.     }
  11.  
  12.     public String getCoordinate() {
  13.         return this.coordinate;
  14.     }
  15.  
  16.     public String getName() {
  17.         return this.name;
  18.     }
  19. }
  20.    
  21. import java.util.Scanner;
  22. import java.util.Set;
  23. import java.util.HashSet;
  24. import java.util.Hashtable;
  25.  
  26. public class TicTacToe {
  27.  
  28.     public static Hashtable<String,String> coordinates = new Hashtable<String, String>() {
  29.         {
  30.             put("X", "notselected");
  31.             put("Y", "notselected");
  32.         }
  33.     };
  34.  
  35.     public static Scanner input = new Scanner(System.in);
  36.  
  37.     public static String[][] matrix = new String[][]{
  38.         { "O", "O", "O" },
  39.         { "O", "O", "O"},
  40.         { "O", "O", "O" },
  41.     };
  42.  
  43.     public static void printMatrix() {
  44.         System.out.println("This is how the matrix looks like at the moment:n");
  45.         for(int i = 0 ; i < matrix.length; i ++ ) {
  46.             for(int j = 0; j < matrix.length; j++ ) {
  47.                 System.out.print(matrix[i][j] + " " );
  48.             }
  49.             System.out.println();
  50.         }
  51.         System.out.println();
  52.     }
  53.  
  54.     public static Player getPlayerInfo() throws InterruptedException {
  55.         Thread.sleep(1000);
  56.         System.out.println("Player name:");
  57.         String playerName = input.next();
  58.  
  59.         if ( coordinates.get("X").equals("selected")) {
  60.             System.out.println("Y is the only coordinate being left.");
  61.             return new Player("Y", playerName);
  62.         } else if ( coordinates.get("Y").equals("selected")) {
  63.             System.out.println("X is the only coordinate being left.");
  64.             return new Player("X", playerName);
  65.         } else {
  66.             System.out.println("Enter your coordinate:");
  67.             String coordinate = input.next();
  68.             coordinates.put(coordinate, "selected");
  69.             return new Player(coordinate, playerName);
  70.         }
  71.     }
  72.  
  73.     public static boolean hasAPlayerWon(Player player ) {
  74.         if (isRowSolved(player.getCoordinate())) {
  75.             System.out.println("Row is solved");
  76.             return true;
  77.         } else if (isColumnSolved(player.getCoordinate())) {
  78.             System.out.println("Column is solved");
  79.             return true;
  80.         } else if (isLeftDiagonalSolved(player.getCoordinate())) {
  81.             System.out.println("Diagonal is solved");
  82.             return true;
  83.         } else if (isRightDiagonalSolved(player.getCoordinate())) {
  84.             System.out.println("Right diagona is solved");
  85.             return true;
  86.         }
  87.  
  88.  
  89.         return false;
  90.     }
  91.  
  92.     private static boolean isRowSolved(String coordinate ) {
  93.         for(int i = 0; i < matrix.length; i ++) {
  94.             Set<String> elements = new HashSet<String>();
  95.  
  96.             for(int j = 0; j < matrix[i].length; j++) {
  97.                 elements.add(matrix[i][j]);
  98.             }
  99.  
  100.             if (elements.contains(coordinate) && elements.size() == 1) {
  101.                 return true;
  102.             }
  103.  
  104.         }
  105.  
  106.         return false;
  107.     }
  108.  
  109.     private static boolean isColumnSolved(String coordinate) {
  110.         for(int i = 0; i < matrix.length; i ++) {
  111.             Set<String> elements = new HashSet<String>();
  112.  
  113.             for(int j = 0; j < matrix[i].length; j++) {
  114.                 elements.add(matrix[j][i]);
  115.             }
  116.  
  117.             if (elements.contains(coordinate) && elements.size() == 1) {
  118.                 return true;
  119.             }
  120.  
  121.         }
  122.  
  123.         return false;
  124.     }
  125.  
  126.     private static boolean isLeftDiagonalSolved(String coordinate) {
  127.         int leftDiagonalIndex = 0;
  128.  
  129.         HashSet<String> leftDiagonalElements = new HashSet<String>();
  130.  
  131.         while(leftDiagonalIndex < matrix.length) {
  132.             leftDiagonalElements.add(matrix[leftDiagonalIndex][leftDiagonalIndex]);
  133.             leftDiagonalIndex += 1;
  134.  
  135.         }
  136.  
  137.         if(leftDiagonalElements.size() == 1 && matrix[leftDiagonalIndex-1][leftDiagonalIndex-1].equals(coordinate)) {
  138.             return true;
  139.         }  
  140.  
  141.         return false;
  142.     }
  143.  
  144.     private static boolean isRightDiagonalSolved(String coordinate) {
  145.         int rightDiagonalIndexRow = 0;
  146.         int rightDiagonalIndexColumn = 2;
  147.  
  148.         HashSet<String> rightDiagonalElements = new HashSet<String>();
  149.  
  150.         while(rightDiagonalIndexRow < matrix.length) {
  151.             rightDiagonalElements.add(matrix[rightDiagonalIndexRow][rightDiagonalIndexColumn]);
  152.             rightDiagonalIndexRow += 1;
  153.             rightDiagonalIndexColumn -= 1;
  154.  
  155.         }
  156.  
  157.         if(rightDiagonalElements.size() == 1 && matrix[rightDiagonalIndexRow-1][rightDiagonalIndexColumn+1].equals(coordinate)) {
  158.             return true;
  159.         }  
  160.  
  161.         return false;
  162.     }
  163.  
  164.     public static boolean isMatrixUnsolvable() {
  165.         for(int i = 0; i < matrix.length; i++) {
  166.             for(int j = 0; j < matrix[0].length; j++) {
  167.                 if(matrix[i][j].equals("O")) {
  168.                     return false;
  169.                 }
  170.             }
  171.         }
  172.  
  173.         return true;
  174.     }
  175.  
  176.     public static void evaluateInput(Player player) throws InterruptedException {
  177.  
  178.         Thread.sleep(1000);
  179.         System.out.println(player.getName() + " enter your indexes, seperated by comma:");
  180.         String[] indexes = input.next().split(",");
  181.  
  182.         int row = Integer.valueOf(indexes[0]);
  183.         int column = Integer.valueOf(indexes[1]);
  184.  
  185.         if(!matrix[row][column].equals("O")) {
  186.             System.out.println("Sorry, you are missing your turn.");
  187.             System.out.println("Either you already marked the square or you are tring to override a value");
  188.             return;
  189.         }
  190.  
  191.         matrix[row][column] = player.getCoordinate();
  192.  
  193.         if(hasAPlayerWon(player )) {
  194.             System.out.println("Congratulations " + player.getName() + ", you WON!");
  195.             System.exit(0);
  196.         }
  197.  
  198.         if(isMatrixUnsolvable( )) {
  199.             System.out.println("Matrix can't be resolved! Thank you for playing");
  200.             System.exit(0);
  201.         }
  202.  
  203.         printMatrix();
  204.     }
  205.     public static void main(String[] args) throws InterruptedException {
  206.         printMatrix();
  207.  
  208.         Player firstPlayer = getPlayerInfo();
  209.         Player secondPlayer = getPlayerInfo();
  210.  
  211.         while (true) {
  212.             evaluateInput(firstPlayer);
  213.             evaluateInput(secondPlayer);
  214.         }
  215.     }
  216. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top