Advertisement
remote87

Tic Tac Toe - working

Nov 26th, 2020
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.94 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 void main(String[] args) {
  21.  
  22.         for(char i = 49; i < 58; i++){
  23.             picksTillNow.add(i);
  24.         }
  25.  
  26.         printTable();
  27.  
  28.         while (true){
  29.             playerOneChoice();
  30.             endGame();
  31.             playerTwoChoice();
  32.             endGame();
  33.         }
  34.     }
  35.  
  36.     /**
  37.      * Prints the table with no players choices made ( 1 - 9 )
  38.      */
  39.     public static void printTable() {
  40.  
  41.         for (int i = 0; i < table.length; i++) {
  42.             for (int j = 0; j < table[i].length; j++) {
  43.                 System.out.print(table[i][j] + " ");
  44.             }
  45.             if (table[i][2] != 9) {
  46.                 System.out.println();
  47.             }
  48.         }
  49.     }
  50.  
  51.  
  52.     /**
  53.      * Prints the table with the players choice included
  54.      * @param player - player 1 or player 2
  55.      * @param choice - char from 1 - 9
  56.      */
  57.     public static void printTable(int player, char choice){
  58.  
  59.         for (int i = 0; i < table.length ; i++) {
  60.             for (int j = 0; j < table[i].length; j++) {
  61.                 if(choice == table[i][j] && player == 1){
  62.                     //X
  63.                     table[i][j] = 'X';
  64.                 }else if(choice == table[i][j] && player == 2){
  65.                     //O
  66.                     table[i][j] = 'O';
  67.                 }
  68.                 System.out.print(table[i][j] + " ");
  69.             }
  70.             if(table[i][2] != 9){
  71.                 System.out.println();
  72.             }
  73.         }
  74.     }
  75.  
  76.  
  77.     /**
  78.      * Asks for player one choice method and prints the table, checks if that position has been chosen
  79.      */
  80.     public static void playerOneChoice(){
  81.         System.out.print("Играч 1: ");
  82.         playerOne = scan.next().charAt(0);
  83.         if(picksTillNow.contains(playerOne)){
  84.             picksTillNow.remove(picksTillNow.indexOf(playerOne));
  85.             printTable(1, playerOne);
  86.         }else{
  87.             playerOneChoice();
  88.         }
  89.     }
  90.  
  91.  
  92.     /**
  93.      * Asks for player one choice method and prints the table, checks if that position has been chosen
  94.      */
  95.     public static void playerTwoChoice(){
  96.         System.out.print("Играч 2: ");
  97.         playerTwo = scan.next().charAt(0);
  98.         if(picksTillNow.contains(playerTwo)){
  99.             picksTillNow.remove(picksTillNow.indexOf(playerTwo));
  100.             printTable(2, playerTwo);
  101.         }else{
  102.             playerTwoChoice();
  103.         }
  104.     }
  105.  
  106.     /**
  107.      * Checks for a winner. If yes - congrats the winner and exits the program, if no - exits the program
  108.      */
  109.     public static void endGame(){
  110.         for(int i = 0; i < table.length; i++){
  111.             if(table[i][0] == table[i][1] && table[i][1] == table[i][2] && table[0][i] != 'О'){
  112.                 System.out.println("Победа!");
  113.                 System.exit(0);
  114.             }
  115.         }
  116.  
  117.         for(int j = 0; j < table.length; j++){
  118.             if(table[0][j] == table[1][j] && table[1][j] == table[2][j] && table[j][0] != 'O'){
  119.                 System.out.println("Победа!");
  120.                 System.exit(0);
  121.             }
  122.         }
  123.  
  124.         if(table[0][0] == table[1][1] && table[1][1] == table[2][2] && table[0][0] != 'O'){
  125.             System.out.println("Победа!");
  126.             System.exit(0);
  127.         }
  128.  
  129.         if(table[0][2] == table[1][1] && table[1][1] == table[2][0] && table[0][2] != 'O'){
  130.             System.out.println("Победа!");
  131.             System.exit(0);
  132.         }
  133.  
  134.         if(picksTillNow.isEmpty()) System.exit(0);
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement