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

tic tac toe help mark=(player == 1) ? \'X\' : \'O\'; if (choice == 0&& square[0]

ID: 3664672 • Letter: T

Question

tic tac toe help

mark=(player == 1) ? 'X' : 'O';
  
           if (choice == 0&& square[0] == '0')
              
               square[0] = mark;
           else if (choice == 1 && square[1] == '1')
              
               square[1] = mark;
           else if (choice == 2 && square[2] == '2')
              
               square[2] = mark;
           else if (choice == 3 && square[3] == '3')
  
               square[3] = mark;
           else if (choice == 4 && square[4] == '4')
  
               square[4] = mark;
           else if (choice == 5 && square[5] == '5')
  
               square[5] = mark;
           else if (choice == 6 && square[6] == '6')
  
               square[6] = mark;
           else if (choice == 7 && square[7] == '7')
  
               square[7] = mark;
           else if (choice == 8 && square[8] == '8')
  
               square[8] = mark;
           else
           {
               cout<<"Invalid move ";  
               player--;
           }
           i=checkwin(square);
           player++;
I need to create 2 function for the code above please help me I need afunction called playerXTurn

and anothef function called playerOTurn using one dimensional array

I apreciate your help

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package practice;

import java.util.Arrays;
import java.util.Scanner;

/**
*
* @author Shivakumar
*/
public class TicTacToe {
  
String win="012345678036147258";

public String[] grids={"0","1","2","3","4","5","6","7","8"};
public String playerXturn(String choice)
{
String sorted="";

String m="";
for(int i=0;i<grids.length;i++)
{
if(grids[i].equals(choice))
{
grids[i]="x";
}
else{}

}
for(int j=0;j<grids.length;j++)
{
if(grids[j].equals("x"))
m=m+j;
}
  
char[] chars=m.toCharArray();
Arrays.sort(chars);
sorted=new String(chars);
if(win.matches(sorted))
return "You Won the Game";

else
return "";
  
}
public String playerOturn(String choice)
{
String sorted="";

String m="";
for(int i=0;i<grids.length;i++)
{
if(grids[i].equals(choice))
{
grids[i]="o";
}
else{}

}
for(int j=0;j<grids.length;j++)
{
if(grids[j].equals("o"))
m=m+j;
}
  
char[] chars=m.toCharArray();
Arrays.sort(chars);
sorted=new String(chars);
if(win.matches(sorted))
return "You Won the Game";

else
return "";
  
}
public static void main(String args[])
{
String choice1="";
TicTacToe tc=new TicTacToe();
Scanner sc=new Scanner(System.in);
  
while(tc.playerXturn(choice1)=="You Won the game")
{
System.out.println("Enter your Grid Choice alternatively");
choice1=sc.nextLine();
  
}
  
  
}
  
  
  
  
}