Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

c++ wont take absolute value of a number

I create a tic tac toe AI in c++. I’ve tried adding code to add X to where the player put it, but when I run it gives me an error related to line 37. There’s an expression that simply converts a set of coordinates into a number. Rearranging the code gives me another error. The error might be related to how I ask for the absolute value.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<vector<string>> Board
    {
        {" ", " ", " "},
        {" ", " ", " "},
        {" ", " ", " "}
    };
    string playerInput;
    
    bool hasMoved = false;
    
    bool run = true;
    while(run) {
        //Prints Tic Tac Toe board to the screen
        for(int h = 0; h < Board.size();h++){
            cout << " ";
            for(int d = 0; d < Board[h].size();d++){
                cout << Board[h][d];
                if(d < Board[h].size() - 1){ cout <<"||"; }
            }
            if(h < Board.size() - 1){ cout << "\n---------\n"; }
            
        }
        cout << "\n choose a number 1-9 to place an X: ";
        cin >> playerInput;
        
        hasMoved = false;
        while(!hasMoved){
            for(int h = 0; h < Board.size();h++){
                for(int d = 0; d < Board[h].size();d++){
                    if(Board[h].size() * abs(h - Board[h].size() + 1) + d == playerInput && Board[h][d] == " ") {
                        Board[h][d] = "X";
                        hasMoved = true;
                    }
                }
            }
            if(!hasMoved){
                cout << "\n choose a number 1-9 to place an X: ";
                cin >> playerInput;
            }
        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This does not work the way you expect:

abs(h - Board[h].size() + 1)

The call to vector::size() returns a size_t value, which you are subtracting from a int value. By the rules of arithmetic conversions the int will be converted to unsigned.

So what you have is an unsigned type whereby your subtraction wraps "below" zero. The result will be a very large number.

One way you can fix your equation is to cast the result to int:

abs(h - static_cast<int>(Board[h].size()) + 1)

But in this case, simply adjusting the equation to not wrap is better:

Board[h].size() - h -  1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading