TicTacToe
Easy to play the game "TicTacToe".
Just read the points given below carefully and do it as written below to play the game.
- First Copy the given code in your notepad.
- Than save the file with the name "TicTacToe" on your desktop, remember don't change the spellings or its format just write it as written over here.
- Also do one more thing save this file with the extension of ".java", i.e. "TicTacToe.java", this should be the complete name of the file.
- After saving the file,
- open your "command prompt" by typing "CMD" in your search menu of your windows
- As shown in the fig above.
- Than press "Enter" key command prompt will open, show the fig below.
- The small black screen apearing to you is the command promt.
- After that write "cd desktop" in the command prompt
- Than type "javac TicTacToe.java" in CMD.
- And than "java TicTacToe".
- After these steps the solution will be occour in front of you.
- There are two players in the game, " player 'X' " , and " player 'O' ".
- And 9 positions to be marked, which are as follows. (0 0), (0 1), (0 2), (1 0), (1 1), (1 2), (2 0), (2 1), (2 2).
- both the players have to write any one of the following positions turn by turn according to their respective positions. For Example if player 'X' puts mark on (0 0), than player 'O' puts mark on (2 2). The position of two players cannot be same.
- When the game ends the command prompt will automatically tells you that either player 'X' is the winner or player 'O' is the winner, or the game is TIE.
import java.io.*;
import java.util.*;
class TicTacToe
{
protected static final int X = 1, O = -1;
protected static final int EMPTY =0;
protected int board[][] = new int[3][3];
protected int player;
public TicTacToe()
{
clearBoard();
}
public void clearBoard()
{
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
board[i][j]=EMPTY;
player = X;
}
public void putMark(int i, int j)throws IllegalArgumentException
{
if((i<0)||(i>2)||(j<0)||(j>2))
throw new IllegalArgumentException("invalid board position");
if(board[i][j]!=EMPTY)
throw new IllegalArgumentException("board position occupied");
board[i][j] = player;
player = -player;
}
public boolean isWin(int mark)
{
return((board[0][0] + board[0][1] + board[0][2] == mark*3)
||(board[1][0] + board[1][1] + board[1][2] == mark*3)
||(board[2][0] + board[2][1] + board[2][2] == mark*3)
||(board[0][0] + board[1][0] + board[2][0] == mark*3)
||(board[0][1] + board[1][1] + board[1][2] == mark*3)
||(board[0][2] + board[1][2] + board[2][2] == mark*3)
||(board[0][0] + board[1][1] + board[2][2] == mark*3)
||(board[2][0] + board[1][1] + board[0][2] == mark*3));
}
public int winner()
{
if(isWin(X))
return (X);
else if(isWin(O))
return(O);
else
return (0);
}
public String toString()
{
String s = "";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
switch(board[i][j])
{
case X: s += "X"; break;
case O: s += "O"; break;
case EMPTY: s += "EMPTY"; break;
}
if(j<2) s += "|";
}
if(i<2) s += "\n----\n";
}
return s;
}
public static void main (String [] args)
{
TicTacToe game = new TicTacToe();
Scanner s = new Scanner(System.in);
for(int i=0; i<9; i++)
{
if(i==0||i==2||i==4||i==6||i==8)
System.out.println("Player X ");
else
System.out.println("Player O ");
int a=s.nextInt();
int b=s.nextInt();
for(int j=0; j<1; j++)
{
if(a==0&&b==0)
{
game.putMark(0,0);
System.out.println(game.toString());
int winningPlayer = game.winner();
}
else if(a==0&&b==1)
{
game.putMark(0,1);
System.out.println(game.toString());
int winningPlayer = game.winner();
}
else if(a==0&&b==2)
{
game.putMark(0,2);
System.out.println(game.toString());
int winningPlayer = game.winner();
}
else if(a==1&&b==0)
{
game.putMark(1,0);
System.out.println(game.toString());
int winningPlayer = game.winner();
}
else if(a==1&&b==1)
{
game.putMark(1,1);
System.out.println(game.toString());
int winningPlayer = game.winner();
}
else if(a==1&&b==2)
{
game.putMark(1,2);
System.out.println(game.toString());
int winningPlayer = game.winner();
}
else if(a==2&&b==0)
{
game.putMark(2,0);
System.out.println(game.toString());
int winningPlayer = game.winner();
}
else if(a==2&&b==1)
{
game.putMark(2,1);
System.out.println(game.toString());
int winningPlayer = game.winner();
}
else
{
game.putMark(2,2);
System.out.println(game.toString());
int winningPlayer = game.winner();
}
}
}
System.out.println(game.toString());
int winningPlayer = game.winner();
if(winningPlayer !=0)
System.out.println("Player " + winningPlayer + " wins");
else
System.out.println("Tie");
}
}
*----------------------------------------------------------------------------------------------*
NOTE:
If you will follow the all above mentioned points you will love the game.
If you like the game than please give your comments below.
thanx.
0 comments:
Post a Comment