Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7906729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:06:42+00:00 2026-06-03T11:06:42+00:00

First off, this is part of an extra credit homework, so please do not

  • 0

First off, this is part of an extra credit homework, so please do not give me the answer. Please just help me understand where I might have a problem with what is going on. It is a Tic-Tac-Toe generator where the game goes through recursively to determine the best move based on the player. (Professor uses white ‘W’ and black ‘B’ instead of X and O)

My main recursive method returns the state score based on an input position on the TTT board; 1 if white will force a win from that position, 0 if it is a draw, and -1 if black will force a win from that position:

public int stateScore(boolean whiteMove, int[] BestMove) {
    return stateScore(whiteMove,BestMove,TTTBoard);
}

which calls my underlying private recursion method:

private int stateScore(boolean whiteMove, int[] BestMove,char[][] TestBoard) {
    char [][] newTestBoard = new char [3][3];
    for(int rowVal = 0; rowVal < 3; rowVal++){
        for(int colVal = 0; colVal < 3; colVal++){
            newTestBoard[rowVal][colVal] = TestBoard[rowVal][colVal];
        }
    }

    int [] bestMove = new int [2];

    for(int rowVal = 0; rowVal < 3; rowVal++){
        for(int colVal = 0; colVal < 3; colVal++){
            if(isFull(newTestBoard) == true){
                return 0;
            }
            else if(newTestBoard[rowVal][colVal] == '-'){
                bestMove[0] = rowVal;
                bestMove[1] = colVal;

                //if boolean is white
                 if(whiteMove == true){
                    newTestBoard = testEntry(rowVal,colVal,'W',newTestBoard);
                    if(threeInRow(newTestBoard) == 1){
                        return 1;
                    }
                    else if(threeInRow(newTestBoard) == 0 && isFull(newTestBoard) == true){
                        return 0;
                    }
                    else if(threeInRow(newTestBoard) == -1 && isFull(newTestBoard) == true){
                        return -1;
                    }
                    else{
                        return stateScore(!whiteMove,bestMove,newTestBoard);
                    }
                }
                //if boolean is black
                else{
                    newTestBoard = testEntry(rowVal,colVal,'B',newTestBoard);
                    if(threeInRow(newTestBoard) == -1){
                        return -1;
                    }
                    else if(threeInRow(newTestBoard) == 0 && isFull(newTestBoard) == true){
                        return 0;
                    }
                    else if(threeInRow(newTestBoard) == 1 && isFull(newTestBoard) == true){
                        return 1;
                    }
                    else{
                        return stateScore(!whiteMove,bestMove);
                    }
                }
            }
        }
    }
    return 0;
}

The boolean value for whiteMove is true if it is white’s move, and false if it is black’s. Secondary methods within the function include threeInRow:

public int threeInRow(char[][] TTTBoard){
    boolean whiteIs = false;
    boolean blackIs = false;
        //Horizontal?
        char [] colChar = new char [3];
        for(int rowVal = 0; rowVal < 3; rowVal ++){
            for(int colVal = 0; colVal < 3; colVal++){
                colChar[colVal] = TTTBoard[rowVal][colVal];
            }
            if(colChar[0] == colChar[1] && colChar[1] == colChar[2]){
                if(colChar[0] == 'W'){
                    whiteIs = true;
                }
                if(colChar[0] == 'B'){
                    blackIs = true;
                }
            }
        }

        //Vertical?
        char [] rowChar = new char [3];
        for(int colVal = 0; colVal < 3; colVal ++){
            for(int rowVal = 0; rowVal < 3; rowVal++){
                rowChar[colVal] = TTTBoard[rowVal][colVal];
            }
            if(rowChar[0] == rowChar[1] && rowChar[1] == rowChar[2]){
                if(rowChar[0] == 'W'){
                    whiteIs = true;
                }
                else if(rowChar[0] == 'B'){
                    blackIs = true;
                }
            }
        }

        //Diagonal
            //topLeft to bottomRight
            if(TTTBoard[0][0] == TTTBoard[1][1] && TTTBoard[1][1] == TTTBoard[2][2]){
                if(TTTBoard[0][0] == 'W'){
                    whiteIs = true; 
                }
                else if(TTTBoard[0][0] == 'B'){
                    blackIs = true;
                }
            }

            //topRight to bottomLeft
            if(TTTBoard[0][2] == TTTBoard[1][1] && TTTBoard[1][1] == TTTBoard [2][0]){
                if(TTTBoard[1][1] == 'W'){
                    whiteIs = true;
                }
                else if(TTTBoard[1][1] == 'B'){
                    blackIs = true;
                }
            }


    //Return Vals
    if(whiteIs == true && blackIs == true){
        return 0;
    }
    else if(blackIs == true && whiteIs == false){
        return -1;
    }
    else if(blackIs == false && whiteIs == true){
        return 1;
    }
    else if(blackIs == false && whiteIs == false){
        return 0;
    }
    else{
        return 0;
    }

}

and testEntry:

public char[][] testEntry(int row,int col,char newChar, char[][] TestBoard){

    char [][] returnBoard = new char[3][3];
    for(int rowVal = 0; rowVal < 3; rowVal++){
        for(int colVal = 0; colVal < 3; colVal++){
            returnBoard[rowVal][colVal] = TestBoard[rowVal][colVal];
        }
    }
    returnBoard[row][col] = newChar;
    return returnBoard;

}

I don’t understand where the stack overflow is coming from. It seems as though my returns cover all cases, and that my methods have appropriate returns. I haven’t ever used a for loop with recursion, am I messing something up with that. Also, I am correct in saying that type [] name = name (same type) does NOT work, right? That is why I did the for loops in that case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:06:44+00:00Added an answer on June 3, 2026 at 11:06 am

    In your black branch your return is wrong.

    You return

    return stateScore(!whiteMove,bestMove);
    

    which restarts the recursion. You want to return

    return stateScore(!whiteMove,bestMove,newTestBoard);
    

    Hints:

    • Fix your booleans:

      if(whiteMove == true) -> if (whiteMove)
      
    • Use UpperCase for Classes, lowerCamelCase for variables.

    • If you return in an if branch, then you don’t need an else.

      Instead of:

      if (condition) {
        ...
        return ...;
      }
      else
      {
        ...
      }
      

      It is better to write:

      if (condition) {
        ...
        return ...;
      }
      ...
      

      Keeps the nesting lower and makes the code easier to follow.

    • Refactor common code: Both branches return the same result:

      return stateScore(!whiteMove,bestMove,newTestBoard);
      

      Why not move this outside the if (whiteMove)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, please forgive the stupidness of this question but Im not from a
First off, this is NOT a duplicate of: Turn a C string with NULL
First off, I am not an AS 400 guy - at all. So please
Please forgive me if I don't explain this very well. First off, I am
First off, if it's not clear from the tag, I'm doing this in PHP
I need a little bit of help here. First off, I am not sure
First off I use this code to make the navigation bar always stay fixed;
First off the html row looks like this: <tr class=evenColor> blahblah TheTextIneed blahblah and
First off I would like to say that this is my first post and
First off, I'm aware this is a bad practice and I have answered many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.