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

Hello I need help in finishing the code for this Tic Tac Toe problem. I need hel

ID: 3784288 • Letter: H

Question

Hello I need help in finishing the code for this Tic Tac Toe problem. I need help in "Reseting the Squares", and also "Making the Computer Move". The areas i need help in are in BOLD. I hope someone will be able to help me, thank you!

namespace TicTacToe
{
    public partial class TTTForm : Form
    {
        public TTTForm()
        {
            InitializeComponent();
        }

        const string USER_SYMBOL = "X";
        const string COMPUTER_SYMBOL = "O";
        const string EMPTY = "";

        const int SIZE = 5;

        // constants for the 2 diagonals
        const int TOP_LEFT_TO_BOTTOM_RIGHT = 1;
        const int TOP_RIGHT_TO_BOTTOM_LEFT = 2;

        // constants for IsWinner
        const int NONE = -1;
        const int ROW = 1;
        const int COLUMN = 2;
        const int DIAGONAL = 3;

        // This method takes a row and column as parameters and
        // returns a reference to a label on the form in that position
        private Label GetSquare(int row, int column)
        {
            int labelNumber = row * SIZE + column + 1;
            return (Label)(this.Controls["label" + labelNumber.ToString()]);
        }

        // This method does the "reverse" process of GetSquare
        // It takes a label on the form as it's parameter and
        // returns the row and column of that square as output parameters
        private void GetRowAndColumn(Label l, out int row, out int column)
        {
            int position = int.Parse(l.Name.Substring(5));
            row = (position - 1) / SIZE;
            column = (position - 1) % SIZE;
        }

        // This method takes a row (in the range of 0 - 4) and returns true if
        // the row on the form contains 5 Xs or 5 Os.
        // Use it as a model for writing IsColumnWinner
        private bool IsRowWinner(int row)
        {
            Label square = GetSquare(row, 0);
            string symbol = square.Text;
            for (int col = 1; col < SIZE; col++)
            {
                square = GetSquare(row, col);
                if (symbol == EMPTY || square.Text != symbol)
                    return false;
            }
            return true;
        }

        //* TODO: finish all of these that return true
        private bool IsAnyRowWinner()
        {
            //UPDATED CODE
            for (int row = 1; row < SIZE; row++)
            {
                if (IsRowWinner(row))
                {
                    return true;
                }
            }
            return false;
        }

        private bool IsColumnWinner(int col)
        {
            //UPDATED CODE
            Label square = GetSquare(0, col);
            string symbol = square.Text;
            for (int row = 1; row < SIZE; row++)
            {
                square = GetSquare(col, row);
                if (symbol == EMPTY || square.Text != symbol)
                    return false;
            }
            return true;
        }
        private bool IsAnyColumnWinner()
        {
            //UPDATED CODE
            for (int col = 1; col < SIZE; col++)
            {
                if (IsColumnWinner(col))
                {
                    return true;
                }
            }
            return false;
        }
        private bool IsDiagonal1Winner()
        {
            //UPDATED CODE
            Label square = GetSquare((SIZE - 1), 0);
            string symbol = square.Text;

            for (int diag = 1; diag < SIZE; diag++)
            {
                square = GetSquare(diag, diag);
                if (symbol == EMPTY || square.Text != symbol)
                    return false;
            }
            return true;
        }
        private bool IsDiagonal2Winner()
        {
            //Original code inputed with form
            Label square = GetSquare(0, (SIZE - 1));
            string symbol = square.Text;
            for (int row = 1, col = SIZE - 2; row < SIZE; row++, col--)
            {
                square = GetSquare(row, col);
                if (symbol == EMPTY || square.Text != symbol)
                    return false;
            }
            return true;
        }

        private bool IsAnyDiagonalWinner()
        {
            //UPDATED CODE
            Label square = GetSquare(0, (SIZE - 1));
            string symbol = square.Text;
            for (int row = 1, col = SIZE - 2; row < SIZE; row++, col--)
            {
                square = GetSquare(row, col);
                if (symbol == EMPTY || square.Text != symbol)
                    return false;
            }
            return true;
        }
        private bool IsFull()
        {
            return true;
        }

        // This method determines if any row, column or diagonal on the board is a winner.
        // It returns true or false and the output parameters will contain appropriate values
        // when the method returns true. See constant definitions at top of form.
        private bool IsWinner(out int whichDimension, out int whichOne)
        {
            // rows
            for (int row = 0; row < SIZE; row++)
            {
                if (IsRowWinner(row))
                {
                    whichDimension = ROW;
                    whichOne = row;
                    return true;
                }
            }
            // columns
            for (int column = 0; column < SIZE; column++)
            {
                if (IsColumnWinner(column))
                {
                    whichDimension = COLUMN;
                    whichOne = column;
                    return true;
                }
            }
            // diagonals
            if (IsDiagonal1Winner())
            {
                whichDimension = DIAGONAL;
                whichOne = TOP_LEFT_TO_BOTTOM_RIGHT;
                return true;
            }
            if (IsDiagonal2Winner())
            {
                whichDimension = DIAGONAL;
                whichOne = TOP_RIGHT_TO_BOTTOM_LEFT;
                return true;
            }
            whichDimension = NONE;
            whichOne = NONE;
            return false;
        }

        // I wrote this method to show you how to call IsWinner
        private bool IsTie()
        {
            int winningDimension, winningValue;
            return (IsFull() && !IsWinner(out winningDimension, out winningValue));
        }

        // This method takes an integer in the range 0 - 4 that represents a column
        // as it's parameter and changes the font color of that cell to red.
        private void HighlightColumn(int col)
        {
            for (int row = 0; row < SIZE; row++)
            {
                Label square = GetSquare(row, col);
                square.Enabled = true;
                square.ForeColor = Color.Red;
            }
        }

        // This method changes the font color of the top right to bottom left diagonal to red
        // I did this diagonal because it's harder than the other one
        private void HighlightDiagonal2()
        {
            for (int row = 0, col = SIZE - 1; row < SIZE; row++, col--)
            {
                Label square = GetSquare(row, col);
                square.Enabled = true;
                square.ForeColor = Color.Red;
            }
        }

        // This method will highlight either diagonal, depending on the parameter that you pass
        private void HighlightDiagonal(int whichDiagonal)
        {
            if (whichDiagonal == TOP_LEFT_TO_BOTTOM_RIGHT)
                HighlightDiagonal1();
            else
                HighlightDiagonal2();

        }

        //* TODO: finish these 2
        private void HighlightRow(int row)
        {
            //UPDATED CODE 1
            for (int col = 0; col < SIZE; col++)

            {
                Label square = GetSquare(row, col);
                square.Enabled = true;
                square.ForeColor = Color.Red;
            }
        }

        private void HighlightDiagonal1()
        {
            //UPDATED CODE 2
            for (int row = SIZE - 1, col = 0; row < SIZE; row++, col--)
            {
                Label square = GetSquare(row, col);
                square.Enabled = true;
                square.ForeColor = Color.Red;
            }
        }

        //* TODO: finish this
        private void HighlightWinner(string player, int winningDimension, int winningValue)
        {
            switch (winningDimension)
            {
                //UPDATED CODE
                case ROW:
                    HighlightRow(winningValue);
                    resultLabel.Text = (player + " wins!");

                    break;
                //UPDATED CODE
                case COLUMN:
                    HighlightColumn(winningValue);
                    resultLabel.Text = (player + " wins!");

                    break;
                case DIAGONAL:
                    HighlightDiagonal(winningValue);
                    resultLabel.Text = (player + " wins!");
                    break;
            }
        }

        //* TODO: finish these 2
        private void ResetSquares()
        {

********NEED CODE FOR THIS******
        }

        private void MakeComputerMove()
        {

******NEED CODE FOR THIS******
        }

        // Setting the enabled property changes the look and feel of the cell.
        // Instead, this code removes the event handler from each square.
        // Use it when someone wins or the board is full to prevent clicking a square.
        private void DisableAllSquares()
        {
            for (int row = 0; row < SIZE; row++)
            {
                for (int col = 0; col < SIZE; col++)
                {
                    Label square = GetSquare(row, col);
                    DisableSquare(square);
                }
            }
        }

        // Inside the click event handler you have a reference to the label that was clicked
        // Use this method (and pass that label as a parameter) to disable just that one square
        private void DisableSquare(Label square)
        {
            square.Click -= new System.EventHandler(this.label_Click);
        }

        // You'll need this method to allow the user to start a new game
        private void EnableAllSquares()
        {
            for (int row = 0; row < SIZE; row++)
            {
                for (int col = 0; col < SIZE; col++)
                {
                    Label square = GetSquare(row, col);
                    square.Click += new System.EventHandler(this.label_Click);
                }
            }
        }

        //* TODO: finish the event handlers
        private void label_Click(object sender, EventArgs e)
        {
            //UPDATED CODE
            int winningDimension = NONE;
            int winningValue = NONE;

            Label clickedLabel = (Label)sender;
            clickedLabel.Text = USER_SYMBOL;
            DisableSquare(clickedLabel);
            if (IsWinner(out winningDimension, out winningValue))
            {
                HighlightWinner("The User", winningDimension, winningValue);

            }


        }
        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Explanation / Answer

import java.util.Scanner;

/**

* Tic-Tac-Toe: Two-player console, non-graphics, non-OO version.

* All variables/methods are declared as static (belong to the class)

* in the non-OO version.

*/

public class TTTConsoleNonOO2P {

   // Name-constants to represent the seeds and cell contents

   public static final int EMPTY = 0;

   public static final int CROSS = 1;

   public static final int NOUGHT = 2;

   // Name-constants to represent the various states of the game

   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;

   // The game board and the game status

   public static final int ROWS = 3, COLS = 3; // number of rows and columns

   public static int[][] board = new int[ROWS][COLS]; // game board in 2D array

                                                      // containing (EMPTY, CROSS, NOUGHT)

   public static int currentState; // the current state of the game

                                    // (PLAYING, DRAW, CROSS_WON, NOUGHT_WON)

   public static int currentPlayer; // the current player (CROSS or NOUGHT)

   public static int currntRow, currentCol; // current seed's row and column

   public static Scanner in = new Scanner(System.in); // the input Scanner

   /** The entry main method (the program starts here) */

   public static void main(String[] args) {

      // Initialize the game-board and current status

      initGame();

      // Play the game once

      do {

         playerMove(currentPlayer); // update currentRow and currentCol

         updateGame(currentPlayer, currntRow, currentCol); // update currentState

         printBoard();

         // Print message if game-over

         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!");

         }

         // Switch player

         currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS;

      } while (currentState == PLAYING); // repeat if not game-over

   }

   /** Initialize the game-board contents and the current states */

   public static void initGame() {

      for (int row = 0; row < ROWS; ++row) {

         for (int col = 0; col < COLS; ++col) {

            board[row][col] = EMPTY; // all cells empty

         }

      }

      currentState = PLAYING; // ready to play

      currentPlayer = CROSS; // cross plays first

   }

   /** Player with the "theSeed" makes one move, with input validation.

       Update global variables "currentRow" and "currentCol". */

   public static void playerMove(int theSeed) {

      boolean validInput = false; // for input validation

      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; // array index starts at 0 instead of 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; // update game-board content

            validInput = true; // input okay, exit loop

         } else {

            System.out.println("This move at (" + (row + 1) + "," + (col + 1)

                  + ") is not valid. Try again...");

         }

      } while (!validInput); // repeat until input is valid

   }

   /** Update the "currentState" after the player with "theSeed" has placed on

       (currentRow, currentCol). */

   public static void updateGame(int theSeed, int currentRow, int currentCol) {

      if (hasWon(theSeed, currentRow, currentCol)) { // check if winning move

         currentState = (theSeed == CROSS) ? CROSS_WON : NOUGHT_WON;

      } else if (isDraw()) { // check for draw

         currentState = DRAW;

      }

      // Otherwise, no change to currentState (still PLAYING).

   }

   /** Return true if it is a draw (no more empty cell) */

   // TODO: Shall declare draw if no player can "possibly" win

   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; // an empty cell found, not draw, exit

            }

         }

      }

      return true; // no empty cell, it's a draw

   }

   /** Return true if the player with "theSeed" has won after placing at

       (currentRow, currentCol) */

   public static boolean hasWon(int theSeed, int currentRow, int currentCol) {

      return (board[currentRow][0] == theSeed         // 3-in-the-row

                   && board[currentRow][1] == theSeed

                   && board[currentRow][2] == theSeed

              || board[0][currentCol] == theSeed      // 3-in-the-column

                   && board[1][currentCol] == theSeed

                   && board[2][currentCol] == theSeed

              || currentRow == currentCol            // 3-in-the-diagonal

                   && board[0][0] == theSeed

                   && board[1][1] == theSeed

                   && board[2][2] == theSeed

              || currentRow + currentCol == 2 // 3-in-the-opposite-diagonal

                   && board[0][2] == theSeed

                   && board[1][1] == theSeed

                   && board[2][0] == theSeed);

   }

   /** Print the game board */

   public static void printBoard() {

      for (int row = 0; row < ROWS; ++row) {

         for (int col = 0; col < COLS; ++col) {

            printCell(board[row][col]); // print each of the cells

            if (col != COLS - 1) {

               System.out.print("|");   // print vertical partition

            }

         }

         System.out.println();

         if (row != ROWS - 1) {

            System.out.println("-----------"); // print horizontal partition

         }

      }

      System.out.println();

   }

   /** Print a cell with the specified "content" */

   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;

      }

   }

}

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