Advertisement
remote87

Tic Tac Toe Second Try

Nov 25th, 2020
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8.     public static Scanner scan = new Scanner(System.in);
  9.  
  10.     public static char[][] table = {
  11.             {49, 50, 51},
  12.             {52, 53, 54},
  13.             {55, 56, 57}
  14.     };
  15.  
  16.     public static ArrayList<Character> picksTillNow = new ArrayList<Character>();
  17.     public static char playerOne = ' ';
  18.     public static char playerTwo = ' ';
  19.  
  20.     public static boolean isWin = false;
  21.  
  22.     public static void main(String[] args) {
  23.  
  24.         for(char i = 49; i < 58; i++){
  25.             picksTillNow.add(i);
  26.         }
  27.  
  28.         System.out.println(picksTillNow);
  29.  
  30.         printTable();
  31.  
  32.         playerOneChoice();
  33.         playerTwoChoice();
  34.  
  35.         playerOneChoice();
  36.         playerTwoChoice();
  37.  
  38.         playerOneChoice();
  39.         playerTwoChoice();
  40.  
  41.         playerOneChoice();
  42.         playerTwoChoice();
  43.  
  44.     }
  45.  
  46.     /**
  47.      * Prints the table with no players choices made ( 1 - 9 )
  48.      */
  49.     public static void printTable() {
  50.  
  51.         for (int i = 0; i < table.length; i++) {
  52.             for (int j = 0; j < table[i].length; j++) {
  53.                 System.out.print(table[i][j] + " ");
  54.             }
  55.             if (table[i][2] != 9) {
  56.                 System.out.println();
  57.             }
  58.         }
  59.     }
  60.  
  61.  
  62.     /**
  63.      * Prints the table with the players choice included
  64.      * @param player - player 1 or player 2
  65.      * @param choice - char from 1 - 9
  66.      */
  67.     public static void printTable(int player, char choice){
  68.  
  69.         for (int i = 0; i < table.length ; i++) {
  70.             for (int j = 0; j < table[i].length; j++) {
  71.                 if(choice == table[i][j] && player == 1){
  72.                     //X
  73.                     table[i][j] = 'X';
  74.                 }else if(choice == table[i][j] && player == 2){
  75.                     //O
  76.                     table[i][j] = 'O';
  77.                 }
  78.                 System.out.print(table[i][j] + " ");
  79.             }
  80.             if(table[i][2] != 9){
  81.                 System.out.println();
  82.             }
  83.         }
  84.     }
  85.  
  86.     /**
  87.      * Asks for player one choice method and prints the table, checks if that position has been chosen
  88.      */
  89.     public static void playerOneChoice(){
  90.         System.out.print("Играч 1: ");
  91.         playerOne = scan.next().charAt(0);
  92.         if(picksTillNow.contains(playerOne)){
  93.             picksTillNow.remove(picksTillNow.indexOf(playerOne));
  94.             printTable(1, playerOne);
  95.         }else{
  96.             playerOneChoice();
  97.         }
  98.  
  99.  
  100.     }
  101.  
  102.     /**
  103.      * Asks for player one choice method and prints the table, checks if that position has been chosen
  104.      */
  105.     public static void playerTwoChoice(){
  106.         System.out.print("Играч 2: ");
  107.         playerTwo = scan.next().charAt(0);
  108.         if(picksTillNow.contains(playerTwo)){
  109.             picksTillNow.remove(picksTillNow.indexOf(playerTwo));
  110.             printTable(2, playerTwo);
  111.         }else{
  112.             playerTwoChoice();
  113.         }
  114.     }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement