簡體   English   中英

打印指向結構的二維指針數組

[英]Printing 2 dimensional array of pointers to structs

昨天我一直在做家庭作業,大部分工作都做完了,但沒什么主要的。 我問其他學生,我不知道為什么它不起作用,但是沒人知道這是什么問題。 基本上這個程序是一個小游戲,每隊有18名球員9。 該程序會隨機為玩家提供坐標和方向,然后他們開始移動。 我基本上已經完成了該程序,但是我在場上遇到了問題,它根本沒有顯示出球員。 我嘗試了很多事情,當測試發現它在我編寫的if語句中甚至無法打印測試字符串時。 當我編寫此部分字段時[i] [j] =&players [k] [0]; 我檢查過field [i] [j]是否真的得到x和y坐標,是的。 但是在print_field類中,它將field [] []設置為null,並且該字段為空。 玩家是一系列結構。 字段是指向播放器或NULL的指針的數組。

我已經盡我所能嘗試了,沒有任何更好的選擇。 此代碼有什么問題? 為什么不顯示在場的球員?

#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");
} 

您需要Player[9][2]而不是Player[8][1] 盡管只能訪問最大length - 1索引,但應使用其長度初始化數組,因為數組是從0開始索引的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM