Hack Forums
[C#] [C#][SRC][TuT]Simple TicTacToe - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] [C#][SRC][TuT]Simple TicTacToe (/showthread.php?tid=1028682)



[C#][SRC][TuT]Simple TicTacToe - Ethereal - Feb 3, 2011

Please note: All the information in this tutorial was written by me. I simplified everything, so as to avoid “Get/Set” clauses and Graphics, and instead use simplistic conditionals. Thank you Deities for helping me format it.

My example project is here. The aim of this tutorial is to promote tidy code, and to utilise proper OOP potential. This means separating everything out into reusable code/functions, in order to increase efficiency.

NOTE* This tutorial is subject to update.


[Image: a819eb162ff9652e00b1636cdb3ec0af.png]

Before we get started! (Click to View)
  • 9 Square shaped buttons with no text – This is your game area
  • 3 other buttons, other shapes is fine. These should contain the text “Reset”, “X To Start” and “O to Start”
  • 5 Labels. Three will be counters with the text “X won 0 times”, “O won 0 times” and “You have drawn 0 times”, and the other two will be ‘””’ and X’s turn.
Next, rename your components to something more memorable, so as to not confuse yourself later on. I left the square buttons as they were, and renamed the other buttons to their respective text(“Reset” to “Reset” etc.). The labels should be something along the lines of “xCounterLabel”, “DrawCounterLabel” and “oCounterLabel”, with the two turn-based labels being “xLabel” and “oLabel”. It always irritates me when I find huge source codes with “button24” and “label9”, with no clear indication of what each one is.
Now, double click the form and create several new variables:
PHP Code

<?php 
bool X 
= true; //Used for testing who's turn it is.
bool O = false; //Ditto
int xWinCounter = 0; //The counters
int oWinCounter = 0; //Ditto
int DrawCounter = 0; //Ditto 
This will be used to determine who’s go it is, and to keep the counters working.

Next, create a new region of your choice, which will contain all of the “button_click” events. This is simply to keep our code tidy, without having to separate it into separate classes(which gets irritating).
PHP Code

<?php 
#region Button Logic
  //This region/section is for all of our "button_click" events. Lets keep our code tidy, eh?

#endregion //End the Button Logic region. 
Next, click all of your buttons, and move their click events to this region(if needed).
PHP Code

<?php 
#region Button Logic
  //This region/section is for all of our "button_click" events. Lets keep our code tidy, eh?
  
private void button1_Click(object sender, EventArgs e) /*The first button. The buttons on my form are numbered the same as a phone.*/
  
{

  }
#endregion 


Button Logic Region! (Click to View)

The Validation Region! (Click to View)

The Extra Functions Region! (Click to View)



* - Cephei - Feb 3, 2011

I like it. Thanks for the release.


* - RiperFish - Feb 3, 2011

Thank's for the Tut budy ( original = 5/5 ).


* - GamingManiac - Feb 3, 2011

Thanks for this - I did a C++ console program that does the same thing, just with no GUI. so i understand the logic

Very nice dude



* - Fake. - Feb 3, 2011

Nooo, don't use simple conditionals to check the wins ):
Think of arrays and such.


* - Ethereal - Feb 3, 2011

(Feb 3, 2011 01:27 PM)jacktheripper Wrote:  Nooo, don't use simple conditionals to check the wins ):
Think of arrays and such.

Incase you didn't read:

I said at the top that I had simplified the code for people to learn from.




* - Fake. - Feb 3, 2011

(Feb 3, 2011 01:31 PM)Ethereal Wrote:  Incase you didn't read:

I said at the top that I had simplified the code for people to learn from.

Ah shit, my bad. I read everything else, sorry about that.


* - Ethereal - Feb 4, 2011

(Feb 3, 2011 10:41 PM)jacktheripper Wrote:  Ah shit, my bad. I read everything else, sorry about that.

It's no problem.

People, feel free to post is something confuses you :)