Jump to content

C++ Looping (Text-Based Board Game)

prolemur

I want to make a Battleship type game that will at first only be text based and I want the game's grid to be made in an intelligent manner. I want it to be made with for loops because I understand them and it is much more compact then just printing everything I want directly to the screen without any shenanigans. 

 

This is what I want: http://prntscr.com/3ce78p

This is the code I currently have:

	for (int i = 0; i < 45; i++){		cout << "_";	} cout << endl;	cout << "|  ";	for (int i = 0; i < 10; i++){		cout << " | " << i;	} cout << " |" << endl;	for (int i = 0; i < 11; i++){		cout << "|___";	} cout << "|" << endl;		for (int i = 0; i < 10; i++){		cout << "| A ";		for (int i = 0; i < 10; i++){			cout << "| " << grid[i][0] << " ";		} cout << "|" << endl;		for (int i = 0; i < 11; i++){			cout << "|___";		} cout << "|" << endl;	}

All I would need to know how to do is loop the A value from A - J and that could take its place. Thanks :)

* Also I need a way to change the grid[0] value with the character values. So for A it would be 0, B : 1, C : 2 ... J : 9

Link to comment
Share on other sites

Link to post
Share on other sites

To loop the char you could do something like

char currChar = 'A';
at the beginning and then

cout << "| " << currChar++ << " ";
or

cout << "| " << currChar + i << " ";
Edited by alpenwasser
code tags :)
Link to comment
Share on other sites

Link to post
Share on other sites

Hey I am trying to make a battleship text game for now and am trying to set up the board nicely. It is all working well right now except I don't know how to make it work for every row. Right now the actual data of the table is just the first row being put in every row. Proof is grid[9][0] = 1 changes the whole column and grid[9][1] = 1 changes nothing because it is not being printed. I tried adding another for loop to take care of the grid[0] and make it grid[j], but it just ended up making ten times the size I wanted. Thanks for the help :)

#include <iostream>using namespace std;int grid[10][10];char row = 'A';int col = 0;int main() {	grid[9][0] = 1;	for (int i = 0; i < 45; i++) {		cout << "_";	} cout << endl;	cout << "|  ";	for (int i = 0; i < 10; i++) {		cout << " | " << i;	} cout << " |" << endl;	for (int i = 0; i < 11; i++) {		cout << "|___";	} cout << "|" << endl;	for (int i = 0; i < 10; i++) {		cout << "| " << row++ << " ";		for (int i = 0; i < 10; i++) {			cout << "| " << grid[i][0] << " ";		} cout << "|" << endl;		for (int i = 0; i < 11; i++) {			cout << "|___";		} cout << "|" << endl;	}	cin.get();	return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

You need to use different variables for your inner for loops.

 

While using i for each one works it won't for what you want to do. You were on the right track with [j]

 

You need to use the value i from the outer loop in your array index. With both loops using i this isn't possible since the outer i gets shadowed by the inner i.

	for (int irow = 0; irow < 10; irow++) {		cout << "| " << row++ << " ";		for (int icol = 0; icol < 10; icol++) {			cout << "| " << grid[icol][irow] << " ";		} cout << "|" << endl;		for (int k = 0; k < 11; k++) {			cout << "|___";		} cout << "|" << endl;	}

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to comment
Share on other sites

Link to post
Share on other sites

-snip-

Thanks I kind of though that may have been a problem, but disregarded because when I hovered over it in vs it highlighted only the i's and j's in the for loop they were initiated. Thanks :) I'm still going to try and condense this as much as possible though. Next is the maybe somehow integrating the bottom pipes and underscores lines completely.. perhaps :P

#include <iostream>using namespace std;int grid[10][10];char row = 'A';int main() {	grid[9][9] = 1; // test variable	// top padding	for (int i = 0; i < 45; i++) {		cout << " ";	} cout << endl;	// column numbers	cout << "   ";	for (int i = 0; i < 10; i++) {		cout << " | " << i;	} cout << " |" << endl;	// seperates headings from data	cout << " ";	for (int i = 0; i < 11; i++) {		cout << "___|";	} cout << endl;	// organized row data	for (int irow = 0; irow < 10; irow++) {		cout << "  " << row++ << " ";		for (int icol = 0; icol < 10; icol++) {			cout << "| " << grid[icol][irow] << " "; // actual data line		} cout << "|" << endl;		// bottom cover made of underscores and pipes		cout << " ";		for (int k = 0; k < 11; k++) {			cout << "___|";		} cout << endl;	}	cin.get();	return 0;}

Just though it would look better if the vertical and horizontal headers were not completely the same as the data cell, now they are not fully boxed in just by switching the pipe to the end of the line and I also added some padding so it wasn't crammed up against the border of the cli.

Link to comment
Share on other sites

Link to post
Share on other sites

i'll add my 2 cents

 

this can be done with just 2 loops, in a way that i think is more intuitive/natural

 

 

 

the second one also contains an ugly workaround for column numbers with more than 1 decimal digit

#include <iostream>using namespace std;int main(){    int grid[10][10] = {0};    int x, y, xCell, yCell;    for(y = 0; y < 23; ++y){        for(x = 0; x < 45; ++x)            if(y == 0)                                    cout << '_';            else if(x % 4 == 0)                           cout << '|';            else if(y % 2 == 0)                           cout << '_';            else{                xCell = x / 4;                yCell = y / 2;                if(y % 2 && x % 4 == 2)                    if(xCell + yCell == 0)                cout << ' ';                    else if(xCell == 0)                   cout << static_cast <char> ('A' + yCell -1);                    else if(yCell == 0)                   cout << xCell -1;                    else                                  cout << grid [xCell - 1][yCell - 1];                else                    cout << ' ';            }            cout << endl;        }    return 0;}
#include <iostream>#define WIDTH 15#define HEIGHT 15#define CELL_X_SIZE 4#define CELL_Y_SIZE 2#define HORIZONTAL_DELIMITER '_'#define VERTICAL_DELIMITER '|'#define BLANK ' 'using namespace std;int main(){    int grid[WIDTH][HEIGHT] = {0};    int x, y, xCell, yCell,        W = (WIDTH + 1) * CELL_X_SIZE + 1,        H = (HEIGHT + 1) * CELL_Y_SIZE + 1;    for(y = 0; y < H; ++y){        for(x = 0; x < W; ++x)            if(y == 0)                                              cout << HORIZONTAL_DELIMITER;            else if(x % CELL_X_SIZE == 0)                           cout << VERTICAL_DELIMITER;            else if(y % CELL_Y_SIZE == 0)                           cout << HORIZONTAL_DELIMITER;            else{                xCell = x / CELL_X_SIZE;                yCell = y / CELL_Y_SIZE;                if(y % CELL_Y_SIZE == CELL_Y_SIZE / 2 && x % CELL_X_SIZE == CELL_X_SIZE / 2)                    if(xCell + yCell == 0)                          cout << BLANK;                    else if(xCell == 0)                             cout << static_cast <char> ('A' + yCell -1);                    else if(yCell == 0){                            cout << xCell -1;                        for(xCell -= 1; xCell /= 10; ++x);                    }else                                           cout << grid [xCell - 1][yCell - 1];                else                    cout << BLANK;            }            cout << endl;        }    return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

  • 6 months later...

I'm actually going to finish this project dammit!

 

I'm thinking there's not much point in making it compact because it is more work to compile it later and it will always be a 10x10 grid anyways. So I have made a revision and also thrown it inside a method. If there are any improvements to this code you can think I'd love to hear them. *I haven't written the code that prevents the method from displaying a boat character because I'm still testing it. Also there is some controversy on using system so that may also be temporary. Also do you think it looks better with the padding it already has on the top and left side or should I remove that?

 

I've also progressed my c++ knowledge a little bit so can do more things :)

void showGrid() {    char r = 'A';    //system("cls");    cout << "                                             " << endl << "   ";    for (int i = 0; i < 10; i++)        cout << " | " << i;    cout << " |" << endl << " ___|___|___|___|___|___|___|___|___|___|___|" << endl;    for (int y = 0; y < 10; y++) {        cout << "  " << r++ << " ";        for (int x = 0; x < 10; x++)            // if grid[x][y] is a boat character don't show it            cout << "| " << grid[x][y] << " ";        cout << "|" << endl << " ___|___|___|___|___|___|___|___|___|___|___|" << endl;    }}
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×