-1

I need some help on a assignment whose documentation follows:

Your task is to simulate a game between two teams of nine players. The reason for nine players is to facilitate reasonable printing out of the playing field onto the screen or into an output file. Smashball is played on a 25 by 25 square field. When printing out game results, be sure to print out the field with a border around the field.

  1. Game Initialization

a. Use a random number generator to randomly place all the players in the playing field. If the random process produces two players starting on the same position, redraw new random positions such that all players start on unique spots.

b. Use a random number generator to generate an initial motion direction for each player. The four motion choices for this assignment are East, West, North and South. These directions correspond to the respective movement of each player on the playing field.

My code (so far) follows:

#include <stdio.h>
#include <stdlib.h>

#define SIZE_AREA 25
#define SIZE_TEAM 25

int team [SIZE_TEAM];
int field [SIZE_AREA][SIZE_AREA];
int main (){

int i;
int j;

for (i = 0; i < SIZE_TEAM; i++){
team [i] = field [(rand() % 24)][(rand() % 24)];
}

enum move_direction {East, West, North, South};

for (j = 0; j < SIZE_TEAM; j++){

It's incomplete, but I still am hitting some roadblocks here and there:

  1. How do if the random process produces two players that are starting on the same spot? I need to compare one player's position to the other players' position to see if it's unique. If not, then I have to redraw the positions, but how do I this recursively in C? What if the redrawn positions still are duplicates to another player's position?

  2. The game initialization section asks me to randomly generate initial motion directions for the players, but I can't assign the direction to the players since I'll overwrite their positions. How can I code directions for the players without overwriting their initial positions and how can I change their direction from turn to turn?

That's all I have to say. Again, read the link before commenting and let me know if you can help me (but please, don't give me full-blown answers. Just indicate the source of my concerns and tell me what I should do).

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
Michformer
  • 51
  • 1
  • 6

2 Answers2

1
  1. For the second and subsequent players, pick randomly from the free locations. One easy way to do that is to shuffle a list of locations. Assign location N in the shuffled list to player N.
  2. Consider keeping two data structures to represent the game state, one representing the current position, the other representing the next position. At the end of calculating the next position, swap them and clear the new next position structure.
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
0
  1. One way to do it is to put all players in incremental positions on the field, then shuffle their positions by using one of the array shuffling algorithms.

  2. You can make a struct for the players, and keep position and direction as separate variables.

Community
  • 1
  • 1
blashyrk
  • 107
  • 1
  • 6
  • What do you mean by "incremental positions"? Also, where should I put the shuffle algorithm? – Michformer Jan 26 '14 at 14:41
  • Also, that shuffle algorithm doesn't apply for a 2d array. – Michformer Jan 26 '14 at 14:59
  • The order in which you put them at start doesn't matter as long as they are on unique positions initially, so you can fill the 2D array incrementally like so: `for (i = 0; i < SIZE_TEAM; i++){ team [i] = field [i][i];}`. Since you asked us not to give you "full blown answers" I'll just give you a hint. Treat both the outer array and the inner array as separate arrays and shuffle them both. – blashyrk Jan 26 '14 at 17:53
  • Also, note that you haven't even initialized the `field` 2D array. Things won't be going the way you expect them to. I suggest you to go back to the drawing board. – blashyrk Jan 26 '14 at 18:03