/projects/snakeCM.cs   Source Browser
// SnakeCM.cpp : Defines the entry point for the console application.
//Cynthia Miller
// C++
// 12.08.07
#include "stdafx.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
//definitions
#define EMPTY_CELL 0
#define VERTICAL_WALL 1
#define HORIZONTAL_WALL 2
#define FOOD 3
#define SNAKE_UP 4
#define SNAKE_DOWN 5
#define SNAKE_LEFT 6
#define SNAKE_RIGHT 7
#define SNAKE_FRONT 8
#define MAX_SIZE_COLUMNS 20
#define MAX_SIZE_ROWS 20
// variables
bool dead = false;
int grid[MAX_SIZE_COLUMNS][MAX_SIZE_ROWS];
int dir;
int frontY = 10;
int frontX = 10;
int backY = 10;
int backX = 10;
int score = 0;
//functions
void setup_grid();
void draw_grid();
void food();
void move();
void check_key_press();
//main part of program
void main()
{//lets you get a random spot
	srand((unsigned)time(0));
	setup_grid();
	draw_grid();
//makes the program run while the snake hasnt hit walls or itself
	while (dead == false)
	{
		//controlls speed thru clock.
		if ( clock() % 150 == 0 )
		{
			move();
			system("cls");
			draw_grid();
		}//check the change of direction
		check_key_press();
	}
	system("pause");
}
//sets up the grid
void setup_grid()
{
	grid[10][10] = SNAKE_FRONT;
	dir = SNAKE_DOWN;
	food();
	for (int n = 0; n < 20; n++)
	{//uses defined values to set up the walls
		grid[0][n] = HORIZONTAL_WALL;
		grid[19][n] = HORIZONTAL_WALL;
		grid[n][0] = VERTICAL_WALL;
		grid[n][19] = VERTICAL_WALL;
	}
}
//draws the grid
void draw_grid()
{
    int x = 0,
        y = 0;
    for (y = 0; y < 20; y++)
    {
        for (x = 0; x < 20; x++)
        {
            if (grid[y][x] == EMPTY_CELL)
			{				
				cout << " ";
			}
			else if(grid[y][x] == VERTICAL_WALL)
            {//wall
                cout << "|";
            }
            else if(grid[y][x] == HORIZONTAL_WALL)
            {//top or bottom wall
                cout << "-";
            }
            else if(grid[y][x] == FOOD)
			{//Apple yay
				cout << "@";
			}
			else if(grid[y][x] == SNAKE_UP || grid[y][x] == SNAKE_DOWN || grid[y][x] == SNAKE_LEFT || grid[y][x] == SNAKE_RIGHT || grid[y][x] == SNAKE_FRONT)
			{//snake
				cout << "X";
			}
		}
        cout << "\n";
    }
//this updates the score
	cout << "Score: " << score << "\n";
}
//sets up the food
void food()
{
	while (true)
	{// gets random spot
		int y = rand()% 18 + 1;
		int x = rand()% 18 + 1;
		if(grid[y][x] == EMPTY_CELL)
		{//assigns the spot for food
			grid[y][x] = FOOD;
			break;
		}
	}
}
//the method to move the snake
void move()
{
	grid[frontY][frontX] = dir;
	if(grid[frontY][frontX] == SNAKE_UP)
	{//assigns the direction
		frontY--;
	}
	else if(grid[frontY][frontX] == SNAKE_DOWN)
	{
		frontY++;
	}
	else if(grid[frontY][frontX] == SNAKE_LEFT)
	{
		frontX--;
	}
	else if(grid[frontY][frontX] == SNAKE_RIGHT)
	{
		frontX++;
	}// look how pretty i made everything
	if(grid[frontY][frontX] == EMPTY_CELL)
	{
		if(grid[backY][backX] == SNAKE_UP)
		{
			grid[backY][backX] = EMPTY_CELL;
			backY--;
		}
		else if(grid[backY][backX] == SNAKE_DOWN)
		{
			grid[backY][backX] = EMPTY_CELL;
			backY++;
		}
		else if(grid[backY][backX] == SNAKE_LEFT)
		{
			grid[backY][backX] = EMPTY_CELL;
			backX--;
		}
		else if(grid[backY][backX] == SNAKE_RIGHT)
		{
			grid[backY][backX] = EMPTY_CELL;
			backX++;
		}
	}
	else if(grid[frontY][frontX] == FOOD)
	{//when you hit a food piece
		food();
		//ups the scoreboard
		score++;
	}
	//Snake hits wall or itself
	else
	{//Bad
		dead = true;
	}
	grid[frontY][frontX] = SNAKE_FRONT;
}
//checks for keyboard press
void check_key_press()
{
	if(kbhit())//kb is keyboard hit
	{//getch took forever!
		int keys = getch();
		if(keys == 224) // Maybe some arrow key pressed
		{
			switch(_getch()) // get another character and check it
			{
				case 75: // left do you read these?
					dir = SNAKE_LEFT; 
					break;
				case 72: // up ahuh last project
					dir = SNAKE_UP; 
					break;
				case 77: // couldnt guess? right
					dir = SNAKE_RIGHT; 
					break;
				case 80: //look how much nicer this is than BattleShip! down
					dir = SNAKE_DOWN; 
					break;
			}
		}
	}
}