Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ----- JAVA CODE -----
- package jsx;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Game {
- static int[] field = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- static Result result = Result.GAME;
- static boolean player = false; // O - true, X - false
- static Console console;
- enum Result {
- GAME, OWIN, XWIN, TIE;
- }
- static void run() {
- while (result == Result.GAME) {
- int num = console.in();
- if (player) {
- console.set("O", num);
- field[num] = 1;
- } else {
- console.set("X", num);
- field[num] = -1;
- }
- check();
- if (result == Result.GAME) {
- player = !player;
- } else if (result == Result.OWIN) {
- System.out.print("Player O win!");
- } else if (result == Result.XWIN) {
- System.out.print("Player X win!");
- } else if (result == Result.TIE) {
- System.out.print("Tie! No one wins!");
- }
- console.draw(result, player);
- }
- }
- static void check() {
- int[] summ = new int[8];
- summ[0] = field[0] + field[1] + field[2];
- summ[1] = field[3] + field[4] + field[5];
- summ[2] = field[6] + field[7] + field[8];
- summ[3] = field[0] + field[3] + field[6];
- summ[4] = field[1] + field[4] + field[7];
- summ[5] = field[2] + field[5] + field[8];
- summ[6] = field[0] + field[4] + field[8];
- summ[7] = field[2] + field[4] + field[6];
- for (int i = 0; i < summ.length; i++) {
- if (summ[i] == 3) {
- result = Result.OWIN;
- return;
- } else if (summ[i] == -3) {
- result = Result.XWIN;
- return;
- }
- }
- result = hasEmpty() ? Result.GAME : Result.TIE;
- }
- static boolean hasEmpty() {
- for (int i = 0; i < 9; i++) {
- if (field[i] == 0) {
- return true;
- }
- }
- return false;
- }
- public static void main(String[] args) {
- console = new Console();
- console.draw(result, player);
- run();
- }
- static class Console {
- private String[] choice = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
- private Scanner scanner;
- public Console() {
- scanner = new Scanner(System.in);
- }
- public int in() {
- int out = 0;
- while (true) {
- String s = scanner.nextLine().substring(0, 1);
- if (Arrays.asList(choice).contains(s)) {
- for (int i = 0; i < choice.length; i++) {
- if (choice[i].equals(s)) {
- out = i;
- }
- }
- break;
- } else {
- System.out.print("Error! Invalid input. Try another.");
- }
- }
- return out;
- }
- public void set(String s, int i) {
- choice[i] = s;
- }
- public void draw(Result result, boolean player) {
- if (result != Result.GAME) {
- for (int i = 0; i < 9; i++) {
- if (choice[i] != "X" && choice[i] != "O") {
- choice[i] = " ";
- }
- }
- }
- System.out.print("\n ");
- System.out.print(choice[0] + " | ");
- System.out.print(choice[1] + " | ");
- System.out.print(choice[2] + "\n ");
- System.out.print("===|===|===\n ");
- System.out.print(choice[3] + " | ");
- System.out.print(choice[4] + " | ");
- System.out.print(choice[5] + "\n ");
- System.out.print("===|===|===\n ");
- System.out.print(choice[6] + " | ");
- System.out.print(choice[7] + " | ");
- System.out.print(choice[8] + "\n\n");
- System.out.print("============\n\n");
- if (result == Result.GAME) {
- System.out.print("Wait for " + (player ? "O" : "X") + " player choose.\n\n");
- }
- }
- public void clear() {
- // TODO
- }
- }
- }
- # ----- BATCH CODE -----
- java -jar PwdGen.jar
- pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement