✅ how do i create a constant process to check for the win condition in tictactoe?
i have created a visual tictactoe game and am struggling on triggering the win condtion, it seems to ignore my if statement looking for win conditions
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138298868971360256/image.png)
169 Replies
these are the 2 win condtions (one for the player where 1 = a cross)
and one for the cpu where 2 = a nought
(0 being blank)
when a cross or nought is placed on the grid
Well, you run this code in the constructor, so it will always execute exactly once: when the form is created
the 2d array board value is changed to fit the value
how can i make it run indefinitely in the background?
Don't
You want to run the check whenever the player or the CPU makes a move
if i make it check each time something is placed
it seems to mess something up and only do 2 full turns
Well, try to fix that, then
whereas right now it does a full game
well i did and it doesnt seem to work
Running the check in the constructor is not the way to go
Running the check constantly, on repeat, is not the way to go either
Your first idea was the best
So now we just need to fix whatever was wrong with it's implementation
ive set bool variables for each button was clicked
but scrapped that idea
if its useful i could utilise them still
uhhh ill just send the code for each button press in 2 parts then ^ ignore this
$paste if it's a lot of code
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Also, what's with all the
global.
s?its uh
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138300369395535912/image.png)
public static class of globalish variables
Why do they need to be global?
the a1clicked and so on are all useless at this point
Why can't they just be a part of the form?
because they dont seem to work if they are
i dont remember the exact issue i had with them but i think it was just that it wasnt being recognised in 2 different button clicks
BlazeBin - zkjqcuazjmzg
A tool for sharing your source code with the world!
wait lemme comment where i implemented the check
BlazeBin - dermlvpcsqpy
A tool for sharing your source code with the world!
there
Here's what I'd do.
this is the window btw
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138301311985659914/image.png)
No booleans, no ambiguity
im sorry i dont quite understand
can you elaborate
Well, in my implementation, the board is a 2D array of enums
what is a enum
And clicking a button changes the enum on the given position
Think of enums as spicy integers
ah
so like
intergers with strings or
ahh i see
Named numbers, you could say
i get that
So you know which number is what
but i dont think thats the greatest concern here
because (although very messy) the 2d int array does work its just that
Yeah, it does
win condition needs to be read properly
So, the check runs on
a1_Click
?
Does it also run when other buttons are clicked?yeah so
the numbering system is like
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138302947810357348/image.png)
like this
global.board[0, 0] being a1
global.board[0, 1] being a2 and so on
b1 being [1, 0]
Aight
the Vs were just placeholders for the score values btw
old screenshot
And does the check run on every button press?
yeah
The same exact check?
it did
i removed them now but yes it was the exact same check for each button in the exact same location
And after a couple of clicks the check would just randomly stop running?
yeah it would get to like
4 buttons being pressed
then images would stop showing up on the rest of the grid when buttons were pressed
and the score would stay at 0
I'd say you'll need to debug the code to see why that happens
let me try to make a image real quick
rough image but
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138304476822900836/image.png)
this is basically how the game would end
the blank buttons would be clickable but nothing would happen if you did
and scores stayed at 0
Yeah, I have no clue why that would happen
yeah this code is silly
I think you really should just debug your code, that would give you more insight
now i would but i get to a point where
i have so many things inside of other things
that i forget how the entire code for that section works
That means it might be a good idea to divide your code into more classes and methods
because i cant see all of the code at one time it also makes it harder for me to see
yeah i would have done that if i knew how without destroying my code
(and also if i remembered the syntax)
Use version control so you can always rollback the changes
how do you use that
And the syntax for classes and methods is extremely simple
that would be wonderful to know
The Git experience in Visual Studio
Learn how Git makes source control in Visual Studio more productive.
since i have to copy and remove the same code 9 times to get a good test on it
uhhhhhhhhhhhhhhhhhh
no idea how to figure that out
guess ill stick to ctrl + c ctrl + v x9
Click the
Git
button in the menu at the topyeah
Since you have no repo yet, there should be an option to make one
yeah
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138306017885691924/image.png)
You can select "local only" if you don't want online backups
created
Now the git menu will have more stuff
the branches right?
Don't worry about branches
oh alright then
You can "commit" the code to basically save it's current state
And you can always go back to any older commit
Like saves in a game
ahh
thats great
Now you won't need to fear destroying your code
huh why does it want a name and email
So it knows who creates those commits
if its storing locally it shouldnt need one unless ive done something wrong
But those can be anything
fair enough ig
A name of
aaa
and email a@a.a
will workshould i set it global git config or whatever the checkbox is
This will set the name and email globally on your PC, so other repositories will also use them
i think ive gone back to the repo
having a hard time getting back to my code tho
found it
alright so i can just
revert back to that one whenever then
neat
alright
now what would you recommend about the implementation of the win check at end of each button press @ZZZZZZZZZZZZZZZZZZZZZZZZZ
Make a method that checks that and call it on each click
alright
thank you
i will try it and get back to you with it
@ZZZZZZZZZZZZZZZZZZZZZZZZZ https://paste.mod.gg/qksemqloozhe/0
BlazeBin - qksemqloozhe
A tool for sharing your source code with the world!
tried this
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138600426908954624/image.png)
can click a button once then program just freezes
Chances are, one of your do..while loops is infinite
yeah guessed that
dont see how it would get stuck in a infinte loop though is the problem
Use the debugger and see
The condition is never
false
Why exactly that is? Dunnouhhh
alright
i may or may not know how to use the debugger
because this is what i added
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138601917845618779/image.png)
so if the if condition is true then the loop breaks
but if it isnt then the loop breaks also
$debug
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
uhh
where should i be starting the breakpoint
or putting
Placing it before the loop seems like a good idea
that could work yeah
@ZZZZZZZZZZZZZZZZZZZZZZZZZ HEY VERY EXCITING NEWS
IT WORKS
(kinda)
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138646154649800724/image.png)
problem is that it works if a box is pressed after the win
but i might be able to fix that real quick
also if another input is made then it adds another point but
it is what it is
could probably solve it with a reset thing
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138647254077546600/image.png)
fixed the uhh
placement thing
problem is if you keep clicking then it keeps adding points
put the win conditions in a if statement to stop em if the game has been won
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138648480336855060/image.png)
does not seem to work
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138648720217481326/image.png)
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138650949544591390/image.png)
GOT EM GREYED OUT WHEN FINISHED
Ayyy, nice!
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138651184450768977/image.png)
WORKS FOR BOTH!!!1
alright
im scrapping player vs player
i no longer have the will to continue on this project
im just gonna add a big NEW GAME button on the bottom
oh yeah thank you btw @ZZZZZZZZZZZZZZZZZZZZZZZZZ
i dont think ive thanked you yet
!!!
Anytime ![Ok](https://cdn.discordapp.com/emojis/1069891770848133160.png)
![Ok](https://cdn.discordapp.com/emojis/1069891770848133160.png)
i sure hope i dont catastrophically mess up my code when making a reset button
You can always rollback to the previous commit
?
oh yeah the
repo thing you taught me
thanks for that btw
The version control, yeah
im gonna cut down on some of the redundant code
maybe put some of the code into subroutines and whatnot
because im on like
698 lines of code
Oof, yeah, could probably use a refactoring lol
You could even push your repository to a remote on Github, share the project in #code-review and get some suggestions that way
ohh
that would be cool
All you need is a Github account, then log into it with VS, and you'll be able to publish your repo
i should make one
also funny thing
i forgot to add the vertical 3 in a row win condition
just added it now but funny how it just passed my mind
oh yeah 1 last thing @ZZZZZZZZZZZZZZZZZZZZZZZZZ how do i remove a image from a button
¯\_(ツ)_/¯
I never worked with Winforms
fair enough
STACK OVERFLOW!!!!!!!!
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138657193529450586/image.png)
!
problem
forgot to put a draw condition too
forgot bout that!
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138657352145440778/image.png)
ALSO
NEW GAME BUTTON WORKS!
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138657440624287885/image.png)
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138658824841072640/image.png)
this should work as a draw condition i think
and i hope
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138659030311641118/image.png)
YES
IT WORKS
@ZZZZZZZZZZZZZZZZZZZZZZZZZ one last thing!
how would i make another repo of this version of the code
the menus changed a bit
Why would you want another repository?
Just make a new commit
alright then
!
ironed out a bug and got the length down to
354
using a subroutine
Cut it in half then, nice
ok i may have been wrong about the ironing out the bug
seems like its still there
its just something simple really
the cpu makes another selection
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138662277478883348/image.png)
after i win
Means the game doesn't stop early enough
yeah thats what i thought
i think its doing the selection faster than the uh
global.finished = true;
tried putting it in a if statement thats like
if (global.finished == false)
and then changed the do while loop that it was in to
do
{
...
}while (... && global.finished == false);
hasnt seemed to have done anything though
atleast it doesnt get points for winning after the game though
seems to stop after a tie though
yeah idk how to fix that one
Now, mind,
do..while
will execute code first, then check the condition to see if it should execute againonly bug ive noticed so far
while
will check first, then execute if it should![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138665001704501348/image.png)
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138665101302448139/image.png)
the if statement in the 2nd picture is redundant because of the do while but still places a nought after the game has been won
Try a
while
instead of a do..while
uh
how do i do that?
just
while (global.finished == false)
{
...
}
?
turns into
wonderful
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138666060166484029/image.png)
changed the do while to that
![](https://answer-overflow-discord-attachments.s3.amazonaws.com/1138666348390666240/image.png)
still places one after winning
@ZZZZZZZZZZZZZZZZZZZZZZZZZ im gonna go to sleep
gonna try fix it tomorrow
A good night's sleep always helps
gonna start doing something to fix it
FIXED IT! @ZZZZZZZZZZZZZZZZZZZZZZZZZ
just had to run the win condition check twice
once after the player turn but before the cpu turn
and once after the cpu turn
Yeah, that makes sense. Good job!
Thanks man!
I've finalised everything and submitted the project
I'm finally finished!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.