Click here to Skip to main content
11,702,130 members (62,026 online)
Click here to Skip to main content

Tic Tac Toe Implemented in C# with Computer Player

, 10 Aug 2010 CPOL 81.4K 7.1K 32
Rate this:
Please Sign up or sign in to vote.
A simple game in C# that can run on Windows, Linux(mono) and Mac(mono)

Introduction

Capture.JPG Screenshot.jpg
The game in Windows (.NET)
The Game in Linux (Mono)

This is a program for Tic Tac Toe game written in C#. It has a built in Computer Player module and can even be played in two player mode. It can be run anywhere where .NET or mono runtime is installed. The source is compatible with Visual Studio and #Develop.

Background

During my exploration of Java and C#, I found some interesting similarities and differences and ultimately learned that with study of one of them, the other one can also be learned. This program is my attempt to demonstrate the same to beginners in programming.

Using the Code

For ease of learning the program, it is divided into modules which are represented as functions in the program. Simple labels are used for display of the nine blocks involved, and corresponding code to be executed is triggered through click event of them. First, the variables used are explained as below:

  • pos: is a two dimensional array to represent the nine blocks as the operations are easy to perform on an array.
  • cnt: is a counter to track the number of moves played.
  • val: is a value corresponding to the letter. 1 for X and 4 for O. In array, the values are used instead of letters.
  • let: is to hold the letter, X or O.
  • a, b, c, d: are the integers to hold the co-ordinates of second last and last move respectively, used by computer player logic.
  • diff and vs: are used to identify difficulty level and game mode selected by the user.
  • rnd & turn: are used to generate random number and to toggle comp player mode.
  • pl1 & pl2: are used to hold the names to be displayed in status strip.

The functions with their use are as explained below:

  • reset(): This function is used to restart the game from start anytime or after win or draw is declared as well as to initialize certain components.
  • play(): This function does the work of commencing a move, thus updating the corresponding label and the array position as well as calling other functions flip() and checkwin(). It uses function link() to evaluate coordinates to corresponding labels.
  • flip(): It does the job of toggling between X and O during the moves eventually doing the same for values 1 & 4 for variables let and val respectively.
  • checkwin(): This function checks for win or draw condition after a move is played in the program. It also declares so using declare() and also manages toggle of opponents as per the rule: The first player continues to play if he wins or game draws, else the 2nd player will play first the next time.
  • compplay(): This is the computer player module which makes a computer move using the difficulty level selected by the player and calling functions winorstop() and doany() appropriately.
  • winorstop(): This function is used to play the winning move if such a situation exists as well as to stop opponents win if a losing situation exists.

The arrangement of labels and array positions are as below:

labe1 label2 label3 0,0 0,1 0,2
label6 label5 label4 1,0 1,1 1,2
label9 label8 label7 2,0 2,1 2,2

The variables as well as the code for different functions is as below:

int[,] pos=new int[3,3];
int cnt,val,a,b,c=1,d=1,diff=1,vs=1;
char let;
String pl1="You",pl2="Computer";
Random rnd=new Random();
bool turn=true; 
void reset()
	    {
	        for (int i=0;i<3 ;i++ )
	        {
	            for (int j=0;j<3 ;j++ ){pos[i,j]=0;}  //Fill array with zeros
	        }
	        foreach(Control ctrl in this.Controls)
			{
				if (ctrl is Label) 
				{
					ctrl.ResetText();  //Clear text for 
							//all labels
				}
			}
	        cnt=0;
	        val=1;  // X->1 and O->4
	        let='X';
	        label10.Text=pl1+" to Play NOW.";  //Setting status label.
	    }
bool play(int l,int m)
	    {
	        if(pos[l,m]==0)// Check to avoid overplays.
	        {
	            a=c;b=d;c=l;d=m;  //Hold coordinates of 2nd last and last moves.
	            Label ctrl=link(l,m);  //Link the coordinates to the label.
					// (used for computer player)
	            ctrl.Text=let.ToString();  //Reflecting move to screen.
	            pos[l,m]=val;  //Reflecting move in array
	            flip();  //Toggling between X and O
	            checkwin(l,m,pos[l,m]);  // Check for win or Draw situation.
	            return true;
	        }
	        else
	            return false;  //Useful if move is to be replayed.
	    }
Label link(int l,int m)  //Returning appropriate label for the passed coordinates.
	    {
	        if(l==0)
	        {
	            if(m==0)
	                    return label1;
	            if(m==1)
	                    return label2;
	            if(m==2)
	                    return label3;
	        }
	        if(l==1)
	        {
	            if(m==0)
	                    return label6;
	            if(m==1)
	                    return label5;
	            if(m==2)
	                    return label4;
	        }
	        if(l==2)
	        {
	            if(m==0)
	                    return label9;
	            if(m==1)
	                    return label8;
	            if(m==2)
	                    return label7;
	        }
	        return null;
	    }
void flip()   //Logic for toggle
	    {
	        if(let=='X')
	        {
	            let = 'O';
	            val=4;
	            cnt++;
	        }
	        else
	        {
	            let = 'X';
	            val=1;
	            cnt++;
	        }
	    } 
void checkwin(int l,int m,int n)
       {
           if(cnt==1)
               if(vs==1)
                   turn=true;
           if(cnt>4)
           {   // Check for corresponding row first.
               if((pos[l,0]+pos[l,1]+pos[l,2]==n*3)||
               (pos[0,m]+pos[1,m]+pos[2,m]==n*3))
               {
                   cnt=n;
               }
               else
               {   //Checking for corresponding column.
                   if((pos[0,0]+pos[1,1]+pos[2,2]==n*3)||
               (pos[2,0]+pos[1,1]+pos[0,2]==n*3))
                   {
                       cnt=n;
                   }
                   else
                   {
                       if(cnt==9)
                       {   //In a draw situation.
                               cnt=0;
                       }
                   }
               }
               if(cnt==1||cnt==0)
               {  // If the first player wins or Draw occurs.
                   if(cnt==1)
                       declare(pl1+" (Playing X) Wins!");
                   if(cnt==0)
                       declare("The Game is a Draw!");
                   reset();
                   if(vs==1)
                   if(pl1=="Computer")
                   {
                       turn=false;
                       compplay(val);      //If the First player happens
                   //to be computer we need to call it.
                   }
                   else
                       turn=false;

               }
               else
               if(cnt==4)
               {
                   declare(pl2+" (Playing O) Wins!");
                   String temp=pl1;
                   pl1=pl2;
                   pl2=temp;
                   reset();
                   if(vs==1)
                   if(pl1=="Computer")
                       compplay(val);      // If the first palyer is computer,
                   // we need to call this.
                   else
                       turn=false;
               }
           }
       }

       void declare(string stmt)
       {
           if(MessageBox.Show(stmt+" Do you want to continue?",
               "",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
                   !=DialogResult.Yes)
           {
               Application.Exit();     //Exit if user does
                           //not click yes.
           }
       }
void compplay(int n)
	    {
	        bool carry=true;  	// Is used so that multiple moves are not played 
				// by computer.
	        if(diff==3)  	//Is called only if Hard difficulty is set.
	            carry=winorstop(a,b,n); // a & b are used so that check 
					// is performed only at last computers move.
	        if((diff==2||diff==3) && carry)  //Is called if Hard or Medium 
						// difficulty is set.
	        {// For stop require to check for opponents pieces using c & d
	            if(n==1)
	                carry=winorstop(c,d,4);
	            else
	                carry=winorstop(c,d,1);
	        }
	        if(carry)
	                doany();  // Executed in all three difficulty levels
	    }  
bool winorstop(int l,int m,int n)
	    {
	        if(pos[l,0]+pos[l,1]+pos[l,2]==n*2) //check for row, 
						// if two of three are filled.
	        {
	            for(int i=0;i<3;i++)
	            {
	                if(play(l,i))
	                    return false;
	            }
	        }
	        else
	            if(pos[0,m]+pos[1,m]+pos[2,m]==n*2) //Check for column for 2/3
	            {
	                for(int i=0;i<3;i++)
	                {
	                    if(play(i,m))
	                        return false;
	                }
	            }
	            else
	                if(pos[0,0]+pos[1,1]+pos[2,2]==n*2) //Check for diagonal 
							//for 2/3 situation.
	                {
	                        for(int i=0;i<3;i++)
	                        {
	                                if(play(i,i))
	                                        return false;
	                        }
	                }
	                else
	                    if(pos[2,0]+pos[1,1]+pos[0,2]==n*2) //Check for 
						//other diagonal for 2/3 situation.

	                    {
	                            for(int i=0,j=2;i<3;i++,j--)
	                            {
	                                    if(play(i,j))
	                                            return false;
	                            }
	                    }
	
	        return true;
	    } 
void doany()
	    {
	        int l=2,m=0;
	        switch(cnt)
	        {
	            case 0: play(0,0);   //First two moves are certainly played.
	                    break;       
	            case 1: if(!(play(1,1)))
	                        play(0,0);
	                    break;
	            case 2: if(!(play(2,2)))
	                        play(0,2);
	                    break;
	            case 3: if((pos[0,1]+pos[1,1]+pos[2,1])==val)
	                        play(0,1);
	                    else
	                        if((pos[1,0]+pos[1,1]+pos[1,2])==val)
	                            play(1,0);
	                        else
	                            if(pos[0,1]!=0)
	                                play(0,2);
	                            else
	                                play(2,0);
	
	                    break;
	            default : while(!(play(l,m)))
	                      {
	                        l=rnd.Next(3);  // Random moves are played
	                        m=rnd.Next(3);  //Until at least one is successful
	                      }
	                    break;
	        }
	    } 
void Label1Click(object sender, EventArgs e)
		{
			if(play(0,0)&&turn==true)
                        compplay(val); // Is executed only if Players move 
				//was successful and the game is in Vs computer mod.
		}     

The fow of operation is as follows

  1. The player will click on label transferring control to play().
  2. Play() will check if the corresponding array position is empty, which is necessary for not overlapping a move by player as well as computer player module.
  3. If the move succeeds, the corresponding label is updated as well the array element corresponding to the label. And soon flip() and checkwin() are called.
  4. flip() will change the value of let and val for use by next move.
  5. checkwin() will use the coordinates passed by play() to check only the corresponding row and column, if that fails both diagonals are checked. The function is active only after at least four moves are played.
  6. The control returns to the click event function where turn determines if comp player module should be activated, in two player mode it waits for second player.
  7. If vs computer is selected, the compplay() is called.
  8. compplay() calls the other functions as per the difficulty level selected by user:
    1. Easy: Only doany() is called.
    2. Medium: winorstop() is called in stop mode first and then doany().
    3. Hard: winorstop() is called in win mode, then stop mode and finally doany() is called.
  9. When a win or draw is encountered, the first player plays again. If it happens to be the computer players turn, the compplay() is called from within the checkwin() itself.
  10. Though doany() is meant to do random moves, certain moves are played so that the Easy mode does not appear CRAZY.

Points of Interest

Applying effects such as transparency in C# and implementing the same in Java was really funny and at the same time educative. Going through the equivalent code the C#, a developer can learn Java and vice versa.

History

This is a replacement for the two player version uploaded earlier, at the same time improvement for the same. The previous version is deleted to eliminate redundancy. Wink | ;)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Tejas_Pathak
Engineer
India India
I was a C# developer initially...
But the urge to write True Platform Independent applications drove me to code in Java Lately.
The IDEs I use are #develop, NetBeans, Eclipse etc.

BE(Computers)Mumbai University.
ME(ETC-Microelectronics)Goa University.

In case of any queries/doubts/questions/clarification, you can always drop me a message on Facebook on the link:

http://www.facebook.com/epathak.in

You may also be interested in...

Comments and Discussions

 
QuestionRegarding the Tic Tac toe game developed by you Pin
Member 1038301528-May-15 5:21
memberMember 1038301528-May-15 5:21 
Questionhelp please Pin
Member 103615998-Nov-13 0:48
memberMember 103615998-Nov-13 0:48 
GeneralTank's Pin
Member 1026501210-Sep-13 6:58
memberMember 1026501210-Sep-13 6:58 
Questioni am doing tic tac toe. Pin
davidwong75021-Jul-13 4:13
memberdavidwong75021-Jul-13 4:13 
GeneralIm doing a tic tac toe game too Pin
Member 1007413623-May-13 18:18
memberMember 1007413623-May-13 18:18 
GeneralMy vote of 3 Pin
Member 1005552919-May-13 17:23
memberMember 1005552919-May-13 17:23 
BugComputer skips its turn Pin
Tony Rueb17-Jan-13 8:45
memberTony Rueb17-Jan-13 8:45 
GeneralRe: Computer skips its turn Pin
Tony Rueb17-Jan-13 8:57
memberTony Rueb17-Jan-13 8:57 
GeneralRe: Computer skips its turn Pin
Tony Rueb17-Jan-13 14:34
memberTony Rueb17-Jan-13 14:34 
GeneralRe: Computer skips its turn Pin
Tejas_Pathak17-Jan-13 18:00
groupTejas_Pathak17-Jan-13 18:00 
QuestionPermission Pin
Deanwilliammills23-Oct-12 4:42
memberDeanwilliammills23-Oct-12 4:42 
AnswerRe: Permission Pin
Tejas_Pathak24-Oct-12 21:04
groupTejas_Pathak24-Oct-12 21:04 
GeneralMy vote of 3 Pin
leena2063-Apr-12 20:55
memberleena2063-Apr-12 20:55 
QuestionRe: My vote of 3 Pin
Tejas_Pathak3-Jul-12 22:53
groupTejas_Pathak3-Jul-12 22:53 
AnswerRe: My vote of 3 Pin
leena2063-Jul-12 23:32
memberleena2063-Jul-12 23:32 
GeneralRe: My vote of 3 Pin
Tejas_Pathak3-Jul-12 23:53
groupTejas_Pathak3-Jul-12 23:53 
GeneralRe: My vote of 3 Pin
leena2064-Jul-12 2:13
memberleena2064-Jul-12 2:13 
GeneralRe: My vote of 3 Pin
leena20617-Jul-12 22:34
memberleena20617-Jul-12 22:34 
GeneralRe: My vote of 3 Pin
Tejas_Pathak26-Jul-12 0:17
groupTejas_Pathak26-Jul-12 0:17 
GeneralMy vote of 1 Pin
Jon Artus1-Apr-12 23:46
memberJon Artus1-Apr-12 23:46 
AnswerRe: My vote of 1 Pin
Tejas_Pathak3-Jul-12 22:49
groupTejas_Pathak3-Jul-12 22:49 
GeneralMy vote of 3 Pin
zoma200928-Feb-11 6:59
memberzoma200928-Feb-11 6:59 
GeneralMy vote of 3 Pin
John Whitmire17-Aug-10 5:50
memberJohn Whitmire17-Aug-10 5:50 
GeneralTwo dimensional Arrays for controls Pin
vytheese16-Aug-10 20:20
membervytheese16-Aug-10 20:20 
GeneralMy vote of 2 Pin
Niklas Lindquist11-Aug-10 0:33
memberNiklas Lindquist11-Aug-10 0:33 
GeneralRe: My vote of 2 Pin
SafarTimura11-Aug-10 1:39
memberSafarTimura11-Aug-10 1:39 
GeneralRe: My vote of 2 Pin
Tejas_Pathak11-Aug-10 3:21
groupTejas_Pathak11-Aug-10 3:21 
GeneralRe: My vote of 2 Pin
Tejas_Pathak11-Aug-10 3:19
groupTejas_Pathak11-Aug-10 3:19 
GeneralRe: My vote of 2 Pin
Niklas Lindquist11-Aug-10 5:34
memberNiklas Lindquist11-Aug-10 5:34 
GeneralRe: My vote of 2 Pin
Tejas_Pathak11-Aug-10 22:29
groupTejas_Pathak11-Aug-10 22:29 
GeneralRe: My vote of 2 Pin
Niklas Lindquist11-Aug-10 23:42
memberNiklas Lindquist11-Aug-10 23:42 
GeneralMy vote of 5 Pin
secretadmirer201010-Aug-10 20:56
membersecretadmirer201010-Aug-10 20:56 
GeneralMy vote of 3 Pin
Mycroft Holmes10-Aug-10 14:08
mvpMycroft Holmes10-Aug-10 14:08 
GeneralMy vote of 3 Pin
Josh_Jackson10-Aug-10 13:43
memberJosh_Jackson10-Aug-10 13:43 
GeneralRe: My vote of 3 Pin
Tejas_Pathak10-Aug-10 20:42
groupTejas_Pathak10-Aug-10 20:42 
GeneralMy vote of 1 Pin
Josh_Jackson9-Aug-10 10:56
memberJosh_Jackson9-Aug-10 10:56 
GeneralRe: My vote of 1 Pin
Tejas_Pathak9-Aug-10 17:30
groupTejas_Pathak9-Aug-10 17:30 
AnswerRe: My vote of 1 Pin
Josh_Jackson10-Aug-10 12:58
memberJosh_Jackson10-Aug-10 12:58 
GeneralRe: My vote of 1 Pin
Nishant Sivakumar10-Aug-10 13:50
mvpNishant Sivakumar10-Aug-10 13:50 
GeneralRe: My vote of 1 Pin
Tejas_Pathak10-Aug-10 20:33
groupTejas_Pathak10-Aug-10 20:33 
GeneralRe: My vote of 1 Pin
Rick York11-Aug-10 7:31
memberRick York11-Aug-10 7:31 
GeneralRe: My vote of 1 Pin
Tejas_Pathak11-Aug-10 22:34
groupTejas_Pathak11-Aug-10 22:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 10 Aug 2010
Article Copyright 2010 by Tejas_Pathak
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid