Aswin Balaji

Aswin Balaji

186 Sadasivam Nagar,Madipakkam,Chennai, TN 600091
Aswin Balaji

Tic Tac Toe Board Designing

September 17, 2015, by Aswin Balaji, category ANDROID
Tic-tac-toe (also known as Noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game.
The following example game is won by the first player, X:

Game of Tic-tac-toe, won by X

Players soon discover that best play from both parties leads to a draw. Hence, Tic-tac-toe is most often played by young children.
Because of the simplicity of Tic-tac-toe, it is often used as a pedagogical tool for teaching the concepts of good sportsmanship and the branch of artificial intelligence that deals with the searching of game trees. It is straightforward to write a computer program to play Tic-tac-toe perfectly, to enumerate the 765 essentially different positions (the state space complexity), or the 26,830 possible games up to rotations and reflections (the game tree complexity) on this space.

The game can be generalized to an m,n,k-game in which two players alternate placing stones of their own color on an m×n board, with the goal of getting k of their own color in a row. Tic-tac-toe is the (3,3,3)-game.

History

An early variant of Tic-tac-toe was played in the Roman Empire, around the first century BC. It was called Terni Lapilli and instead of having any number of pieces, each player only had three, thus they had to move them around to empty spaces to keep playing. The game’s grid markings have been found chalked all over Rome. However, according to Claudia Zaslavsky’s book Tic Tac Toe: And Other Three-In-A Row Games from Ancient Egypt to the Modern Computer, Tic-tac-toe could originate back to ancient Egypt.Another closely related ancient game is Three Men’s Morris which is also played on a simple grid and requires three pieces in a row to finish.
The different names of the game are more recent . The first print reference to “Noughts and crosses”, the British name, appeared in 1864. In his novel “Can You Forgive Her”, 1864, Anthony Trollope refers to a clerk playing “tit-tat-toe”. The first print reference to a game called “tick-tack-toe” occurred in 1884, but referred to “a children’s game played on a slate, consisting in trying with the eyes shut to bring the pencil down on one of the numbers of a set, the number hit being scored”. “Tic-tac-toe” may also derive from “tick-tack”, the name of an old version of backgammon first described in 1558. The U.S. renaming of Noughts and crosses as Tic-tac-toe occurred in the 20th century.
In 1952, OXO (or Noughts and Crosses) for the EDSAC computer became one of the first known video games. The computer player could play perfect games of Tic-tac-toe against a human opponent.
In 1975, Tic-tac-toe was also used by MIT students to demonstrate the computational power of Tinkertoy elements. The Tinkertoy computer, made out of (almost) only Tinkertoys, is able to play Tic-tac-toe perfectly.It is currently on display at the Museum of Science, Boston.
For Single Player

private void setBoard() {
ai = new AI();
b = new Button[4][4];
c = new int[4][4];

/*   tx1=(TextView)findViewById(R.id.dialog);*/
b[1][1]=(Button)findViewById(R.id.one);
b[1][2]=(Button)findViewById(R.id.two);
b[1][3]=(Button)findViewById(R.id.three);
b[2][1]=(Button)findViewById(R.id.four);
b[2][2]=(Button)findViewById(R.id.five);
b[2][3]=(Button)findViewById(R.id.six);
b[3][1]=(Button)findViewById(R.id.seven);
b[3][2]=(Button)findViewById(R.id.eight);
b[3][3]=(Button)findViewById(R.id.nine);

for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
c[i][j]=2; //for verification
}
}
/*  tx1.setEnabled(true);
tx1.setText(“Start a New Game”);*/
//////////////////////////////////////////////////////////////////////////////////////
// add the click listeners for each buttons
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(“X”);
c[x][y] = 0;//For Player 1
/* tx1.setText(” “);*/
if (!checkBoard())
{
ai.takeTurn();
}
/* else
{
b[x][y].setText(“O”);
Log.d(“ans”, String.valueOf(b[x][y]));
if(checkBoard())
b[x][y].setText(null);
}*/

}

}
}
//////////////////////////////////////////////////////////////////////////////

public 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[1][3] == 0 && c[3][1] == 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(3)+1);
int b = (rand.nextInt(3)+1);
while (a == 0 || b == 0 || c[a][b] != 2) {
a =(rand.nextInt(3)+1);
b =(rand.nextInt(3)+1);
Log.i(“ans1”, String.valueOf(a));
Log.i(“ans2”, String.valueOf(a));
}
markSquare(a, b);
}

}
///////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////
//for CPU playing
private void markSquare(int i1, int i2) {
b[i1][i2].setEnabled(false);
b[i1][i2].setText(“O”);
c[i1][i2]=1;//For Player 2(CPU)
checkBoard();

}
///////////////////////////////////////////////////////////////////////////
//For Checking The Condition
private boolean checkBoard() {
AlertDialog.Builder alert=new AlertDialog.Builder(this);

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))
{
/* if(tx1.isEnabled()){
tx1.setText(“Game over. You win! Play Again”);
tx1.setEnabled(false);
}*/
alert.setMessage(“Game over. You win! Play Again”);
alert.setTitle(“You Win”);
alert.setPositiveButton(“Ok”,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setBoard();
}
});
alert.create().show();

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))
{
/* if(tx1.isEnabled()){
tx1.setText(“Game over. You lost! Play Again”);
tx1.setEnabled(false);

}*/
alert.setMessage(“Game over. You Lose! Play Again”);
alert.setTitle(“You Lose”);
alert.setPositiveButton(“Ok”,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setBoard();
}
});
alert.create().show();
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;
/*  tx1.setText(“Game over. It’s a draw! Play Again”);*/
alert.setMessage(“Game over. Match Draw! Play Again”);
alert.setTitle(“Draw”);
alert.setPositiveButton(“Ok”,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setBoard();
}
});
alert.create().show();

}
}
return gameOver;
}

For Two Player My part of assumption is

public void onClick(View view) {
if (b[x][y].isEnabled()) {
b[x][y].setText(turn ? “O” : “X”);
CharSequence var =  b[x][y].getText();
if(var.toString().equals(“X”)){
c[x][y]=0;
//    Log.i(“String ans”, String.valueOf(c[x][y]));
}
else{
c[x][y]=1;
//   Log.i(“String ans 1”, String.valueOf(c[x][y]));

}
b[x][y].setEnabled(false);
turn = !turn;
checkBoard();

}