I'm really new at C++ and I'm building a Snake game in C++ but I'm having some trouble. Basically I want to have 20 random cookies on my grid and 20 random blocks there too for the snake to eat/avoid. However they aren't showing up for some reason. There is no errors but I'm guessing I'm maybe missing some code. I know it says here don't do my homework as such
Thanks again
Header file:
#ifndef _GRID_HPP
#define _GRID_HPP
#include <vector>
struct Field {
unsigned x_;
unsigned y_;
bool blocked_;
bool edible_;
char char_;
Field (int x =0, int y =0, bool b=false, bool e=false, char c='-'):
x_(x),y_(y), blocked_(B)/>, edible_(e), char_(c)
{}
};
class Grid {
public:
Grid();
bool fieldIsBlocked(unsigned x, unsigned y);
bool Grid::fieldIsEdible(unsigned x, unsigned y);
bool Grid::fieldIsNonEdible(unsigned x, unsigned y);
int setFieldNonEdible(unsigned x, unsigned y);
int setRandomBlocks();
int setRandomCookies();
int draw();
int clear();
private:
std::vector <Field> grid_;
unsigned rows_;
unsigned cols_;
};
#endif
My source file
#include "grid.hpp"
#include "display.hpp"
#include <iostream>
#include <ctime>
const unsigned gmin_x = 10;
const unsigned gmax_x = 60;
const unsigned gmin_y = 5;
const unsigned gmax_y = 20;
const unsigned XLEN = (gmax_x-gmin_x)+1;
const unsigned YLEN = (gmax_y-gmin_y)+1;
const char COOKIE = char(1);
const char DOT = '.';
const char PERIMETER = '#';
const char BLOCK = char(219);
Grid::Grid () :rows_ (YLEN), cols_ (XLEN)
{
Field f;
for(int y = gmin_y; y <= gmax_y; y++){
for(int x = gmin_x; x <= gmax_x; x++){
f.x_ = x;
f.y_ = y;
f.blocked_=false;
f.edible_=false;
f.char_=DOT;
if((x==gmin_x)||(x==gmax_x)||(y==gmin_y)||(y==gmax_y)) {
#ifdef _DEBUG
std::cout<< " x "<< x << "y->" << y<< std::endl;
f.blocked_=true;
f.char_= PERIMETER;
#else
f.blocked_=true;
f.char_= PERIMETER;
#endif
}
grid_.push_back(f);
bool Grid::fieldIsBlocked(unsigned x, unsigned y)
{
unsigned relx = x - gmin_x;
unsigned rely = y - gmin_y;
return Grid::grid_.at((Grid::cols_*rely) + relx).blocked_;
}
bool Grid::fieldIsEdible(unsigned x, unsigned y)
{
unsigned relx = x - gmin_x;
unsigned rely = y - gmin_y;
return Grid::grid_.at((Grid::cols_*rely) + relx).edible_;
}
int Grid::setFieldNonEdible(unsigned x, unsigned y)
{
unsigned relx = x - gmin_x;
unsigned rely = y - gmin_y;
return Grid::grid_.at((Grid::cols_*rely) + relx).edible_=false;
return 0;
}
int Grid::setRandomBlocks()
// sets 20 random blockes into Grid
{
srand ((unsigned int)time(NULL) );// random seed- set at starting second of prog //
unsigned x = 0;
unsigned y = 0;
unsigned velement = 0;
for(unsigned counter = 0; counter < 20 ; counter++ ) {
x = (rand() % (XLEN - 2)) + (gmin_x +1);
y = (rand() % (YLEN - 2)) + (gmin_y +1);
velement = ((cols_* (y - gmin_y)) + (x - gmin_x));
try {
grid_.at(velement).char_=BLOCK ;
grid_.at(velement).blocked_ = true;
grid_.at(velement).edible_= false;
}catch (std::out_of_range) { std::cout << "bad vector index !->" << velement << std::endl; }
return 0;
}
}
int Grid::setRandomCookies()
//places 20 random cookies into Grid
{
srand ((unsigned int)time(NULL) );
unsigned x = 0;
unsigned y = 0;
unsigned velement = 0;
for(unsigned counter = 0; counter < 20 ; counter++ ) {
x = (rand() % (XLEN - 2)) + (gmin_x +1);
y = (rand() % (YLEN - 2)) + (gmin_y +1);
velement =((cols_* (y - gmin_y)) + (x - gmin_x));
try {
grid_.at(velement).char_=COOKIE ;
grid_.at(velement).blocked_ = false;
grid_.at(velement).edible_= true;
}catch (std::out_of_range) { std::cout << "bad vector index !->" << velement << std::endl; }
#ifdef _DEBUG
std::cout << "position in vector ->" << velement << '\t'
<<"grid_.size(): "<< grid_.size()
<<"difference: "<< (grid_.size() - velement)
<<std::endl;
std::cout << "i :" << " random x ->" << x
<<" random y - >" << y << std::endl;
if ((x==gmin_x) || (x==gmax_x)) { std::cout << "oops: x" << std::endl ; }
if ((y== gmin_y) || (y ==gmax_x)) {std::cout << "oops: y" << std::endl; }
if ((velement < 0)|| (velement >= grid_.size())) { std::cout <<"oops:vector index! "; }
#endif
}
return 0;
}
int Grid::draw()
{
for(unsigned i=0; i< grid_.size(); i++){
gotoXY (grid_.at(i).x_, grid_.at(i).y_);
std::cout << (grid_.at(i).char_);
}
return 0;
}
int Grid::clear()
{
setColor(15,0);
gotoXY (0,0);
for (int i=0; i <26;i++) {
std::cout << " \n";
}
return 0;
}

New Topic/Question
Reply



MultiQuote






|