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

  • Home
  • SEARCH
  • 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 5989409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:05:50+00:00 2026-05-22T23:05:50+00:00

Please help me understand why this isn’t working. I don’t know if there is

  • 0

Please help me understand why this isn’t working. I don’t know if there is a bug in my code, or whether my algorithm is fundamentally logically flawed.

My algorithm is based on minimax, but I’ve forgone a heuristic evaluation function for a more simple technique. Because of the simplicity of plain 3×3 tic tac toe, I just want to calculate all possible game outcomes for each potential move, and select the one with the highest ‘score’. I create a ‘top level’ vector of valid moves as well as a matching sized vector for their corresponding ‘scores’ -i.e. for every possible outcome subsequent to that move: ++ for a win and — for a loss.

However my vector of move scores is getting strange non-symmetrical values. Although even if the code worked, logically is it possible that a move which is calculated to lead to the most wins and least losses, would be blind to a simple tactic such as a fork? My instincts say yes, but I haven’t worked out the math in detail.

char board [9] = { '.','.','.','.','.','.','.','.','.' };

int com_turn(int turn) 
    {
    char player=COM; // keeps track of current player  

    cout<<"Computer turn. \n";  

    vector<int> moves = get_valid_moves(board); // top level move list
    vector<int> m_scores (moves.size(), 0);  // top level move scores

    for (int m=0; m < moves.size(); m++) // eval each top level move
    {
        board[moves[m]] = player; // do move

        evaluate(board, turn, &m_scores[m], player); 
        cout<< m_scores[m] <<' '; // for debugging

        board[moves[m]]='.'; // undo move
    }

    int bestmove;
    for (int i=0; i < moves.size(); i++) // find best score
    {
        bestmove = max(bestmove, m_scores[i]);
    }
    for (int i=0; i < moves.size(); i++) // match to best move
    {
        if (bestmove == m_scores[i])
        {
            bestmove = moves[i];
            break;
        }
    }

    board[bestmove]=COM; // finally make com move
    print_board();
}

vector<int> get_valid_moves(char *board) 
{
    vector<int> vmoves;
    for (int i=0; i < 9; i++)
    {
        if (board[i]=='.') vmoves.push_back(i);
    }
    return vmoves;
}


void evaluate(char *board, int turn, int *mscore, char player) 
{
    if (check_win(board)) 
    {
        (player==HUMAN)? *mscore -= 1: *mscore += 1;  
        return;  
    }
    if (turn > 9) return;

    vector<int> child_moves = get_valid_moves(board);
    if (child_moves.size() < 1) return;

    (player==COM)? player=HUMAN: player=COM; // switch player

    for (int m=0; m < child_moves.size(); m++) 
    {
        board[child_moves[m]] = player; // do move

        evaluate(board, ++turn, mscore, player);

        board[child_moves[m]]='.'; // undo move
    }
}
  • 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-05-22T23:05:50+00:00Added an answer on May 22, 2026 at 11:05 pm

    I think you’ll see where the problem is if you make evaluate return the score rather than using return-by-reference.

    Evaluate should be minimaxing, but right now I think it’s doing some weird sum of the leaf nodes because of the side-effect of additions and subtractions.

    Why summing up the scores is not correct

    Suppose I have the board:

    . . O
    . . .
    . X X
    

    Then O only has one move, (block), because X’s next move will win if O doesn’t make it. However, there are lots of game paths that start at O making other moves, with O winning, like:

    O2 O1 O
    .  .  X1
    .  X  X
    

    Where the number indicates which move came first.

    So you see, just getting the sum isn’t going to give you the right answer.

    The reason I recommend passing the values up the tree is that that forces you to write out what the score at a node is as a function of the children. In your code right now the function is the sum, in minimax it’s either min or max, depending on the player’s turn.

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

Sidebar

Related Questions

If there is anyone that can help me understand why this git repo isn't
Please help me to understand below code. This is the script for drag and
I cannot understand how this is possible. Please help!! I have an app with
Please help me understand this program's execution and what concepts apply here in the
Could someone please help me understand that why am I getting this error at
I have this javascript function I am trying to understand. I don't know if
Please help me understand this. Here you can see that I have PYTHONPATH set
please help me understand this. You have a function that calls a few methods:
Could you please help me understand this Tabu search page 7 example: TS is
Please help me convert this line to C#. objManagementBaseObject.SetPropertyValue(hDefKey, CType(&H & Hex(RegistryHive.LocalMachine), Long)) Related

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.