#include "stdafx.h" // Used for Visual Studio
#include <iostream> // Used for cout/cin
#include <conio.h> // Used for getch()
#include <windows.h> // Used for clearing screen
#include <ctime> // Used for managing time (speeding/slowing gmae)
#include <vector> // Used for items
#include <fstream> // USed for high score
using namespace std;
#define WIDTH 20
#define HEIGHT 20
char Level[HEIGHT][WIDTH];
void Initialize(int size, int snakeX[], int snakeY[]); void ClearScreen(); void BuildLevel(); void ReDraw(); void Update(int &size, int snakeX[], int snakeY[], int tempX[], int tempY[], int &item_count, int &points); void SpeedUpdate(); void Items(vector<int> &itemX, vector<int> &itemY, int &item_count, int snakeX[], int snakeY[], int &size, int tailX, int tailY, int &points); bool IsGameOver(int snakeX[], int snakeY[], int size); void HighScore(int points);
int main()
{
int size = 3; int item_count = 0;
int points = 0;
int snakeX[100], snakeY[100], tempX[100], tempY[100];
Initialize(size, snakeX, snakeY);
BuildLevel();
Update(size, snakeX, snakeY, tempX, tempY, item_count, points);
HighScore(points);
_getch();
return 0;
}
void Initialize(int size, int snakeX[], int snakeY[])
{
snakeX[0] = WIDTH / 2; snakeY[0] = 3; snakeX[1] = WIDTH / 2; snakeY[1] = 2;
snakeX[2] = WIDTH / 2; snakeY[2] = 1;
for (int i = 3; i < 100; i++)
{
snakeX[i] = NULL;
snakeY[i] = NULL;
}
for (int i = 0; i < size; i++)
Level[snakeY[i]][snakeX[i]] = 'o';
}
void BuildLevel()
{
for (int i = 0; i<height;> {
for (int j = 0; j<width;> {
Level[0][j] = '*';
Level[i][0] = '*';
Level[i][WIDTH - 1] = '*';
Level[HEIGHT - 1][j] = '*';
}
}
}
void ReDraw()
{
for (int i = 0; i < HEIGHT; i++)
{
cout << endl;
for (int j = 0; j < WIDTH; j++)
cout << " " << Level[i][j];
}
}
void ClearScreen()
{
HANDLE hOut;
COORD Position;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
Position.X = 0;
Position.Y = 0;
SetConsoleCursorPosition(hOut, Position);
}
void Update(int &size, int snakeX[], int snakeY[], int tempX[], int tempY[], int &item_count, int &points)
{
int count = 0;
char input = ' ';
char previnput = 's';
int tailX, tailY;
bool gameOver = false;
vector<int> itemX, itemY;
while (!gameOver) {
SpeedUpdate();
tailY = snakeY[size - 1];
tailX = snakeX[size - 1];
Level[tailY][tailX] = ' ';
Items(itemX, itemY, item_count, snakeX, snakeY, size, tailX, tailY, points);
for (int i = 0; i < size - 1; i++)
{
tempX[i + 1] = snakeX[i]; tempY[i + 1] = snakeY[i]; }
tempX[0] = snakeX[0];
tempY[0] = snakeY[0];
for (int i = 0; i < size; i++)
{
snakeX[i] = tempX[i]; snakeY[i] = tempY[i]; }
if (_kbhit())
input = _getch();
if (previnput == 's')
{
snakeY[0] += 1; if (input == 'a' || input == 'A') previnput = 'a';
else if (input == 'd' || input == 'D') previnput = 'd';
}
else if (previnput == 'w')
{
snakeY[0] -= 1; if (input == 'a' || input == 'A') previnput = 'a';
else if (input == 'd' || input == 'D') previnput = 'd';
}
else if (previnput == 'd')
{
snakeX[0] += 1; if (input == 'w' || input == 'W') previnput = 'w';
else if (input == 's' || input == 'S') previnput = 's';
}
else if (previnput == 'a')
{
snakeX[0] -= 1; if (input == 'w' || input == 'N') previnput = 'w';
else if (input == 's' || input == 'S') previnput = 's';
}
for (int i = 0; i < size; i++)
Level[snakeY[i]][snakeX[i]] = 'o';
gameOver = IsGameOver(tempX, tempY, size);
if (gameOver == true)
{
cout << "\a\a";
break;
}
ClearScreen();
ReDraw();
}
}
void Items(vector<int> &itemX, vector<int> &itemY, int &item_count, int snakeX[], int snakeY[], int &size, int tailX, int tailY, int &points)
{
clock_t interval = clock() % 3000;
cout << "\t\t Points: " << points << endl;
if (interval > 2800)
{
item_count++;
srand((unsigned)time(NULL));
itemX.push_back( rand() % (WIDTH - 2) + 1);
itemY.push_back( rand() % (HEIGHT - 2) + 1);
Level[itemY.back()][itemX.back()] = '@';
}
for (int i = 0; i < item_count; i++)
{
if ( (snakeX[0] == itemX.at(i)) && (snakeY[0] == itemY.at(i)) )
{
points += 100;
item_count--;
itemY.erase(itemY.begin() + i);
itemX.erase(itemX.begin() + i);
size++;
snakeX[size - 1] = tailX;
snakeY[size - 1] = tailY;
cout << "\a";
}
}
}
void SpeedUpdate()
{
if (clock() <= 9000) {
Sleep(100);
cout << endl << " Level 1";
}
else if ((clock() > 9000) && (clock() < 18000)) {
Sleep(50);
cout << endl << " Level 2";
}
else if ((clock() > 18000) && (clock() < 36000)) {
Sleep(25);
cout << endl << " Level 3";
}
else
cout << endl << " Level 4";
}
bool IsGameOver(int snakeX[], int snakeY[], int size)
{
for (int i = 2; i << size; i++)
{
if ( (snakeX[0] == snakeX[i]) && (snakeY[0] == snakeY[i]) )
{
cout << endl << "\t\t YOU LOSE!";
_getch();
return true;
}
}
if ((snakeX[0] == WIDTH - 1) || (snakeY[0] == HEIGHT - 1) || (snakeX[0] == 0) || (snakeY[0] == 0))
{
cout << endl << "\t\t YOU LOSE!";
_getch();
return true;
}
else
return false;
}
void HighScore(int points)
{
char fileName[] = "HighScore.txt";
int score[5];
int temp[5];
int count = 0;
int position = 5;
bool save = false;
system("cls"); ifstream inputFile;
inputFile.open(fileName);
while (inputFile.eof()) {
inputFile >> score[count]; count++;
}
inputFile.close();
for (int i = 4; i > 0; i--) {
if (points > score[i]) {
if (position > 0)
position--; save = true; }
}
if (save == true) {
if (position == 0)
cout << "CONGRATULATIONS, YOU HAVE ACHIEVED THE HIGHEST SCORE!" << endl;
else
cout << "Well done, you are now raked #" << position + 1 << "!" << endl;
for (int i = 4; i >= position; i--)
temp[i + 1] = score[i];
for (int i = 4; i >= position + 1; i--)
score[i] = temp[i];
cout << endl << "Score: " << points << endl << endl;
score[position] = points;
ofstream outputFile;
outputFile.open(fileName);
cout << "HIGH SCORES" << endl;
cout << "-----------" << endl;
for (int i = 0; i < 5; i++)
{
cout << i + 1 << ". " << score[i] << endl;
outputFile << score[i] << endl; }
outputFile.close(); }
else
{
cout << "You did't make it to the hall of fame..." << endl << "Make sure you do better next time!" << endl;
cout << endl << "Score: " << points << endl << endl;
inputFile.open(fileName); while (!inputFile.eof()) inputFile >> score[count];
cout << "HIGH SCORES" << endl;
cout << "-----------" << endl;
for (int i = 0; i < 5; i++)
cout << i + 1 << ". " << score[i] << endl;
inputFile.close(); }
}