1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
//This is a snake game. The snake is a vector that moves around the board according to the commands input by the player. If it
//encounters food, the snake grows, and the food is moved to a new location. If the snake runs into itself, the game ends.
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
//The food class is simply a small integer array representing it's x and y coordinates. It has the field as a friend class to allow
//the field to read it's location, and access it's change location function.
class Food
{
public:
friend class Field;
Food(int a, int b)
{
foodarr[0]=a;
foodarr[1]=b;
}
//The change location function is called by the field whenever the player intersects food. It just changes the x and y coordinates
//of the food to make it jump to a new location.
void Changelocation(int x, int y)
{
foodarr[0] = x;
foodarr[1] = y;
}
private:
int foodarr[2];
};
//The player is the snake, and moves around the field eating food, and growing larger. Once again, the Field is a friend class to
//allow it to access the players location data, and the Eat method, when the player comes into contact with food.
class Player
{
public:
friend class Field;
// The constructor initializes the starting location of the player(a and b), and loads them into the body of the player. (The vector)
//it also initializes the bool value istherefood (who's purpose is explained later) to false.
Player(int a, int b)
{
playervector.push_back(b);
playervector.push_back(a);
istherefood = false;
}
//Move described below where defined.
void Move(char movechar);
//Eat is called by the field when the player intersects food. It changes the bool value istherefood to true. The purpose of this
//bool value is explained below in the Move function explanation.
void Eat()
{
istherefood = true;
}
//Defined for use during creation of code.
void Showvector();
private:
vector<int>playervector;
vector<int>::iterator i;
bool istherefood;
};
void Player::Showvector()
{
for(vector<int>::iterator i = playervector.begin(); i != playervector.end(); i++)
{
cout<<*i<<endl;
}
}
//This is the function that moves the player around the board. It does so by recieving a character input from the user: w,a,s, or d.
//It then translates this into movement by: reading the value of the first two elements of the vector representing the player
//(which correspond to the x and y coordinates of the head); it then alters them appropriately (subtract 1 from the x coordinate
//to move left, etc.) then pushes the new values onto the front end of the vector. Finally, it checks the bool value istherefood
//to determine whether the last move made intersected with food. If no food was intersected on the last move (istherefood == false)
//then it pops the back two numbers off the vector, thus preventing the vector (the player) from growing in length. If there was
//food intersected on the last move (istherefood ==true) then it doesn't pop the values off the back of the array, and it re-sets
//istherefood to false.
void Player::Move(char movechar)
{
if (movechar=='a')
{
i = playervector.begin();
playervector.insert(i,playervector[1]-1);
i = playervector.begin();
playervector.insert(i,playervector[1]);
if(istherefood==false)
{
playervector.pop_back();
playervector.pop_back();
}
else
{
istherefood = false;
}
}
else if (movechar=='d')
{
i = playervector.begin();
playervector.insert(i,playervector[1]+1);
i = playervector.begin();
playervector.insert(i,playervector[1]);
if(istherefood==false)
{
playervector.pop_back();
playervector.pop_back();
}
else
{
istherefood = false;
}
}
else if (movechar=='w')
{
i = playervector.begin();
playervector.insert(i,playervector[1]);
i = playervector.begin();
playervector.insert(i,playervector[1]-1);
if(istherefood==false)
{
playervector.pop_back();
playervector.pop_back();
}
else
{
istherefood = false;
}
}
else if (movechar=='s')
{
i = playervector.begin();
playervector.insert(i,playervector[1]);
i = playervector.begin();
playervector.insert(i,playervector[1]+1);
if(istherefood==false)
{
playervector.pop_back();
playervector.pop_back();
}
else
{
istherefood = false;
}
}
}
|