Board not printing correctly.

Hello. I'm writing a version of the classic Snake game. However, my board is not printing correctly. The right hand border is in the incorrect location. Also, when I randomly generate where the food ('X') is located, it only generates on the edges of the boundaries. Here is the code, it's fairly short:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <cstdlib>
using namespace std;

const int ROWS = 21;
const	int COLS = 61;

class Snake{
	public:
		void make_board();
		void print();
		void play();
	private:
		string board[ROWS][COLS];
		void set_food();
};

int main()
{
	Snake game;
	
	game.make_board();
	game.play();
	game.print();
	
	return 0;
}

void Snake::make_board()
{
	//Creates empty board
	for(int i = 0; i < ROWS; i++){
		for(int j = 0; j < COLS; j++){
			board[i][j] == " ";
		}
	}
	
	//Top boundary
	for(int i = 0; i < COLS; i++){
		board[0][i] = "-";
	}
	board[0][0] = "+";
	board[0][60] = "+";
	
	//Bottom boundary
	for(int i = 0; i < COLS; i++){
		board[20][i] = "-";
	}
	board[20][0] = "+";
	board[20][60] = "+";
	
	//Left boundary
	for(int i = 1; i < ROWS - 1; i++){
		board[i][0] = "|";
	}
	
	//Right boundary
	for(int i = 1; i < ROWS - 1; i++){
		board[i][60] = "|";
	}
}

void Snake::print()
{
	for(int i = 0; i < ROWS; i++){
		for(int j = 0; j < COLS; j++){
			cout << board[i][j];
		}
		cout << endl;
	}
	cout << endl;
}

void Snake::play()
{
	set_food();
}

void Snake::set_food()
{
	int pos1 = 0;
	int pos2 = 0;
	
	srand(pos1);
	srand(pos2);
	srand(time(NULL));
	
	pos1 = rand() % 21;
	pos2 = rand() % 61;
	
	board[pos1][pos2] = "X";
}


Here is the current and incorrect output:
+-----------------------------------------------------------+
||
||
||
||
||
||
||
||
|X|
||
||
||
||
||
||
||
||
||
||
+-----------------------------------------------------------+


Thank you.
Might i may a suggestion, instead of having the borders apart of the 2d array, just print them apart from it.

1
2
3
4
5
6
7
8
9
10
11
std::cout << "xxxx..... width ......xxxx"
for (int i........ )
{
std::cout << '|'';
for ( int j..... )
{
print board code
}
std::cout << '|';
}
std::cout << "xxxx..... width ......xxxx" 


EDIT: added the bottom border
sorry for no indentation

this is how i make borders in the console, it makes it alot easier
Last edited on
@ line 34 you use two equals signs instead of one

Move the seeding of the random numbers ( line 86 ) to the beginning of main(). srand() should be called once per program.

As a suggestion, make the board a 2d array of char, there's no need for strings here.

I'm also getting food that is ending up on the top and bottom border, I would imagine working that out is all part of the fun though :)

Paoletti301 brings up a good point though, you might want to look into a specific way to differentiate between the board and its borders.
Last edited on
Aha! It was the double equals signs! Thank you very much for the help LowestOne. Also, I'll change the program and print the board separately. Thanks for the idea Paoletti301.
I personally would keep the borders in the same array, mainly because console is so stupid to try to make look pretty ...

I don't even know how at the top of my head, maybe something like:
1
2
3
4
5
6
7
8
9
10
const int PLAY_AREA_X; = 20;
const int PLAY_AREA_Y = 60;

const int BOARD_X = PLAY_AREA_X + 2; // one character extra for each side
const int BOARD_Y = PLAY_AREA_Y + 2;

char board[BOARD_Y][BOARD_X];

int random_playable( const int remainder ) { return rand() % remainder + 1; }
int random_row( void) { return random_playable( PLAY_AREA_X ); }


Maybe try to define the border widths somehow.
Last edited on
why keep the border in the same array?
you can just hardcode the border outputs in the print function, which works great for square grids

also question to the op, how do you plan to handle animation with the game?
Topic archived. No new replies allowed.