lundi 26 décembre 2016

About the random number in snake games

#include <iostream>
#include <string>
#include <windows.h>
#include <conio.h>
#include <time.h>
using namespace std;
int map_height=22;
int map_width=22;
string border="ùþ";
int snakeheadX=map_width/2-1;
int snakeheadY=map_height/2;
string head_symbol="¡·";
int snakesize;
bool gameover=false;
enum action{up, down, right1, left1, stop};
action moving=stop;
int foodx;
int foody;
void food();

void draw(){
    system("cls");
    food();
    for(int i=0;i<map_width;i++){ // Game frame
        cout<<border;
    }
    cout<<endl;
    for(int i=0;i<map_height;i++){
        cout<<border;
        for(int j=0;j<map_width-2;j++){
            if(i==snakeheadY && j==snakeheadX){
                cout<<head_symbol;
            }
            else if(i==foody && j==foodx){
                cout<<"¡¯";
            }
            else
            cout<<"  ";
        }
        cout<<border;
        cout<<endl;
    }
    for(int i=0;i<map_width;i++){
        cout<<border;
    }
}

void running(int moving){
    if(moving==up){ //Up
        snakeheadY--;
    }
    if(moving==down){ //Down
        snakeheadY++;
    }
    if(moving==left1){ //Left
        snakeheadX--;
    }
    if(moving==right1){ //Right
        snakeheadX++;
    }
    if(moving==stop){
        snakeheadX=snakeheadX;
        snakeheadY=snakeheadY;
    }
}
void food(){
    int prev_foodx=foodx;
    int prev_foody=foody;
    int a=rand()%(map_width-3)+1;
    int b=rand()%(map_height-1)+1;
    if(foodx==snakeheadX && foody==snakeheadY){
        if(prev_foodx==a && prev_foody==b){
                foodx=rand()%(map_width-3)+1;
                foody=rand()%(map_height-1)+1;
        }
        else{
            foodx=a;
            foody=b;
        }
    }
}

void move_logic(int getkeyboarddown){
    if(getkeyboarddown=='w'){ //Up
        snakeheadY--;
        moving=up;
    }
    if(getkeyboarddown=='s'){ //Down
        snakeheadY++;
        moving=down;
    }
    if(getkeyboarddown=='a'){ //Left
        snakeheadX--;
        moving=left1;
    }
    if(getkeyboarddown=='d'){ //Right
        snakeheadX++;
        moving=right1;
    }
}

void game_logic(){
    if (snakeheadX>=map_width || snakeheadX<=0 || snakeheadY<=0 || snakeheadY>=map_height){

        cout<<endl<<"Game over!";
        gameover=true;
    }
}

int main()
{
    srand(time(0));
    foodx=rand()%(map_width-3)+1;
    foody=rand()%(map_height-1)+1;
    while(!gameover){
        Sleep(50);
        cout<<foodx<<" "<<foody;
        running(moving);
        draw();
        game_logic();
        if(kbhit()){
            move_logic(getch());
        }

    }
    return 0;
}

The code above is my code of snake game in C++. Although the game is not finished, I found the food may sometimes appear in same location after the snake ate. Therefore, in the function food(), I add a script of code below to prevent this state.

if(foodx==snakeheadX && foody==snakeheadY){
    if(prev_foodx==a && prev_foody==b){
            foodx=rand()%(map_width-3)+1;
            foody=rand()%(map_height-1)+1;
            cout<<"ok";
    }
    else{
        foodx=a;
        foody=b;
    }

But I still found the food sometimes appear in same location after the snake ate that means the code I wrote is useless. How can I solve this problem?




Aucun commentaire:

Enregistrer un commentaire