0

Definitions.hpp:

#ifndef DEFINITIONS_HPP
#define DEFINITIONS_HPP

#define SQUARES_NUM 120

#define SQ120_INDEX_TO_SQ64_INDEX(S, A, R) (S - (A + (R * 2)))
#define SQ64_INDEX_TO_SQ120_INDEX(S, A, R) (S + (A + (R * 2)))

typedef unsigned long long uint_64;

enum
{
    EMPTY, WhitePawn, WhiteKnight, WhiteBishop, WhiteRook, WhiteQueen, WhiteKing, BlackPawn, BlackKnight, BlackBishop, BlackRook, BlackQueen, BlackKing
};

enum
{
    FILE_A, FILE_B, FILE_C, FILE_D, FILE_F, FILE_G, FILE_H, FILE_NONE
};

enum
{
    RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NONE
};

enum
{
    A1=21, B1, C1, D1, E1, F1, G1, H1,
    A2=31, B2, C2, D2, E2, F2, G2, H2,
    A3=41, B3, C3, D3, E3, F3, G3, H3,
    A4=51, B4, C4, D4, E4, F4, G4, H4,
    A5=61, B5, C5, D5, E5, F5, G5, H5,
    A6=71, B6, C6, D6, E6, F6, G6, H6,
    A7=81, B7, C7, D7, E7, F7, G7, H7,
    A8=91, B8, C8, D8, E8, F8, G8, H8, NO_SQ
};

enum {WHITE, BLACK, BOTH};

static int Square120toSquare64[SQUARES_NUM] { };
static int Square64toSquare120[64] { };

#endif

Definitions.cpp

#include "Definitions.hpp"
#include <iostream>
void InitSquare120ToSquare64()
{
    int square = NO_SQ;
    int square64 = 0;
    for (int i = 0; i < SQUARES_NUM; i++)
    {
        Square120toSquare64[i] = 65;
    }
    for (int j = 0; j < 64; j++)
    {
        Square64toSquare120[j] = 121;
    }
    for (int RANK = RANK_1; RANK <= RANK_8; RANK++)
    {
        for (int FILE = FILE_A; FILE <= FILE_H; FILE++)
        {
            square = SQ64_INDEX_TO_SQ120_INDEX(square64, A1, RANK);
            Square120toSquare64[square] = square64;
            Square64toSquare120[square64] = square;
            square64++;
            std::cout << square << " ";
        }
    }
}

InitEngine.hpp

#ifndef INIT_ENGINE_HEADER_HPP
#define INIT_ENGINE_HEADER_HPP

#include "Definitions.hpp"

class InitEngine
{
public:
    InitEngine();
    ~InitEngine();

public:
    void InitChessEngine();
};

#endif

InitEngine.cpp

#include "InitEngine.hpp"

InitEngine::InitEngine()
{
    Square64toSquare120[1] = 1;
}

InitEngine::~InitEngine()
{

}

void InitEngine::InitChessEngine()
{
    InitSquare120ToSquare64();
}

ChessEngine.cpp

#include <iostream>
#include "InitEngine.hpp"
int main()
{
    InitEngine InitTEngine;
    InitTEngine.InitChessEngine();

    Square64toSquare120[2] = 65;

    for (int i = 0; i < 64; i++)
    {
        if (i % 8 == 0) std::cout << "\n";

        std::cout << Square64toSquare120[i] << " ";
    }
}

Output will be like this:

Output will be this(see picture):

So, the problem is, that I can't change valuse of elements of an Arrays static int Square120toSquare64 and static int Square64toSquare120 nowhere, except of Main function - calling a function InitSquare120ToSquare64() in InitChessEngine method of InitEngine class gives no changes of elements of arrays, but it has console output of array fulled with zeros, but I evem hadn't initialize array Square64toSquare120 with zeros... I tried to change 1 element of this array in constructor of InitEngine class, but it is not working too. Only Square64toSquare120[2] = 65; in Main function gives visible result. In adoption, for loop in InitSq120ToSq64 function having 64 iterations, but only 56 itertions seen in console output(output of this starts with 21). I don't know what is wrong and can't even imagine what is happening, so I hope You help me. Thank You :)

Aamir
  • 1,974
  • 1
  • 14
  • 18
luk_chesnok_xren
  • 147
  • 1
  • 1
  • 11
  • Hint: one definition rule – lorro Aug 16 '22 at 14:20
  • 1
    If you explained what you think `static` does, in this context, we may be able to clear up some confusion. You probably wanted to use `extern`. – Drew Dormann Aug 16 '22 at 14:24
  • @DrewDormann ```static``` was added cause of error of unresolved external element, so I tried to fix it like this – luk_chesnok_xren Aug 16 '22 at 14:28
  • 1
    `static` is your problem, because it's doing [exactly what `static` promises to do](https://stackoverflow.com/questions/15235526/the-static-keyword-and-its-various-uses-in-c). And more fundamentally, guessing what to change until your code compiles is a _huge problem_. It will not go well for you. C++ is a very difficult language to guess. – Drew Dormann Aug 16 '22 at 14:30
  • @DrewDormann I used extern keyword and everything works thank you – luk_chesnok_xren Aug 16 '22 at 15:41

0 Answers0