简体   繁体   English

C数组的值随机变化

[英]C array's values changing randomly

Okay so I have been working on a small rouge like game to teach myself c however I cannot figure out why the array's end up changing randomly after initialization. 好的,所以我一直在研究诸如游戏之类的小胭脂,以自学c,但是我无法弄清楚为什么初始化后数组最终会随机改变。 Here is the code I have: 这是我的代码:

// 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();
    }

}

You are treating your array indexing as 1-based. 您将数组索引视为基于1的索引。 Arrays in C are 0-based. C语言中的数组基于0。

For example, you declare: 例如,您声明:

int player[]={1,1};

The only valid indices for this array are 0 and 1 ( ie player[0] and player[1] ). 该数组的唯一有效索引是0和1( player[0]player[1] )。

There is no such thing as player[2] - that is accessing memory outside the array bounds. 没有诸如player[2]这样的东西-正在访问数组范围之外的内存。 This is probably why you think your arrays are changing randomly. 这可能是您认为阵列随机变化的原因。 They are either being affected by out-of-bound writes to other arrays, or you are simply experiencing undefined behaviour . 它们或者受到对其他数组的超限写入的影响,或者您只是在经历未定义的行为

Your arrays useless and enemy are declared to contain two values each at initialization. 您的数组uselessenemy在初始化时声明为包含两个值。 Your main is printing array indices 1 and 2 . 您的main是打印数组索引12 In C, however, array indices start at 0 . 但是,在C中,数组索引从0开始。 So you should be printing indices 0 and 1 . 因此,您应该打印索引01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM