How to Make a Game in C++ Visual Studio: Tic Tac Toe

The visual studio is an IDE (Integrated Development Environment) mainly used to create various applications and chiefly gaming applications. This software provides its user with a comfortable environment to produce any game or application. 

It supports some built-in high-level programming languages like C, C++, and C#. Moreover, it also sustains other languages like python and java. Its free version, containing all the features, is also available on its website https://www.visualstudio.com/downloads for programmers.

Related:

Requirements to Develop a Game

There are certain requirements to develop a game in C++.

Windows Vista/7/8/8.1:: These windows can be used to create any game in C#.Net using Visual Studio.

Visual Studio:: It is also packaged software, install it on your system.

Skills: Requirement of basic and root knowledge of C, C++, and C#.

How to Make a Game in C++ Visual Studio

The game development on C++ visual studio is a piece of cake if you understand this article properly.

To make this guide more practical for you, I will be giving you an example of a tic tac toe game, so the difficulty level decreases to some extent. So my impatient readers, let’s start with our mission.

Creating Tic tac toe game

Setting up Visual Studio for game development

Step:1 First, you have to open the Visual Studio app, which you installed in the Windows 7 or 8, or 8.1 version of your PC.

Step:2 After opening the application, you need to click on the FILE option in the menu bar. Some options will appear when you click on the file. It would be best if you opted for the NEW PROJECT option.

Open new game Project in Visual Studio

Step: 3 Then give your project a good attractive name like if I want to develop a tic tac toe game, I will name my project Mind Boosting Tic Tac Toe Game.

Step:4 In the middle of the screen, you will see a basic dialogue box with “FORM 1”.

Step:5 A dialogue box of properties will appear on the right side of the screen. There you can see a property named “Show Icon”. If you do not want to show your icon, you can remove it by switching it from “True to False”. And your icon will disappear.

Step: 6 You can also change the form name by clicking on the “Text” option.

Step:7 Drag the tool named “Tool Strip” to the form from the toolbox dialogue.

Step: 8 You need to create “File” and “Help” (two menus). There will be an empty box that will ask you to “Type Here”. You can type the menus over there by writing in it these words. “The menus are not functional after we had changed the names. We need to have further proceedings”. 

Update the form properties and hide icon, disable maximize

Step:9 Add “New Game” and “Quit” as sub-menus by clicking on the “File” in the form. You can also add the “About” submenu to the help menu. 

Creating games in a visual code studio

Step: 1 Drag the “Button” named tool to the form from the toolbox dialogue. As I am making a Tic Tac Toe Game, there will be nine buttons in the game. It would help if you dragged the button tool nine times.

Step.2 Click on one of the button tools after you are done adjusting the workspace’s size (buttons).

Add button and resize it

Step.3 Then, a “Property Dialogue” is added to regulate the game’s font. As I am developing the tic tac toe game, I will use the font of “X” and “O” to appear big enough on the screens. The font of each button should be the same, so the game gives a synchronized look.

In this Tic tac Toe game, there will be a need to rename each button. There are nine buttons in my game, so I will name them in a Matrix form: (Button1 as A1, Button 2 as A2, Button 3 as A3, Button 4 as B1, Button 5 as B2, Button 6 as B3, Button7 as C1, Button 8 as C2 and Button 9 as C3). Here A, B, and C are rows 1, 2, and 3. It will be helpful for us to further develop our game. 

Coding Tic Tac toe game in visual code studio

Step.1 At this point, you will come across a basic C# program. Now, you need to type a message in “private void aboutToolStripMenu…….”. The message will be as follows:

  MessageBox.Show("By //Your name//”, " Tic Tac Toe About ");

Step.2 After typing MessageBox.Show a set of 21 string options that will appear. You will click on the third string.

Step.3 A task for the “//Exit//menu” is added. Here you will type “‘private void exitToolStripMenu…… ‘.

Application.Exit();

Step.4 you need to do the same with the “Exit” and “About” menus. With the “New Game” menu, you need to double-click it. Now type this function in “ private new game tool……….”

turn = ture;
turn_count = 0;
 
 
foreach(Control c in Controls){
  Button b = (Button)c;
  b.Enabled = true;
  b.Text = "";
}
catch { }

Writing Tic Tac toe game code in visual code studio

Step.1 After you have entered the programming section, under “Public Partial Class” write this code:

bool turn = true;
int turn_count = 0;

Step.2 Now, tap on any one of the buttons in the form. Then, click on the “Thunder Bolt” Icon present in the properties.

Step.3 “Click Button” should be typed under the “Click” property. Apply it to all nine buttons.

Step.4 The code written below should be typed in the “Public Partial Class”.

gray out box on click
private void button_click(object sender , EventArgs e){
  Button b = (Button)sender;
  if(turn)
    b.Text = "X";
  else
    b.Text = "O";
  turn = !turn;
  b.Enabled = false;
  turn_count++;
  check();
}

Declaring the Winner

  • Checking and declaring the winner is our main achievement. 
  • Here we will develop a new class.
  • So, ultimately the renaming of the button is proved handy over here.
  • Type this piece of code under “public partial class”.
private void check(){ 
bool winner = false; 

//Horizontal checking 
if((A1.Text==A2.Text)&&(A2.Text==A3.Text)&&(!A1.Enabled)) 
  winner = true; 
else if((B1.Text==B2.Text)&&(B2.Text==B3.Text)&&(!B1.Enabled)) 
  winner = true; 
else if((C1.Text==C2.Text)&&(C2.Text==C3.Text)&&(!C1.Enabled)) 
  winner = true; 

//Vertical Checking 
else if((A1.Text==B1.Text)&&(B1.Text==C1.Text)&&(!A1.Enabled)) 
  winner = true; 
else if((A2.Text==B2.Text)&&(B2.Text==C2.Text)&&(!A2.Enabled)) 
  winner = true; 
else if((A3.Text==B3.Text)&&(B3.Text==C3.Text)&&(!A3.Enabled)) 
  winner = true; 

//Oblique Checking 
else if((A1.Text==B2.Text)&&(B2.Text==C3.Text)&&(!A1.Enabled)) 
  winner = true; 
else if((A3.Text==B2.Text)&&(B2.Text==C1.Text)&&(!A3.Enabled)) 
  winner = true; 

if(winner){ 
  disableButtons(); 
  String win = ""; 
  if(turn) 
    win = "O"; 
  else 
    win = "X"; 
  MessageBox.Show(win + " Wins ! " , "Yay"); 
 // Click Image for more clarity(string operation 2/21) 
} 
else{ 
  if(turn_count == 9) 
    MessageBox.Show("Its a Draw !"); 
  } 
}//End of class
Tik Tac Toe game in C#.net winner
  • As soon as the winner is declared and announced, the buttons should be disabled.
  • We have to create a new class under “public partial class”. And type this:
private void disableButtons() {
  try{
    foreach(Control c in Controls){
      Button b = (Button)c;
      b.Enabled = false;
    }
  }
  catch { }
}

Enjoy the Game

Now your game is developed and is fully functional to play. You can enjoy it. But before playing, you need to know that tic tac toe is a multi-player game.

Player 1 will be you, and player two will be a computer. And it would help if you were very clever to play with the computer as it is an expert in this game. Let’s see who will win this game, the computer or you, because the computer will never lose the game and I think so is you.

You can also use this video link to develop your own tic tac toe game in the visual studio.

Concluding Thoughts

 I hope you can now easily develop any game, especially a tic tac toe game. You just need to follow the steps accordingly, as reported in the article, and the basic thing is that you need to have a strong grip on C, C++, and C# computer languages so you can type the codes in your program without any error and mistake. 

Now, enjoy and develop your games in C++ Visual studio and give the world a chance to play the games developed by you.

Written by

I am a software engineer with over 10 years of experience in blogging and web development. I have expertise in both front-end and back-end development, as well as database design, web security, and SEO.