写入system(“ cls”)时,进程以返回值3221226356退出

问题描述 投票:-1回答:1

我正在控制台中制作蛇游戏,但是每当我在draw函数中编写system(“ cls”)时,它都会在Output屏幕上写出“ Processed exited with return value 3221226356”。我认为有一些内存泄漏,但是我找不到发生这种情况的确切原因。

您也可以在以下位置看到此代码:https://github.com/Lakshay-Dhingra/Snake-Game

这是我的代码://使用mingw std-c ++ 11编译器在Dev-C ++(Windows)中编译

#include<iostream>
#include<conio.h>
#include <unistd.h>
#include<cstdlib>
using namespace std;

class Node
{
    int x;
    int y;
    Node *link;

    public:
    Node(int ix,int iy)
    {
        x=ix;
        y=iy;
        link=nullptr;
    }

    int getx()
    {
        return x;
    }
    void setx(int x)
    {
        this->x=x;
    }
    int gety()
    {
        return y;
    }
    void sety(int y)
    {
        this->y=y;
    }

    Node* getLink()
    {
        return link;
    }
    void setLink(Node* x)
    {
        link=x;
    }
};

class LL
{
    public:
    Node* head;
    LL()
    {
        head=nullptr;
    }

    void insert(int x,int y)
    {
        Node* mynode=new Node(x,y);
        Node* temp=head;
        head=mynode;
        head->setLink(temp);
    }

//  For Debugging:
//  void showList()
//  {
//      Node* cur=head;
//      while(cur->getLink()!=nullptr)
//      {
//          cout<<cur->getx()<<" "<<cur->gety()<<" --> ";
//          cur=cur->getLink();
//      }
//      cout<<cur->getx()<<" "<<cur->gety()<<endl;
//  }

    int* show(int l)
    {

        int* arr=new int(2*l);
        int i=0;
        Node* cur=head;
        while(cur!=nullptr)
        {
            arr[i]=cur->getx();
            arr[i+1]=cur->gety();
            i+=2;
            cur=cur->getLink();
        }
        return arr;
    }

    ~LL()
    {
        Node* temp=head;
        while(head!=nullptr)
        {
            temp=head->getLink();
            delete head;
            head=temp;
        }
    }
};

class Snake
{
    int length;
    LL* mysnake;

    public:
    Snake(int l,int x,int y)
    {
        mysnake=new LL();
        for(int i=0;i<l;i++)
        {
            mysnake->insert((x+i),y);
        }
        length=l;
    }

    int getLength()
    {
        return length;
    }

    void eatFood(int x,int y)
    {
        length+=1;
        mysnake->insert(x,y);
    }

    int* showSnake()
    {
        return mysnake->show(length);
    }

    int getHeadX()
    {
        return mysnake->head->getx();
    }
    int getHeadY()
    {
        return mysnake->head->gety();
    }
};

class Window
{
    int length;
    int hieght;
    Snake* snk;
    int* arr;

    public:
    Window(int l,int h,int posx,int posy)
    {

        length=l;
        hieght=h;
        snk=new Snake(4,posx-3,posy);
        arr=snk->showSnake();

//      rungame();
//      For Debugging:
//      for(int i=0;i<2*(sk.getLength());i++)
//      {
//          cout<<arr[i]<<" ";
//      }
    }

    void rungame()
    {
        char ch;
        while(true)
        {
            if(kbhit())
            {
            ch = _getch();
            if(int(ch)==27)
                break;
            }
            draw();
        }
    }

    void draw()
    {
        system("cls");
//      arr=snk->showSnake();
//      cout<<"world ";
        bool flag;
        for(int i=0;i<length;i++)
        {
            for(int j=0;j<hieght;j++)
            {
                flag=0;
                if(i==0||i==length-1||j==0||j==hieght-1)
                {
                    cout<<"#";
                    flag=1;
                }
                else
                {
                    for(int k=0;k<snk->getLength();k++)
                    {
                        if(i==arr[2*k]&&j==arr[2*k+1])
                        {
                            cout<<"O";
                            flag=1;
                        }
                    }
                }

                if(flag==0)
                {
                    cout<<" ";
                }
            }
            cout<<endl;
        }
//      For Debugging:
//      for(int k=0;k<snk->getLength();k++)
//          cout<<arr[2*k]<<" "<<arr[2*k+1]<<" ";
        usleep(100000);
    }
};

int main()
{
    Window win(30,50,15,25);
    win.rungame();
    return 0;
}
c++ oop pointers console
1个回答
0
投票

unistd.h是Linux标头,不应将其包含在Windows项目中。这可能是原因,因为在Linux SO中该调用为system("clear");,所以由于我没有Windows系统,因此您必须自己对其进行测试。

© www.soinside.com 2019 - 2024. All rights reserved.