I am a university student and my teacher is making us to create a prey/predator simulation with OMP for. I had covid during his lectures and he didn’t upload them, so sorry I am being stupid here
Onto the question,
I have implemented Age as the absolute value for the cell as he asked but I simply cannot think of a way to make each cell have their own individual age after breeding. Here is my code:
#pragma omp parallel num_threads(4)
{
#pragma omp for schedule(dynamic, 1)
for (int i = 1; i <= Y; i++) //for loop Y
{
for (int j = 1; j <= X; j++) //for loop X
{
int Left = i - 1;
int Right = i + 1;
int Bottom = j - 1;
int Top = j + 1;
int Fish = 0;
int Shark = 0;
int FishAge = 1;
int SharkAge = -1;
int NewFishAge = 1;
//COUNTING SHARK AND FISH
//SHARK OR FISH - TOP LEFT
if (oldOcean[Left][Top] > 0)
{
Fish++;
}
else if (oldOcean[Left][Top] < 0)
{
Shark--;
}
//SHARK OR FISH - TOP
if (oldOcean[i][Top] > 0)
{
Fish++;
}
else if (oldOcean[i][Top] < 0)
{
Shark--;
}
//SHARK OR FISH - TOP RIGHT
if (oldOcean[Right][Top] > 0)
{
Fish++;
}
else if (oldOcean[Right][Top] < 0)
{
Shark--;
}
//SHARK OR FISH - LEFT
if (oldOcean[Left][j] > 0)
{
Fish++;
}
else if (oldOcean[Left][j] < 0)
{
Shark--;
}
//SHARK OR FISH - LEFT
if (oldOcean[Right][j] > 0)
{
Fish++;
}
else if (oldOcean[Right][j] < 0)
{
Shark--;
}
//SHARK OR FISH - LEFT
if (oldOcean[Left][Bottom] > 0)
{
Fish++;
}
else if (oldOcean[Left][Bottom] < 0)
{
Shark--;
}
//SHARK OR FISH - LEFT
if (oldOcean[i][Bottom] > 0)
{
Fish++;
}
else if (oldOcean[i][Bottom] < 0)
{
Shark--;
}
//SHARK OR FISH - LEFT
if (oldOcean[Right][Bottom] > 0)
{
Fish++;
}
else if (oldOcean[Right][Bottom] < 0)
{
Shark--;
}
//AGE
if (oldOcean[i][j] > 0)
{
oldOcean[i][j]++;
FishAge = oldOcean[i][j] - 2;
}
else if (oldOcean[i][j] < 0)
{
oldOcean[i][j]--;
SharkAge = oldOcean[i][j] * -1;
//std::cout << "AGE" << SharkAge;
}
//EMPTY CELLS & BREEDING
if (oldOcean[i][j] == 0)
{
//newOcean[i][j] = 0;
if (Fish >= 4 && Shark > -4 && FishAge >= 2)
{
newOcean[i][j]=1;
}
else if (Shark <= -4 && Fish < 4 && SharkAge >= 3)
{
newOcean[i][j] = -1;
}
else
{
newOcean[i][j] = 0;
}
newOcean[i][j] = oldOcean[i][j];
}
//FISH CELLS
if (oldOcean[i][j] > 0)
{
if (FishAge < 10)
{
FishAge++;
}
//std::cout <<FishAge;
if (FishAge >= 10 || Shark <= -5)
{
oldOcean[i][j] = 0; //kill fish
FishAge = 0;
}
if (Fish >= 8)
{
newOcean[i][j] = 0; //overpopulation
}
newOcean[i][j] = oldOcean[i][j];
}
//SHARK CELLS
if (oldOcean[i][j] < 0)
{
if (SharkAge <= 20 )
{
SharkAge++;
}
if ((Shark <= -6 && Fish == 0) || SharkAge >= 20)
{
oldOcean[i][j] = 0; // dies of Starvation
SharkAge = 0;
}
if (ChanceofDeath <= rand() / ((int)RAND_MAX + 32))
{
newOcean[i][j] = 0; // Random Death ???
}
newOcean[i][j] = oldOcean[i][j];
}
}
}
}
I am a very amateur coder, so any improvement feel free to tell me too please, I love to learn
my code for the age is under the "//AGE" section
Sorry again, and thanks for the help.
Even without considering age, you must check every cell in every generation. And it should be easy to maintain an age per cell during these checks:
If a dead cell has three neighbors, it gets born (cell.age = 0).
If an alive cell has two or three neighbors, it survives (cell.age++).
If an alive cell has less than two or more than three neighbors, it dies. (No cell.age any more.)
Related
So here is the code I wrote for the question(https://codingcompetitions.withgoogle.com/kickstart/round/0000000000436140/000000000068c509#problem). For the sample input I am getting the right answer but it is not clearing test set 1.
I have created the code such that it checks for "1" up down right and left for each element of array and sees whether from that junction an L can be made.
For reference these are the conditions in the question:
"A segment is called "good" if all the cells in the segment contain only 1s.
An "L-shape" is defined as an unordered pair of segments, which has all the following properties:
Each of the segments must be a "good" segment.
The two segments must be perpendicular to each other.
The segments must share one cell that is an endpoint of both segments.
Segments must have length at least 2.
The length of the longer segment is twice the length of the shorter segment."
#include <bits/stdc++.h>
int main()
{
using namespace std;
int t, u;
cin >> t;
for (u = 1; u <= t; u++) {
int i, j, k, l = 0, a[40][40], r, c, right = 0, left = 0, up = 0, down = 0, downc, upc, lc, rc;
cin >> r >> c;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
cin >> a[i][j];
}
}
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
if (a[i][j] == 1) {
right = 0;
left = 0;
up = 0;
down = 0;
downc = 0;
upc = 0;
lc = 0;
rc = 0;
for (k = i; k < r; k++) {
if (a[k][j] == 0)
break;
else
down++;
}
for (k = i; k >= 0; k--) {
if (a[k][j] == 0)
break;
else
up++;
}
for (k = j; k < c; k++) {
if (a[i][k] == 0)
break;
else
right++;
}
for (k = j; k >= 0; k--) {
if (a[i][k] == 0)
break;
else
left++;
}
if (!(up > 1 && down > 1 && right > 1 && left > 1)) {
downc = down;
upc = up;
rc = right;
lc = left;
if (up >= 2) {
if (right >= 4) {
while ((upc * 2) > right)
upc--;
l = l + upc - 1;
}
upc = up;
if (left >= 4) {
while ((upc * 2) > left)
upc--;
l = l + upc - 1;
}
upc = up;
}
if (down >= 2) {
if (right >= 4) {
while ((downc * 2) > right)
downc--;
l = l + downc - 1;
}
downc = down;
if (left >= 4) {
while ((downc * 2) > left)
downc--;
l = l + downc - 1;
}
downc = down;
}
if (right >= 2) {
if (up >= 4) {
while ((rc * 2) > up)
rc--;
l = l + rc - 1;
}
rc = right;
if (down >= 4) {
while ((rc * 2) > down)
rc--;
l = l + rc - 1;
}
rc = right;
}
if (left >= 2) {
if (up >= 4) {
while ((lc * 2) > up)
lc--;
l = l + lc - 1;
}
lc = left;
if (down >= 4) {
while ((lc * 2) > down)
lc--;
l = l + lc - 1;
}
lc = left;
}
}
}
}
}
cout << "Case #" << u << ": " << l << "\n";
}
}
Ok so I found the issue finally :
There was no need for " if (!(up > 1 && down > 1 && right > 1 && left > 1)) {.....}"
But still this code is too time consuming to run 1000x1000 grid case. Hence it fails test 2.
Edit: It was a mistake on my end. I bounded the array as 40x40. On changing it to a[1000][1000] it ran both tests successfully. Congos to me.
The other day, I wrote a console game of Tic-Tac-Toe in c++ for my son. He wanted me to add a computer, and I ended us using the minimax algorithm for the first time. I did some quick testing, but really just gave my laptop to my son as soon as it was printing stuff, who played with it for a couple minuets. I looked over his sholder once or twice, and noticed that it wasn't playing optimally, iv'e been trying to debug it, but I can't see where it goes wrong. I tried getting rid of alpha beta prunning, but that did not change anything.
For context, on the board the computer is -1, blank is 0, and the player is 1.
Here is the minimax function:
int minimax(int board[9], int depth, int alpha, int beta, bool isMaxizimaizingPlayer)
{
bool found = false;
for (int i = 0; i < 9; i++)
{
if (board[i] == 0)
{
found = true;
}
}
if (!found)
{
return eval(board);
}
if (depth == 0 || eval(board) != 0)
{
return eval(board);
}
if (isMaxizimaizingPlayer)
{
int maxEval = -2;
for (int spot = 0; spot < 9; spot++)
{
if (board[spot] == 0)
{
board[spot] = 1;
int e = minimax(board, depth - 1, alpha, beta, false);
if (e > maxEval)
{
maxEval = e;
}
//if (beta < alpha)
//{
// break;
//}
board[spot] = 0;
}
}
return maxEval;
}
else {
int minEval = 2;
for (int spot = 0; spot < 9; spot++)
{
if (board[spot] == 0)
{
board[spot] = -1;
int e = minimax(board, depth - 1, alpha, beta, true);
if (e < minEval)
{
minEval = e;
}
//if (beta < alpha)
//{
// break;
//}
board[spot] = 0;
}
}
return minEval;
}
}
To be compleate, here is my eval function:
int eval(int board[9])
{
/*horizontial*/
for (int i = 0; i < 3; i++)
{
if (board[i * 3] == board[i * 3 + 1] && board[i * 3 + 2] == board[i * 3] && board[i * 3] != 0)
{
return board[i * 3];
}
}
/*vertical*/
for (int i = 0; i < 3; i++)
{
if (board[i] == board[i + 3] && board[i] == board[i + 6] && board[i] != 0)
{
return board[i];
}
}
/*Both diags*/
if (board[4] != 0) {
if (board[0] == board[4] && board[0] == board[8])
{
return board[4];
}
if (board[2] == board[4] && board[4] == board[6])
{
return board[4];
}
}
return 0;
}
And here is the inital call:
int spot = 0;
int minEval = 2;
for (int i = 0; i < 9; i++)
{
if (board[i] == 0)
{
board[i] = -1;
int score = minimax(board, 3, -2, 2, false);
if (score < minEval) {
minEval = score;
spot = i;
}
board[i] = 0;
}
}
std::cout << "The computer went in spot " << spot + 1 << std::endl;
board[spot] = -1;
printBoard(board);
It looks like you only call minimax with a depth of three, so the algorithm will only look up to three moves ahead, if you want optimal play you need to set the depth to > 9, so that the agent is always looking ahead to the end of the game.
I have been trying to make a minesweeper game where given coordinates for a cell it will recursively reveal adjacent cells until a cell adjacent to a bomb is found. I have a method that given coordinates x and y calculates how many mines are surrounding it.
// Counts how many mines are adjacent to a given coordinate cell if any
void board::mineCount(int x, int y) {
// North
if (y > 0) {
if (board[x][y - 1].hasMine) {
board[x][y].mineCount++;
}
}
// South
if (y < dimensions[1] - 1) {
if (board[x][y + 1].hasMine) {
board[x][y].mineCount++;
}
}
// East
if (x < dimensions[0] - 1) {
if (board[x + 1][y].hasMine) {
board[x][y].mineCount++;
}
}
// West
if (x > 0) {
if (board[x - 1][y].hasMine) {
board[x][y].mineCount++;
}
}
// North East
if (x < dimensions[0] - 1 && y > 0) {
if (board[x + 1][y - 1].hasMine) {
board[x][y].mineCount++;
}
}
// North West
if (x > 0 && y > 0) {
if (board[x - 1][y - 1].hasMine) {
board[x][y].mineCount++;
}
}
// South East
if (x < dimensions[0] - 1 && y < dimensions[1] - 1) {
if (board[x + 1][y + 1].hasMine) {
board[x][y].mineCount++;
}
}
// South West
if (x > 0 && y < dimensions[1] - 1) {
if (board[x - 1][y + 1].hasMine) {
board[x][y].mineCount++;
}
}
}
Each cell is a struct which has a mineCount field that gets incremented by 1 each time a mine is found adjacent to it. I am having trouble figuring out where my recursion logic would go. I tried doing something like:
// North
if (y > 0) {
if (board[x][y - 1].hasMine) {
board[x][y].mineCount++;
} else {
minecount(x, y-1);
}
}
for each position but to no avail. Any pointers would be appreciated.
The recursion shouldn't be a part of the code that performs the mine count itself. It should be part of the function that's responsible for revealing nearby tiles.
int get_adjacent_mine_count(point p) {
int mine_count = 0;
for(int i = -1; i <= 1; i++) {
for(int j = -1; j <= 1; j++) {
point this_point(p.x + i, p.y + j);
//is_inside_board checks to see if the point's coordinates are less than 0
//or greater than the board size
if(!is_inside_board(board, this_point)) continue;
//We ignore the center tile
if(i == 0 && j == 0) continue;
if(board(this_point).hasMine)
mine_count++;
}
}
return mine_count;
}
void reveal_tiles(point p) {
//We shouldn't throw if the recursion is correct
if(board(p).hasMine) throw Explosion("Stepped on a Mine!");
//Single call to previously defined function
int num_of_adjacent_mines = get_adjacent_mine_count(p);
//I'm assuming this gets initialized to -1 beforehand
board(p).revealed = num_of_adjacent_mines;
if(num_of_adjacent_mines == 0) {
for(int i = -1; i <= 1; i++) {
for(int j = -1; j <= 1; j++) {
point this_point(p.x + i, p.y + j);
if(!is_inside_board(board, this_point)) continue;
if(i == 0 && j == 0) continue;
if(board(this_point).revealed == -1)
reveal_tiles(this_point);
}
}
}
}
I'm going to strongly recommend you write a simple Matrix class to represent board, which my code implies you've done, because that's a much more robust solution than just trying to interact with a 2D array the C-style way you're doing it.
I'm designing and programming an elevator-like robot for a high school project. Could I possibly do anything to make this any simpler? Or better? I have attached a picture of my design that I made in AutoCAD Inventor with labels.
For those not familiar with RobotC or VEX (it is VERY similar to C and C++): the limit switches (limit1, limit2, ...) and bump switches (floor1, floor2, ...) are analog buttons and return a value of 0 if not pressed and 1 if pressed. The motor (mainMotor) rotates the gear which causes the mechanism to travel upwards on the slide. When the shaft sticking out the motor mechanism moves up and down, it presses limit switches and causes it to return a value of 1.
int callup [3];
int calldown [3];
int floorat[3];
int main ()
{
if (SensorValue[limit1] == 1)
{
floorat[0] = 1;
}
else
{
floorat[0] = 0;
}
if (SensorValue[limit2] == 1)
{
floorat[1] = 1;
}
else
{
floorat[1] = 0;
}
if (SensorValue[limit3] == 1)
{
floorat[2] = 1;
}
else
{
floorat[2] = 0;
}
if (SensorValue[floor1] == 1)
{
calldown[0] = 1;
SensorValue[LED1] = 1;
}
if (SensorValue[floor2] == 1 && floorat[2] == 1)
{
calldown[1] = 1;
SensorValue[LED2] = 1;
}
if (SensorValue[floor2] == 1 && floorat[0] == 1)
{
callup[1] = 1;
SensorValue[LED2] = 1;
}
if (SensorValue[floor3])
{
callup[2] = 1;
SensorValue[LED3] = 1;
}
motors ();
}
void motors ()
{
if (callup[2] == 1 && floorat[2] == 1)
{
int x = 1;
while (x < 3)
{
SensorValue[LED3] = 1;
wait(0.5);
SensorValue[LED3] = 0;
wait(0.5);
}
callup[2] = 0;
main ();
}
else if (callup[1] == 1 && floorat[1] == 1)
{
int x = 1;
while (x < 3)
{
SensorValue[LED2] = 1;
wait(0.5);
SensorValue[LED2] = 0;
wait(0.5);
}
callup[1] = 0;
main ();
}
else if (callup[0] == 1 && floorat[0] == 1)
{
int x = 1;
while (x < 3)
{
SensorValue[LED1] = 1;
wait(0.5);
SensorValue[LED1] = 0;
wait(0.5);
}
callup[0] = 0;
main ();
}
if (callup[2] == 1 && floorat[1] == 1 && calldown[0] == 0 || callup[2] == 1 && floorat[0] == 1 && callup[1] == 0)
{
startMotor(mainMotor, 60);
untilTouch(limit3);
stopMotor(mainMotor);
callup[2] = 0;
wait(1);
main ();
}
if (callup[1] == 1 && floorat[0] == 1)
{
startMotor(mainMotor, 60);
untilTouch(limit2);
stopMotor(mainMotor);
callup[1] = 0;
wait(1);
main();
}
if (calldown[1] == 1 && floorat[2] == 1)
{
startMotor(mainMotor, -60);
untilTouch(limit2);
stopMotor(mainMotor);
calldown[1] = 0;
wait(1);
main();
}
if (calldown[0] == 1 && floorat[2] == 1 && calldown[1] == 0 || calldown[0] == 1 && floorat[1] == 1)
{
startMotor(mainMotor, -60);
untilTouch(limit1);
stopMotor(mainMotor);
calldown[0] = 0;
wait(1);
main();
}
}
Although it shouldn't be a concern for this question, the 60 in the startMotor command is the speed of the motor, just to make it clearer.
Feel free to ask any more questions.
Let's define what are the states of an elevator at a given moment:
An elevator can go up, down, or be idle.
The elevator is at a given floor and go from one floor to the other when it trigger a switch:
Now, if we translate this into some pseudo code (which should be easily translated to RobotC) :
enum elevator_status = { idle, down, up };
int currentfloor; //1, 2, 3
switch(elevator_status)
{
case idle:
//we check if a button is pressed and possibly go up or down
if(SensorValue(floor1))
{
if(currentfloor > 1)
elevator_status = down;
}
else if(SensorValue(floor2))
{
if(currentfloor > 2)
elevator_status = down;
else if(currentfloor < 2)
elevator_status = up;
}
else if(SensorValue(floor3))
{
if(currentfloor < 3)
elevator_status = up;
}
break;
case up:
case down:
//we check if we trigger a floor switch and stop the elevator
if(SensorValue(limit1))
{
currentfloor = 1;
elevator_status = idle;
}
else if(SensorValue(limit2))
{
currentfloor = 2;
elevator_status = idle;
}
else if(SensorValue(limit3))
{
currentfloor = 3;
elevator_status = idle;
}
break;
}
//we set the speed of the motor
if(elevator_status == up)
{
set_motorstate(cw);
)
else if(elevator_status == down)
{
set_motorstate(ccw);
}
else if(elevator_status == idle)
{
set_motorstate(idle);
}
Note : in this code the elevator will only take care of new up and down floor calls when the elevator is idle. It does not store up and down call while it is moving and go there later. I do not know if it was a requirement for you.
I'm not familiar with RobotC or VEX, however I've noticed a certain amount of replicated operations that could be made into their own functions.
The following code snippets I would make into separate functions. So in the large function called motors you have the following set of operations:
int x = 1;
while (x < 3)
{
SensorValue[LED3] = 1;
wait(0.5);
SensorValue[LED3] = 0;
wait(0.5);
}
callup[2] = 0;
main ();
This is repeated with slightly different values.
Here I'd write a function like the following:
void adjust_sensors( size_t led, size_t level )
{
int x = 1;
while (x < 3)
{
SensorValue[led] = 1;
wait(0.5);
SensorValue[led] = 0;
wait(0.5);
}
callup[level] = 0;
main ();
}
You can do the same for the following code as well:
startMotor(mainMotor, 60);
untilTouch(limit3);
stopMotor(mainMotor);
callup[2] = 0;
wait(1);
main ();
Also it seems like the while loop will never end because the value of x never changes.
You also have a typo at the top when you declare:
int callown [2];
I presume you meant:
int calldown [2];
Would be good to add some comments to your code as well for clarity.
Hope this helps.
I could be way off, because I'm just a student with questions of my own but it looks like you may have made a mistake in your array sizes. For instance, when you declared:
int floorat[2];
This made the array size 2. Then you refer to 3 element locations in this array [0, 1, 2]. Also, can't you just use a regular integer, and assign it values 1, 2, or 3?
I would recommend redefining these varaibles as:
int callup;
int calldown;
int floorat;
Then you can avoid extra lines of code and simplify the if/else clauses to:
if (SensorValue[limit1] == 1)
{
floorat = 1;
}
if (SensorValue[limit2] == 1)
{
floorat = 2;
}
if (SensorValue[limit3] == 1)
{
floorat = 3;
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I'm writing Snake in C++, using OpenGL and GLFW. I'm trying to implement a feature where the game exits, when the snakes head crashes into its body.
Here are the move() and CrashCheck() functions of the Snake class that I wrote.
x_pos is a floating point array that stores the x_coordinate of a segment of the snake body. y_pos does the same with the y_coordinate. length is the number of body segments in the snake, and increases when the snake eats food(not yet implemented). x_speed and y_speed store the speeds of the segments along the axis. The snake can never move along both the axes simultaneously; Also, float MAX_S = 0.00075;. I've included my draw() function as well. And Fix_Snake_x and Fix_Snake_y are functions that realign the segments of the snake (because they kept separating and causing havoc with the game). I know it's a stupid way to correct the problem, so if you can suggest fixes in the move() function, that would helpful.
void draw(float win_aspect)
{
for(int a = 0; a < length; a++)
{
Fix_Snake_y();
glBegin(GL_QUADS);
glColor3f(1.0,0.0,0.0);
glVertex2f(x_pos[a],y_pos[a]);
glVertex2f((x_pos[a]+0.05),y_pos[a]);
glVertex2f((x_pos[a]+0.05),y_pos[a]-0.05);
glVertex2f(x_pos[a],y_pos[a] - 0.05);
glEnd();
Fix_Snake_x();
}
}
void move()
{
for(int a = length ; a >= 0; a--)
{
if(a > 0)
{
if(x_pos[a] >= x_pos[a-1] && x_speed[a] < 0)
{
x_pos[a] += -MAX_S;
Fix_Snake_y();
Fix_Snake_x();
if(x_pos[a] <= x_pos[a - 1])
{
x_speed [a] = 0;
if(y_pos[a] <= y_pos[a-1])
{
y_speed[a] = MAX_S;
}
else
{
y_speed[a] = -MAX_S;
}
}
}
if(x_pos[a] <= x_pos[a-1] && x_speed[a] > 0)
{
x_pos[a] += MAX_S;
Fix_Snake_y();
Fix_Snake_x();
if(x_pos[a] >= x_pos[a - 1])
{
x_speed [a] = 0;
if(y_pos[a] <= y_pos[a-1])
{
y_speed[a] = MAX_S;
}
else
{
y_speed[a] = -MAX_S;
}
}
}
if(y_pos[a] <= y_pos[a-1] && y_speed[a] > 0)
{
y_pos[a] += MAX_S;
Fix_Snake_y();
Fix_Snake_x();
if(y_pos[a] >= y_pos[a-1])
{
y_speed[a] = 0;
if(x_pos[a] >= x_pos[a-1])
{
x_speed[a] = -MAX_S;
}
if(x_pos[a] <= x_pos[a-1])
{
x_speed[a] = MAX_S;
}
}
}
if(y_pos[a] >= y_pos[a-1] && y_speed[a] < 0)
{
y_pos[a] += -MAX_S;
Fix_Snake_y();
Fix_Snake_x();
if(y_pos[a] <= y_pos[a-1])
{
y_speed[a] = 0;
if(x_pos[a] >= x_pos[a-1])
{
x_speed[a] = -MAX_S;
}
if(x_pos[a] <= x_pos[a-1])
{
x_speed[a] = MAX_S;
}
}
}
}
if(a == 0)
{
x_pos[0] += x_speed[0];
y_pos[0] += y_speed[0];
Fix_Snake_y();
Fix_Snake_x();
}
CrashCheck();
}
}
void CrashCheck()
{
for(int a = 1; a < length; a++)
{
if(y_speed[0] > 0 && y_speed[a] == 0)
{
if(x_pos[0] < x_pos[a] && x_pos[0] < x_pos[a] + 0.05)
{
if(y_pos[0] < y_pos[a] && y_pos[0] > y_pos[a] - 0.05)
{
exit(0);
}
}
}
else if(y_speed[0] < 0 && y_speed[a] == 0)
{
if(x_pos[0] > x_pos[a] && x_pos[0] < x_pos[a] + 0.05)
{
if(y_pos[0] < y_pos[a] && y_pos[0] > y_pos[a] - 0.05)
{
exit(0);
}
}
}
}
}
void Fix_Snake_x()
{
for(int a = 1; a<length; a++)
{
if(a > 0)
{
if(x_pos[a] <= x_pos[a-1] - 0.05)
{
x_pos[a] = x_pos[a-1] - 0.05;
}
if(x_pos[a] >= x_pos[a -1] + 0.05)
{
x_pos[a] = x_pos[a-1] + 0.05;
}
}
}
}
void Fix_Snake_y()
{
for(int a = 1; a < length; a++)
{
if(a > 0)
{
if(y_pos[a] <= y_pos[a-1] - 0.05)
{
y_pos[a] = y_pos[a-1] - 0.05;
}
if(y_pos[a] >= y_pos[a-1] + 0.05)
{
y_pos[a] = y_pos[a-1] + 0.05;
}
}
}
}
Edit:
New move function
for(int a = 0; a < length; a++)
{
if(a > 0)
{
if(x_speed[a] < 0 && x_pos[a] >= x_pos[a-1])
{
x_pos[a] += x_speed[a];
if(x_pos[a] == x_pos[a-1])
{
y_speed[a] = y_speed[a-1];
x_speed[a] = 0;
continue;
}
}
if(x_speed[a] > 0 && x_pos[a] <= x_pos[a-1])
{
x_pos[a] += x_speed[a];
if(x_pos[a] == x_pos[a-1])
{
y_speed[a] = y_speed[a-1];
x_speed[a] = 0;
continue;
}
}
if(y_speed[a] > 0 && y_pos[a] <= y_pos[a-1])
{
y_pos[a] += y_speed[a];
if(y_pos[a] == y_pos[a-1])
{
x_speed[a] = x_speed[a-1];
y_speed[a] = 0;
}
}
if(y_speed[a] < 0 && y_pos[a] >= y_pos[a-1])
{
y_pos[a] += y_speed[a];
if(y_pos[a] == y_pos[a-1])
{
x_speed[a] = x_speed[a-1];
y_speed[a] = 0;
}
}
}
else
{
x_pos[0] += x_speed[0];
y_pos[0] += y_speed[0];
}
}
Is causing a few problems. The snake breaks it there are too many simultaneous turns. Only the first two blocks remain in motion
If I were you, I would store a std::set with all the invalid coordinates that the snake can't go to. That would include:
the border of the "playground"
obstacles
the snake's body
Then for each move of the snake, considering the x/y speed(s) I would first try to InsertLocation into CInvalidPlaces, if that returns true then I can step there, if false then the snake's just about to hit a wall, the border or it's own body and the "game" can finish. Here's the code for that:
#include <set>
using namespace std;
typedef pair<int,int> tInvalidLocation;
struct ltSeCmp
{
bool operator()(tInvalidLocation s1, tInvalidLocation s2) const
{
if (s1.first == s2.first) return s1.second > s2.second;
return s1.first > s2.first;
}
};
typedef set<tInvalidLocation, ltSeCmp> tInvalidLocations;
class CInvalidPlaces
{
private:
tInvalidLocations mInvalid; //this set will hold all the invalid locations for the snake to go to
public:
bool InsertLocation(tInvalidLocation iLoc)
{
if (mInvalid.find(iLoc) != mInvalid.end()) return false; //check if the location is already in the set
//we survived.. it's safe to go there :)
mInvalid.insert(iLoc);
return true;
}
bool RemoveLocation(tInvalidLocation iLoc)
{
if (mInvalid.find(iLoc)== mInvalid.end()) return false;
mInvalid.insert(iLoc);
return true;
}
};
What you will have to do additionally is :
initially add the margins, all the obstacles, and all the positions of the snake just as they are from where the snake starts
modify the move routine, so that when the snake moves, it also has to remove from CInvalidPlaces it's tail using RemoveLocation
after you implement the "enlargement" of the snake you'll also have to add to CInvalidPlaces the extra segment.
If need be, you can find in the following places extra information about an stl::set :
SGI
CPP.com
HTH,JP
I highly recommend that you use a dynamic container to hold the coordinates of the snakes's body. This allows to you take the coordinate of the snakes new position and search the container for the coordinates. If the point is found, the snake has run into itself.
Similarly you can have container for points of walls and blocks and other entities that is not part of the board.
An alternative is to use a grid data structure (or matrix), and place values in it representing the snakes body and other obstacles.