Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Creating a TicTacToe Game in C#

Author: DevASP
Download Source Code : 450_tictactoe.zip

In this sample program I will create a TicTacToe game using C#.

Create a new application in C# and drag main Menu Control and Nine Button Controls on the form. Create a menu file and sub menu new in it. Now create global variablesto check the turn, game status and arrays of button

private System.Windows.Forms.Button [] _buttonArray; 

private bool _isX;

private bool _isGameOver;

  • Create a function:

public void InitTicTacToe()

{

for(int i=0;i<9;i++)

{

_buttonArray[i].Text = "";

_buttonArray[i].ForeColor = Color.Black;

_buttonArray[i].BackColor = Color.LightGray;

_buttonArray[i].Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

}

this._isX = true;

this._isGameOver = false;

}

  • Now write code on from load event

private void Form1_Load(object sender, System.EventArgs e)

{

_buttonArray = new Button[9] {but1, but2, but3, but4, but5, but6, but7, but8, but9};

for(int i = 0; i < 9; i++)

   this._buttonArray[i].Click += new System.EventHandler(this.ClickHandler);

InitTicTacToe();

}

  • On "new" menu item click

private void menuItem2_Click(object sender, System.EventArgs e)

{

   InitTicTacToe();

}

  • For buttons we have a following funtion

private void ClickHandler(object sender, System.EventArgs e)

{

Button tempButton = (Button)sender;

if( this._isGameOver )

{

MessageBox.Show("please start new game!","Game End",MessageBoxButtons.OK);

return;

}

if( tempButton.Text != "" )

{

return;

}

if( _isX )

tempButton.Text = "X";

else

tempButton.Text = "O";

_isX = !_isX;

this._isGameOver = result.CheckWinner(_buttonArray );

}

}

  • Finally create a class to check the result

public class result

{

static private int [,] Winners = new int[,]

{

{0,1,2},

{3,4,5},

{6,7,8},

{0,3,6},

{1,4,7},

{2,5,8},

{0,4,8},

{2,4,6}

};

static public bool CheckWinner( Button [] myControls )

{

bool gameOver = false;

for(int i = 0; i < 8; i++)

{

int a = Winners[i,0],b=Winners[i,1],c=Winners[i,2];

Button b1=myControls[a], b2=myControls[b], b3=myControls[c];

if(b1.Text == "" || b2.Text == "" || b3.Text == "" )

continue;

if(b1.Text == b2.Text && b2.Text == b3.Text )

{

b1.BackColor = b2.BackColor = b3.BackColor = Color.LightCoral;

b1.Font = b2.Font = b3.Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Italic & System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

gameOver = true;

MessageBox.Show(b1.Text + " .... Wins the game!","Game End",MessageBoxButtons.OK);

break;

}

}

return gameOver;

}

}

Article Comments
thanks alot.
this is a simple and fine program.
bye.

Posted on 3/1/2007 12:59:33 PM by mojtaba barari

el codigo de tic-tac-toe C# con su respectiva imagen gracias.

Posted on 5/30/2007 2:55:28 PM by dario fernando

I want to make same game using GDI Drawer. Can you send me code for that if you have.

Thank you very much

Posted on 3/16/2010 11:01:00 PM by dhaval

helped me alot...very much simple... thanks..

Posted on 5/3/2010 12:17:18 AM by raja..

please help me to become a good programmer.. heheeee
THANKS!!

Posted on 7/27/2010 3:49:21 AM by jhenina

plz help me tic tak code

Posted on 10/30/2010 5:29:20 AM by mandeep kaur

hey can u plz email me about menuitem_click event....whats mean of InitTicTacToe().

Posted on 11/23/2010 9:14:57 AM by payal

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Creating Console Application to copy Array in C#

Disclaimer - Privacy
© 2002-2017 DevASP.net