How do I stop flickering in a console application? - c++

I made a simple Ping pong game in c++ through visual studio community(in windows). I made it, but the final product keeps flickering and it does not look good.
I am new to c++. And this is one of the first big projects I tried making. Doing some research, I found out that I made a console app, and to stop the flicker, I will have to remake my app in Graphics, which I can't,so I am looking for a way to modify my console app to reduce flicker.
My draw function-:
void Draw()
{
system("cls");
for (int i = 0; i < width + 2; i++)
cout << "\xB2";
cout << endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int ballx = ball->getX();
int bally = ball->getY();
int player1x = player1->getX();
int player2x = player2->getX();
int player1y = player1->getY();
int player2y = player2->getY();
if (j == 0)
cout << "\xB2";
if (ballx == j && bally == i)
cout << "O";//ball
else if (player1x == j && player1y == i)
cout << "\xDB";//player1_segment0
else if (player2x == j && player2y == i)
cout << "\xDB";//player2_segment0
/*Prints different segments of player1 in y-dir*/
else if (player1x == j && player1y + 1 == i)
cout << "\xDB";//player1_segment1
else if (player1x == j && player1y + 2 == i)
cout << "\xDB";//player1_segment2
else if (player1x == j && player1y + 3 == i)
cout << "\xDB";//player1_segment3
/*Prints different segments of player2 in y-dir*/
else if (player2x == j && player2y + 1 == i)
cout << "\xDB";//player2_segment1
else if (player2x == j && player2y + 2 == i)
cout << "\xDB";//player2_segment2
else if (player2x == j && player2y + 3 == i)
cout << "\xDB";//player2_segment3
else
cout << " ";
if (j == width - 1)
cout << "\xB2";
}
cout << endl;
}
for (int i = 0; i < width + 2; i++)
cout << "\xB2";
cout << endl;
cout << "Score 1: " << score1 << endl;
cout << "Score 2: " << score2 << endl;
}
Thanks.
This is what it looks like without flickers

Clearing the screen will make the entire screen go black before you start drawing, causing flicker.
I'd recommend looking into the Windows Console API (assuming you're happy for this to work only in windows). Set the cursor position and then draw everything.
COORD coord;
coord.X = 0;
coord.Y = 0;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
This is not code I've tested, and I don't know if it will work correctly with cout. You might want to replace look at other operations such as 'WriteConsole(...)'

Instead of 'system("cls");' implement code to "delete" the moving elements, then paint them in the new position.

Related

How to check for neighbors in Game of Life C++?

I am working on an assignment for school and of course I receive very vague feedback on our code. The code I am working on is for Conway's Game of Life. I know I am super close. I have code that prints out the new generation but it's definitely not the correct one. It seems it is not counting the neighbors correctly - what should be identified as an alive neighbor doesn't seem to happen.
From our assignment as well (seeing examples of generations being formed) I notice the border cells do change which means I have to access them without going out of bounds. I feel I have been fruitless in my attempts to do this and I think I'm just missing something super obvious.
Please, any feedback would be amazing.
I have several print lines in attempts of debugging.
void gameOfLife(vector<vector<string>> &originalGrid, vector<vector<string>> &grid, int row, int col,
int Rows, int Cols){
//counts # of alive neighbors
int aliveNeighbors = 0;
string alive = "*";
for(int posX = row-1; posX <= row+1; posX++){
for(int posY = col-1; posX <= col+1; posX++){
std::cout << "I am in function - nested loop " << row << " " << col << std::endl;
if(posX == row && posY == col){
continue;
}
else if((posX >= 0 && posX < Rows) && (posY >= 0 && posY < Cols)){
std::cout << "I am in function - nested loop - else if " << row << " " << col << std::endl;
if(grid[posX][posY] == alive){
aliveNeighbors++;
std::cout << "alive neighbors: " << aliveNeighbors << std::endl;
}
}
}
}
/*
//top cell
if(grid[row][col-1] == "*"){
std::cout << "top cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
//bottom cell
if(grid[row][col+1] == "*"){
std::cout << "bottom cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
//left cell
if(grid[row-1][col] == "*"){
std::cout << "left cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
//right cell
if(grid[row+1][col] == "*"){
std::cout << "right cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
//top left
if(grid[row-1][col-1] == "*"){
std::cout << "top left cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
//top right
if(grid[row+1][col-1] == "*"){
std::cout << "top right cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
//bottom left
if(grid[row-1][col+1] == "*"){
std::cout << "bottom left cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
//bottom right
if(grid[row+1][col+1] == "*"){
std::cout << "bottom right cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
*/
//test cases
//test case 1: Any live cell with fewer than two live neighbors dies (as if by underpopulation).
if(grid[row][col] == alive && aliveNeighbors < 2){
originalGrid[row][col] = ".";
}
//test case 2: Any live cell with more than three live neighbors dies (as if by overpopulation/overcrowding).
if(grid[row][col] == alive && aliveNeighbors > 3){
originalGrid[row][col] = ".";
}
//test case 3: Any live cell with two or three live neighbors lives, unchanged, to the next generation.
if(grid[row][col] == alive && (aliveNeighbors == 3 || aliveNeighbors == 2)){
originalGrid[row][col] = grid[row][col];
}
//test case 4: Any dead cell with exactly three live neighbors will come to life (as if by reanimation or birth).
if(grid[row][col] == "." && aliveNeighbors == 3){
originalGrid[row][col] = alive;
}
//prints updated grid
for(int i = 0; i < Rows; i++){
for(int j = 0; j < Cols; j++){
std::cout << originalGrid[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
return;
}
int main() {
int rows, col, numOfGen;
std::cin >> rows >> col >> numOfGen;
string cell;
vector<vector<string>> game;
for(int i = 0; i < rows; i++){
vector<string> temp;
for(int j = 0; j < col; j++){
std::cin >> cell;
temp.push_back(cell);
}
game.push_back(temp);
}
vector<vector<string>> firstGen;
firstGen.insert(firstGen.end(),game.begin(),game.end());
if(numOfGen == 0){
std::cout << "numOfGen == 0" << std::endl;
for(int i = 0; i < rows; i++){
for(int j = 0; j < col; j++){
std::cout << game[i][j] << " ";
}
std::cout << std::endl;
}
}
for(int g = 0; g <= numOfGen; g++){
for(int i = 1; i < rows; i++){
for(int j = 1; j < col; j++){
gameOfLife(game, firstGen, i, j, rows, col);
}
}
if(g == numOfGen){
for(int i = 0; i < rows; i++){
for(int j = 0; j < col; j++){
std::cout << game[i][j] << " ";
}
std::cout << std::endl;
}
}
}
return 0;
}
Looks like firstGen never gets updated, so you're just computing the first generation over and over. So your output is probably correct for a single generation, but it's the same for any number of generations. Also, check the conditions on your main driver loop: with for(int g = 0; g <= numOfGen; g++) the loop executes numOfGen+1 times.

When using the goto statement code does not output at wanted inside of a loop

I am trying to make a simple tic-tac-toe game. Right now I am trying to make it so the computer can't place inside of certain places in a 2d array. The computer is set to place randomly, and I am using loops and goto to make it random until it gets a suitable spot.
#include <iostream>
#include <string>
#include <ctime>
#include <Windows.h>
using namespace std;
string team;
string a = " ";
const int rows = 5;
const int elements = 5;
string Board[rows][elements] = { a, "| ", a, "| ", a,
"- ", "+ ", "- ", "+ ", "- ",
a, "| ", a, "| ", a,
"- ", "+ ", "- ", "+ ", "- ",
a, "| ", a, "| ", a };
void showBoard()
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
cout << Board[i][j];
}
cout << endl;
}
}
int main()
{
int nonFilled = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
if (Board[i][j] == a && a == " ")
nonFilled++;
}
}
int circleFilled = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
if (Board[i][j] == a && a == "0 ")
circleFilled++;
}
}
int crossFilled = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
if (Board[i][j] == a && a == "X ")
crossFilled++;
}
}
cout << "Welcome to tic-tac-toe game." << endl;
cout << endl;
showBoard();
cout << "Please select team if circle or cross: (0/X)" << endl;
cin >> team;
if (team == "0") {
int ifCircle = 1;
cout << "You have selected circle." << endl;
while (nonFilled > circleFilled + crossFilled) {
srand(time(NULL));
int x, y = 0;
int xx, xy = 0;
cout << "Select square: " << endl;
cout << "Y cord (0-4): ";
cin >> x;
cout << endl;
cout << "X cord (0-4): ";
cin >> y;
if (x == 0 || y == 0)
return 0;
/*if (Board[x - 1][y - 1] = "| ", "+ ", "- ")
{
cout << "You cannot place one there. " << endl;
nonFilled = 100;
}*/
int b = 1;
if (Board[x - 1][y - 1] == a)
b = 1;
else
b = 2;
switch (b) {
case 1:
Board[x - 1][y - 1] = "0 ";
break;
case 2:
cout << "You cannot place one there. " << endl;
continue;
}
/* if (Board[x - 1][y - 1] == a)
{
Board[x - 1][y - 1] = "0 ";
}
else
{
cout << "You cannot place one there." << endl;
}*/
cout << endl;
showBoard();
cout << "The opponent will now pick a square:" << endl;
system("pause");
xx = rand() % rows;
xy = rand() % elements;
int c = 1;
if (Board[xx][xy] == a)
c = 1;
else if (Board[xx][xy] == "| ", "+ ", "- ")
c = 2;
switch (c) {
case 1:
Board[xx][xy] = "X ";
break;
case 2: {
LOOP: // here is my label for the goto statement
while (true) // this loop
{
xx = rand() % rows;
xy = rand() % elements;
if (Board[xx][xy] == "| ", "+ ", "- ") {
goto LOOP; // goto statement
}
else {
Board[xx][xy] = "X ";
}
}
}
}
cout << endl;
cout << "The opponent has picked:" << endl;
showBoard();
}
}
else if (team == "X") {
int ifCircle = 0;
}
system("pause");
return 0;
}
The loop in question is at the very bottom, I am not sure if it is a problem with how I placed the label or how I am using the statement or if it is a problem with a different part of the code.
I have looked at loads of questions on goto statements inside of while loops and I couldn't find anything.
The immediate problem lies neither with the placement of your LOOP: label nor with the use of the goto statement (and I am not here going to get involved in the argument over whether or not that keyword should ever be used in a C++ program).
The problem is in the following line:
if (Board[xx][xy] == "| ", "+ ", "- ") {
(and the similar if else... statement a few lines earlier).
This does not do what you may want it to! In fact, it will always return a true value, as the nett result of the expression is simply, if ("- ") - which will always be a non-null (i.e. non-zero) address of the string literal.
What you need (if you are looking to match the indexed Board[][] string to any one of the three literals) is the following:
if (Board[xx][xy] == "| " || Board[xx][xy] == "+ " || Board[xx][xy] == "- ") {
//...
The code you have, using the comma operator, evaluates each of the comma-separated expressions (in left-to-right order), discarding each but the last (right-most) value; the overall result is just that of the right-most expression.
EDIT: Having applied the fixes I suggested above, I then noticed another problem: Your while loop, as it stands, will never exit (either it will goto to the LOOP: or just keep running). Here is one (quick) way to fix the loop, which also removes the need for the goto statement:
case 2:
{
bool done = false;
while (!done)
{
xx = rand() % rows;
xy = rand() % elements;
if (Board[xx][xy] == "| " || Board[xx][xy] == "+ " || Board[xx][xy] == "- ") {
continue;
}
else {
Board[xx][xy] = "X ";
done = true;
}
}
}
There are more 'elegant' ways to achieve the same result but, hopefully, you will at least be able to follow (and understand) the fairly minor changes I have made to your code. Please feel free to ask for any further clarification and/or explanation.

c++ coordinate grid map marking coordinates

ifstream myfile;
myfile.open("config.txt");
if (myfile.fail())
{
cerr << "Error opening config file" << endl;
myfile.close();
}
int line_no = 0;
while (line_no != 3 && getline(myfile, line3)) {
++line_no;
}
while (line_no != 7 && getline(myfile, line7)) {
++line_no;
}
while (line_no != 10 && getline(myfile, line10)) {
++line_no;
}
while (line_no != 14 && getline(myfile, line14)) {
++line_no;
}
while (line_no != 18 && getline(myfile, line18)) {
++line_no;
}
cout << line3 << endl;
cout << line7 << endl;
cout << line10 << endl;
cout << line14 << endl;
cout << line18 << endl;
gridXIdxA = stoi(ExtractString(line3, "=", "-"));
gridXIdxB = stoi(ExtractString(line3, "-", "\n"));
gridYIdxA = stoi(ExtractString(line7, "=", "-"));
gridYIdxB = stoi(ExtractString(line7, "-", "\n"));
cout << gridXIdxA << endl;
cout << gridXIdxB << endl;
cout << gridYIdxA << endl;
cout << gridYIdxB << endl;
int y = gridYIdxB + 1;
y > -1;
mapBoundaryX = gridXIdxB + 6; // dynamic array to print out boundary of city map
mapBoundaryY = gridYIdxB + 4;
int** dMapBoundaryArray;
dMapBoundaryArray = new int*[mapBoundaryX]();
for (int i = 0; i < mapBoundaryX; i++)
{
dMapBoundaryArray[i] = new int[mapBoundaryY];
}
for (int i = 0; i < mapBoundaryX; i++)
{
cout << endl;
for (int j = 0; j < mapBoundaryY; j++)
{
dMapBoundaryArray[i][j] = i;
if (i == 0 && j > 0 && j < gridXIdxB+4)
{
cout << "# "; // top
}
if (i == 0 && j == 0)
{
cout << " ";
}
if (i == (gridYIdxB+2) && j > 0 && j < gridXIdxB+4)
{
cout << "# "; // bottom
}
if (i == (gridYIdxB + 2) && j == 0)
{
cout << " ";
}
if (i>0 && i<12 && j==1)
{
cout << "#"; // left
}
else if (i == 6 && j == 3)
{
cout << " ";
cout << 3;
}
else if (i == 6 && j == 4)
{
cout << " ";
cout << 3;
}
else if (i == 7 && j == 4)
{
cout << " ";
cout << 3;
}
else if (i > 0 && i < 12 && j == 13) //right
{
cout << setw(24)<<right << "#";
}
if (i == 13 && j > -1 && j < 2) //x axis
{
cout << " ";
}
if (i == 13 && j > 1 && j < 13)
{
x = x++;
cout <<" " << x; //x axis
}
if (j == 0 && i <= gridYIdxB+1 && i >= 1) // y axis
{
y = --y;
cout << y; //y axis
}
}
}
Hi, have some questions that i need help with, beginner to c++ currently so definitely appreciate if stuff can be explained in simple terms. I can manage to create the edges of the coordinate map but I also need to pinpoint certain coordinates within the map and mark them for eg. with a '3'. Is there anyway to pinpoint the coords without moving the column of '#' on the right?
Output
before marking coords
after marking coords
You may already have noticed that C++ alone has no facilities to set a character at a certain position on the screen. curses is a rather wide-spread library that helps with that. However, if you want to stay with your own code you can still get some inspiration from how curses handles output. The contents of the screen are buffered in memory and only when you request it all of it is updated. You can do similar in your own code. Store the contents you want to print on the screen in a std::vector<std::vector<char> or a std::vector<std::string>. Modify the contents as desired and when printing you do not have to bother about alignment and formatting anymore, it is just a simple loop:
for (const auto& line : screen) {
for (const auto& character : line) std::cout << character;
std::cout << "\n";
}

Segmentation fault: 11 c++ Error

this is my first time asking a question on this forum so here it goes. I am creating a tic-tac-toe game for practice and am using enumerators and recursion because I have never really done enumeration and could always get some recursion practice in. Well anyway I just finished coding for the player2 to take a random move and after about 3 turns it gives a segmentation fault and I cannot figure out why... I hope you guys can figure it out and thank you!
#include <iostream>
#include <string>
#include <cstdlib>
const int size = 3;
enum play {none,X,O};
void NPC(play (&board)[size][size],play player2) {
srand(time(NULL));
int tempx = rand() % 3;
int tempy = rand() % 3;
if(board[tempx][tempy] == none)
board[tempx][tempy] = player2;
else
NPC(board,player2);
}
void getBoardState(play (&board)[size][size],int y,int x) {
if(board[x][y] == none) std::cout << " ";
else if(board[x][y] == X) std::cout << "X";
else std::cout << "O";
}
void printboard(play (&board)[size][size]){
int length = 4 * size - 1;
for(int i = 1; i <= length; i++) {
for(int j = 1; j <= length; j++) {
if(i % 4 == 0 && j % 4 == 0) std::cout << "+";
else if(i % 4 == 0) std::cout << "-";
else if(j % 4 == 0) std::cout << "|";
else if(i % 2 == 0 && j % 2 == 0) getBoardState(board,(i - 2)/4,(j - 2)/4);
else std::cout << " ";
}
std::cout << std::endl;
}
}
int main() {
play player = O, player2 = X;
bool over = false;
play board[size][size];
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
board[i][j] = none;
}
}
std::string player1 = "";
std::cout << "What would You like to be? An X or an O?" << std::endl;
while(((player1 != "X") + (player1 != "O")) == 2) {
std::cin >> player1;
if(((player1 != "X") + (player1 != "O")) == 2)
std::cout << "Invalid entry! Please enter X or an 0!" << std::endl;
}
if(player1 == "X") {
player2 = O;
player = X;}
int tempx,tempy;
while(!over) {
std::cout << "Please enter an x and then a y (1 to " << size << ")" << std::endl;
std::cin >> tempx;
std::cin >> tempy;
while(tempx > size || tempy > size || board[tempx-1][tempy-1] != none) {
std::cout << "Invalid entry! Try again!" << std::endl;
std::cin >> tempx;
std::cin >> tempy;
}
board[tempx-1][tempy-1] = player;
NPC(board,player2);
printboard(board);
}
return 0;
}
You're running out of stack space in your recursion because you call srand(time(NULL)) every time. The random number generator should only be seeded once, in main, and not in NPC. time(NULL) returns a number of seconds, so it changes infrequently (compared to how fast your recursive function calls will occur) which will consume all available stack space.

Dynamic array error in Conways Game of Life

I am working on a program that emulates conways game of life, and it works perfectly with the preset dimensions. However, once i try to use the dynamic dimensions as seen in option e, i start having problems. The main problem is in the "life" function which iterates throughout the array and decides if it should bring to life a cell. I have been debugging for a while and it i enter the dimensions as 50*40, it iterates until 61, 1. This should technically work but it just breaks everytime. Keep in mind that I add 12 to each dimension to account for the buffer zone I put around the edges. Technically it should work then right? If you have any suggestions I would really appreciate it!
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <new> // i havent used this one yet
#include <cmath>
using namespace std;
// REMEMBER: The outside of the array is 6 more than what we show so that nothing interferes
//also that it goes y,x and that x is going to be bigger so that we get a rectange
//we use the copy function to copy an array from eachother, either the current one to the temp one or
//vise versa. This is so that we can alter the cells one step at a time without affecting everything else.
void copy(int **array1, int **array2, int o, int p)
{
for(int j = 0; j < o; j++)
{
for(int i = 0; i < p; i++)
array2[j][i] = array1[j][i];
}
} // the second array sent is assigned the first array sent!
//this array will initialize our arrays so that we can use them later
int** init(int n, int m)
{
int **array;
array = new int*[m]; // x
array = new int*[n]; // y
for (int q=0; q < n; q++)
{
array[q] = new int[n];
for (int w=0; w < m; w++)
{
array[w] = new int[m];
}
}
return array;
}
void populate(int o, int p, int** board){ // THIS FUNCTION HASN'T BEEN USED YET
for(int i=0; i < p; i++)
{
for(int j=0; j < o; j++) // It was in a in-class demo but i dont think i need it
{
board[i][j] = pow(i, j);
}
}
}
//The life function looks at the pieces around the cell and figures out what happens next.
// Probably the most important in the entire program, feast your eyes!
void life(int **array, int o, int p)
{
//Copies the main array to a temp array so changes can be made without affecting anyone else
int **temp;
temp = init(o, p);
copy(array, temp, o, p);
for(int j = 1; j < o; j++)
{
for(int i = 1; i < p; i++)
{
// checks all 8 cells surrounding it
int count = 0;
cout << " j is " << j << " and i is " << i << endl;
// cout << array[j][i]; // DEBUGGING
count =
array[j-1][i] + array[j-1][i-1] +
array[j][i-1] + array[j+1][i-1] +
array[j+1][i] + array[j+1][i+1] +
array[j][i+1] + array[j-1][i+1];
//cell dies.
if(count < 2 || count > 3)
{
temp[j][i] = 0;
}
//nothing happens.
if(count == 2)
{
temp[j][i] = array[j][i];
}
//now the cell will be born, or if it already is alive then it stays that way.
if(count == 3)
{
temp[j][i] = 1;
}
}
}
//Copies the temp array back to the main array.
copy(temp, array, o, p);
}
//This function prints the 40 x 50 part of the array, a 1 means that there will be a cell there,
//otherwise it will just be an empty space.
void print(int **array, int o, int p)
{
// WE ONLY CHECK WHAT WE SEE, WHICH IS 6 LESS THAN THE ARRAY!!!
for(int j = 6; (j < (o-6)); j++)
{
for(int i = 6; (i < (p-6)); i++)
{
if(array[j][i] == 1 )
cout << '*';
else
cout << '.';
}
cout << endl;
}
}
//I read somewhere it would be a good idea to make sure to end the program early if it somehow
//became stable by itself early. so this compares the old array with the new one to check if they
//are the same. This commonly occurs if a glider runs off the screen for example.
bool compare(int **array1, int **array2,int o,int p)
{
int counter = 0;
for(int j = 0; j < o; j++)
{
for(int i = 0; i < p; i++)
{
if(array1[j][i]==array2[j][i])
counter++;
}
}
if(counter == o*p)
return true;
else
return false;
}
int main()
{
int o= 52, p=62;
int **firstgen;
int **next;
int **backup;
// 40 + 12, 50 + 12
int x, y;
char starty;
char again;
char cont;
bool comparison;
//Here is where we initialize our arrays
firstgen = init(o,p);
next = init(o,p);
backup = init(o,p);
cout << endl << "Welcome to John Conway's Game of Life." << endl;
//This loop is for if we are still simulating, don't get confused!
do
{
//this loop checks for inputs.
do
{
menu: //this is a goto we use for if we change dimensions
x = 0, y = 0;
//now we get the menu
cout << endl << "--- Choose an option Below ---" << endl;
cout << "(a) Glider" << endl;
cout << "(b) Gosper Gilder gun" << endl;
cout << "(c) R Pentomino Pattern" << endl;
cout << "(d) Oscillator" << endl;
cout << "(e) Change the dimensions (it defaults to (50*40)" << endl;
cin >> starty;
}while(starty != 'a' && starty != 'b' && starty != 'c' && starty != 'd' && starty != 'e');
int i = 0;
//we need to assign firstgen in this area
//choose a glider position
if (starty == 'a'){
x= 0, y= 0;
cout << " X dimension: ";
cin >> x;
cout << " Y dimension: ";
cin >> y;
if (x < 0 || x > p || y < 0 || y > o){
cout << endl << "you entered invalid dimensions" << endl;
goto menu;
}
x = x+6; //we add 6 because there are six spots to the left that aren't shown we need to account for
y = y+6;
//creates the glider
firstgen[y][x] = 1;
firstgen[y][x+1] = 1;
firstgen[y][x+2] = 1;
firstgen[y-1][x] = 1;
firstgen[y-2][x+1] = 1;
}
else if (starty == 'b'){
x= 0, y= 0;
cout << "Your dimensions are based on the farthest left point" << endl;
cout << " X dimension: ";
cin >> x;
cout << " Y dimension: ";
cin >> y;
if (x < 0 || x > p || y < 0 || y > o){
cout << endl << "you entered invalid dimensions" << endl;
goto menu;
}
//this is because we have the buffer zone of 6
x = x+6;
y = y+6;
//Gosper gun
//box on left
firstgen[y][x] = 1;
firstgen[y][x+1] = 1;
firstgen[y+1][x] = 1;
firstgen[y+1][x+1] = 1;
//left circle starting in top of the left curve (flat part)
firstgen[y][x+10] = 1;
firstgen[y-1][x+11] = 1;
firstgen[y-2][x+12] = 1;
firstgen[y-2][x+13] = 1;
firstgen[y+1][x+10] = 1;
firstgen[y+2][x+10] = 1;
firstgen[y+3][x+11] = 1;
firstgen[y+4][x+12] = 1;
firstgen[y+4][x+13] = 1;
//dot in middle
firstgen[y+1][x+14] = 1;
//arrow thing on the right
firstgen[y-1][x+15] = 1;
firstgen[y][x+16] = 1;
firstgen[y+1][x+16] = 1;
firstgen[y+1][x+17] = 1;
firstgen[y+2][x+16] = 1;
firstgen[y+3][x+15] = 1;
//boomerang bit on the far right section
firstgen[y][x+20] = 1;
firstgen[y][x+21] = 1;
firstgen[y-1][x+20] = 1;
firstgen[y-1][x+21] = 1;
firstgen[y-2][x+20] = 1;
firstgen[y-2][x+21] = 1;
firstgen[y-3][x+22] = 1;
firstgen[y-3][x+24] = 1;
firstgen[y-4][x+24] = 1;
firstgen[y+1][x+22] = 1;
firstgen[y+1][x+24] = 1;
firstgen[y+2][x+24] = 1;
//tiny box on farthest right, almost done!
firstgen[y-1][x+34] = 1;
firstgen[y-1][x+35] = 1;
firstgen[y-2][x+34] = 1;
firstgen[y-2][x+35] = 1;
}
else if (starty == 'c')
{
x= 0, y= 0;
cout << "Your dimensions are based on the farthest left point" << endl;
cout << " X dimension: ";
cin >> x;
cout << " Y dimension: ";
cin >> y;
if (x < 0 || x > p || y < 0 || y > o){
cout << endl << "you entered invalid dimensions" << endl;
goto menu;
}
x = x+6;
y = y+6;
//creates R Pentamino pattern
firstgen[y][x] = 1;
firstgen[y][x+1] = 1;
firstgen[y+1][x+1] = 1;
firstgen[y-1][x+1] = 1;
firstgen[y-1][x+2] = 1;
}
// creates the simple oscillator
else if (starty == 'd')
{
x= 0, y= 0;
cout << "Your dimensions are based on the top of the oscillator" << endl;
cout << " X dimension: ";
cin >> x;
cout << " Y dimension: ";
cin >> y;
if (x < 0 || x > p || y < 0 || y > o){
cout << endl << "you entered invalid dimensions" << endl;
goto menu;
}
x = x+6;
y = y+6;
firstgen[y][x] = 1;
firstgen[y+1][x] = 1;
firstgen[y+2][x] = 1;
}
// allows you to choose your dimensions
else if (starty == 'e')
{
o= 0, p= 0;
x= 0, y= 0;
cout << "choose the height and width of your field, between 0 and 100" << endl;
cout << " X dimension: ";
cin >> x;
cout << " Y dimension: ";
cin >> y;
if (x < 0 || x > 100 || y < 0 || y > 100){
cout << endl << "Please keep dimensions between 0 and 100" << endl;
goto menu;
}
// the problem is that it is adding my x dimension and my placement choice together and then
// starts to run the program, which threadbreaks. I need to find out why these two values are
// adding together and fix it
x = x+12;
y = y+12; // plus twelve so that we have 6 around all sides
p = x;
o = y;
firstgen = init(o,p);
next = init(o,p);
backup = init(o,p);
// is this part below necessary?
//firstgen[o][p];
// next[o][p];
// backup[o][p];
// idk
// cout << "y value is: " << o << " and the x value is " << p << endl; // debugging
goto menu;
}
//Loop that does the simulation.
do
{
//Prints the generation. If i == 0, the firstgen array is copied to the
//next array, and is printed before any functions act upon it.
cout << endl << "Generation " << i << ":" << endl << endl;
//Initializes the arrays by copying the firstgen array to the next array.
if(i == 0)
copy(firstgen, next, o, p);
//this stuff below happens in every cycle
cout << "the x/p value is" << p << "and the y/o value is " << o << endl;
copy(next, backup, o, p);
print(next, o, p);
life(next, o, p);
i++;
//Pauses the system .2 seconds so that it doesn't flash past you super fast and you
// can't appreciate its beauty
system("sleep .2");
//Checks whether the generation is a multiple of 100 to ask
//the user if they want to continue
if(i % 100 == 1 && i != 1)
{
cout << endl;
//Loop to check for proper inputs.
do
{
cout << "Continue? (y or n): ";
cin >> cont;
}while(cont != 'y' && cont != 'n');
if(cont == 'n')
break;
}
//Compares the current generation with a backup generation.
//The idea is that if it is the same with the backup generation then
//something boring is going on or smething went wrong. It will end if that
//is the case.
comparison = compare(next, backup, o, p);
if(comparison == false)
// system("clear");
//cout << string( 10, '\n' );
if(comparison == true)
cout << endl;
}while(comparison == false);
//Loop to check if we want to keep going.
do
{
cout << "Run another Simulation? (y or n): ";
cin >> again;
}
while(again != 'y' && again != 'n');
//this is where we clean out all our firstgen values
//i used to have this at the top but didn't really need it
for(int y = 0; y < o; y++)
{
for(int x = 0; x < p; x++)
{
firstgen[y][x] = 0;
}
}
}
while(again == 'y');
return 0;
}
I figured it out!
The thing to take away from this is to make sure that your initiation function creates the array with the same size as the one you will be accessing. I was trying to get values from array[52][1] which didn't exist because in my init function i only had the for loop running while n < o, which means it didn't create the 52nd row. what a relief!