Thread: Survivor Program

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    8

    Survivor Program

    Hello everyone. I am currently working on a program for school where I have to stimulate a survivor game. In the game, there is a "creature" that is randomly generated on a 6x6 island grid. The game requires no user input, so the creature will just randomly "hop" around the island until it reaches a randomly generated bridge, at which point it escapes and ends the game, hops into the water on the sides of the island, or reaches turn 20, which means that the "creature" dies due to it having starved. So far I have generated an array, where w represents water on the edge of the island. Survivor Program-survivor-game-jpg . So far I have this code written:

    Code:
     #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    
    
    int main(void)
    {
        int i = 0;
        int rabbit = 0;
        int go = 0;
        int row = 0;
        int column = 0;
        int line = 0;
        char island[6][6] =
        {
        {'w','w','w','w','w','w'},
        {'w',' ',' ',' ',' ','w'},
        {'w',' ',' ',' ',' ','w'},
        {'w',' ',' ',' ',' ','w'},
        {'w',' ',' ',' ',' ','w'},
        {'w','w','w','w','w','w'},
        };
    
    
    for( i = 0; i<=19; i++)
    
    
    {
    printf("\n\n");
    printf(" %c | %c | %c | %c | %c | %c |\n", island[0][0], island[0][1], island[0][2], island[0][3], island[0][4], island[0][5]);
    printf("--------------------------\n");
    printf(" %c | %c | %c | %c | %c | %c |\n", island[1][0], island[1][1], island[1][2], island[1][3], island[1][4], island[1][5]);
    printf("--------------------------\n");
    printf(" %c | %c | %c | %c | %c | %c |\n", island[2][0], island[2][1], island[2][2], island[2][3], island[2][4], island[2][5]);
    printf("--------------------------\n");
    printf(" %c | %c | %c | %c | %c | %c |\n", island[3][0], island[3][1], island[3][2], island[3][3], island[3][4], island[3][5]);
    printf("--------------------------\n");
    printf(" %c | %c | %c | %c | %c | %c |\n", island[4][0], island[4][1], island[4][2], island[4][3], island[4][4], island[4][5]);
    printf("--------------------------\n");
    printf(" %c | %c | %c | %c | %c | %c |\n", island[5][0], island[5][1], island[5][2], island[5][3], island[5][4], island[5][5]);
    printf("--------------------------\n");
    
    
    
    
    srand (time(NULL));
        int i = 0;
        while(i < 20)
        {
            int moves = rand() % 9 + 1;
    
    
        }
    }
    }
    My plan to simulate the creature hopping is to use a random number generator that randomly chooses a number between 0-8. Each number corresponds to the creature moving 1 space diagonally, vertically or to the sides. For example, 0 would correspond to the creature moving up to the left 1 would correspond to the creature moving up one space vertically etc. This would just correspond to a column and/or row change of 1. I wish to know how I can accomplish this. And I also wish to know how I can randomly generate a creature on the island.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Ayu12 View Post
    My plan to simulate the creature hopping is to use a random number generator that randomly chooses a number between 0-8. Each number corresponds to the creature moving 1 space diagonally, vertically or to the sides. For example, 0 would correspond to the creature moving up to the left 1 would correspond to the creature moving up one space vertically etc. I wish to know how I can accomplish this.
    Your sample code already generates random numbers, but I guess what you actually want is a number in the range 0...7 inclusive if you number them the way you propose. So I would change your code to rand() % 8.

    Quote Originally Posted by Ayu12 View Post
    And I also wish to know how I can randomly generate a creature on the island.
    Can the creature only appear on land? If there are N land spots on your map, then generate a number 1...N and place the creature in the land spot indicated by that number. 1 is the first land spot, and so on.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    you only need to call srand() once i.e. it should not be in a loop
    Last edited by africanwizz; 07-01-2014 at 07:09 PM. Reason: meh..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C-Survivor: Outwit, Outplay, Outlast
    By Cshot in forum A Brief History of Cprogramming.com
    Replies: 119
    Last Post: 11-29-2002, 02:41 PM
  2. C-Survivor Team 1 thread
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 09-03-2002, 01:57 PM
  3. C-Survivor Team 2 Thread
    By Cshot in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 08-24-2002, 01:21 PM
  4. C-Survivor threads moved back...
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-21-2002, 04:43 PM

Tags for this Thread