C
C#12mo ago
Based

✅ 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
169 Replies
Based
Based12mo ago
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
Angius
Angius12mo ago
Well, you run this code in the constructor, so it will always execute exactly once: when the form is created
Based
Based12mo ago
the 2d array board value is changed to fit the value how can i make it run indefinitely in the background?
Angius
Angius12mo ago
Don't You want to run the check whenever the player or the CPU makes a move
Based
Based12mo ago
if i make it check each time something is placed it seems to mess something up and only do 2 full turns
Angius
Angius12mo ago
Well, try to fix that, then
Based
Based12mo ago
whereas right now it does a full game well i did and it doesnt seem to work
Angius
Angius12mo ago
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
Based
Based12mo ago
ive set bool variables for each button was clicked but scrapped that idea if its useful i could utilise them still
Based
Based12mo ago
Based
Based12mo ago
uhhh ill just send the code for each button press in 2 parts then ^ ignore this
Angius
Angius12mo ago
$paste if it's a lot of code
MODiX
MODiX12mo ago
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!
Angius
Angius12mo ago
Also, what's with all the global.s?
Based
Based12mo ago
its uh
Based
Based12mo ago
Based
Based12mo ago
public static class of globalish variables
Angius
Angius12mo ago
Why do they need to be global?
Based
Based12mo ago
the a1clicked and so on are all useless at this point
Angius
Angius12mo ago
Why can't they just be a part of the form?
Based
Based12mo ago
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
Based
Based12mo ago
BlazeBin - zkjqcuazjmzg
A tool for sharing your source code with the world!
Based
Based12mo ago
wait lemme comment where i implemented the check
Based
Based12mo ago
BlazeBin - dermlvpcsqpy
A tool for sharing your source code with the world!
Based
Based12mo ago
there
Angius
Angius12mo ago
Here's what I'd do.
enum Mark {
Circle,
Cross,
Empty
}
enum Mark {
Circle,
Cross,
Empty
}
private Mark[,] _board = new Mark[,] {
{ Mark.Empty, Mark.Empty, Mark.Empty },
{ Mark.Empty, Mark.Empty, Mark.Empty },
{ Mark.Empty, Mark.Empty, Mark.Empty }
}
private Mark[,] _board = new Mark[,] {
{ Mark.Empty, Mark.Empty, Mark.Empty },
{ Mark.Empty, Mark.Empty, Mark.Empty },
{ Mark.Empty, Mark.Empty, Mark.Empty }
}
void button1_Click()
{
_board[0,0] = Mark.Circle;
}
void button1_Click()
{
_board[0,0] = Mark.Circle;
}
Based
Based12mo ago
this is the window btw
Angius
Angius12mo ago
No booleans, no ambiguity
Based
Based12mo ago
im sorry i dont quite understand can you elaborate
Angius
Angius12mo ago
Well, in my implementation, the board is a 2D array of enums
Based
Based12mo ago
what is a enum
Angius
Angius12mo ago
And clicking a button changes the enum on the given position Think of enums as spicy integers
Based
Based12mo ago
ah so like intergers with strings or
Angius
Angius12mo ago
enum Mark {
Circle, // 0
Cross, // 1
Empty // 2
}
enum Mark {
Circle, // 0
Cross, // 1
Empty // 2
}
Based
Based12mo ago
ahh i see
Angius
Angius12mo ago
Named numbers, you could say
Based
Based12mo ago
i get that
Angius
Angius12mo ago
So you know which number is what
Based
Based12mo ago
but i dont think thats the greatest concern here because (although very messy) the 2d int array does work its just that
Angius
Angius12mo ago
Yeah, it does
Based
Based12mo ago
win condition needs to be read properly
Angius
Angius12mo ago
So, the check runs on a1_Click? Does it also run when other buttons are clicked?
Based
Based12mo ago
yeah so the numbering system is like
Based
Based12mo ago
Based
Based12mo ago
like this global.board[0, 0] being a1 global.board[0, 1] being a2 and so on b1 being [1, 0]
Angius
Angius12mo ago
Aight
Based
Based12mo ago
the Vs were just placeholders for the score values btw old screenshot
Angius
Angius12mo ago
And does the check run on every button press?
Based
Based12mo ago
yeah
Angius
Angius12mo ago
The same exact check?
Based
Based12mo ago
it did i removed them now but yes it was the exact same check for each button in the exact same location
Angius
Angius12mo ago
And after a couple of clicks the check would just randomly stop running?
Based
Based12mo ago
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
Angius
Angius12mo ago
I'd say you'll need to debug the code to see why that happens
Based
Based12mo ago
let me try to make a image real quick
Based
Based12mo ago
rough image but
Based
Based12mo ago
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
Angius
Angius12mo ago
Yeah, I have no clue why that would happen
Based
Based12mo ago
yeah this code is silly
Angius
Angius12mo ago
I think you really should just debug your code, that would give you more insight
Based
Based12mo ago
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
Angius
Angius12mo ago
That means it might be a good idea to divide your code into more classes and methods
Based
Based12mo ago
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)
Angius
Angius12mo ago
Use version control so you can always rollback the changes
Based
Based12mo ago
how do you use that
Angius
Angius12mo ago
And the syntax for classes and methods is extremely simple
Based
Based12mo ago
that would be wonderful to know
Based
Based12mo ago
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
Angius
Angius12mo ago
Click the Git button in the menu at the top
Based
Based12mo ago
yeah
Angius
Angius12mo ago
Since you have no repo yet, there should be an option to make one
Based
Based12mo ago
yeah
Based
Based12mo ago
Angius
Angius12mo ago
You can select "local only" if you don't want online backups
Based
Based12mo ago
created
Angius
Angius12mo ago
Now the git menu will have more stuff
Based
Based12mo ago
the branches right?
Angius
Angius12mo ago
Don't worry about branches
Based
Based12mo ago
oh alright then
Angius
Angius12mo ago
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
Based
Based12mo ago
ahh thats great
Angius
Angius12mo ago
Now you won't need to fear destroying your code
Based
Based12mo ago
huh why does it want a name and email
Angius
Angius12mo ago
So it knows who creates those commits
Based
Based12mo ago
if its storing locally it shouldnt need one unless ive done something wrong
Angius
Angius12mo ago
But those can be anything
Based
Based12mo ago
fair enough ig
Angius
Angius12mo ago
A name of aaa and email a@a.a will work
Based
Based12mo ago
should i set it global git config or whatever the checkbox is
Angius
Angius12mo ago
This will set the name and email globally on your PC, so other repositories will also use them
Based
Based12mo ago
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
Angius
Angius12mo ago
Make a method that checks that and call it on each click
class Form1 : Form
{
private int[,] _board = ...

private int CheckWinner()
{
// check who is the winner
}

public void button1_Click()
{
// do stuff
CheckWinner();
}
}
class Form1 : Form
{
private int[,] _board = ...

private int CheckWinner()
{
// check who is the winner
}

public void button1_Click()
{
// do stuff
CheckWinner();
}
}
Based
Based12mo ago
alright thank you i will try it and get back to you with it
Based
Based12mo ago
@ZZZZZZZZZZZZZZZZZZZZZZZZZ https://paste.mod.gg/qksemqloozhe/0
BlazeBin - qksemqloozhe
A tool for sharing your source code with the world!
Based
Based12mo ago
tried this
Based
Based12mo ago
Based
Based12mo ago
can click a button once then program just freezes
Angius
Angius12mo ago
Chances are, one of your do..while loops is infinite
Based
Based12mo ago
yeah guessed that dont see how it would get stuck in a infinte loop though is the problem
Angius
Angius12mo ago
Use the debugger and see The condition is never false Why exactly that is? Dunno
Based
Based12mo ago
uhhh alright i may or may not know how to use the debugger
Based
Based12mo ago
because this is what i added
Based
Based12mo ago
so if the if condition is true then the loop breaks but if it isnt then the loop breaks also
Angius
Angius12mo ago
$debug
MODiX
MODiX12mo ago
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.
Based
Based12mo ago
uhh where should i be starting the breakpoint or putting
Angius
Angius12mo ago
Placing it before the loop seems like a good idea
Based
Based12mo ago
that could work yeah @ZZZZZZZZZZZZZZZZZZZZZZZZZ HEY VERY EXCITING NEWS IT WORKS (kinda)
Based
Based12mo ago
Based
Based12mo ago
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
Based
Based12mo ago
Based
Based12mo ago
fixed the uhh placement thing problem is if you keep clicking then it keeps adding points
Based
Based12mo ago
put the win conditions in a if statement to stop em if the game has been won
Based
Based12mo ago
does not seem to work
Based
Based12mo ago
Based
Based12mo ago
Based
Based12mo ago
GOT EM GREYED OUT WHEN FINISHED
Angius
Angius12mo ago
Ayyy, nice!
Based
Based12mo ago
Based
Based12mo ago
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 !!!
Angius
Angius12mo ago
Anytime Ok
Based
Based12mo ago
i sure hope i dont catastrophically mess up my code when making a reset button
Angius
Angius12mo ago
You can always rollback to the previous commit
Based
Based12mo ago
? oh yeah the repo thing you taught me thanks for that btw
Angius
Angius12mo ago
The version control, yeah
Based
Based12mo ago
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
Angius
Angius12mo ago
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
Based
Based12mo ago
ohh that would be cool
Angius
Angius12mo ago
All you need is a Github account, then log into it with VS, and you'll be able to publish your repo
Based
Based12mo ago
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
Angius
Angius12mo ago
¯\_(ツ)_/¯ I never worked with Winforms
Based
Based12mo ago
fair enough STACK OVERFLOW!!!!!!!!
Based
Based12mo ago
Based
Based12mo ago
! problem forgot to put a draw condition too forgot bout that!
Based
Based12mo ago
Based
Based12mo ago
ALSO NEW GAME BUTTON WORKS!
Based
Based12mo ago
Based
Based12mo ago
Based
Based12mo ago
this should work as a draw condition i think and i hope
Based
Based12mo ago
Based
Based12mo ago
YES IT WORKS @ZZZZZZZZZZZZZZZZZZZZZZZZZ one last thing! how would i make another repo of this version of the code the menus changed a bit
Angius
Angius12mo ago
Why would you want another repository? Just make a new commit
Based
Based12mo ago
alright then ! ironed out a bug and got the length down to 354 using a subroutine
Angius
Angius12mo ago
Cut it in half then, nice
Based
Based12mo ago
ok i may have been wrong about the ironing out the bug seems like its still there its just something simple really
Based
Based12mo ago
the cpu makes another selection
Based
Based12mo ago
after i win
Angius
Angius12mo ago
Means the game doesn't stop early enough
Based
Based12mo ago
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
Angius
Angius12mo ago
Now, mind, do..while will execute code first, then check the condition to see if it should execute again
Based
Based12mo ago
only bug ive noticed so far
Angius
Angius12mo ago
while will check first, then execute if it should
Based
Based12mo ago
Based
Based12mo ago
Based
Based12mo ago
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
Angius
Angius12mo ago
Try a while instead of a do..while
Based
Based12mo ago
uh how do i do that? just while (global.finished == false) { ... } ?
Angius
Angius12mo ago
do
{
// stuff
} while (condition);
do
{
// stuff
} while (condition);
turns into
while (condition)
{
// stuff
}
while (condition)
{
// stuff
}
Based
Based12mo ago
wonderful
Based
Based12mo ago
Based
Based12mo ago
changed the do while to that
Based
Based12mo ago
Based
Based12mo ago
still places one after winning @ZZZZZZZZZZZZZZZZZZZZZZZZZ im gonna go to sleep gonna try fix it tomorrow
Angius
Angius12mo ago
A good night's sleep always helps
Based
Based12mo ago
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
Angius
Angius12mo ago
Yeah, that makes sense. Good job!
Based
Based12mo ago
Thanks man! I've finalised everything and submitted the project I'm finally finished!
Accord
Accord12mo ago
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.