Dynamic array error in Conways Game of Life - c++

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!

Related

Binary search algorithm in c++

I am new to programming so please help me completing the task
the problem is:
After pressing y the while loop does not run again.
and secondly, how to print or get the array elements in descending order?
thank you!
#include <iostream>
using namespace std;
int main()
{
int item;
int flaging = 0;
int ind_low = 0;
int ind_high = 9;
int ind_mid = (ind_low + ind_high) / 2;
char conti;
//Array declaration and taking user input
int arr[10];
cout << "enter some values here : \n" << endl;
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
// for sorthing the array
int temp;
for (int p = 1; p <= 9; p++)
for (int c = 0; c <= 8; c++)
if (arr[c] > arr[c + 1])
{
temp = arr[c];
arr[c] = arr[c + 1];
arr[c + 1] = temp;
}
do {
//asking for searching
cout << "Enter the value you want to search : " << endl;
cin >> item;
while (ind_low <= ind_high)
{
if (item == arr[ind_mid])
{
cout << "At " << ind_mid << " index the value " << item << " is found " << endl;
flaging++;
break;
}
if (item < arr[ind_mid])
{
ind_high = ind_mid - 1;
ind_mid = (ind_low + ind_high) / 2;
}
else
{
ind_low = ind_mid + 1;
ind_mid = (ind_low + ind_high) / 2;
}
}
if (flaging == 0)
{
cout << "Value not found" << endl;
}
cout << "To search again press 'y', to exit press any key" << endl;
cin >> conti;
} while ((conti == 'y') || (conti == 'Y'));
}
when I ran it on my pc after pressing y it did run again, can you provide the input that failed you?
for the second question what do you mean?
you can do a for loop that goes like this:
for(int index = ARR_SIZE -1 ; index >= 0 ; --index){
cout << array[index];
}
edit: I understand what you mean. after each run you should reset your indexes otherwise you will always run on the same once:
before you end the loop the values should be reseted.
ind_low = 0;
ind_high = 9;
ind_mid = (ind_low + ind_high) / 2;
that gonna print the array from end to start.

Multidimensional array not changing elements from function for some reason

The function player_attack() changes the elements of the multi-dimensional array pc_board but when i reprint it in main, the array prints unchanged.
I removed all unnecessary code.
I tried to pass is at as a parameter to the function but i got an error for using a multidimensional array in the parameter.
$
bool game_won = false;
string board[5][5];
string pc_board[5][5];
void initialize_player_board() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
board[i][j] = "-";
}
}
}
void print_map() {
for (int i = 0; i < 5; i++) {
cout << setw(5);
cout << i << setw(5);
for (int j = 0; j < 5; j++) {
cout << board[i][j] << setw(5);
}
cout << setw(10);
for (int j = 0; j < 5; j++) {
cout << pc_board[i][j] << setw(5);
}
cout << endl;
}
}
void pc_add_battleship() {
int x = 0;
int y = 0;
int choice_generator = 0;
char choice;
x = rand() % 4 + 1;
y = rand() % 4 + 1;
choice_generator = rand() % 2;
if (choice_generator == 0) {
choice = 'h';
}
else {
choice = 'v';
}
if (choice == 'h') {
pc_board[y - 1][x] = 'O';
pc_board[y][x] = 'O';
pc_board[y + 1][x] = 'O';
}
if (choice == 'v') {
pc_board[y][x - 1] = 'O';
pc_board[y][x] = 'O';
pc_board[y][x + 1] = 'O';
}
}
void player_attack() {
int x = 0;
int y = 0;
cout << "Choose an x coordinate to attack: " << endl;
cin >> x;
cout << "Choose a y coordinate to attack: " << endl;
cin >> y;
if (pc_board[y][x] == "O") {
cout << "HIT!" << endl;
pc_board[y][x] == "H";
}
else {
cout << "Miss." << endl;
pc_board[y][x] == "M";
}
}
int main()
{
srand(time(0));
initialize_player_board();
initialize_pc_board();
cout << "Welcome to the battleship game." << endl;
print_map();
Add_battleship();
pc_add_battleship();
while (!game_won) {
print_map();
player_attack();
}
return 0;
}
$
I expected for the multidimensional array to change its elements due to the function
In your function player_attack you use wrong operator:
if (pc_board[y][x] == "O") {
cout << "HIT!" << endl;
pc_board[y][x] == "H"; // here
}
else {
cout << "Miss." << endl;
pc_board[y][x] == "M"; // and here
}
instead of == that is a comparison operator you should use = that is the assignment operator.
Using operator == in this context is still valid C++ syntax, which produces a boolean value, however it does not modify the arguments (which are left and right side of comparison), which is probably what you want to do in most cases. Enabling compiler flags like -Wall or Wextra along with Werror helps to avoid this kind of bugs.

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.

comparation of 2 binary numbers

I need to write program that will compare 2 binary numbers and return the result
I wrote this code using XOR, but idk how to improve my code, so for example 100101 and 101001 will return a < b. Can you help me fix this please?
string a,b;
la = a.length();
lb = b.length();
int x = 0;
if (la == lb)
{
for (int i = 0; i < la; i++)
{
if (a[i]^b[i] == 1)
{
if(a[i] > b[i])
x++;
}
else {x--;}
}
if (x > 0)
cout << a << " > " << b << endl;
if (x < 0)
cout << a << " < " << b << endl;
if (x == 0)
cout << a << " = " << b << endl;
}
Guess this would be better
Here no need to iterate th whole string unless they are equal.
Just find the first '1' and the one which has 1 before will be bigger.
As simple as that
int main()
{
string a,b;
cin>>a>>b;
size_t aLoc=-1,bLoc=-1;
do
{
aLoc = a.find("1",aLoc+1);
bLoc = b.find("1",bLoc+1);
if(aLoc<bLoc)
{
cout<<a<<">"<<b;
return 0;
}
else if(aLoc>bLoc)
{
cout<<a<<"<"<<b;
return 0;
}
}while(aLoc==bLoc && aLoc!=string::npos);
cout<<a<<"="<<b;
return 0;
}
Edit:
aLoc give the position of 1 in first string, and bLoc for 2nd string
Lets take 1 exmaple
a = 10111000;
here first aLoc = 1 as the first '1' is at position 1
b = 10001100;
here bLoc = 1;
now for next iteration,
aLoc will be 3 as next '1' is at 3rd position
and bLoc will be 5 so naturally aLoc
which means '1' comes first in string a.
Hence
if(aLoc<bLoc)
a>b;
Let a = 01, b = 10.
In your program, in the first iteration, the value of a[i]^b[i] will be 1. But a[i] < b[i] and you have not accounted for this case, thus the value of x remains 0.
In the second iteration, again the XOR condition is true, and the condition (a[i] > b[i]) is also true, thus incrementing the value of x to 1.
After the loop, it will print 01 > 10 which is obviously false.
Modified Code:
#include <iostream>
int main() {
std::string a, b;
std::cin >> a >> b;
int la = a.length(), lb = b.length();
int x = 0;
// -1 if both are equal
// 0 if a > b
// 1 if b > a
int flag = -1;
// if you only want to check for equal length strings
if (la == lb)
{
for (int i = 0; i < la; i++)
{
if (a[i]^b[i] == 1)
{
if(a[i] > b[i])
flag = 0;
else
flag = 1;
break;
}
}
if (flag == 0)
std::cout << a << " > " << b << std::endl;
if (flag == 1)
std::cout << a << " < " << b << std::endl;
if (flag == -1)
std::cout << a << " = " << b << std::endl;
}
}

Array printing out multiple characters in basic dungeon crawler [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
My name is Eric, and I ran into a problem while programming a basic "dungeon crawler" using c++.
The problem is that when the player moves certain spaces to the left or right, there is another player character generated so that there is two on the screen instead of one.
Here is my code:
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>
using namespace std;
const int columns = 7, rows = 10;
string gridArray[rows][columns];
bool xIsGenerated = false;
bool gISGenerated = false;
int playerX = 0, playerY = 0;
void displayGrid(int rows, int columns){
for(int i = 0; i < columns; i++){
for(int x = 0; x < rows; x++){
cout << gridArray[i][x];
}
cout << endl;
}
cout << gISGenerated << endl;
return;
}
void generatePieces(int rows, int columns){
int tcount = 0;
for(int i = 0; i < rows; i++){
for(int x = 0; x < columns; x++){
srand(time(NULL) + x + i);
int r = rand() % 5;
if(r == 1 && tcount < 4){
gridArray[i][x] = "T ";
tcount++;
}else if(r == 2 && !xIsGenerated){
gridArray[i][x] = "X ";
xIsGenerated = true;
}
}
}
if(!xIsGenerated){
srand(time(NULL)*3);
int r = rand() % rows+1;
int c = rand() % columns+1;
gridArray[r][c] = "X ";
xIsGenerated = true;
}
return;
}
void generatePlayer(int rows, int columns){
if(!gISGenerated){
srand(time(NULL)*3);
int r = rand() % rows+1;
int c = rand() % columns+1;
gridArray[r][c] = "G ";
playerX = r;
playerY = c;
gISGenerated = true;
}
}
void initGrid(int rows, int columns){
for(int i = 0; i < columns; i++){
for(int x = 0; x < rows; x++){
gridArray[i][x] = ". ";
}
}
generatePieces(rows, columns);
generatePlayer(rows, columns);
return;
}
//i is the rows
//x is the columns
void movePlayer(){
char input = 'x';
cin >> input;
if(input == 'w' && playerX != 0){
gridArray[playerX][playerY] = ". ";
gridArray[playerX-1][playerY] = "G ";
playerX--;
}
if(input == 's' && playerX != 6){
gridArray[playerX][playerY] = ". ";
gridArray[playerX+1][playerY] = "G ";
playerX++;
}
if(input == 'a' && playerY != 0){
gridArray[playerX][playerY] = ". ";
gridArray[playerX][playerY-1] = "G ";
playerY--;
}
if(input == 'd' && playerY != 9){
gridArray[playerX][playerY] = ". ";
gridArray[playerX][playerY+1] = "G ";
playerY++;
}
system("CLS");
displayGrid(rows, columns);
cout << playerX << ": " << playerY << endl;
}
void firstTime(){
displayGrid(rows, columns);
cout << playerX << ": " << playerY << endl;
return;
}
int main()
{
initGrid(rows,columns);
firstTime();
while(true){
movePlayer();
}
return 0;
}
Quick explanation of code:
A multidimensional array will serve as the graphic for displaying what is going on. This array starting with the function "initGrid" will print out ". " strings in the array and on the screen
The Generate Pieces function takes that array filled with ". " strings, and using a random number generator, it places "T " strings and 1 "X " string. This "X " will be the goal while the T's will be traps that kill the player.
Generate Player does the same thing, but instead places only 1 "G " string. This is the player.
After initGrid is called, then inside of the main function is the "firstTime" function, nothing complex, just displays data to the screen.
Then lastly, I have a while loop that calls the function "movePlayer", using the same array, based on what the user inputs, it will move the "G " string accordingly and replace the last location of the g string with an empty ". " string.
I have tried to return the location of the second G string and once I did that I tried to replace it with a ". " string, but it failed as the code didn't remove the second one, and once the second one was out of the array (the second g character moves corresponding to the first g character) the first G character got removed.
I am drawing a blank here as to what I should do next, it seemed to be a simple problem at first, but it is giving me a run for it's money.
Thanks for reading, and I hope to get an answer soon to my problem.
I ran your code with asan and ubsan which told me you have an out of bounds access in line 23 cout << gridArray[i][x]; where you access gridArray[8] which doesn't exist.
Looks like you mixed up rows and columns. I recommend you use sanitizers too.