Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TicTacToe
- {
- private AutoIt.GUI GUI;
- private AutoIt.Button[][] Button;
- private int[][] Game = new int[3][3];
- public static final int Human = 1;
- public static final int AI = -1;
- public static final int None = 0;
- private int Turn = None;
- private int ScoreHuman = 0;
- private int ScoreAI = 0;
- private boolean End = false;
- public static void main(String[] args)
- {
- TicTacToe t3 = new TicTacToe();
- }
- public TicTacToe()
- {
- GUI = AutoIt.GUICreate("Tic-Tac-Toe", 307, 329, 200, 100);
- Button = new AutoIt.Button[3][3];
- for (int y = 0; y < 3; y++)
- {
- for (int x = 0; x < 3; x++)
- {
- Button[x][y] = AutoIt.GUICtrlCreateButton("", x*100, y*100, 100, 100, GUI);
- AutoIt.GUICtrlSetOnEvent(Button[x][y], this, "Button"+x+y);
- }
- }
- AutoIt.GUISetState(true, GUI);
- NewGame();
- }
- private void Button00() { Human(0, 0); }
- private void Button10() { Human(1, 0); }
- private void Button20() { Human(2, 0); }
- private void Button01() { Human(0, 1); }
- private void Button11() { Human(1, 1); }
- private void Button21() { Human(2, 1); }
- private void Button02() { Human(0, 2); }
- private void Button12() { Human(1, 2); }
- private void Button22() { Human(2, 2); }
- private void NewGame()
- {
- for (int y = 0; y < 3; y++)
- {
- for (int x = 0; x < 3; x++)
- {
- Game[x][y] = None;
- AutoIt.GUICtrlSetData(Button[x][y], "");
- }
- }
- Turn = Human;
- End = false;
- }
- private boolean Human(int x, int y)
- {
- if (End) { NewGame(); return false; }
- if (Turn != Human) return false;
- if (Game[x][y] != 0) return false;
- Game[x][y] = Human;
- AutoIt.GUICtrlSetData(Button[x][y], "X");
- Turn = AI;
- if (Winner() == Human)
- {
- ScoreHuman += 1;
- AutoIt.ConsoleWrite("Score (Human).: "+ScoreHuman+"\n");
- } else AI();
- return true;
- }
- private boolean AI()
- {
- if (End) return false;
- if (Turn != AI) return false;
- int x = 1, y = 1;
- while (Game[x][y] != None)
- {
- x = (int) (Math.random() * 3);
- y = (int) (Math.random() * 3);
- }
- Game[x][y] = AI;
- AutoIt.GUICtrlSetData(Button[x][y], "O");
- Turn = Human;
- if (Winner() == AI)
- {
- ScoreAI += 1;
- AutoIt.ConsoleWrite("Score (AI)....: "+ScoreAI+"\n");
- }
- return true;
- }
- private int Winner()
- {
- int[] s = {448, 56, 7, 292, 146, 73, 273, 84};
- int h = GameToInt(Human), a = GameToInt(AI);
- for (int i = 0; i < s.length; i++)
- {
- if ( (h & s[i]) == s[i] ) { End = true; return Human; }
- if ( (a & s[i]) == s[i] ) { End = true; return AI; }
- }
- if (GameToInt(None) == 0) End = true;
- return None;
- }
- private int GameToInt(int q)
- {
- int i = 0;
- for (int y = 0; y < 3; y++)
- {
- for (int x = 0; x < 3; x++)
- {
- if (Game[x][y] == q) i += Math.pow(2, x+3*y);
- }
- }
- return i;
- }
- private void IntToGame(int q, int i)
- {
- for (int y = 0; y < 3; y++)
- {
- for (int x = 0; x < 3; x++)
- {
- if (i-Math.pow(2, x+3*y) >= 0)
- {
- i -= Math.pow(2, x+3*y);
- Game[x][y] = q;
- }
- }
- }
- }
- private void IntToGame(int i)
- {
- IntToGame(AI, i);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement