
Patrick B. answered 04/16/21
Math and computer tutor/teacher
import java.util.Scanner;
class TicTacToe
{
class Cell
{
protected char ch;
public Cell() { ch='?'; }
public void SetX() { ch='X'; }
public void SetO() { ch='O'; }
public void Clear() { ch = '?'; }
public char Get() { return(ch); }
}
protected Cell[][] A;
public TicTacToe ()
{
A = new Cell[3][3];
for (int iloop=0; iloop<3; iloop++)
{
for (int jloop=0; jloop<3; jloop++)
{
A[iloop][jloop]= new Cell();
}
}
}
public char IndexerGetAtIndex( int iRow, int iColumn )
{
char chReturn=0;
if ((iRow>=0) && (iRow<3))
{
if ((iColumn>=0) && (iColumn<3))
{
chReturn = A[iRow][iColumn].Get();
}
}
return(chReturn);
}
public void IndexerSetXatIndex(int iRow, int iColumn)
{
if ((iRow>=0) && (iRow<3))
{
if ((iColumn>=0) && (iColumn<3))
{
A[iRow][iColumn].SetX();
}
}
}
public void IndexerSetOatIndex(int iRow, int iColumn)
{
if ((iRow>=0) && (iRow<3))
{
if ((iColumn>=0) && (iColumn<3))
{
A[iRow][iColumn].SetO();
}
}
}
public void IndexerClearIndex(int iRow, int iColumn)
{
if ((iRow>=0) && (iRow<3))
{
if ((iColumn>=0) && (iColumn<3))
{
A[iRow][iColumn].Clear();
}
}
}
public void Display()
{
for (int iloop=0; iloop<3; iloop++)
{
for (int jloop=0; jloop<3; jloop++)
{
char curChar = A[iloop][jloop].Get();
if (curChar=='?')
{
System.out.print((iloop*3+jloop+1) + " " );
}
else
{
System.out.print(A[iloop][jloop].Get() + " " );
}
}
System.out.println("");
}
}
//returns 0 if no win, 1 on row #1 win, 2 on row #2 win, 3 on row #3 win;
// -1 on column #1 win, -2 on column #2 win, -3 on column #3 win;
// 4 on diagonal win (0,0),(1,1),(2,2) win;
// -4 on digaonal win (0,2),(1,1),(2,0) win
public int CheckWin()
{
int iReturn=0;
//1st row
if ((A[0][0].Get()== A[0][1].Get()) && (A[0][1].Get()==A[0][2].Get()))
{
iReturn=1;
}
//2nd row
if ((A[1][0].Get()== A[1][1].Get()) && (A[1][1].Get()==A[1][2].Get()))
{
iReturn=2;
}
//3rd row
if ((A[2][0].Get()== A[2][1].Get()) && (A[2][1].Get()==A[2][2].Get()))
{
iReturn=3;
}
//1st column
if ((A[0][0].Get()== A[1][0].Get()) && (A[1][0].Get()==A[2][0].Get()))
{
iReturn=1;
}
//2nd column
if ((A[0][1].Get()== A[1][1].Get()) && (A[1][1].Get()==A[2][1].Get()))
{
iReturn=2;
}
//3rd column
if ((A[0][2].Get()== A[1][2].Get()) && (A[1][2].Get()==A[2][2].Get()))
{
iReturn=3;
}
//diagonal : northwest to southeast
if ((A[0][0].Get()== A[1][1].Get()) && (A[1][1].Get()==A[2][2].Get()))
{
iReturn=4;
}
//diagonal : southwest to northeast
if ((A[2][0].Get()== A[1][1].Get()) && (A[1][1].Get()==A[0][2].Get()))
{
iReturn=-4;
}
return(iReturn);
}
public void Go()
{
Scanner scanner = new Scanner(System.in);
boolean playerFlag=true;
int winResult=-1;
for (int playLoop=0; playLoop<9; playLoop++)
{
Display();
int cell=-1;
char displayChar = (playerFlag) ? 'X' : 'O';
int iRow=0,iColumn=0;
while ((cell<1) || (cell>9))
{
System.out.print("Player " + displayChar + " , please input 1-9 :>");
cell = scanner.nextInt();
cell--;
if ((cell>=0) && (cell<9))
{
iRow = cell/3;
iColumn = cell%3;
//System.out.println(" row = " +iRow + " : column = " + iColumn);
if (A[iRow][iColumn].Get()!='?')
{
System.out.println(" invalid selection ");
cell=-1;
}
else
{
break;
}
}
} //while
if (playerFlag)
{
IndexerSetXatIndex(iRow,iColumn);
}
else
{
IndexerSetOatIndex(iRow,iColumn);
}
if (playLoop>=4)
{
winResult = CheckWin();
if (winResult!=0)
{
System.out.println("Winner");
break;
}
}
playerFlag = !playerFlag;
} //for playLoop
if (winResult==0) { System.out.println(" TIE "); }
}
public static void main(String args[])
{
TicTacToe t = new TicTacToe();
t.Go();
}
}