Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static String[] lineOne = {"1", "2", "3"};
- public static String[] lineTwo = {"4", "5", "6"};
- public static String[] lineThree = {"7", "8", "9"};
- public static String plOnePick = "";
- public static String plTwoPick = "";
- public static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- printTable(lineOne, lineTwo, lineThree);
- //rotation 1
- playerOne();
- playerTwo();
- //rotation 2
- playerOne();
- playerTwo();
- //rotation 3
- playerOne();
- playerTwo();
- //rotation 4
- playerOne();
- playerTwo();
- }
- /**
- * Prints the game table
- * @param a - String array representing Line One {1, 2, 3}
- * @param b - String array representing Line Two {4, 5, 6}
- * @param c - String array representing Line Three {7, 8, 9}
- */
- public static void printTable(String[] a, String[] b, String[] c){
- for (int i = 0; i < 3; i++){
- System.out.print(a[i] + " ");
- }
- System.out.println();
- for (int i = 0; i < 3; i++){
- System.out.print(b[i] + " ");
- }
- System.out.println();
- for (int i = 0; i < 3; i++){
- System.out.print(c[i] + " ");
- }
- System.out.println();
- }
- /**
- *
- * @param a - players choice
- */
- public static void gameMethod(int playersTurn, String a){
- isTaken(playersTurn, a);
- }
- /**
- * 1. Asks player one to enter a position to print X on that position
- * 2. Calls the gameMethod, where the entered position is replaced with X
- * 3. Calls the printTable method
- */
- public static void playerOne(){
- System.out.print("Играч 1: ");
- int player = 1;
- plOnePick = sc.nextLine();
- gameMethod(player ,plOnePick);
- printTable(lineOne, lineTwo, lineThree);
- }
- /**
- * 1. Asks player two to enter a position to print X on that position
- * 2. Calls the gameMethod, where the entered position is replaced with X
- * 3. Calls the printTable method
- */
- public static void playerTwo(){
- System.out.print("Играч 2: ");
- int player = 2;
- plTwoPick = sc.nextLine();
- gameMethod(player, plTwoPick);
- printTable(lineOne, lineTwo, lineThree);
- }
- /**
- * Checks if the position is already taken by any of the players ( aka X or 0 )
- * If position is taken - calls the player to choose again
- * If position is free - enters the current player mark on that position
- */
- public static void isTaken(int player, String playersChoice){
- //check for player 1
- if(player == 1){
- for(int i = 0; i < 3; i++){
- //check if position 1, 2 or 3 is taken by any player
- if(lineOne[i].equals("X") || lineOne[i].equals("O")){
- //if taken - asks the player to choose another free position
- if(lineOne[i].equals(playersChoice)) playerOne();
- else{
- if(playersChoice.equals(lineOne[i])) lineOne[i] = "X";
- if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "X";
- if(playersChoice.equals(lineThree[i])) lineThree[i] = "X";
- }
- }
- else if(lineTwo[i].equals("X") || lineTwo[i].equals("O")){
- if(lineTwo[i].equals(playersChoice)) playerOne();
- else{
- if(playersChoice.equals(lineOne[i])) lineOne[i] = "X";
- if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "X";
- if(playersChoice.equals(lineThree[i])) lineThree[i] = "X";
- }
- }
- else if(lineThree[i].equals("X") || lineThree[i].equals("O")){
- if(lineThree[i].equals(playersChoice)) playerOne();
- else{
- if(playersChoice.equals(lineOne[i])) lineOne[i] = "X";
- if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "X";
- if(playersChoice.equals(lineThree[i])) lineThree[i] = "X";
- }
- }
- else {
- if(playersChoice.equals(lineOne[i])) lineOne[i] = "X";
- if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "X";
- if(playersChoice.equals(lineThree[i])) lineThree[i] = "X";
- }
- }
- }
- // check for player 2
- if(player == 2){
- for(int i = 0; i < 3; i++){
- //check if position 1, 2 or 3 is taken by any player
- if(lineOne[i].equals("X") || lineOne[i].equals("O")){
- //if taken - asks the player to choose another free position
- if(lineOne[i].equals(playersChoice)) playerTwo();
- else {
- if(playersChoice.equals(lineOne[i])) lineOne[i] = "O";
- if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "O";
- if(playersChoice.equals(lineThree[i])) lineThree[i] = "O";
- }
- }
- else if(lineTwo[i].equals("X") || lineTwo[i].equals("O")){
- if(lineTwo[i].equals(playersChoice)) playerTwo();
- else {
- if(playersChoice.equals(lineOne[i])) lineOne[i] = "O";
- if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "O";
- if(playersChoice.equals(lineThree[i])) lineThree[i] = "O";
- }
- }
- else if(lineThree[i].equals("X") || lineThree[i].equals("O")){
- if(lineThree[i].equals(playersChoice)) playerTwo();
- else {
- if(playersChoice.equals(lineOne[i])) lineOne[i] = "O";
- if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "O";
- if(playersChoice.equals(lineThree[i])) lineThree[i] = "O";
- }
- }
- else {
- if(playersChoice.equals(lineOne[i])) lineOne[i] = "O";
- if(playersChoice.equals(lineTwo[i])) lineTwo[i] = "O";
- if(playersChoice.equals(lineThree[i])) lineThree[i] = "O";
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement