Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions G group Project evaluation 1/game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package gameAdvanceJava;
import java.util.*;
public class TicTacToeTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
TicTacToe t = new TicTacToe();
Scanner s = new Scanner(System.in);
int x=0,y=0;
do
{
System.out.println(t.player==t.X?"Player X turn":"Player O turn");
System.out.println("Enter x and y places");
x=s.nextInt();
y=s.nextInt();

t.putSign(x, y);
System.out.println(t.toString());
System.out.println("_____________________________");
t.displayWinner();

}while(t.isEmpty);
}

}class TicTacToe
{
public static final int X = 1, O = -1;
public static final int EMPTY = 0;

public int player = X;
private int[][] board = new int[3][3];
public boolean isEmpty = false;

/** Puts an X or O mark at position i,j. */
public void putSign(int x, int y)
{
if(x<0 || x>2 || y<0 || y>2)
{
System.out.println("Invalid board position");
return;
}
if(board[x][y] != EMPTY)
{
System.out.println("Board position occupied");
return;
}
board[x][y] = player; // place the mark for the current player
player = -player; // switch players (uses fact that O = - X)
}

/** Checks whether the board configuration is a win for the given player. */
public boolean isWin(int player)
{
return ((board[0][0] + board[0][1] + board[0][2] == player*3) ||
(board[1][0] + board[1][1] + board[1][2] == player*3) ||
(board[2][0] + board[2][1] + board[2][2] == player*3) ||
(board[0][0] + board[1][0] + board[2][0] == player*3) ||
(board[0][1] + board[1][1] + board[2][1] == player*3) ||
(board[0][2] + board[1][2] + board[2][2] == player*3) ||
(board[0][0] + board[1][1] + board[2][2] == player*3) ||
(board[2][0] + board[1][1] + board[0][2] == player*3));
}

/**display the winning player or indicate a tie (or unfinished game).*/
public void displayWinner()
{
if(isWin(X))
{
System.out.println("\n X wins...!!");
isEmpty=false;
}
else if(isWin(O))
{
System.out.println("\n O wins...!!");
isEmpty=false;
}
else
{
if(!isEmpty)
{
System.out.println("its a tie");
}

}
}

public String toString()
{
StringBuilder s = new StringBuilder();
isEmpty = false;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
switch(board[i][j])
{
case X:
s.append(" X ");
break;
case O:
s.append(" O ");
break;
case EMPTY:
s.append(" ");
isEmpty=true;
break;
}
if(j<2)
{
s.append("|");
}

}
if(i<2)
{
s.append("\n-----------\n");
}
}
return s.toString();
}
}