How to update a game board? c++ - c++

I'm trying to make a dungeon crawlesque game and I have this code to create a game board. I'm using 'F' as the finish point and 'P' for the player.
void Gameboard::CreateGameboard()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
GameGrid[i][j] = 'x';
}
}
cout << " 0 1 2 3 4 5 6 7 8 9 10" << endl;
cout << " +---------------------+" << endl;
for (int i = 0; i < 10; i++)
{
cout << " " << "|" << GameGrid[i][0];
for (int j = 0; j < 10; j++)
{
if (i == Spawn[0] && j == Spawn[0])
{
GameGrid[0][0] = 'P';
}
cout << " " << GameGrid[i][j];
}
cout << "|" << endl;
}
cout << " +---------------------+" << endl;
}
The problems I'm facing are. 'P' is being placed in the first two slots of the board and unsure why. And how would I update the board with player movement? I have a Player class with x,y position variables and my thought is to increment down/up based on where they're going. Is it required to reprint the whole board after every movement?

With your drawing of the board.
for (int i = 0; i < 10; i++)
{
cout << " " << "|" << ***GameGrid[i][0]***;
for (int j = 0; j < 10; j++)
{
cout << " " << GameGrid[i][j];
}
cout << "|" << endl;
}
You print out the first item of the row, then print the whole row, including the first item again. so each row will have a double up of the first item.
As for your second question, clearing the screen is exactly what you'll have to do. If you're using windows then you can use system("cls"); to 'clear' the console and then redraw. I would recommend putting the board drawing and board creation into different functions.

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.

How can I print 2D arrays with four columns

I am struggling with printing an array with 4 rows and 4 columns, when I initialized the array and entered all the values. Then, I used for loop to get all the values together so I can print them. But I get is an array that companied all the values in one row.
I have attached the output when I run the code.
Here is a portion of my code, it is long code but I am struggling in specific part:
#include <iostream>
using namespace std;
int main()
{
cout << "The martix before I flipped it: " << endl;
cout << endl;
int array[4][4] = { 16,3,2,13,5,10,11,8,9,6,7,12,4,5,14,1 };
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << array[i][j] << " ";
}
}
return 0;
The standard output utility std::cout is a stream from the stl and, as such, its << operator does not usually automagically append a linebreak.
Which is quite practical since, otherwise, you would not be able to print multiple numbers on a single line.
That being, said, you'll need to add the linebreak manually, like so :
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
std::cout << array[i][j] << " ";
}
std::cout << std::endl;
}
Alternatively, you can consider printing lines 4 at a time since your matrix is of constant size :
for (int i = 0; i < 4; i++) {
std::cout << array[i][0] << " "
<< array[i][1] << " "
<< array[i][2] << " "
<< array[i][3] << " " << std::endl;
}
Have a great day,

Fix array not properly being assigned

I am trying to assign a specific part of an array a new value, but it doesn't seem to be inserting the new value into the array.
char matrix[20][8] = {/*160 * '#'*/};
void Draw() {
system("CLS");
cout << "Welcome to Primitive Pong v1.0!" << endl;
for (int i = 0; i < 8; i++) {
cout << endl;
for (int j = 0; j < 20; j++) {
cout << matrix[i][j] << " ";
}
}
}
while (gameOver == false) {
matrix[10][4] = 'O';
Draw();
this_thread::sleep_for(chrono::milliseconds(1000));
}
I expect this to output a grid of 160 "#" with a "O" near the middle, but instead it just prints 160 "#". I am trying to make a game of console pong. I have tried using 'matrix[10][4] = {'O'};, but that does nothing different.
The problem is that you declare matrix[20][8] but then you access it as if its dimensions are [8][20] instead.
The total is the same but the access doesn't work correctly and, unfortunately, C++ will not check about this kind of mistake. Changing the code to
cout << matrix[j][i] << " ";
should make things work a you expect.

Draw a square/board 5x5 grid in C++ (fix)

I have some code to draw a grid with simple characters like | | | for verticals and ----------------- for the horizontal line.
But as you play the game for some reason one of the rows gets shifted to the right. I have no clue why this happens. Even initially the zeroth row gets shifted
int blankflag = 0;
cout << "\n ------------------------------------------------- ";
for (int i = 4; i >= 0; i--)
{
cout << "\n";
cout << i;
for (int j = 0; j <= 4; j++)
{
if (blankflag)
{
cout << " |";
blankflag = 0;
}
else
{
cout << " |";
}
for (int k = 0; k < 4; k++)
{
if ('k'th queen is at i,j)
{
if (player 1's queen)
{
cout << "# ";
}
else if (player 2's queen)
{
cout << "O ";
}
blankflag = 1;
}
}
if (arrow at i,j and no queen)
{
cout << "X";
blankflag = 1;
}
}
if (blankflag)
{
cout << " |";
}
else
{
cout << " |";
}
cout << "\n ------------------------------------------------- ";
}
cout << " \n a b c d e";
The arrow is another mechanic. Anyways as arrows are shot and game progresses randomly the lowest and second lowest rows get shifted when i redraw the board. Anyone has a more elegant grid solution? I just want to draw a X or a # or a O depending on my game state(which is built)

Draw A Rectangle With Asterisks

I am trying to write a C++ Program to display a rectangle drawn in asterisks. I have the program running properly except for the fact that only one side of the heights of my rectangles print. Here is the code I have currently written for the display rectangle method.
void Rectangle::displayRectangle()
{
int i=0, j=0;
for (int i = 0; i < width; i++)
{
cout << "*";
}
cout << endl;
for (int i = 0; i < height - 2; i++)
{
cout << "*";
for (int j = 0; j < width; j++)
{
cout << " ";
}
cout << endl;
}
for (int i = 0; i < width; i++)
{
cout << "*";
}
cout << endl;
}
Specify a width and height at the start then you only need 3 loops. The first will print the top line of the rectangle. The second will print both sides of the rectangle (minus the very top and very bottom of the sides). The third will print the bottom line of the rectangle.
Like so
// Width and height must both be at least 2
unsigned int width = 7; // Example value
unsigned int height = 5; // Example value
// Print top row
for(unsigned int i = 0; i < width; i++);
{
std::cout << "*";
}
std::cout << std::endl;
// Print sides
for(unsigned int i = 0; i < height - 2; i++)
{
std::cout << std::setw(width - 1) << std::left << "*";
std::cout << "*" << std::endl;
}
// Print bottom row
for(unsigned int i = 0; i < width; i++)
{
std::cout << "*";
}
std::endl;
You will need to include both iostream and iomanip for this to work (setw is part of iomanip).
The top and bottom rows could also be done using the method to fill spaces with a given character, but I cannot recall that method right now.
This can be done much easier and clearer.
The logic here is to draw from line to line, so you only need one loop
(I chose to use the auto specifier in this example because I think it looks neater and used often in modern c++, if your compiler doesn't support c++11, use char, int etc.):
int main()
{
using namespace std;
auto star = '*';
auto space = ' ';
auto width = 20;
auto height = 5;
auto space_cnt = width-2;
for (int i{0}; i != height+1; ++i) {
// if 'i' is the first line or the last line, print stars all the way.
if (i == 0 || i == height)
cout << string(width, star) << endl;
else // print [star, space, star]
cout << star << string(space_cnt, space) << star << endl;
}
}
Well, you don't see the second vertical line, because you don't draw it in your line loop.
void DrawRect(int w, int h, char c)
{
cout << string(w, c) << '\n';
for (int y = 1; y < h - 1; ++y)
cout << c << string(w - 2, ' ') << c << '\n';
cout << string(w, c) << '\n';
}
Try to prompt the user for the number of rows and columns. Then, using nested loops, display a rectangle of stars based on the user input.