Creating Tic-Tac-Toe with C# -
Form1 design
okay, this form will look something like this:
I used a TableLayoutPanel to centralize the buttons.
Form1 Coding
double click the 2-Player button and write this code:
1
2
3
| Form2 f2= new Form2();this.Hide();f2.Show(); |
This code will hide the current form, and open the second form.
I will leave the second button "Player cs PC" for the Part 2 of this tutorial.Form2 design
- Create a tableLayoutPanel, set the Dock property to Fill, divide it to 4 columns with percentage 25% of each, and 4 rows with the percentages: 33.33%, 16.66%, 16.66%, 33.33%.
- Add a button with the Dockproperty set to Fill and an empty Text property. Copy and paste until you have 9 buttons filling the cells: (0,0) ; (0,1) ; (0,2) ; (1,0) ; (1,1) ; (1,2) ; (3,0) ; (3,1) ; (3,2) ;
- Select the 3 middle small height buttons and change theirRowSpan property to 2.
- Select all created buttons and set their Font property to: Tahoma; 36pt.
- Fill the last column with 2 buttons and two labels as the picture above shows, change their names to: resetButton, xLabel(blue), oLabel(red), backButton. We will use these names in our code.
Form2 Coding
Declarations
12345Button btn;//to get the clicked button dataintturn/*to determine wich turn*/,counter/*for the played turns*/;bool[,] X/*X player buttons*/, O/*O player buttons*/, cells/*Occupied cells*/, current/*X or O*/;//using 3x3 coordinate systemstringplayer;//X or Oboolfinished;// true when the game finishReset button and Form load
you can notice that I used a helper method in the last line to reset the variables. I'm resetting variables on both events. I'm going to write the code of all helper methods after finishing the main ones.1234567891011121314151617181920privatevoidresetButton_Click(objectsender, EventArgs e){//clear buttons text propertybutton1.Text =button2.Text =button3.Text =button4.Text =button5.Text =button6.Text =button7.Text =button8.Text =button9.Text ="";resetVariables();}privatevoidForm2_Load(objectsender, EventArgs e){resetVariables();}Back button
This code will take us back to the menu (Form1).123456privatevoidbackButton_Click(objectsender, EventArgs e){this.Hide();Form1 f1 =newForm1();f1.Show();}Gaming buttons (9 buttons)EXPLAINED (Improved, thanks to Yahia)
Now we still have to write five helper methods, that we used earlier but we didn't write their codes.12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152privatevoidgamePlayButtons_Click(objectsender, EventArgs e){intx,y;//ensure that the user is clicking a button, if yes give me its coordinate (x,y)//ensure that the game has not finished yetif(!wasClickedBefore(senderasButton,outx,outy)&&!finished){counter ++;btn = (senderasButton);//the clicked button from the 9 buttonsif(turn == 0){(senderasButton).ForeColor = Color.Blue;//changing color to Blueplayer ="X";X[x,y]=true;//the position(x,y) is occupied now by an Xcurrent = X;}else{(senderasButton).ForeColor = Color.Red;//changing color to Redplayer ="O";O[x, y] =true;//the position(x,y) is occupied now by an Ocurrent = O;}(senderasButton).Text = player;//writing text propertycells[x,y]=true;// this cell is no longer emptyturn = (turn + 1) % 2;//switching turns, this will switch between 0 and 1//a Win occurs when at least 5 turns are playedif(counter>=5)//Check for a winif(Row(0) || Row(1) || Row(2) || Column(0) || Column(1) || Column(2) || Diagonals()){//updating scoreif(player=="X")xLabel.Text=(int.Parse(xLabel.Text)+1).ToString();elseoLabel.Text=(int.Parse(oLabel.Text)+1).ToString();finished =true;//game finishedMessageBox.Show(string.Format("{0} Wins", player));//showing result}if(counter==9&&!finished)//It's a draw here{finished =true;MessageBox.Show("Draw");}}}Check the win on diagonals
12345678privateboolDiagonals(){return((current[0, 0] && current[1, 1] && current[2, 2])||(current[0, 2] && current[1, 1] && current[2, 0]));}Check the win on a Row/Column
12345678privateboolRow(intp){return(current[p, 0] && current[p, 1] && current[p, 2]);}privateboolColumn(intp){return(current[0, p] && current[1, p] && current[2, p]);}Was it clicked before?
1234567891011121314151617privateboolwasClickedBefore(Button button,outintx,outinty){x = y = 0;switch(button.Name){case"button1": x = 0;y=0 ;break;case"button2": x = 0;y=1 ;break;case"button3": x = 0;y=2 ;break;case"button4": x=1;y= 0;break;case"button5": x=1;y= 1;break;case"button6": x=1;y= 2;break;case"button7": x=2;y=0;break;case"button8": x=2;y= 1;break;case"button9": x = 2;y= 2;break;}return(X[x, y]||O[x,y]);}Reset Variables
123456789privatevoidresetVariables(){finished =false;turn = 0;O =newbool[3, 3];X =newbool[3, 3];cells =newbool[3, 3];counter=0;}Now we still have to write five helper methods, that we used earlier but we didn't write their codes.


