14 Replies - 4196 Views - Last Post: 30 March 2012 - 04:53 PM Rate Topic: -----

#1 Oddfuture  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-March 12

Snake in C++

Posted 29 March 2012 - 04:47 PM

Hey guys

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 :P but maybe a hint or push in the right direction will get me going because I'm kind of confused.

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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Snake in C++

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Snake in C++

Posted 29 March 2012 - 05:02 PM

You say it isn't 'printing your cookies'. Is it printing everything else fine?
Was This Post Helpful? 0
  • +
  • -

#3 Oddfuture  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-March 12

Re: Snake in C++

Posted 29 March 2012 - 05:08 PM

View PostButchDean, on 29 March 2012 - 05:02 PM, said:

You say it isn't 'printing your cookies'. Is it printing everything else fine?


Yeah it's printing out the grid, the dots on screen and the snake. The snake can also move aswell however it won't show up the cookies or blocks.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Snake in C++

Posted 29 March 2012 - 05:17 PM

Some observations:

1. You have a char type for both Cookies and Blocks.
2. The char type is one byte, and you have it signed instead of unsigned, so your values are going out of range. Refer to the ASCII Table.
3. As you can see from the ASTII Table 1 is a non-printable character (SOH).
4. 219 is part of the extended character set that you do not have the range to represent.

Choose printable characters.
Was This Post Helpful? 0
  • +
  • -

#5 Oddfuture  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-March 12

Re: Snake in C++

Posted 30 March 2012 - 04:07 AM

View PostButchDean, on 29 March 2012 - 05:17 PM, said:

Some observations:

1. You have a char type for both Cookies and Blocks.
2. The char type is one byte, and you have it signed instead of unsigned, so your values are going out of range. Refer to the ASCII Table.
3. As you can see from the ASTII Table 1 is a non-printable character (SOH).
4. 219 is part of the extended character set that you do not have the range to represent.

Choose printable characters.


Hi there, thanks for your reply. I have changed the il]Cookies[/il] and Blocks to unsigned and have changed the characters so that they are within range however they still are not showing up

New code:

(Just the 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 unsigned char COOKIE = char(167);
const char DOT = '.';
const char PERIMETER = '#';
const unsigned char BLOCK = char(219);


Grid::Grid () :rows_ (YLEN), cols_ (XLEN)
{
	Field f; //xylocked char

	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;

			//block the outer fields of the Grid -> no escape for the Snake
			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);//container for Fields
	}
	}
	
  
}

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
	}//for
	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;
}


Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Snake in C++

Posted 30 March 2012 - 07:35 AM

How about if you change:

const unsigned char COOKIE = 'C';
const unsigned char BLOCK = 'B';



?
Was This Post Helpful? 0
  • +
  • -

#7 Oddfuture  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-March 12

Re: Snake in C++

Posted 30 March 2012 - 02:46 PM

View PostButchDean, on 30 March 2012 - 07:35 AM, said:

How about if you change:

const unsigned char COOKIE = 'C';
const unsigned char BLOCK = 'B';



?


Nope still nothing. Really stumped with this one.
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Snake in C++

Posted 30 March 2012 - 02:56 PM

:nono:

Please run this code:

#include <iostream>
using namespace std;

const unsigned char COOKIE = 'C';
const char DOT = '.';
const char PERIMETER = '#';
const unsigned char BLOCK = 'B';

int main()
{
	cout << COOKIE << endl;
	cout << DOT << endl;
	cout << PERIMETER << endl;
	cout << BLOCK << endl;

	return 0;
}



What do you see?
Was This Post Helpful? 0
  • +
  • -

#9 Oddfuture  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-March 12

Re: Snake in C++

Posted 30 March 2012 - 04:21 PM

View PostButchDean, on 30 March 2012 - 02:56 PM, said:

:nono:

Please run this code:

#include <iostream>
using namespace std;

const unsigned char COOKIE = 'C';
const char DOT = '.';
const char PERIMETER = '#';
const unsigned char BLOCK = 'B';

int main()
{
	cout << COOKIE << endl;
	cout << DOT << endl;
	cout << PERIMETER << endl;
	cout << BLOCK << endl;
        return 0;
}



What do you see?


Well I'm missing the using namespace std; . I also don't have a main either, or so I thought but when I try to run it I get this error "1>Main.obj : error LNK2005: _main already defined in grid.obj"

#include "grid.hpp"
#include "display.hpp"
#include <iostream>
#include <ctime>
using namespace std;


const unsigned char COOKIE = 'C';
const char DOT = '.';
const char PERIMETER = '#';
const unsigned char BLOCK = 'B';

int main () {

 cout << COOKIE << endl;
 cout << DOT << endl;
 cout << PERIMETER << endl;
 cout << BLOCK << endl;
}

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;


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);//container for Fields
	}
	}
	
  
}


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;
}

Was This Post Helpful? 0
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Snake in C++

Posted 30 March 2012 - 04:30 PM

using namespace std; just prevents you from putting std:: before every std method used in your source file.

Where is the other main()? I'm guessing that it's somewhere in grid.cpp that you haven't copied here.
Was This Post Helpful? 0
  • +
  • -

#11 Oddfuture  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-March 12

Re: Snake in C++

Posted 30 March 2012 - 04:41 PM

View PostButchDean, on 30 March 2012 - 04:30 PM, said:

using namespace std; just prevents you from putting std:: before every std method used in your source file.

Where is the other main()? I'm guessing that it's somewhere in grid.cpp that you haven't copied here.



Durr I have a main cpp, sorry not thinking tonight,

Main cpp

#include "Menu.h"
#include "display.hpp"
#include <vector>
#include <iostream>
#include <conio.h>
#include "Snake.hpp"
#include "grid.hpp"
#include "game.hpp"
using namespace std;

const unsigned min_x = 10;
const unsigned min_y = 5;
const unsigned max_x = 60;
const unsigned max_y = 20;

int main () {
	//displayMainMenu();
	        Grid g;
		Snake s1;
		char key =' ';
		Snake s;
	        Game game;
	        g.clear();
		
                bool endofgame = false ;
		while(!endofgame){
			unsigned choice =0;
			choice = displayMainMenu();

			if (choice ==0) {
			g.clear();
			game.play();
			}
			if(choice ==1){
				gotoXY(0,0);
				
			}
			if(choice == 2) {
			g.clear();
			game.displayhighscores();
			cin.get();
			g.clear();
			}
			if(choice ==3){
			g.clear();
			cout << "--------------------end of game -----------------";
}
	   
	   
	 //  for (;;)/> {
		//	key= _getch();

		//	//std::cout << key << std::ed1;
		//	s1.move(key, g);
		//	s1.showScore();

		//}//end for
		//}
		}
		//_getch();
		//system("pause");
		return 0;
	}


Was This Post Helpful? 0
  • +
  • -

#12 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Snake in C++

Posted 30 March 2012 - 04:43 PM

Cool. you're not using Dev-CPP are you? I'm seeing suspicious old code in there.
Was This Post Helpful? 0
  • +
  • -

#13 Oddfuture  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-March 12

Re: Snake in C++

Posted 30 March 2012 - 04:49 PM

View PostButchDean, on 30 March 2012 - 04:43 PM, said:

Cool. you're not using Dev-CPP are you? I'm seeing suspicious old code in there.


Nope, just using Visual C++
Was This Post Helpful? 0
  • +
  • -

#14 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Snake in C++

Posted 30 March 2012 - 04:50 PM

Which version?
Was This Post Helpful? 0
  • +
  • -

#15 Oddfuture  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-March 12

Re: Snake in C++

Posted 30 March 2012 - 04:53 PM

View PostButchDean, on 30 March 2012 - 04:50 PM, said:

Which version?


2010 10.0
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1