Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This tutorial shows you how to use GitHub Copilot to quickly create a C++ console application in Visual Studio. You create a console version of Conway's Game of Life, a classic cellular automaton.
Conway's Game of Life was devised by mathematician John Conway. It consists of a grid of cells that can be either alive or dead. The game evolves automatically based on simple rules and produces complex, evolving patterns that demonstrate how intricate behavior can emerge from basic mathematical rules.
GitHub Copilot is an AI-powered coding assistant that helps you write code faster, reduce errors, and explore new solutions. Some benefits of using Copilot when coding in C++:
- Generate entire C++ functions or classes as you type.
- Suggest code completions based on plain-language comments or prompts.
- Get help with complex algorithms, data structures, and standard library usage.
- Learn new APIs and modern C++ patterns through in-context examples.
- Receive context-aware suggestions based on your comments or code.
- Debug errors in your code.
- Simplify and refactor existing code.
Prerequisites
- Visual Studio 2022 or later with the Desktop development with C++ workload installed.
- GitHub Copilot. For more information, see Get started with GitHub Copilot.
To verify you have the C++ workload installed:
Open Visual Studio Installer
Select Modify next to your Visual Studio installation
Ensure Desktop development with C++ is checked:
If it isn't installed, select it and choose Modify.
For more information about installing Copilot, see Manage GitHub Copilot installation and state.
Create a project
Visual Studio uses projects to organize the code for an app, and solutions to organize your projects. A project contains all the options, configurations, and rules used to build your apps. It manages the relationship between all the project's files and any external files. To create your app, first, create a new project and solution.
Open Visual Studio and select Create a new project.
Search for "Console App" and select the Console App template for C++.
The Create a new project dialog is shown with the Console App template selected. The template says: Run code in a windows terminal. Prints hello world by default. Has the tags c++, Windows, and Console.
Select Next.
Set the project name to Life and choose the location for the project.
Select Create.
Once the project opens, find the
Life.cppfile in Solution Explorer.Open
Life.cppand delete the default "Hello, World!" code to start with a clean slate.
Use Copilot to create an app
You prompt Copilot by describing the functionality you want. In this section, you'll learn how to prompt Copilot to generate an implementation of Conway's Game of Life.
Open the Copilot chat window by selecting the Copilot icon in the toolbar:
In the chat window, enter the following prompt:
Create a C++ console application that implements Conway's Game of Life. The program should: - Use a 40x20 grid displayed with asterisks (*) for live cells and spaces for dead cells - Start with a random initial pattern - Display each generation for 500ms before showing the next - Allow the user to press any key to exit the program - Include proper headers and use standard C++ practices - Clear the console between generations to provide an animation effectCopilot generates C++ code for Conway's Game of Life.
Copy the generated code and paste it into your empty
Life.cppfile.Build the project by pressing F6 or selecting Build > Build Solution.
Run the program by pressing F5 or Ctrl+F5.
Note
The exact code generated by Copilot may vary slightly from run to run and model to model, but the core functionality should be consistent. If the generated code doesn't compile immediately, you can ask Copilot to fix any compilation errors. Copilot is powered by AI, so surprises and mistakes are possible. For more information, see Copilot FAQs.
Example of typical generated code structure
Your generated code will likely include these key components:
- Headers for console manipulation, random number generation, and timing
- A 2D array or vector to represent the game grid
- Functions to initialize the grid, apply Conway's rules, and display the current state
- A main loop that continues until a key is pressed
Here's an example of the code that Copilot generated using the previous prompt:
// Code generated by GitHub Copilot
#include <iostream>
#include <chrono>
#include <thread>
#include <conio.h> // for _kbhit and _getch
using namespace std;
int step = 0;
const int rows = 20;
const int cols = 40;
void printGrid(int grid[rows][cols])
{
cout << "Step: " << step << endl;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
cout << (grid[i][j] ? '*' : ' ');
}
cout << endl;
}
}
int countNeighbors(int grid[rows][cols], int x, int y)
{
int count = 0;
for (int i = -1; i <= 1; ++i)
{
for (int j = -1; j <= 1; ++j)
{
if (i == 0 && j == 0)
{
continue;
}
int ni = x + i;
int nj = y + j;
if (ni >= 0 && ni < rows && nj >= 0 && nj < cols)
{
count += grid[ni][nj];
}
}
}
return count;
}
void updateGrid(int grid[rows][cols])
{
int newGrid[rows][cols] = { 0 };
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
int neighbors = countNeighbors(grid, i, j);
if (grid[i][j] == 1)
{
newGrid[i][j] = (neighbors < 2 || neighbors > 3) ? 0 : 1;
}
else
{
newGrid[i][j] = (neighbors == 3) ? 1 : 0;
}
}
}
// Copy newGrid back to grid
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
grid[i][j] = newGrid[i][j];
}
}
}
int main()
{
int grid[rows][cols] = { 0 };
// Initial configuration (a simple glider)
grid[1][2] = 1;
grid[2][3] = 1;
grid[3][1] = 1;
grid[3][2] = 1;
grid[3][3] = 1;
while (true)
{
if (_kbhit()) // Check if a key has been pressed
{
_getch(); // Consume the key
break; // Exit the loop
}
printGrid(grid);
updateGrid(grid);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
cout << "\033[H\033[J"; // Clear the screen
step++;
}
return 0;
}
When you run the application, you should see an animated display of Conway's Game of Life with patterns evolving over time. To exit the program, press a key.
In the preceding code example, the code generates a warning: Return value ignored: '_getch'. You can ask Copilot to fix it. Select the code editor and press Alt+/ (Windows) to open the Copilot chat, then enter:
Copilot suggests a fix to handle the return value properly. To accept the changes, select Tab:
Congratulations! You successfully used GitHub Copilot to create a fully functional Conway's Game of Life console application in C++. You learned how to:
- Craft an effective prompt to generate C++ code
- Use Copilot's chat interface to create an entire application from scratch
- Fix compilation warnings with AI assistance
Improve your prompting skills
For better results with Copilot, see these prompting resources:
- GitHub Copilot documentation - Official best practices and tips
- OpenAI's GPT best practices - General AI prompting techniques
- Copilot prompting guide - Specific guidance for code generation
Troubleshooting
Missing console app template
The New Project dialog should show a Console App template that has C++, Windows, and Console tags. If you don't see it, it might be filtered out of the list, or it might not be installed. First, check the filter dropdowns at the top of the list of templates. Set them to C++, Windows, and Console. The C++ Console App template should appear; otherwise, the Desktop development with C++ workload isn't installed.
To install Desktop development with C++, you can run the installer right from the Create a new project dialog. Choose the Install more tools and features link at the bottom of the Create a new project dialog, beneath the list of templates. If the User Account Control dialog requests permissions, choose Yes. In the installer, make sure the Desktop development with C++ workload is checked. Then choose Modify to update your Visual Studio installation.
Copilot not responding
- Ensure you have an active GitHub Copilot subscription.
- Try signing out and back into your GitHub account in Visual Studio
Generated code doesn't compile
- Ask Copilot to fix specific compilation errors by pasting the error message into Copilot chat.
- Try refining your prompt to be more specific about what you want the app to do.
Next steps
- GitHub Copilot in Visual Studio
- GitHub Copilot documentation - Dive deeper into AI-assisted development
- Awesome ChatGPT Prompts - Community-driven prompting examples for inspiration