Advertisement
TheRightGuy

TikTakToe

Jan 31st, 2022
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. package tictactoe;
  2.  
  3. public class App {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.           new TicTacToe();
  8.     }
  9. }
  10.  
  11. package tictactoe;
  12.  
  13. import java.util.Arrays;
  14. import java.util.Scanner;
  15.  
  16. public class TicTacToe {
  17.  
  18.     private static final String PLAYER_CHARACTER = "X";
  19.     private static final String COMPUTER_CHARACTER = "O";
  20.     private static final String[] grid = new String[9];
  21.  
  22.     static {
  23.         for (int i = 0; i < grid.length; i++) {
  24.             grid[i] = String.valueOf(i + 1);
  25.         }
  26.     }
  27.  
  28.     public TicTacToe(){
  29.         gameLoop();
  30.     }
  31.  
  32.     private void gameLoop() {
  33.         Scanner input = new Scanner(System.in);
  34.         int player;
  35.         int computer;
  36.         while (Arrays.stream(grid).distinct().count() > 2) {
  37.             System.out.println("Pick a number from 1 - 9 ");
  38.             player = input.nextInt();
  39.             computer = (int) (Math.random() * 9 + 1);
  40.  
  41.             while(hasTiles(grid[player - 1])){
  42.                 System.out.println("Try again that tile is taken");
  43.                 player = input.nextInt();
  44.             }
  45.             grid[player - 1] = PLAYER_CHARACTER;
  46.  
  47.             while(hasTiles(grid[computer-1]) || computer == player){
  48.                 if(Arrays.stream(grid).distinct().count() == 2){
  49.                     gridView();
  50.                     System.exit(200);
  51.                 }
  52.                 computer = (int) (Math.random() * 9 + 1);
  53.             }
  54.  
  55.             grid[computer - 1] = COMPUTER_CHARACTER;
  56.             gridView();
  57.  
  58.             winner();
  59.         }
  60.     }
  61.  
  62.     private boolean hasTiles(String tile) {
  63.         return tile.equals("X") || tile.equals("O");
  64.     }
  65.  
  66.     private void gridView() {
  67.         System.out.println(grid[0] + "|" + grid[1] + "|" + grid[2]);
  68.         System.out.println("-----");
  69.         System.out.println(grid[3] + "|" + grid[4] + "|" + grid[5]);
  70.         System.out.println("-----");
  71.         System.out.println(grid[6] + "|" + grid[7] + "|" + grid[8]);
  72.     }
  73.  
  74.     private void winner() {
  75.         if(isWinner(PLAYER_CHARACTER)){
  76.             System.out.println("Player Won\nGame Over");
  77.             System.exit(200);
  78.         }else if(isWinner(COMPUTER_CHARACTER)){
  79.             System.out.println("Computer Won\nGame Over");
  80.             System.exit(205);
  81.         }
  82.     }
  83.  
  84.     private boolean isWinner(String userCharacter) {
  85.         return hasWonHorizontally(userCharacter)
  86.                 || hasWonVertically(userCharacter)
  87.                 || hasWonDiagonally(userCharacter);
  88.     }
  89.  
  90.     private boolean hasWonDiagonally(String userCharacter) {
  91.         return grid[0].equals(userCharacter) && grid[4].equals(userCharacter) && grid[8].equals(userCharacter)
  92.                 || grid[2].equals(userCharacter) && grid[4].equals(userCharacter) && grid[6].equals(userCharacter);
  93.     }
  94.  
  95.     private boolean hasWonVertically(String userCharacter) {
  96.         return grid[0].equals(userCharacter) && grid[3].equals(userCharacter) && grid[6].equals(userCharacter)
  97.                 || grid[1].equals(userCharacter) && grid[4].equals(userCharacter) && grid[7].equals(userCharacter)
  98.                 || grid[2].equals(userCharacter) && grid[5].equals(userCharacter) && grid[8].equals(userCharacter);
  99.     }
  100.  
  101.     private boolean hasWonHorizontally(String userCharacter) {
  102.         return grid[0].equals(userCharacter) && grid[1].equals(userCharacter) && grid[2].equals(userCharacter)
  103.                 || grid[3].equals(userCharacter) && grid[4].equals(userCharacter) && grid[5].equals(userCharacter)
  104.                 || grid[6].equals(userCharacter) && grid[7].equals(userCharacter) && grid[8].equals(userCharacter);
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement