# Game of Life improvement

I have finished my code for Conway's Game of Life. I am looking for any advice on how to improve it. I am a C++ beginner, so I used only basic libraries. I am accepting all suggestions.

/*
Author: Stevan Milic
Date: April 2018
Class: COSC 1560
Description: Game Of Life
*/
#include <iostream>
#include <cstdlib>  // Library needed for CLS command
#include <iomanip>
using namespace std;

const int boardsize = 10;

void Display(bool board[boardsize + 1][boardsize + 1])
{
for (int x = 1; x < boardsize; x++)
{
for (int y = 1; y < boardsize; y++)
{
if (board[x][y] == true)
{
cout << " * ";
}
else
{
cout << " - ";
}
if (y == boardsize - 1)
{
cout << endl;
}
}
}
}
// Copying the board in order to compare the existing cells
void CopyBoard(bool board[boardsize + 1][boardsize + 1], bool board2[boardsize + 1][boardsize + 1])
{
for (int x = 0; x < boardsize; x++)
{
for (int y = 0; y < boardsize; y++)
{
board2[x][y] = board[x][y];
}
}
}
//Calculates function to determine if the cell is alive or dead
void Calc(bool board[boardsize + 1][boardsize + 1])
{
bool board2[boardsize + 1][boardsize + 1] = {};
CopyBoard(board, board2);
for (int x = 1; x < boardsize; x++)
{
for (int y = 1; y < boardsize; y++)
{
int alive = 0;
for (int z = -1; z < 2; z++)
{
for (int t = -1; t < 2; t++)
{
if (!(z == 0 && t == 0))
{
if (board2[x + z][y + t])
{
alive++;
}
}
}
}
if (alive < 2)
{
board[x][y] = false;
}
else if (alive == 3)
{
board[x][y] = true;
}
else if (alive > 3)
{
board[x][y] = false;
}
}
}
}

int main()
{

//const int boardsize = 10;
bool board[boardsize + 1][boardsize + 1] = {};

// Starting cells
board[5][4] = true;
board[5][5] = true;
board[5][6] = true;
board[4][4] = true;
board[4][5] = true;
board[4][6] = true;
board[3][4] = true;
board[3][5] = true;
board[3][6] = true;

while (true)
{
Display(board);     // Displaying function
Calc(board);        // Calculating function
system("CLS");      // Clearing the Screen
}
}


I'm unsure what you mean by saying "improve the user interface". AFAIK Game of life doesn't have much of a user interface as it's a simulation that only displays the grid and whatever happens on it.
However there are some other things that can definitely be improved.

• Don't use using namespace std

• Generally functions in C++ are lowercase

• Think about better variable names.
x and y might be acceptable but t and z are probably not the best names.

• Prefer using \n over std::endl

• Prefer prefix over postfix operator

• Don't compare to booleans. Instead do if (condition) and
if (!condition).

• You might want to use vectors for your board instead of arrays.

• Don't use global variables (which you don't need when you're using vectors)

• There is another way to write an endless loop which is a for loop without conditions for (;;) (read as forever).
Which one is better is highly opinion based however.

• Your cluster of for loops is quite hard to grasp. It would improve readability and understanding if you break it up into smaller parts. For example you could write a function to iterate over the cells and for every cell call another function that checks the adjacent ones for live cells.

• I appreciate your answer, Yuri. I will update my code as soon as I can. For the UI, I wanted the terminal window to be more complex and interesting than just outputting "*" and "-". The solution could be maybe to output a symbol, or a box, or something like that. Thank you again for your answer, it really helped. – Stevan Milic Apr 23 '18 at 9:24
• @StevanMilic You could maybe spruce it up visually by using certain characters. – yuri Apr 23 '18 at 9:31
• I'm not sure why you suggest using vectors instead of arrays for a fixed-size collection. Could you please elaborate? – Vogel612 Apr 23 '18 at 10:10
• @Vogel612 In short: because it's easier. You don't have to worry about how to pass an array or getting it's size when using a vector. So from a beginners perspective it simplifies things. If your goal is to reduce overhead or learn how to use arrays then by all means go ahead and use arrays to your hearts content. – yuri Apr 23 '18 at 10:31
• @yuri Could you please help me on how to output Unicode characters? (I am using Visual Studio) – Stevan Milic Apr 23 '18 at 16:52