Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Relevent misc info: Project = Tic Tac Toe (console app)
My issue is the following:
I have an array that initially is filled with 0's which MUST stay as 0's unless the user takes a move (then it'll become a 1) or the computer makes a move then it becomes a 2.

Originally, if one row/collumn/diagonal was empty (3 0's) then if you do the calculation 0 + 0 + 0 % 2 you'll discover it equals 0 which conflicts with my code..

So I was recommended to just make sure three collumns/rows/diags are not all at 0 but this causes the computer not to write a result in the column/row/diag if they are all 0.
Example of code:
C#
else if (posStatus[2] != 0 && posStatus[5] != 0 && posStatus[8] != 0 && (posStatus[2] + posStatus[5] + posStatus[8] % 2 == 0))
            {
                if (posStatus[2] == 0)
                {
                    posStatus[2] = 2;
                    return;
                }
                else if (posStatus[5] == 0)
                {
                    posStatus[5] = 2;
                    return;
                }
                else if (posStatus[8] == 0)
                {
                    posStatus[8] = 2;
                    return;
                }

I believe that if the initial if else condition is changed my issues will be resolved but I'm not sure how to.

Thanks in advance.
Posted
Comments
Richard C Bishop 12-Feb-14 12:12pm    
Are you aware of what "%" does?
Member 10591689 12-Feb-14 12:15pm    
I am aware, why did you ask that?
Richard C Bishop 12-Feb-14 12:20pm    
Because the way you are using it seems a bit odd. Your order of operations in the modulus expression might be causing unintended consequences. I would end the parenthesis after "polStatus[8]". That will add the two variables before getting the modulus of it.

1 solution

a basic mistake:

Quote:
else if (posStatus[2] != 0 && posStatus[5] != 0 && posStatus[8] != 0 &&
(posStatus[2] + posStatus[5] + posStatus[8] % 2 == 0))


if posStatus[2] != 0 and posStatus[2]>0 then it is not possible to have
posStatus[2]+ STG ==0 unless STG<0

therefore else if condition will never be true in your case.

check out where to put your parenthesis and operator precedence as you need.

http://msdn.microsoft.com/en-us/library/aa691323%28v=vs.71%29.aspx[^]

[update on request]
enclose with parenthesis for modulus operator since its precedence higher than addition.
else if (posStatus[2] != 0 && posStatus[5] != 0 && posStatus[8] != 0 && (posStatus[2] + posStatus[5] + posStatus[8]) % 2 == 0)
 
Share this answer
 
v3
Comments
Member 10591689 13-Feb-14 5:41am    
You said what I said in a different way.. I am aware the issue is in that line but what I am not aware of how to fix it without essentially breaking my program.
Vedat Ozan Oner 13-Feb-14 6:23am    
I didn't say what you said. if so, you would know how to solve it. whatever, I have updated the solution.
Member 10591689 13-Feb-14 6:54am    
Man.. that's quite rude what you said.. Like why did you say 'whatever'? You're analysis is incorrect.. Let me word the issue out for you as you cannot see the full program.
The logic is the issue. Read what the two parts of the condition say.


Basically the row all of them mustn't be set to 0. The second part says condition 1 must be true and array 1 + 2 + 3 % 2 must equal 0.
If I were to take away the first part of the condition I'd get an issue because all the array is set to 0 to begin with (I cannot modify this unless you have a suggestion for omitting 0 temporarily). If all 3 are 0 then 0+0+0 % 2 = 0 which means the computer will always play a move here when with every other situation apart from 0+0+0 my program works fine.

So I added in the first part of the condition but now it will never play in a row/collumn that contains only 0s because array 1 2 3 are all at 0 so it'll skip the statement.

In regards to me knowing how to resolve the issue: I know what the issue is; I do not know how to solve it without breaking my program. I could break it down further and deal with it with much un-needed code but I need to learn how to program fairly fast so I should avoid tactics like that.
Vedat Ozan Oner 13-Feb-14 7:13am    
could you please read the solution carefully? if you don't read it carefully, you cannot understand what I tried to say. then you think I am saying the same thing with you, although not. from your example:
1+2+3%2 never and never results in 0. it is equal to 4!! that is what you do in your code. you need to convert it to (1+2+3)%2. do you see the difference? here is what operator precedence matters, which is obviously what you don't know. please read the solution carefully, then we can talk if there still exists some bug.
Member 10591689 13-Feb-14 7:34am    
How can it equal 4 if the array is holding 0 when the issue happens and can hold a maximum of 2? Modulus isn't multiplication. You do realize I tried your code and it changed nothing, I am aware what I wrote was odd but it didn't negatively affect the program.
Currently, it still has your recommendation in place and the issue still occurs. Can you stop ignoring what I'm saying though. I am stating I know the issue better than you, if you would like to prove me wrong let me know and I'll provide you with my code and you can take a more in depth look at it.
The issues that occur and what happens when stepping through the code and reading the code all suggest that the issue I stated is the issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900