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)
Related
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.
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.
I want to cout an array as a row vector but when I write:
int main() {
int B[3]={0};
for (int w = 0; w <2; w++) {
cout <<"B="<<" "<< B[w] << " ";
}
cout << endl;
return 0;
}
The output is B=0 B=0
But I want output to be like:
B=(0 0)
For a fixed size array of only I would probably even prefer a oneliner like this, because I can read it at first glance:
cout << "B=(" << B[0] << " " << B[1] << " " << B[2] << ")\n";
For a container B with a dynamic or very high number of elements n, you should probably do something like this:
cout << "B=(";
if(n > 0)
{
cout << B[0];
// note the iteration should start at 1, because we've already printed B[0]!
for(int i=1; i < n; i++)
cout << ", " << B[i]; //I've added a comma here, so you get output like B=(0, 1, 2)
}
cout << ")\n";
This has the advantage, that no matter what number of elements, you don't end up with trailing commas or unwanted whitespace.
I'd reccommend making a generic (template) function for the purpose of printing array/std::vector content anyways - it's really useful for debugging purposes!
int main() {
int B[3] = { 0 };
cout << "B=(";
for (int w = 0; w < 3; w++) {
cout << B[w];
if (w < 2) cout << " ";
}
cout << ")" << endl;
return 0;
}
Output should be now:
B=(0 0 0)
The simplest way to do this is:-
#include<iostream>
using namespace std;
int main()
{
int B[3]={0};
cout << "B=(";
for (int w = 0; w < 3; w++)
{
cout << B[w] << " ";
}
cout << ")" << endl;
return 0;
}
the output will be B= (0 0 0 )
You can try this one if you want:
#include <iostream>
using namespace std;
int main() {
int B[3]={0};
cout << "B=(";
for (int w = 0; w <2; w++) {
cout << B[w];
if(w != 1) cout << " ";
}
cout << ")" << endl;
cout << endl;
return 0;
}
The output is:
B=(0 0)
The line if(w != 1) checks whether you 've reached the last element of the array. In this case the last index is 1, but in general the if statement should be: if(w != n-1) where n is the size of the array.
I need the user input to be saved into my array and then output the array before the user inputs the next time. I have been moving things around different ways but cannot seem to get them to perform properly. I tried to cut down the code to the two functions I am having issues with.
void PlayGame()
{
const int HighestNum = 50;
const int LowestNum = 1;
int RandomNumber = LowestNum + rand() % HighestNum; //set for better random results
cout << "Guess the random number between " << LowestNum << " and " << HighestNum << "!\n\n";
const int attempts = 15;// limits the attempts to guess the random number to 15
int Guess [attempts] = {};
cout << "Enter your guess " << endl;
for (int count = 0; count < attempts; count++)
{
cin >> Guess[count];
int z = RandomNumber, y = Guess[count], r;
r = reviewGuess (z,y);//calling the function that determines the results
switch (r)//switch statement for function results, letting the user know if they matched the number, if the number is higher, or lower
{
case 0:
cout << "You Win!!" << endl;
cout << "\n";
cin.get();
return;
case 1:
cout << "The number is higher than your guess" << endl;
break;
case -1:
cout << "The number is lower than your guess" <<endl;
break;
}
if (count == 15)
{
cout << "Sorry, no guesses remain. The random number was... " << RandomNumber << "!";//so the user can see the random number at the end of their attempts
cout << "\n";
cin.get();
Again();
}
}
return;
}
int DisplayGuess(int member[])
{
for(int i = 0; i < 15; ++i)
cout << "\nGuess " << i + 1 << ": " << member[i];
cout << endl;
return;
}
Try this inside your loop
if(count > 0)
{
for (int j= 0; j < count; j++)
{
cout<<Guess[j];
}
}
Call DisplayGuess() in the first line of the for loop. Since the first you time you call it your array is empty, it shouldn't output anything.
So,
for (int count = 0; count < attempts; count++)
{
DisplayGuess(Guess[count]);
cin >> Guess[count];
int z = RandomNumber, y = Guess[count], r;
r = reviewGuess (z,y);//calling the function that determines the
results
. . . . . .
This is the code:
for (i = 0; i < max; i++) { //RIGHT-SHIFT
cout << " "; //blank space before padding
cout << "***"; //left padding
for (j = i; j >= 0; j--) { // this prints one more * than the last line (left side)
cout << "**";
}
for (k = 0; k < width; k++) { // print the white space
cout << " ";
}
for (j = max - i; j > 0; j--) { // this prints one less than the last line (right side)
cout << "**";
}
cout << "***\n";//right padding
}
for (i = max; i > 0; i--) { //LEFT-SHIFT
cout << " "; //blank space before padding
cout << "***"; //left padding
for (j = i; j >= 0; j--) { // this prints one more * than the last line (left side)
cout << "**";
}
for (k = 0; k < width; k++) { // print the white space
cout << " ";
}
for (j = max - i; j > 0; j--) { // this prints one less than the last line (right side)
cout << "**";
}
cout << "***\n";//right padding
}
It looks something like
I want the first row to have 4 - 19 instead of 5- 20. How do i change it?
Print out one fewer asterisk in your initial cout << "***" in each outer loop. ie. do cout << "**" instead for your "left padding". That will make all the rows narrower by one asterisk, but will preserve the overall pattern, which is what I presume you wanted.