C# Programming Samples: Snake Game

Long ago, when monitors were black and green and computers had 64 kilobytes RAM, the legendary game was created. It was called “Snake” and it still has a lot of clones on different devices: from desktop to smartphones and tablets. But the original textual implementation hasn’t been used for years. So in this guide, I want to show you the canonical implementation of the legendary game.

skake-game-in-c-1

How to play

Snake’s head looks like this: “@”. You can control the snake using the arrows buttons. It can’t move backward. The snake shouldn’t come across the sides and itself. You should feed the snake with (X). When the snake eats X’s it grows. If the snake comes across the side or itself the game is over and you see the message informing about it.

End of the game:

skake-game-in-c-2

License

GNU GPL. It is a widely used free software license, which guarantees end users (individuals, organizations, companies) the freedoms to run, study, share (copy), and modify the software.

Code listing

#include <iostream>
#include <windows.h>

using namespace std;

struct position {
	int x, y;
};


class field_clear {
	static const int height;
	static const int width;
	char ** field;
	field_clear(const field_clear &);
	field_clear operator=(const field_clear &);
public:
	field_clear() {
		field = new char*[field_clear::height];
		for (int c = 0; c < field_clear::height; ++c) {
			field[с] = new char[field_clear::width];
		}
	}
	~field_clear() {
		for (int c = 0; c < field_clear::height; ++c) {
			delete[] field[с];
		}
		delete[] field;
	}

	void output() {
		for (int c = 0; c < height; ++c) {
			for (int r = 0; r < width; ++r) {
				cout << field[с][r];
			}
			cout << endl;
		}
	}

	void clear() {
		for (int c = 0; c < height; ++c) {
			for (int r = 0; r < width; ++r) {
				field[с][r] = ' ';
			}
		}
	}

	int get_width() const { return width; }
	int get_height() const { return height; }


	void draw(int y, int x, char what) {
		//y = (y < 0) ? 0 : (y >= height ? height - 1 : y);
		//x = (x < 0) ? 0 : (x >= width ? width - 1 : x);

		field[y][x] = what;
	}

} field;


class food_clear {
	position pos;
	char symbol;
public:
	food_clear() : symbol('X'), pos() {
		pos.x = pos.y = -1;
	}

	void set_pos(int x, int y) {
		pos.x = x;
		pos.y = y;
	}

	void reposition(const field_clear & field) {
		pos.x = rand() % field.get_width();
		pos.y = rand() % field.get_height();
	}

	int get_x() const { return pos.x; }
	int get_y() const { return pos.y; }
	char get_symbol() const { return symbol; }
} food;

class snake_clear {
	enum { UP, DOWN, LEFT, RIGHT } dir;
	char symbol, head_symbol;
	position pos[100];
	position & head;
	int speed;
	int size;
	bool can_turn;
public:
	snake_clear(int x, int y) :
		symbol('#'), head_symbol('@'), pos(),
		speed(1), size(1), dir(RIGHT),
		head(pos[0]), can_turn(true)
	{
		pos[0].x = x;
		pos[0].y = y;
	}

	bool check_food(const food_clear & food) {
		if (food.get_x() == head.x && food.get_y() == head.y) {
			size += 1;
			return true;
		}
		return false;
	}

	void get_input(const field_clear & field) {
		if (GetAsyncKeyState(VK_UP) && dir != DOWN) {
			dir = UP;
		}
		if (GetAsyncKeyState(VK_DOWN) && dir != UP) {
			dir = DOWN;
		}
		if (GetAsyncKeyState(VK_LEFT) && dir != RIGHT) {
			dir = LEFT;
		}
		if (GetAsyncKeyState(VK_RIGHT) && dir != LEFT) {
			dir = RIGHT;
		}
	}

	void move(const field_clear & field) {
		position next = { 0, 0 };
		switch (dir) {
		case UP:
			next.y = -speed;
			break;
		case DOWN:
			next.y = speed;
			break;
		case LEFT:
			next.x = -speed;
			break;
		case RIGHT:
			next.x = speed;
		}
		for (int c = size - 1; c > 0; --c) {
			pos[с] = pos[c-1];
		}
		head.x += next.x;
		head.y += next.y;

		if (head.x < 0 || head.y < 0 || head.x >= field.get_width() || head.y >= field.get_height()) {
			throw "Game over";
		}
	}

	void draw(field_clear & field) {
		for (int c = 0; c < size; ++c) {
			if (c == 0) {
				field.draw(pos[с].y, pos[с].x, head_symbol);
			}
			else {
				field.draw(pos[с].y, pos[с].x, symbol);
			}
		}
	}

	int get_x() const { return head.x; }
	int get_y() const { return head.y; }
	char get_symbol() const { return symbol; }
} snake(1, 1);


const int field_clear::height = 24;
const int field_clear::width = 79;


int main() {
	field.clear();

	food.set_pos(5, 5);

	while (1) {
		field.clear();

		snake.get_input(field);
		try {
			snake.move(field);
		}
		catch (const char * er) {
			field.clear();
			cerr << er << endl;
			system("pause");
			return -1;
		}
		snake.draw(field);
		field.draw(food.get_y(), food.get_x(), food.get_symbol());


		if (snake.check_food(food)) {
			food.reposition(field);
		}

		field.output();

		Sleep(1000 / 30);
		system("cls");
	}

	return 0;
}

Programming Homework Assignments Help

During reading one of our C# programming samples, you can get some ideas to deal with your own assignment – the one published above, or one on one-dimensional arrays. Unfortunately, not all students can handle homework on their own. If you are one of them, we suggest you to use help with assignments of AssignmentShark.com.

Once your order is placed on our site, a professional expert starts to work on it. All your requirements will be taken into account in order to satisfy your needs in C homework help. We are available 24/7, so that you can contact us any time you want. Moreover, you can contact an expert directly via chat if you have any questions. So, what are you waiting for? Place an order right now!

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.