0

I have been doing homework yesterday, I have done most of it, but couldn't make the main thing. I don't know why it's not working I have asked other students, but nobody knows what's the problem. Basically this program is a small game, there are 18 players 9 on each team. the program randomly gives players coordinates and directions and they start to move. I have basically done the program, but I have problem with field, It doesn't show the players at all. I tried many things and when testing noticed that it doesn't print even testing string in the if statement I wrote. when I write this part field[i][j] = &players[k][0]; I have checked if field[i][j] really gets the x and y coordinate and yes it does. but in the print_field class it takes field[][] as null and the field is empty. players is an array of structs. field is an array of pointers that point to players or NULL.

I have tried with all of my knowledge and couldn't make any better. What is wrong with this code? Why isn't it showing the players on the field?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h> 
#define LENGTH 25 
#define WIDTH 25 
enum direction {Right, Left, Up, Down};             /*Right = 0, Left = 1, Up = 2, Down = 3*/

void print_field();
void random_positions();
void playerdirection();
void motion();
void game();

struct player
{
 char *dora;      
 char *team;
 char *name;       //string?
 int x,y;          //coordinates       
 int direction;      
};
typedef struct player Player;
struct player *field[WIDTH][LENGTH];
Player players[8][1];
int main()
{   
    srand (time(NULL));
    int i;              //players 9 in each team  team1 = 0  team2 = 1
    players[0][0].name =  "A1";
    players[1][0].name =  "A2";
    players[2][0].name =  "A3";
    players[3][0].name =  "A4";
    players[4][0].name =  "A5";
    players[5][0].name =  "A6";
    players[6][0].name =  "A7";
    players[7][0].name =  "A8";
    players[8][0].name =  "A9";
    players[0][1].name =  "B1";
    players[1][1].name =  "B2";
    players[2][1].name =  "B3";
    players[3][1].name =  "B4";
    players[4][1].name =  "B5";
    players[5][1].name =  "B6";
    players[6][1].name =  "B7";
    players[7][1].name =  "B8";
    players[8][1].name =  "B9";
    for(i = 0; i < 9 ; i++)
    {
          players[i][0].team = "Team A";
          players[i][1].team = "Team B";
          players[i][0].dora = "Alive";
          players[i][1].dora = "Alive";     
    }
    random_positions();
    playerdirection();
    print_field();
    motion (Player player);
    print_field();
    game();       
    return 0;
}

void random_positions()
{
     int i,j,k;
     int xs[17],ys[17];
     for(i= 0; i<9 ; i++)
     {
      players[i][0].x = rand() % 25;
      players[i][0].y = rand() % 25;
      players[i][1].x = rand() % 25;
      players[i][1].y = rand() % 25;
      printf("A%d x = %d y = %d \n",i+1,players[i][0].x,players[i][0].y);
      printf("B%d x = %d y = %d \n",i+1,players[i][1].x,players[i][1].y);          
     }
     for(i = 0; i < 9 ; i++)
     {
           xs[i] = players[i][0].x; 
           xs[i+8] = players[i][1].x;
           ys[i] = players[i][0].y; 
           ys[i+8] = players[i][1].y;
           for(j = 0; j <= i ; j++)
           {
                 //printf("j%d start\n",j);
                 if(i != j && xs[i] == xs[j])
                 {
                      //printf("i%d start\n",j);
                      if(ys[i] == ys[j])
                      {
                               return random_positions();                                    
                      }
                      //("j%d done\n",j);
                 }
                 //printf("j%d done\n",j);
           }  
     }
     for(i = 0; i < 25; i++)
         {
              for(j = 0; j < 25; j++)
              {
                    for(k = 0; k < 9; k++)
                    {
                          if(i == players[k][0].x && j == players[k][0].y)
                          {
                               field[i][j] = &players[k][0];
                          }
                          if(i == players[k][1].x && j == players[k][1].y)
                          {
                               field[i][j] = &players[k][1];
                          }
                          else field[i][j] = NULL;                             //I da J sheidzleba shesacvleli iyos
                    }           
              }      
         }    
}     

/*this function prints out the given state of the field*/
void print_field(){
int i,j;
printf("\n");
printf("|0 1 2 3 4 5 6 7 8 9 101112131415161718192021222324|\n"); /*just to show easier the allignment*/
for(j=0; j<WIDTH+2; j++)        /*This first loop goes through row and creates them each by each*/
{
         if(j == 0 || j == WIDTH +1)         /*creates the upper and lower part of the field*/
              for(i=0; i<LENGTH+2; i++)       /*there should be space for frame so I added 2 to LENGTH in the loop*/
              {        
                  if(i==0) 
                  printf("-");
                  else if(i == LENGTH+1)
                  printf("-\n");
                  else printf("--");        /*3 decimals*/
              }
         else 
              for(i=0; i<LENGTH+2; i++)     /*Goes through the columns in this row and creates either frame or puts the nodeid*/
              {
                  if(i==0)printf("|");      /*frame*/
                  else if(i == LENGTH+1) printf("| %d\n",(j-1));  /*frame*/
                  else if(field[j-1][i-1] != NULL) 
                  {
                       printf("aaa");
                       printf("%-*s",2,(*field[j-1][i-1]).name);       /*putting nodeid 3 decimals*/
                  }
                  else printf("  "); 
              }         
}
printf("\n");
} 
8
  • Here void motion (Player player); and here void game(); you will need to remove the void. You do not call a function with its return type. Just motion (Player player); and game(). And do you have function prototypes above your main()? Commented Feb 3, 2014 at 16:55
  • Oh yeah that's right, thx for notice ill correct that :) Commented Feb 3, 2014 at 16:57
  • players is a array of structs Commented Feb 3, 2014 at 17:01
  • You defined "Player players[8][1];" But you used "field[i][j] = &players[k][1]", is this "player[k][1]" with overflow? Commented Feb 3, 2014 at 17:12
  • hmm I don't think so. Why? i think as k is less then 9 it will not ovewflow. Commented Feb 3, 2014 at 17:16

1 Answer 1

1

You need Player[9][2] instead of Player[8][1]. You should initialize an array with its length although you could only access index up to length - 1 because arrays are indexed from 0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.