Saturday, February 11, 2012

Android Tutorial : Tic Tac Toe Part 1

This is the first part of our tutorial series on making a tic tac toe app for android.  In this part we code the java file that will be used "behind the scenes" to generate the computer's moves and check to see if there is a winner, if the game is a tie, or if there are spaces available to play in.







Files to go with this tutorial:

TicTacToeTutorialActivity.java

1:  package va.indiedevelopment.tictactoe;  
2:    
3:  import java.util.Random;  
4:    
5:  public class TicTacToeGame {  
6:         
7:       private char mBoard[];  
8:       private final static int BOARD_SIZE = 9;  
9:         
10:       public static final char HUMAN_PLAYER = 'X';  
11:       public static final char ANDROID_PLAYER = '0';  
12:       public static final char EMPTY_SPACE = ' ';  
13:         
14:       private Random mRand;  
15:         
16:       public static int getBOARD_SIZE() {  
17:            return BOARD_SIZE;  
18:       }  
19:         
20:       public TicTacToeGame(){  
21:              
22:            mBoard = new char[BOARD_SIZE];  
23:              
24:            for (int i = 0; i < BOARD_SIZE; i++)  
25:                 mBoard[i] = EMPTY_SPACE;  
26:              
27:            mRand = new Random();  
28:       }  
29:         
30:       public void clearBoard()  
31:       {  
32:            for (int i = 0; i < BOARD_SIZE; i++)  
33:            {  
34:                 mBoard[i] = EMPTY_SPACE;  
35:            }  
36:       }  
37:         
38:       public void setMove(char player, int location)  
39:       {  
40:            mBoard[location] = player;  
41:       }  
42:         
43:       public int getComputerMove()  
44:       {  
45:            int move;  
46:              
47:            for (int i = 0; i < getBOARD_SIZE(); i++)  
48:            {  
49:                 if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != ANDROID_PLAYER)  
50:                 {  
51:                      char curr = mBoard[i];  
52:                      mBoard[i] = ANDROID_PLAYER;  
53:                      if (checkForWinner() == 3)  
54:                      {  
55:                           setMove(ANDROID_PLAYER, i);  
56:                           return i;  
57:                      }  
58:                      else  
59:                           mBoard[i] = curr;  
60:                 }  
61:            }  
62:              
63:            for (int i = 0; i < getBOARD_SIZE(); i++)  
64:            {  
65:                 if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != ANDROID_PLAYER)  
66:                 {  
67:                      char curr = mBoard[i];  
68:                      mBoard[i] = HUMAN_PLAYER;  
69:                      if (checkForWinner() == 2)  
70:                      {  
71:                           setMove(ANDROID_PLAYER, i);  
72:                           return i;  
73:                      }  
74:                      else  
75:                           mBoard[i] = curr;  
76:                 }  
77:            }  
78:              
79:            do  
80:            {  
81:                 move = mRand.nextInt(getBOARD_SIZE());  
82:            } while (mBoard[move] == HUMAN_PLAYER || mBoard[move] == ANDROID_PLAYER);  
83:                   
84:                 setMove(ANDROID_PLAYER, move);  
85:            return move;  
86:       }  
87:         
88:       public int checkForWinner()  
89:       {  
90:            for (int i = 0; i <= 6; i += 3)  
91:            {  
92:                 if (mBoard[i] == HUMAN_PLAYER &&  
93:                   mBoard[i+1] == HUMAN_PLAYER &&  
94:                   mBoard[i+2] == HUMAN_PLAYER)  
95:                      return 2;  
96:                 if (mBoard[i] == ANDROID_PLAYER &&  
97:                      mBoard[i+1] == ANDROID_PLAYER &&  
98:                      mBoard[i+2] == ANDROID_PLAYER)  
99:                      return 3;  
100:            }  
101:              
102:            for (int i = 0; i <= 2; i++)  
103:            {  
104:                 if (mBoard[i] == HUMAN_PLAYER &&  
105:                      mBoard[i+3] == HUMAN_PLAYER &&  
106:                      mBoard[i+6] == HUMAN_PLAYER)  
107:                      return 2;  
108:                 if (mBoard[i] == ANDROID_PLAYER &&  
109:                      mBoard[i+3] == ANDROID_PLAYER &&  
110:                      mBoard[i+6] == ANDROID_PLAYER)  
111:                      return 3;  
112:            }  
113:              
114:            if ((mBoard[0] == HUMAN_PLAYER &&  
115:                  mBoard[4] == HUMAN_PLAYER &&  
116:                  mBoard[8] == HUMAN_PLAYER) ||  
117:                  mBoard[2] == HUMAN_PLAYER &&  
118:                  mBoard[4] == HUMAN_PLAYER &&  
119:                  mBoard[6] == HUMAN_PLAYER)  
120:                  return 2;  
121:            if ((mBoard[0] == ANDROID_PLAYER &&  
122:                  mBoard[4] == ANDROID_PLAYER &&  
123:                  mBoard[8] == ANDROID_PLAYER) ||  
124:                  mBoard[2] == ANDROID_PLAYER &&  
125:                  mBoard[4] == ANDROID_PLAYER &&  
126:                  mBoard[6] == ANDROID_PLAYER)  
127:                  return 3;  
128:              
129:            for (int i = 0; i < getBOARD_SIZE(); i++)  
130:            {  
131:                 if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != ANDROID_PLAYER)  
132:                      return 0;  
133:            }  
134:              
135:            return 1;  
136:       }  
137:  }  


Check us out on Google+ (Indie Development).
Check us out on our .co site (Indie Development).

No comments:

Post a Comment