Advertisement
ChristophX86

Tic-Tac-Toe

Jun 29th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. public class TicTacToe
  2. {
  3.     private AutoIt.GUI GUI;
  4.     private AutoIt.Button[][] Button;
  5.  
  6.     private int[][] Game = new int[3][3];
  7.     public static final int Human = 1;
  8.     public static final int AI = -1;
  9.     public static final int None = 0;
  10.     private int Turn = None;
  11.     private int ScoreHuman = 0;
  12.     private int ScoreAI = 0;
  13.     private boolean End = false;
  14.  
  15.     public static void main(String[] args)
  16.     {
  17.         TicTacToe t3 = new TicTacToe();
  18.     }
  19.  
  20.     public TicTacToe()
  21.     {
  22.         GUI = AutoIt.GUICreate("Tic-Tac-Toe", 307, 329, 200, 100);
  23.         Button = new AutoIt.Button[3][3];
  24.         for (int y = 0; y < 3; y++)
  25.         {
  26.             for (int x = 0; x < 3; x++)
  27.             {
  28.                 Button[x][y] = AutoIt.GUICtrlCreateButton("", x*100, y*100, 100, 100, GUI);
  29.                 AutoIt.GUICtrlSetOnEvent(Button[x][y], this, "Button"+x+y);
  30.             }
  31.         }
  32.         AutoIt.GUISetState(true, GUI);
  33.  
  34.         NewGame();
  35.     }
  36.  
  37.     private void Button00() { Human(0, 0); }
  38.     private void Button10() { Human(1, 0); }
  39.     private void Button20() { Human(2, 0); }
  40.     private void Button01() { Human(0, 1); }
  41.     private void Button11() { Human(1, 1); }
  42.     private void Button21() { Human(2, 1); }
  43.     private void Button02() { Human(0, 2); }
  44.     private void Button12() { Human(1, 2); }
  45.     private void Button22() { Human(2, 2); }
  46.  
  47.     private void NewGame()
  48.     {
  49.         for (int y = 0; y < 3; y++)
  50.         {
  51.             for (int x = 0; x < 3; x++)
  52.             {
  53.                 Game[x][y] = None;
  54.                 AutoIt.GUICtrlSetData(Button[x][y], "");
  55.             }
  56.         }
  57.  
  58.         Turn = Human;
  59.         End = false;
  60.     }
  61.  
  62.     private boolean Human(int x, int y)
  63.     {
  64.         if (End) { NewGame(); return false; }
  65.         if (Turn != Human) return false;
  66.         if (Game[x][y] != 0) return false;
  67.  
  68.         Game[x][y] = Human;
  69.         AutoIt.GUICtrlSetData(Button[x][y], "X");
  70.         Turn = AI;
  71.  
  72.         if (Winner() == Human)
  73.         {
  74.             ScoreHuman += 1;
  75.             AutoIt.ConsoleWrite("Score (Human).: "+ScoreHuman+"\n");
  76.         } else AI();
  77.  
  78.         return true;
  79.     }
  80.  
  81.     private boolean AI()
  82.     {
  83.         if (End) return false;
  84.         if (Turn != AI) return false;
  85.  
  86.         int x = 1, y = 1;
  87.         while (Game[x][y] != None)
  88.         {
  89.             x = (int) (Math.random() * 3);
  90.             y = (int) (Math.random() * 3);
  91.         }
  92.         Game[x][y] = AI;
  93.         AutoIt.GUICtrlSetData(Button[x][y], "O");
  94.         Turn = Human;
  95.  
  96.         if (Winner() == AI)
  97.         {
  98.             ScoreAI += 1;
  99.             AutoIt.ConsoleWrite("Score (AI)....: "+ScoreAI+"\n");
  100.         }
  101.  
  102.         return true;
  103.     }
  104.  
  105.     private int Winner()
  106.     {
  107.         int[] s = {448, 56, 7, 292, 146, 73, 273, 84};
  108.         int h = GameToInt(Human), a = GameToInt(AI);
  109.  
  110.         for (int i = 0; i < s.length; i++)
  111.         {
  112.             if ( (h & s[i]) == s[i] ) { End = true; return Human; }
  113.             if ( (a & s[i]) == s[i] ) { End = true; return AI; }
  114.         }
  115.  
  116.         if (GameToInt(None) == 0) End = true;
  117.         return None;
  118.     }
  119.  
  120.     private int GameToInt(int q)
  121.     {
  122.         int i = 0;
  123.         for (int y = 0; y < 3; y++)
  124.         {
  125.             for (int x = 0; x < 3; x++)
  126.             {
  127.                 if (Game[x][y] == q) i += Math.pow(2, x+3*y);
  128.             }
  129.         }
  130.         return i;
  131.     }
  132.     private void IntToGame(int q, int i)
  133.     {
  134.         for (int y = 0; y < 3; y++)
  135.         {
  136.             for (int x = 0; x < 3; x++)
  137.             {
  138.                 if (i-Math.pow(2, x+3*y) >= 0)
  139.                 {
  140.                     i -= Math.pow(2, x+3*y);
  141.                     Game[x][y] = q;
  142.                 }
  143.             }
  144.         }
  145.     }
  146.     private void IntToGame(int i)
  147.     {
  148.         IntToGame(AI, i);
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement