Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

[C#][SRC][TuT]Simple TicTacToe

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)
μεγαλομανία
I like it. Thanks for the release.
Thank's for the Tut budy ( original = 5/5 ).

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
Knowledge is power and the only source of knowledge is experience.
Nooo, don't use simple conditionals to check the wins ):
Think of arrays and such.
[Image: Xtddh8V.png]
(Feb 3, 2011 01:27 PM)Fake. 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.

μεγαλομανία
(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.
[Image: Xtddh8V.png]
(Feb 3, 2011 10:41 PM)Fake. 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 :)
μεγαλομανία


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by charmeurJul 23, 2016 10:34 AM 1 97 Jul 23, 2016 10:34 AM
Last Post: Stealth™
[Python] by m00dyJun 8, 2015 07:48 PM 7 364 Jun 8, 2015 07:48 PM
Last Post: LeKlutch
[Java] by High On QuackJan 29, 2015 07:07 PM 9 380 Jan 29, 2015 07:07 PM
Last Post: High On Quack