1
I am a beginner programmer and have been spending a lot of time programming an old game. I made it work with the bot playing randomly, so to create another level of difficulty, I started creating a very basic I.A. through a chain of if and Else.
The problem lies precisely in this chain.
At first, I looked on the internet and after some research, I came to the conclusion that I wrote some part of this chain wrong. I searched, I searched, but I couldn’t find, so I came to ask for your help.
void jogadaPChard(void)
{
int coluna, linha;
if(_tabuleiro[0][0]+_tabuleiro[0][1]==10)
{linha=0;coluna=2;}
else
if(_tabuleiro[0][1]+_tabuleiro[0][2]==10)
{linha=0;coluna=0;}
else
if(_tabuleiro[0][0]+_tabuleiro[0][2]==10)
{linha=0;coluna=1;}
else
if(_tabuleiro[1][0]+_tabuleiro[1][1]==10)
{linha=1;coluna=2;}
else
if(_tabuleiro[1][1]+_tabuleiro[1][2]==10)
{linha=1;coluna=0;}
else
if(_tabuleiro[1][0]+_tabuleiro[1][2]==10)
{linha=1;coluna=1;}
else
if(_tabuleiro[2][0]+_tabuleiro[2][1]==10)
{linha=2;coluna=2;}
else
if(_tabuleiro[2][1]+_tabuleiro[2][2]==10)
{linha=2;coluna=0;}
else
if(_tabuleiro[2][0]+_tabuleiro[2][2]==10)
{linha=2;coluna=1;}
else
if(_tabuleiro[0][0]+_tabuleiro[1][0]==10)
{linha=0;coluna=2;}
else
if(_tabuleiro[1][0]+_tabuleiro[2][0]==10)
{linha=0;coluna=0;}
else
if(_tabuleiro[0][0]+_tabuleiro[2][0]==10)
{linha=1;coluna=2;}
else
if(_tabuleiro[0][1]+_tabuleiro[1][1]==10)
{linha=2;coluna=1;}
else
if(_tabuleiro[1][1]+_tabuleiro[2][1]==10)
{linha=0;coluna=1;}
else
if(_tabuleiro[0][1]+_tabuleiro[2][1]==10)
{linha=1;coluna=1;}
else
if(_tabuleiro[0][2]+_tabuleiro[1][2]==10)
{linha=2;coluna=2;}
else
if(_tabuleiro[1][2]+_tabuleiro[2][2]==10)
{linha=0;coluna=2;}
else
if(_tabuleiro[0][2]+_tabuleiro[2][2]==10)
{linha=1;coluna=2;}
else
if(_tabuleiro[0][0]+_tabuleiro[1][1]==10)
{linha=2;coluna=2;}
else
if(_tabuleiro[1][1]+_tabuleiro[2][2]==10)
{linha=0;coluna=0;}
else
if(_tabuleiro[0][0]+_tabuleiro[2][2]==10)
{linha=1;coluna=1;}
else
if(_tabuleiro[0][2]+_tabuleiro[1][1]==10)
{linha=2;coluna=0;}
else
if(_tabuleiro[1][1]+_tabuleiro[2][0]==10)
{linha=0;coluna=2;}
else
if(_tabuleiro[2][0]+_tabuleiro[0][2]==10)
{linha=1;coluna=1;}
else
jogadaPC();
if(_tabuleiro[linha][coluna]==0)
_tabuleiro[linha][coluna] = 250;
else
jogadaPC();
}
I just want to say that the function played PC(); is the simplest and is working without errors.
You have identified the location of the problem well. This is not to do. This is too confusing. If you make a loop and reduce it to a few lines, then it’s easier to spot other problems. At least you could take out that lot of
else
unnecessary.– Maniero
but how would I make that loop? I’ll take all of them out and see what happens
– shy0102
Then the problem is lower then. I think you have a problem too complicated for what you do. It would be interesting to start with something simpler, take one step at a time, skip steps will not accelerate your learning, on the contrary, it will leave gaps difficult to tidy up later.
– Maniero
Thank you man, remove all the Lse solved the problem. But pq all those Lse were harming the running of the program?
– shy0102
Because they prevented normal execution. But I’m still afraid of what’s left.
– Maniero
It probably worked because it was necessary to go through all ifs and perform each one that was true. As it is, when one of the ifs runs it does not proceed by the other ifs that are chained by elses (the sequence of ifs is broken). Note that Jogadapc() only runs if all ifs are fake, I don’t think that’s what you planned.. Apart from elses, Jogadapc() will always run after each if has been checked and regardless of whether they give true or false. That’s not it?
– Leo
in vdd, my intention was just to execute the CP() move if all if’s give false, but dps of the bigown comments, I realized that they were useless, because the program did not need them to create the condition I wanted, thank you, anyway, for commenting
– shy0102