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

https://www.dropbox.com/sh/bwl697bi8thmic7/AAAyErAfFYO-niaQw-yYaN-ja?dl=0 Above

ID: 3765006 • Letter: H

Question

https://www.dropbox.com/sh/bwl697bi8thmic7/AAAyErAfFYO-niaQw-yYaN-ja?dl=0

Above drop box link (copy link and paste on browser) have my entire visual studio project files. click "Form1.cs" if you just need to see source code

i am have 3 problems with this 5x5x4 tic tac toe project.

problem 1: when a player wins x or o, the other empty buttons are not disabling. after the first player wins( need to match 4 columns, vertically, horizontally or diagonally) ,the game is letting me click every other button, and when i click , it says x wins or o wins for every click. this is big problem when playing COMPUTER (AI), lets say X win, after pressing ok for the message "x wins, good job" program displays o wins right after due to the empty button not disabling.

private void disableButton() is my attemt to fix but not working

problem 2: to play AI (computer), i made it were, either the user click "play ai" or type in "A.I" in the space for player 2, the first game goes ok excluding the problem 1, but after game 1 , i can no longer play ai without closing and re running the program,clicking "play ai" or type in "A.I" for player 2 is not responding, it is playing single player instead. i can reset the game and score, but its not responding to my "PLAY AI" Code, i can close then re run, then it works.

problem 3: since this is a 5x5x4 tic tac toe, the way i code is little harder, i want my AI to be unbeatable, meaning AI either wins or the game ends in a draw. if you look at "private Button hard_o(string mark)" i don't have all the combinations typed, there are 100's more, is there an easy way to code this, instead of me typing all 600 something combinations. ideally, i want an easy, medium or hard mode, but i am settling for just hard. but even thats not complete.

i will appreciate all help and criticism thank you.

Explanation / Answer

import java.util.Scanner;
public class CU
{
public static final int EMPTY = 0;
public static final int CROSS = 1;
public static final int NOUGHT = 2;
public static final int PLAYING = 0;
public static final int DRAW = 1;
public static final int CROSS_WON = 2;
public static final int NOUGHT_WON = 3;
public static final int ROWS = 3, COLS = 3;
public static int[][] board = new int[ROWS][COLS];
public static int currentState;
public static int currentPlayer;
public static int currntRow, currentCol;
public static Scanner in = new Scanner(System.in);
public static void main(String[] args)
{
initGame();
do
{
playerMove(currentPlayer);
updateGame(currentPlayer, currntRow, currentCol);
printBoard();
if (currentState == CROSS_WON)
{
System.out.println("'X' won! Bye!");
}
else if (currentState == NOUGHT_WON)
{
System.out.println("'O' won! Bye!");
}
else if (currentState == DRAW)
{
System.out.println("It's a Draw! Bye!");
}
currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS;
}
while (currentState == PLAYING);
}
public static void initGame()
{
for (int row = 0; row < ROWS; ++row)
{
for (int col = 0; col < COLS; ++col)
{
board[row][col] = EMPTY;
}
}
currentState = PLAYING;
currentPlayer = CROSS;
}
public static void playerMove(int theSeed)
{
boolean validInput = false;
do
{
if (theSeed == CROSS)
{
System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");
}
else
{
System.out.print("Player 'O', enter your move (row[1-3] column[1-3]): ");
}
int row = in.nextInt() - 1;
int col = in.nextInt() - 1;
if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY)
{
currntRow = row;
currentCol = col;
board[currntRow][currentCol] = theSeed;
validInput = true;
}
else
{
System.out.println("This move at (" + (row + 1) + "," + (col + 1)+ ") is not valid. Try again...");
}
}
while (!validInput);
}
public static void updateGame(int theSeed, int currentRow, int currentCol)
{
if (hasWon(theSeed, currentRow, currentCol))
{
currentState = (theSeed == CROSS) ? CROSS_WON : NOUGHT_WON;
}
else if (isDraw())
{
currentState = DRAW;
}
}
public static boolean isDraw()
{
for (int row = 0; row < ROWS; ++row)
{
for (int col = 0; col < COLS; ++col)
{
if (board[row][col] == EMPTY)
{
return false;
}
}
}
return true;
}
public static boolean hasWon(int theSeed, int currentRow, int currentCol)
{
return (board[currentRow][0] == theSeed && board[currentRow][1] == theSeed&& board[currentRow][2] == theSeed
|| board[0][currentCol] == theSeed
&& board[1][currentCol] == theSeed
&& board[2][currentCol] == theSeed
|| currentRow == currentCol   
&& board[0][0] == theSeed
&& board[1][1] == theSeed
&& board[2][2] == theSeed
|| currentRow + currentCol == 2
&& board[0][2] == theSeed
&& board[1][1] == theSeed
&& board[2][0] == theSeed);
}
public static void printBoard()
{
for (int row = 0; row < ROWS; ++row)
{
for (int col = 0; col < COLS; ++col)
{
printCell(board[row][col]);
if (col != COLS - 1)
{
System.out.print("|");
}
}
System.out.println();
if (row != ROWS - 1)
{
System.out.println("-----------");
}
}
System.out.println();
}
public static void printCell(int content)
{
switch (content)
{
case EMPTY: System.out.print(" "); break;
case NOUGHT: System.out.print(" O "); break;
case CROSS: System.out.print(" X "); break;
}
}
}