-1

さて、私は自分自身を教えるためにゲームのような小さなルージュに取り組んできましたが、初期化後に配列がランダムに変化する理由がわかりません。ここに私が持っているコードがあります:

// ConsoleApplication2.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

const int x=10;
const int y=10;

int rexit[]={5,5};
int player[]={1,1};
int enemy[]={x,y};
int useless[]={1,1};




/*
int getinput(int len){
    char temp[100];
    return(atoi(strtok(fgets(temp,len+1,stdin),"\n")));

}
*/


void bad(){
  float bob;
  float temp[8]={1,1};
  temp[1]=player[1]-enemy[1];
  temp[2]=player[2]-enemy[2];
  bob=pow(temp[1],2)+pow(temp[2],2);
  printf("%f\n",bob);

   if (sqrt(bob)<=5){
    if (abs(player[1]-enemy[1])>abs(player[2]-enemy[2])){
        if (player[1]-enemy[1]<0 && enemy[1]-1>=0){
         enemy[1]=enemy[1]-1;
        }
        else if (enemy[1]+1<=x && player[1]-enemy[1]!=0){
            enemy[1]=enemy[1]+1;
        }
    }
    else;
     if (player[2]-enemy[2]<0 && enemy[2]-1>=0){

  enemy[2]=enemy[2]-1;
     }
     else if (enemy[2]+1<=y && enemy[2]-player[2]!=0){
      enemy[2]=enemy[2]+1;
     }
 }
}

void map() {
    int s;
    int a;
    bad();
    printf("%i %i\n",rexit[1],rexit[2]);
    printf("+");
    for (a=0;a<=x;a++){
    printf("-");
    }

    printf("+\n");
    for (s=0;s<=y;s++){
        printf("|");
        for (a=0;a<=x;a++){
         if ((player[1]==rexit[1] && player[2]==rexit[2]) || (player[1]==enemy[1] & player[2]==enemy[2])){
          exit(EXIT_SUCCESS);
         }
         else if (rexit[1]==s && rexit[2]==a){
          printf("E");
         }
         else if (player[1]==s && player[2]==a){
          printf("*");
         }
         else if (enemy[1]==s && enemy[2]==a){
          printf("@");
         }
         else{
         printf(".");
         }
        }
        printf("|\n");
    }
    printf("+");
    for (a=0;a<=x;a++){
    printf("-");
    }

    printf("+\n");
}


void move(){
    char me=_getch();
    int temp=0;

    me=toupper(me);
    if (me=='W'){ player[1]=player[1]-1; if (player[1]<=0) player[1]=0;}

    else if (me=='S'){ player[1]=player[1]+1; if (player[1]>=y) player[1]=y;}

    else if (me=='A'){ player[2]=player[2]-1; if (player[2]<=0) player[2]=0;}

    else if (me=='D'){ player[2]=player[2]+1; if (player[2]>=x) player[2]=x;}

    else {temp=1;}
    if (temp==1){
        move();}
    else{
        system("cls");
        map();}
}


void circle(char c,int x)
{
     int i,j;
    for(i=-x;i<x;i++)
    {
        for(j=-x;j<x;j++)
        {
            if(i*i+j*j<x*x)
                printf("%c",c);
            else
                printf(" ");

        }
        printf("\n");
    }
}



void main(){
    printf("%i %i\n",enemy[1],enemy[2]);
    printf("%i %i\n",useless[1],useless[2]);
    system("title BioGames");
    system("color A"); // the colours are from 1 to 15

    map();
    while (true){
     move();
    }

}
4

2 に答える 2

5

配列のインデックスを 1 ベースとして扱っています。C の配列は 0 ベースです。

たとえば、次のように宣言します。

int player[]={1,1};

この配列の有効なインデックスは 0 と 1 (つまり player[0]player[1]) だけです。

player[2]- 配列境界外のメモリにアクセスするというようなことはありません。これがおそらく、配列がランダムに変化していると思われる理由です。それらは、他の配列への範囲外の書き込みの影響を受けているか、単に未定義の動作を経験しています。

于 2013-09-02T00:16:45.657 に答える
4

配列はuselessenemy初期化時にそれぞれ 2 つの値を含むように宣言されています。あなたmainは配列インデックス1とを印刷して2います。ただし、C では、配列インデックスは から始まり0ます。0したがって、インデックスと を印刷する必要があり1ます。

于 2013-09-02T00:16:32.670 に答える