Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8695061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:56:21+00:00 2026-06-13T00:56:21+00:00

Im trying to create a program that shows a character matrix 10×10, and shows

  • 0

Im trying to create a program that shows a character matrix 10×10, and shows a character in it, and in every 600 miliseconds the character would move randomly. But my problem is that everytime i run the program, it is the same movement.

If you look at the function Random_move i used the function Rand()… i also tried with srand(time(null)); before, but that only makes both characters, a and b, to move always in the same direction. Can someone please help.

#include <iostream>
#include <windows.h>
#include <time.h>
using namespace std;

class mapa
{
    private :
    char map[10][10];
    char background[10][10];
    public :
    mapa();
    void cpy_btom();
    void copy_to_map(int, int, char);
    void print();
};
class character
{
    private :
    int posx;
    int posy;
    char type;
    public :
    character(char,int,int);
    void send_print(mapa &);
    void random_move(mapa &);
    void delay(int);
};

int main()
{
    character C1('A', 5, 5);
    character C2('B', 8, 2);
    mapa Mapa;
    while(!GetAsyncKeyState(VK_ESCAPE))
    {
        Mapa.print();

        C1.delay(750);

        C1.random_move(Mapa);
        C2.random_move(Mapa);
    }
}

void mapa :: cpy_btom()
{
    for(int a = 0;a < 10;a++)
    {
        for(int b = 0;b<10;b++)
        {
            map[a][b] = background[a][b];
        }
    }
}
void mapa :: print()
{
    system("cls");

    for(int a = 0;a<10;a++)
    {
        for(int b = 0;b<10;b++)
        {
            cout << map[a][b];
        }
        cout << endl;
    }
    cpy_btom();
}
character :: character(char kind = 'a', int x = 5, int y = 5)
{
    type = kind;
    posx = x;
    posy = y;
}
void character :: send_print(mapa & mapa)
{
    mapa.copy_to_map(posx, posy, type);
}

void character :: random_move(mapa & MAP)
{

    int a = rand() % 5;
    int b = rand() % 50;
    if(a == 0) //x --
    {
        if(b < 45)
        {
            if(posx > 0)
                posx--;
        }
        else
        {
            if(posx > 1)
            posx = posx - 2;
        }
    }
    else if(a == 1)
    {
        if(b < 45)
        {
            if(posx < 10)
            posx++;

        }
        else
        {
            if(posx < 9)
            posx = posx + 2;
        }
    }
    else if(a == 2)
    {
        if(b < 45)
        {
            if(posy > 0)
            posy--;
        }
        else
        {
            if (posy > 1)
            posy = posy - 2;
        }
    }
    else if(a == 3)
    {
        if(b < 45)
        {
            if(posy < 10)
            posy++;
        }
        else
        {
            if(posy < 9)
            posy = posy + 2;
        }
    }
    send_print(MAP);
}

void character :: delay(int time)
{
    int a = clock();
    int b = clock() + time;
    while(a < b)
    {
        a = clock();
    }
}
mapa :: mapa()
{
    for(int a = 0;a < 10;a++)
    {
        for(int b = 0;b < 10;b++)
        {
            map[a][b] = ' ';
            background[a][b] = ' ';
        }
    }
}

void mapa :: copy_to_map(int x, int y, char kind)
{
    map[x][y] = kind;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:56:22+00:00Added an answer on June 13, 2026 at 12:56 am

    Try calling srand(time(0)) in the beginning of your main.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a program that will get data directly from socket
I am trying to create a program that will be passed input data from
I'm trying to create a program that takes a text file of c++ code
I am trying to create a program that will do some simple calculations, but
I am trying to create a program that has one tableview and when you
I'm currently trying to create a program that estimates location based on signal strength.
I'm trying to create a c program that implements the radon transform algorithm. I
I am trying to create a Jenkins job that restarts a program that runs
I have a small program that I'm trying to create to get ip addresses
Alright so what I am trying to do is essentially create a program that,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.