문제

나는이 코드를 만들었고, 실행할 때 화살표가 화면을 떠날 때까지 오류가 발생하지 않으며 (예 : (*i)-> x> maxx). 무작위가 아니지만 패턴을 찾으려고 노력하고 있습니다).

편집 : 위로 올라가면 임의의 순간 이동이 발생하지 않는 것 같습니다. 아래로 이동하면 O가 바닥으로 직접 순간 이동됩니다. 또한 O가 A '>'가되는 곳에서 결함이 발생했습니다. (나는 그것이 어떻게 일어나는지 알아 내려고 노력하고 있습니다)

편집 : 변환-인토-'>'글리치는 O가 화면의 오른쪽 하단에 있고 (player.x = 9; player.y = 9), "wqs"가 입력됩니다.

편집 : 오류가 _move () s 및 check () 내에 있는지 확인하기 때문에 클래스 선언을 제거했습니다.

편집 : 'wq'가 입력되면 변환 결함이 발생하는 것으로 보이며 다른 문자가 입력됩니다 (즉, 다음 움직임 "건너 뛰기")

편집 : Tranform 글리치는 player.x = 9; player.y = 8; 그런 다음 'Q'가 눌려지면 다음으로 플레이어가 '>'로 전환합니다.

이것은 코드입니다.

#include<vector>
#include<iostream>
#include<string>
using namespace std;
const int maxx = 10, maxy = 10; //two constants that show the size of the sector
char sector[maxx][maxy]; //array of characters used to display the sector
prgm player(0, 0, 'O'); //player definition at x0,y0,and displayed with 'O'
const int vsize = 1; //size of the enemy array (ie: how many enemies there will be
X1 a(9, 5, 'X', 10); //enemy "a", has a move function that moves it back and forth
virus * viral_data[vsize] = {&a}; //array of enemies used to set the sector
vector<antivirus*> antiviral_data; //vector of pointers to "antivirus" the weapon used
vector<antivirus*>::iterator I; //iterator for previous vector

void display() //function to display the sector
{
    for(int i = 0; i < maxy; i++) 
    {
        for(int j = 0; j < maxx; j++)
        {
            cout<<sector[j][i];
        }
        cout<<endl;
    }
    return;
}

void p_move() //function to get players input, then move the player or create "antivirus"
{
    char dir;
    cin>>dir;
    switch(dir)
    {
    case 'w':
        player.y--;
        break;
    case 'a':
        player.x--;
        break;
    case 's':
        player.y++;
        break;
    case 'd':
        player.x++;
        break;
    case 'q':
        antiviral_data.push_back(new aX1(player.x, player.y, '>')); //creates a new aX1 at the players position
        break;
    }
    return;
}

void v_move() //uses the enemies move
{
    for(int i = 0; i < vsize; i++)
    {
        viral_data[i]->move();
    }
    return;
}

void a_move() //uses the weapon (ie: moves the weapon forward)
{
    for(I = antiviral_data.begin(); I < antiviral_data.end(); I++)
    {
        (*I)->move();
    }
    return;
}

void set() //sets the sector array (char)
{
    for(int i = 0; i < maxy; i++)
    {
        for(int j = 0; j < maxx; j++)
        {
            sector[j][i] = ' '; makes the entire sector blank
        }
    }
    sector[player.x][player.y]=player.sym; //sets the sector at the player's position to 'O'
    for(int i = 0; i < vsize; i++)
    {
        sector[viral_data[i]->x][viral_data[i]->y] = viral_data[i]->sym; //sets the sector at each enemy's position to be 'X'
    }
    for(I = antiviral_data.begin(); I < antiviral_data.end(); I++)
    {
        sector[(*I)->x][(*I)->y] = (*I)->sym; //sets the sector at each weapon's position to be '>'
    }
    return;
}

void check() //prevents the player from moving off the screen, erases bullet if it moves of the screen (to prevent access to non-allocated memory)
{
    if(player.x < 0)
    {
        player.x = 0;
    }
    if(player.y < 0)
    {
        player.y = 0;
    }
    if(player.x > (maxx-1))
    {
        player.x = (maxx-1);
    }
    if(player.y > (maxy-1))
    {
        player.y = (maxy-1);
    }
     //PROBLEM APPEARS TO OCCUR HERE
    for(I = antiviral_data.begin(); I! = antiviral_data.end();)
    {
        if((*I)->x > maxx)
        {
            I = antiviral_data.erase(I);
        }
        else
        {
            I++;
        }
    }
     //*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    return;
}
int main()
{
    while(true)
    {
        set(); //set sector
        display(); //display sector
        p_move(); //player's move
        v_move(); //enemy's move
        a_move(); //bullet's move
        check();//check moves
    }
    return 0;
}
도움이 되었습니까?

해결책

check ()에서 테스트

((*I)->x > maxx)

해야한다

((*I)->x >= maxx)

. 이것은> 오류로 오류가 발생하여> 화면에서 하나의 정사각형을 얻을 수 있습니다. 디스플레이 루틴이 표시를 표시하려고하면 X의 디스플레이 기호를 깎습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top