remote87

Tic Tac Toe First Try

Nov 24th, 2020
684
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static String[] lineOne = {"1", "2", "3"};
  7.     public static String[] lineTwo = {"4", "5", "6"};
  8.     public static String[] lineThree = {"7", "8", "9"};
  9.  
  10.     public static String plOnePick = "";
  11.     public static String plTwoPick = "";
  12.  
  13.     public static Scanner sc = new Scanner(System.in);
  14.  
  15.     public static void main(String[] args) {
  16.  
  17.         printTable(lineOne, lineTwo, lineThree);
  18.  
  19.         //rotation 1
  20.         playerOne();
  21.         playerTwo();
  22.  
  23.         //rotation 2
  24.         playerOne();
  25.         playerTwo();
  26.  
  27.         //rotation 3
  28.         playerOne();
  29.         playerTwo();
  30.  
  31.         //rotation 4
  32.         playerOne();
  33.         playerTwo();
  34.     }
  35.  
  36.     /**
  37.      * Prints the game table
  38.      * @param a - String array representing Line One {1, 2, 3}
  39.      * @param b - String array representing Line Two {4, 5, 6}
  40.      * @param c - String array representing Line Three {7, 8, 9}
  41.      */
  42.     public static void printTable(String[] a, String[] b, String[] c){
  43.         for (int i = 0; i < 3; i++){
  44.             System.out.print(a[i] + " ");
  45.         }
  46.         System.out.println();
  47.         for (int i = 0; i < 3; i++){
  48.             System.out.print(b[i] + " ");
  49.         }
  50.         System.out.println();
  51.         for (int i = 0; i < 3; i++){
  52.             System.out.print(c[i] + " ");
  53.         }
  54.         System.out.println();
  55.     }
  56.  
  57.     /**
  58.      *
  59.      * @param a - players choice
  60.      */
  61.     public static void gameMethod(int playersTurn, String a){
  62.         isTaken(playersTurn, a);
  63.     }
  64.  
  65.  
  66.     /**
  67.      * 1. Asks player one to enter a position to print X on that position
  68.      * 2. Calls the gameMethod, where the entered position is replaced with X
  69.      * 3. Calls the printTable method
  70.      */
  71.     public static void playerOne(){
  72.         System.out.print("Играч 1: ");
  73.         int player = 1;
  74.         plOnePick = sc.nextLine();
  75.         gameMethod(player ,plOnePick);
  76.         printTable(lineOne, lineTwo, lineThree);
  77.     }
  78.  
  79.  
  80.     /**
  81.      * 1. Asks player two to enter a position to print X on that position
  82.      * 2. Calls the gameMethod, where the entered position is replaced with X
  83.      * 3. Calls the printTable method
  84.      */
  85.     public static void playerTwo(){
  86.         System.out.print("Играч 2: ");
  87.         int player = 2;
  88.         plTwoPick = sc.nextLine();
  89.         gameMethod(player, plTwoPick);
  90.         printTable(lineOne, lineTwo, lineThree);
  91.     }
  92.  
  93.  
  94.     /**
  95.      * Checks if the position is already taken by any of the players ( aka X or 0 )
  96.      * If position is taken - calls the player to choose again
  97.      * If position is free - enters the current player mark on that position
  98.      */
  99.     public static void isTaken(int player, String playersChoice){
  100.         //check for player 1
  101.         if(player == 1){
  102.             for(int i = 0; i < 3; i++){
  103.                 //check if position 1, 2 or 3 is taken by any player
  104.                 if(lineOne[i].equals("X") || lineOne[i].equals("O")){
  105.                     //if taken - asks the player to choose another free position
  106.                     if(lineOne[i].equals(playersChoice)) playerOne();
  107.                     else{
  108.                         if(playersChoice.equals(lineOne[i])) lineOne[i] = "X";
  109.                         if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "X";
  110.                         if(playersChoice.equals(lineThree[i])) lineThree[i] = "X";
  111.                     }
  112.                 }
  113.                 else if(lineTwo[i].equals("X") || lineTwo[i].equals("O")){
  114.                     if(lineTwo[i].equals(playersChoice)) playerOne();
  115.                     else{
  116.                         if(playersChoice.equals(lineOne[i])) lineOne[i] = "X";
  117.                         if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "X";
  118.                         if(playersChoice.equals(lineThree[i])) lineThree[i] = "X";
  119.                     }
  120.                 }
  121.                 else if(lineThree[i].equals("X") || lineThree[i].equals("O")){
  122.                     if(lineThree[i].equals(playersChoice)) playerOne();
  123.                     else{
  124.                         if(playersChoice.equals(lineOne[i])) lineOne[i] = "X";
  125.                         if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "X";
  126.                         if(playersChoice.equals(lineThree[i])) lineThree[i] = "X";
  127.                     }
  128.                 }
  129.                 else {
  130.                     if(playersChoice.equals(lineOne[i])) lineOne[i] = "X";
  131.                     if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "X";
  132.                     if(playersChoice.equals(lineThree[i])) lineThree[i] = "X";
  133.                 }
  134.             }
  135.         }
  136. //        check for player 2
  137.         if(player == 2){
  138.             for(int i = 0; i < 3; i++){
  139.                 //check if position 1, 2 or 3 is taken by any player
  140.                 if(lineOne[i].equals("X") || lineOne[i].equals("O")){
  141.                     //if taken - asks the player to choose another free position
  142.                     if(lineOne[i].equals(playersChoice)) playerTwo();
  143.                     else {
  144.                         if(playersChoice.equals(lineOne[i])) lineOne[i] = "O";
  145.                         if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "O";
  146.                         if(playersChoice.equals(lineThree[i])) lineThree[i] = "O";
  147.                     }
  148.                 }
  149.                 else if(lineTwo[i].equals("X") || lineTwo[i].equals("O")){
  150.                     if(lineTwo[i].equals(playersChoice)) playerTwo();
  151.                     else {
  152.                         if(playersChoice.equals(lineOne[i])) lineOne[i] = "O";
  153.                         if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "O";
  154.                         if(playersChoice.equals(lineThree[i])) lineThree[i] = "O";
  155.                     }
  156.                 }
  157.                 else if(lineThree[i].equals("X") || lineThree[i].equals("O")){
  158.                     if(lineThree[i].equals(playersChoice)) playerTwo();
  159.                     else {
  160.                         if(playersChoice.equals(lineOne[i])) lineOne[i] = "O";
  161.                         if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "O";
  162.                         if(playersChoice.equals(lineThree[i])) lineThree[i] = "O";
  163.                     }
  164.                 }
  165.                 else {
  166.                     if(playersChoice.equals(lineOne[i])) lineOne[i] = "O";
  167.                     if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "O";
  168.                     if(playersChoice.equals(lineThree[i])) lineThree[i] = "O";
  169.                 }
  170.             }
  171.         }
  172.  
  173.     }
  174. }
  175.  
RAW Paste Data