Advertisement
MagisterRain

TicTacToe_console

Jul 18th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. # ----- JAVA CODE -----
  2. package jsx;
  3.  
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. public class Game {
  8.  
  9.     static int[] field = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  10.  
  11.     static Result result = Result.GAME;
  12.     static boolean player = false; // O - true, X - false
  13.  
  14.     static Console console;
  15.  
  16.     enum Result {
  17.         GAME, OWIN, XWIN, TIE;
  18.     }
  19.  
  20.     static void run() {
  21.         while (result == Result.GAME) {
  22.             int num = console.in();
  23.             if (player) {
  24.                 console.set("O", num);
  25.                 field[num] = 1;
  26.             } else {
  27.                 console.set("X", num);
  28.                 field[num] = -1;
  29.             }
  30.             check();
  31.             if (result == Result.GAME) {
  32.                 player = !player;
  33.             } else if (result == Result.OWIN) {
  34.                 System.out.print("Player O win!");
  35.             } else if (result == Result.XWIN) {
  36.                 System.out.print("Player X win!");
  37.             } else if (result == Result.TIE) {
  38.                 System.out.print("Tie! No one wins!");
  39.             }
  40.             console.draw(result, player);
  41.         }
  42.     }
  43.  
  44.     static void check() {
  45.         int[] summ = new int[8];
  46.         summ[0] = field[0] + field[1] + field[2];
  47.         summ[1] = field[3] + field[4] + field[5];
  48.         summ[2] = field[6] + field[7] + field[8];
  49.  
  50.         summ[3] = field[0] + field[3] + field[6];
  51.         summ[4] = field[1] + field[4] + field[7];
  52.         summ[5] = field[2] + field[5] + field[8];
  53.  
  54.         summ[6] = field[0] + field[4] + field[8];
  55.         summ[7] = field[2] + field[4] + field[6];
  56.         for (int i = 0; i < summ.length; i++) {
  57.             if (summ[i] == 3) {
  58.                 result = Result.OWIN;
  59.                 return;
  60.             } else if (summ[i] == -3) {
  61.                 result = Result.XWIN;
  62.                 return;
  63.             }
  64.         }
  65.         result = hasEmpty() ? Result.GAME : Result.TIE;
  66.     }
  67.  
  68.     static boolean hasEmpty() {
  69.         for (int i = 0; i < 9; i++) {
  70.             if (field[i] == 0) {
  71.                 return true;
  72.             }
  73.         }
  74.         return false;
  75.     }
  76.  
  77.     public static void main(String[] args) {
  78.         console = new Console();
  79.         console.draw(result, player);
  80.         run();
  81.     }
  82.  
  83.     static class Console {
  84.  
  85.         private String[] choice = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
  86.         private Scanner scanner;
  87.  
  88.         public Console() {
  89.             scanner = new Scanner(System.in);
  90.         }
  91.  
  92.         public int in() {
  93.             int out = 0;
  94.             while (true) {
  95.                 String s = scanner.nextLine().substring(0, 1);
  96.                 if (Arrays.asList(choice).contains(s)) {
  97.                     for (int i = 0; i < choice.length; i++) {
  98.                         if (choice[i].equals(s)) {
  99.                             out = i;
  100.                         }
  101.                     }
  102.                     break;
  103.                 } else {
  104.                     System.out.print("Error! Invalid input. Try another.");
  105.                 }
  106.             }
  107.             return out;
  108.         }
  109.  
  110.         public void set(String s, int i) {
  111.             choice[i] = s;
  112.         }
  113.  
  114.         public void draw(Result result, boolean player) {
  115.             if (result != Result.GAME) {
  116.                 for (int i = 0; i < 9; i++) {
  117.                     if (choice[i] != "X" && choice[i] != "O") {
  118.                         choice[i] = " ";
  119.                     }
  120.                 }
  121.             }
  122.             System.out.print("\n  ");
  123.             System.out.print(choice[0] + " | ");
  124.             System.out.print(choice[1] + " | ");
  125.             System.out.print(choice[2] + "\n ");
  126.             System.out.print("===|===|===\n  ");
  127.             System.out.print(choice[3] + " | ");
  128.             System.out.print(choice[4] + " | ");
  129.             System.out.print(choice[5] + "\n ");
  130.             System.out.print("===|===|===\n  ");
  131.             System.out.print(choice[6] + " | ");
  132.             System.out.print(choice[7] + " | ");
  133.             System.out.print(choice[8] + "\n\n");
  134.             System.out.print("============\n\n");
  135.             if (result == Result.GAME) {
  136.                 System.out.print("Wait for " + (player ? "O" : "X") + " player choose.\n\n");
  137.             }
  138.         }
  139.  
  140.         public void clear() {
  141.             // TODO
  142.         }
  143.     }
  144. }
  145.  
  146. # ----- BATCH CODE -----
  147. java -jar PwdGen.jar
  148. pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement