package com.games.ttt;
// sua new game danh gia
import java.util.ArrayList;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
public class Game extends Activity {
public enum STATE { START, PLAY, END, DRAW };
public STATE state;
public String[][] block;
public String turn;
int xtam,ytam;
int gturn,symbol;
int dem;
int w = 20;
int h = 20;
int [][] score = new int[20][20];
private String current;
private Board board;
int depth=0;
int maxDepth=11;
int maxMove=3;
public boolean fWin = false;
public boolean firstTurn = true;
public int fEnd = 1;
int _x, _y;
Block[] PCMove = new Block[maxMove+2];
Block[] HumanMove = new Block[maxMove+2];
Block[] WinMove = new Block[maxDepth+2];
Block[] LoseMove = new Block[maxDepth + 2];
public int[] DScore = { 0, 1, 9, 81, 729 };
public int[] AScore = { 0, 2, 18, 162, 1458 };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
symbol = b.getInt("symbol");
gturn = b.getInt("turn");
dem=0;
this.startNewGame();
}
private void startNewGame()
{
dem=0;
state = STATE.START;
if (symbol == 1 ) current = Symbol.X();
else current = Symbol.O();
setContentView(R.layout.game);
board = (Board) findViewById(R.id.board);
if (gturn == 1 ) {
firstTurn = false;
turn = "You";
updateGameInfo("Your turn");}
else {
turn = "Android";
updateGameInfo("Android turn");
}
state = STATE.PLAY;
block = new String[20][20];
// it's Android's turn
if( turn == "Android" ) androidTakeTurn();
}
private void changeTurn()
{
if( current == Symbol.X() ) current = Symbol.O(); else current = Symbol.X();
if(turn=="Android") turn = "You"; else turn="Android";
updateGameInfo("Your turn");
androidTurn(turn);
}
private void androidTurn(String turn) {
// it's Android's turn
// simulate thinking...
Random r = new Random();
if( turn == "Android" ) {
board.disableInput();
updateGameInfo(turn + " is thinking...");
// SLEEP 2 SECONDS HERE ...
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
androidTakeTurn();
}
}, 500 + r.nextInt(2000));
} else {
board.enableInput();
}
}
private void updateGameInfo(String info)
{
TextView infoView = (TextView) findViewById(R.id.gameInfo);
infoView.setText(info);
}
private void quitApplication(){
new AlertDialog.Builder(this)
.setTitle("Exit")
.setMessage("Abandon this game?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {}
})
.show();
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_ingame, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
startActivity(new Intent(this, Settings.class));
return true;
case R.id.menu_exit:
quitApplication();
return true;
// More items go here (if any) ...
}
return false;
}
private void checkState() {
// TODO Auto-generated method stub
if(state == STATE.END) {
new AlertDialog.Builder(this)
.setTitle("Win!")
.setMessage(turn + " Win!\nPlay another game?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
firstTurn = true;
startNewGame();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.show();
}
if(state == STATE.DRAW) {
new AlertDialog.Builder(this)
.setTitle("Draw!")
.setMessage("No player wins the game!\nPlay another game?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startNewGame();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.show();
}
}
// called when board receives touch
protected void onTouch(int x, int y)
{
if( block[x][y] == null ) {
setBlock(x, y);
if( current == Symbol.X() ) {
board.put(x, y, Symbol.X());
block[x][y]= Symbol.X();
}
// Them bien luu lai block de xet trang thai
else {
board.put(x, y, Symbol.O());
block[x][y]= Symbol.O();
}
xtam=x;
ytam=y;
checkLines();
checkState();
if(state == STATE.PLAY) changeTurn();
}
}
private void setBlock(int x, int y) {
if(block[x][y] == null) block[x][y] = current;
}
private int emptyBlock()
{
int sum = 0;
for( int i = 0; i < 20; i++ ){
for( int j = 0; j < 20; j++ ){
if( block[i][j] == null ) sum++;
}
}
return sum;
}
private void checkLines() { // Can sua lai tong quat
int i=0;
int j=0;
for( i=1;i<5;i++){
if (xtam+i>19) {
i--;
break;
}
if (block[xtam][ytam]!=block[xtam+i][ytam]) {
i--;
break;
}
}
if (i<4) {
for( j=1;j<5;j++){
if (xtam-j<0) {
j--;
break;
}
if (block[xtam][ytam]!=block[xtam-j][ytam]) {
j--;
break;
}
}
}
int sum = i+j;
if (sum>3){
//ve duong ngang
this.state = STATE.END;
return;
}
i=0;
j=0;
for( i=1;i<5;i++){
if (ytam+i>19) {
i--;
break;
}
if (block[xtam][ytam]!=block[xtam][ytam+i]){
i--;
break;
}
}
if (i<4) {
for( j=1;j<5;j++){
if (ytam-j<0) {
j--;
break;
}
if (block[xtam][ytam]!=block[xtam][ytam-j]){
j--;
break;
}
}
}
if ((i+j)>3){
//ve duong doc
this.state = STATE.END;
return;
}
i=0;
j=0;
for( i=1;i<5;i++){
if (xtam+i>19 || ytam-i<0) {
i--;
break;
}
if (block[xtam][ytam]!=block[xtam+i][ytam-i]) {
i--;
break;
}
}
if (i<4) {
for( j=1;j<5;j++){
if (xtam-j<0 || ytam+j>19){
j--;
break;
}
if (block[xtam][ytam]!=block[xtam-j][ytam+j]) {
j--;
break;
}
}
}
if ((i+j)>3){
//ve duong cheo sac
this.state = STATE.END;
return;
}
i=0;
j=0;
for( i=1;i<5;i++){
if (xtam-i<0 || ytam-i<0) {
i--;
break;
}
if (block[xtam][ytam]!=block[xtam-i][ytam-i]) {
i--;
break;
}
}
if (i<4) {
for( j=1;j<5;j++){
if (xtam+j>19 || ytam+j>19) {
j--;
break;
}
if (block[xtam][ytam