mardi 21 avril 2015

C++ random number generating

So, I made this game in c++ that has one problem : random number generating. The problem is that it generates a number bigger than it is supposed to, which breaks the game.The game is supposed to work like this : you have a character that you can move around with arrow keys. On the screen, you can see a circle which is put in a random location. If you collect it, the circle goes to another place and you get a point. The problem with it is that when c++ tries to generate a random number, it generates one way bigger than I need to and the game doesn't know what to do. Any suggestions on how to get random number generation working? This is my code :

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <string>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>

using namespace std;

void yGoSmall(int z){
    int go;
    for(go = 0; go < z; go++){
            cout << "" << endl;
        }
}
void yGoBig(int z, int a){
    int go;
    for(go = 0; go < z - a; go++){
            cout << "" << endl;
        }
}
void xGoSmall(int z){
    int go;
    for(go = 0; go < z; go++){
            cout << "  ";
        }
}
void xGoBig(int z, int a){
    int go;
    for(go = 0; go < z - a; go++){
            cout << "  ";
        }
}
void specialXGoSmall(int z){
    int go;
    z = z * 2 - 1;
    for(go = 0; go < z; go++){
        cout << " ";
    }
}
void specialXGoBig(int z, int a){
    int go;
    z = z * 2 - 1;
    a = a * 2;
    for(go = 0; go < z - a; go++){
        cout << " ";
    }
}
void menuMaker(int score,int scoreToBar){
    int scoreCopy = score;
while(true){
        score = score / 10;
        scoreToBar = scoreToBar - 1;
        if(score == 0)break;
    }
        cout << "+------------------------------------------------------------------------------" ;
        cout << "+|Score : " << scoreCopy ;
        while(true){
        scoreToBar = scoreToBar - 1;
        cout << " ";
        if(scoreToBar <= 0)break;
        }
        cout << "|";
        cout << "+------------------------------------------------------------------------------+";
}

int main()
{
    int x, y, go, yToMenu, yToScore, score, scoreToBar, helper, xCircle, yCircle;
    char direction;
    y=0;
    x=0;
    srand((unsigned)time(0));
    xCircle = (rand()%76)+1;
    yCircle = (rand()%20)+1;
    yToScore=0;
    score = 0;
    helper = 1;
    while(true){
    system("cls");
    scoreToBar = 70;
    menuMaker(score,scoreToBar);
    if(y < yCircle){
        yGoSmall(y);
        xGoSmall(x);
        cout << "@";
        yGoBig(yCircle, y);
        xGoSmall(xCircle);
        cout << "O";
    }
    if(y > yCircle){
        yGoSmall(yCircle);
        xGoSmall(xCircle);
        cout << "O";
        yGoBig(y, yCircle);
        xGoSmall(x);
        cout << "@";
    }
    if(y == yCircle && x < xCircle){
        yGoSmall(y);
        xGoSmall(x);
        cout << "@";
        specialXGoBig(xCircle, x);
        cout << "O";
    }
    if(y == yCircle && x > xCircle){
        yGoSmall(y);
        xGoSmall(xCircle);
        cout << "O";
        specialXGoBig(x, xCircle);
        cout << "@";
    }
    if(y == yCircle && x == xCircle){
        score++;
        system("cls");
        menuMaker(score,scoreToBar);
        xCircle = (rand()%76)+1;
        yCircle = (rand()%20)+1;
        if(y < yCircle){
        yGoSmall(y);
        xGoSmall(x);
        cout << "@";
        yGoBig(yCircle, y);
        xGoSmall(xCircle);
        cout << "O";
    }
    if(y > yCircle){
        yGoSmall(yCircle);
        xGoSmall(xCircle);
        cout << "O";
        yGoBig(y, yCircle);
        xGoSmall(x);
        cout << "@";
    }
    if(y == yCircle && x < xCircle){
        yGoSmall(y);
        xGoSmall(x);
        cout << "@";
        specialXGoBig(xCircle, x);
        cout << "O";
    }
    if(y == yCircle && x > xCircle){
        yGoSmall(y);
        xGoSmall(xCircle);
        cout << "O";
        specialXGoBig(x, xCircle);
        cout << "@";
    }
    }
    while(true){
            if(GetAsyncKeyState(VK_UP) != 0 && y!=0){
                if(GetAsyncKeyState(VK_UP) == 0)break;
                y=y-1;
                yToScore=yToScore-1;
                _sleep(250);
                system("cls");
                break;
                }
            if(GetAsyncKeyState(VK_DOWN) && y!=21){
                if(GetAsyncKeyState(VK_DOWN) == 0)break;
                y=y+1;
                yToScore=yToScore+1;
                _sleep(250);
                system("cls");
                break;
                }
            if(GetAsyncKeyState(VK_LEFT) != 0 && x!=0){
                if(GetAsyncKeyState(VK_LEFT) == 0)break;
                x=x-1;
                _sleep(250);
                system("cls");
                break;
                }
            if(GetAsyncKeyState(VK_RIGHT) != 0 && x!=77){
                if(GetAsyncKeyState(VK_RIGHT) == 0)break;
                x=x+1;
                _sleep(250);
                system("cls");
                break;
                }
            }
    }
return 0;
}




Aucun commentaire:

Enregistrer un commentaire