Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Without using graphics API in Raptor Design a program that allows two players to

ID: 3692799 • Letter: W

Question

Without using graphics API in Raptor Design a program that allows two players to play a game of tic-tac-toe .Use a two-dimensional String array with three rows and three columns as the game board. Each element of the arrays should be initialized with an asterisk (*). The program should run a loop that does the following: 1.Displays the contents of the board array 2.Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and column number. 3.Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row and column number. 4.Determines whether a player has won or if a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should say so and end. 5.Player 1 wins when there are three Xs in a row on the game board. Player 2 wins when there are three Os in a row on the game board. The winning Xs or Ox can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner.

Explanation / Answer

TicTacToe.java


public class TicTacTOe {
int _row=3;
char _board[][]=new char[_row][_row];
public void makeBoard(){
for (int i = 0; i <_row; i++) {
for (int j = 0; j < _row; j++) {
   _board[i][j]='o';
}

}
}
boolean isValid(int r,int c){
return((r>=0&&r<_row)&&(c>=0&&c<_row));
}
boolean isFill(int r,int c){
return(_board[r][c]!='o');
}
public boolean gamePlay(int r ,int c,int playerNo){
if (!(isValid(r,c))) {
System.out.println("Enter Correct index");
return true ;
}
if (isFill(r,c)) {
System.out.println("This index already fill");
return true;
}
if (playerNo==1) {
_board[r][c]='*';
} else {
_board[r][c]='+';
}
return false;
}

boolean horizontalAndVerticalMatch(char ch,int n){
// for row and col math
    boolean flag=true;
for (int i = 0; i < _row; i++) {
      flag=true;
   for (int j = 0; j < _row; j++) {
      System.out.println("i="+i+"j="+j);
      char ch1=(n==1)?_board[i][j]:_board[j][i];
    if (ch1!=ch) {
      System.out.println("y");
    flag=false;
    break;
   }
  }
   if (flag) {
      return flag;
}
}
return flag;
}
boolean majorDiagonal(char ch){
return((_board[0][0]==ch)&&(_board[1][1]==ch)&&(_board[2][2]==ch));
}
boolean minorDiagonal(char ch){
return((_board[2][0]==ch)&&(_board[1][1]==ch)&&(_board[0][2]==ch));
}
public boolean isGameOver(int PlayerNo){
char ch;
if (PlayerNo==1) {
ch='*';
} else {
ch='+';
}
return(horizontalAndVerticalMatch(ch,1)||horizontalAndVerticalMatch(ch,2)||majorDiagonal(ch)||minorDiagonal(ch));
}
public void display(){
for (int i = 0; i <_row; i++) {
for (int j = 0; j < _row; j++) {
   System.out.print(" "+_board[i][j]);
}
System.out.println("");
}
}

}

MainTicTacToe.java

import java.util.Scanner;

public class MainTicTacToe extends TicTacTOe {

public static void main(String[] args) {

TicTacTOe obj=new TicTacTOe();

obj.makeBoard();

obj.display();

int r,c;

Scanner input=new Scanner(System.in);

while(true){

boolean flag=true;

while(flag){

   System.out.println("Player 1");

   System.out.println("enter row");

   r=input.nextInt();

   System.out.println("enter col");

   c=input.nextInt();

   flag=obj.gamePlay(r,c,1);

}

if (obj.isGameOver(1)) {

   obj.display();

   System.out.println("Player 1 Win Game");  

   break;

}

System.out.println("============");

obj.display();

flag=true;

while(flag){

System.out.println("Player 2");

System.out.println("enter row");

r=input.nextInt();

System.out.println("enter col");

c=input.nextInt();

flag=obj.gamePlay(r,c,2);

  }

if (obj.isGameOver(2)) {

   obj.display();

   System.out.println("Player 2 Win Game");  

   break;

}

System.out.println("============");

obj.display();

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote