Simple TicTacToe App Android Game Project
I found this code pretty helpful for beginners to find out java programming and at an equivalent time developing with one thing fun and helpful.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | main.xml: <?xml version="1.0" encoding="utf-8"?> <!-- Demonstrates using a relative layout to create a form --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/one" android:layout_width="100px" android:layout_height="100px" android:layout_alignParentRight="true" android:layout_marginLeft="5px" android:text=" " android:textSize="70px" /> <Button android:id="@+id/two" android:layout_width="100px" android:layout_height="100px" android:layout_toLeftOf="@id/one" android:layout_alignTop="@id/one" android:layout_marginLeft="5px" android:text=" " android:textSize="70px" /> <Button android:id="@+id/three" android:layout_width="100px" android:layout_height="100px" android:layout_toLeftOf="@id/two" android:layout_alignTop="@id/two" android:layout_marginLeft="5px" android:text=" " android:textSize="70px" android:padding="0px"/> <Button android:id="@+id/four" android:layout_width="100px" android:layout_height="100px" android:layout_alignParentRight="true" android:layout_marginLeft="5px" android:layout_below="@id/one" android:text=" " android:textSize="70px" /> <Button android:id="@+id/five" android:layout_width="100px" android:layout_height="100px" android:layout_toLeftOf="@id/four" android:layout_alignTop="@id/four" android:layout_marginLeft="5px" android:text=" " android:textSize="70px" /> <Button android:id="@+id/six" android:layout_width="100px" android:layout_height="100px" android:layout_toLeftOf="@id/five" android:layout_alignTop="@id/five" android:layout_marginLeft="5px" android:text=" " android:textSize="70px" /> <Button android:id="@+id/seven" android:layout_width="100px" android:layout_height="100px" android:layout_alignParentRight="true" android:layout_marginLeft="5px" android:layout_below="@id/four" android:text=" " android:textSize="70px" /> <Button android:id="@+id/eight" android:layout_width="100px" android:layout_height="100px" android:layout_toLeftOf="@id/seven" android:layout_alignTop="@id/seven" android:layout_marginLeft="5px" android:text=" " android:textSize="70px" /> <Button android:id="@+id/nine" android:layout_width="100px" android:layout_height="100px" android:layout_toLeftOf="@id/eight" android:layout_alignTop="@id/eight" android:layout_marginLeft="5px" android:text=" " android:textSize="70px" /> <TextView android:id="@+id/dialogue" android:layout_width="fill_parent" android:layout_below="@id/nine" android:layout_height="wrap_content" android:text="Click a button to start" android:gravity="center_horizontal" android:layout_marginTop="20px" android:textSize="20px"/> </RelativeLayout> TicTacToe.java: package com.games.tictactoe; import java.util.Random; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class TicTacToe extends Activity { int c[][]; int i, j, k = 0; Button b[][]; TextView textView; AI ai; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setBoard(); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuItem item = menu.add("New Game"); return true; } public boolean onOptionsItemSelected(MenuItem item) { setBoard(); return true; } // Set up the game board. private void setBoard() { ai = new AI(); b = new Button[4][4]; c = new int[4][4]; textView = (TextView) findViewById(R.id.dialogue); b[1][3] = (Button) findViewById(R.id.one); b[1][2] = (Button) findViewById(R.id.two); b[1][1] = (Button) findViewById(R.id.three); b[2][3] = (Button) findViewById(R.id.four); b[2][2] = (Button) findViewById(R.id.five); b[2][1] = (Button) findViewById(R.id.six); b[3][3] = (Button) findViewById(R.id.seven); b[3][2] = (Button) findViewById(R.id.eight); b[3][1] = (Button) findViewById(R.id.nine); for (i = 1; i <= 3; i++) { for (j = 1; j <= 3; j++) c[i][j] = 2; } textView.setText("Click a button to start."); // add the click listeners for each button for (i = 1; i <= 3; i++) { for (j = 1; j <= 3; j++) { b[i][j].setOnClickListener(new MyClickListener(i, j)); if(!b[i][j].isEnabled()) { b[i][j].setText(" "); b[i][j].setEnabled(true); } } } } class MyClickListener implements View.OnClickListener { int x; int y; public MyClickListener(int x, int y) { this.x = x; this.y = y; } public void onClick(View view) { if (b[x][y].isEnabled()) { b[x][y].setEnabled(false); b[x][y].setText("O"); c[x][y] = 0; textView.setText(""); if (!checkBoard()) { ai.takeTurn(); } } } } private class AI { public void takeTurn() { if(c[1][1]==2 && ((c[1][2]==0 && c[1][3]==0) || (c[2][2]==0 && c[3][3]==0) || (c[2][1]==0 && c[3][1]==0))) { markSquare(1,1); } else if (c[1][2]==2 && ((c[2][2]==0 && c[3][2]==0) || (c[1][1]==0 && c[1][3]==0))) { markSquare(1,2); } else if(c[1][3]==2 && ((c[1][1]==0 && c[1][2]==0) || (c[3][1]==0 && c[2][2]==0) || (c[2][3]==0 && c[3][3]==0))) { markSquare(1,3); } else if(c[2][1]==2 && ((c[2][2]==0 && c[2][3]==0) || (c[1][1]==0 && c[3][1]==0))){ markSquare(2,1); } else if(c[2][2]==2 && ((c[1][1]==0 && c[3][3]==0) || (c[1][2]==0 && c[3][2]==0) || (c[3][1]==0 && c[1][3]==0) || (c[2][1]==0 && c[2][3]==0))) { markSquare(2,2); } else if(c[2][3]==2 && ((c[2][1]==0 && c[2][2]==0) || (c[1][3]==0 && c[3][3]==0))) { markSquare(2,3); } else if(c[3][1]==2 && ((c[1][1]==0 && c[2][1]==0) || (c[3][2]==0 && c[3][3]==0) || (c[2][2]==0 && c[1][3]==0))){ markSquare(3,1); } else if(c[3][2]==2 && ((c[1][2]==0 && c[2][2]==0) || (c[3][1]==0 && c[3][3]==0))) { markSquare(3,2); }else if( c[3][3]==2 && ((c[1][1]==0 && c[2][2]==0) || (c[1][3]==0 && c[2][3]==0) || (c[3][1]==0 && c[3][2]==0))) { markSquare(3,3); } else { Random rand = new Random(); int a = rand.nextInt(4); int b = rand.nextInt(4); while(a==0 || b==0 || c[a][b]!=2) { a = rand.nextInt(4); b = rand.nextInt(4); } markSquare(a,b); } } private void markSquare(int x, int y) { b[x][y].setEnabled(false); b[x][y].setText("X"); c[x][y] = 1; checkBoard(); } } // check the board to see if someone has won private boolean checkBoard() { boolean gameOver = false; if ((c[1][1] == 0 && c[2][2] == 0 && c[3][3] == 0) || (c[1][3] == 0 && c[2][2] == 0 && c[3][1] == 0) || (c[1][2] == 0 && c[2][2] == 0 && c[3][2] == 0) || (c[1][3] == 0 && c[2][3] == 0 && c[3][3] == 0) || (c[1][1] == 0 && c[1][2] == 0 && c[1][3] == 0) || (c[2][1] == 0 && c[2][2] == 0 && c[2][3] == 0) || (c[3][1] == 0 && c[3][2] == 0 && c[3][3] == 0) || (c[1][1] == 0 && c[2][1] == 0 && c[3][1] == 0)) { textView.setText("Game over. You win!"); gameOver = true; } else if ((c[1][1] == 1 && c[2][2] == 1 && c[3][3] == 1) || (c[1][3] == 1 && c[2][2] == 1 && c[3][1] == 1) || (c[1][2] == 1 && c[2][2] == 1 && c[3][2] == 1) || (c[1][3] == 1 && c[2][3] == 1 && c[3][3] == 1) || (c[1][1] == 1 && c[1][2] == 1 && c[1][3] == 1) || (c[2][1] == 1 && c[2][2] == 1 && c[2][3] == 1) || (c[3][1] == 1 && c[3][2] == 1 && c[3][3] == 1) || (c[1][1] == 1 && c[2][1] == 1 && c[3][1] == 1)) { textView.setText("Game over. You lost!"); gameOver = true; } else { boolean empty = false; for(i=1; i<=3; i++) { for(j=1; j<=3; j++) { if(c[i][j]==2) { empty = true; break; } } } if(!empty) { gameOver = true; textView.setText("Game over. It's a draw!"); } } return gameOver; } } |