How do you add a C++ library in Xcode? (Ncurses)

Viewed 79

I am a beginner to Xcode and coding in general. I have this code for a snake game, but it cannot run because I do not know how to correctly install ncurses. This is the code:

#include <iostream>
#include <cstdlib>
#include <ncurses.h>

using namespace std;


bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirection dir;

int TailX[100], TailY[100];
int nTail = 0;

void Setup()
{
    initscr();
    clear();
    noecho();
    cbreak();
    curs_set(0);
    
    gameOver = false;
    dir = STOP;
    x = width / 2;
    y = height / 2;
    fruitX = (rand() % width) + 1;
    fruitY = (rand() % height) + 1;
    score = 0;
}

void Draw() // draw a game map
{
    clear();
    for (int i = 0; i < width + 2; i++) {
        mvprintw(0,i,"#"); // print top wall
    }
    cout << endl;
    
    for (int i = 0; i < height+2; i++)
    {
        for (int j = 0; j < width+2; j++)
        {
            if (i == 0 || i==21)
                mvprintw(i,j,"#"); // outside walls
            else if (j == 0 || j == 21)
                mvprintw(i, j, "#"); // outside walls
            
            else if (i == y && j == x)
                mvprintw(i,j,"O"); // print head of snake
            else if (i == fruitY && j == fruitX)
                mvprintw(i, j, "@"); // print fruit
            else
                for (int k = 0; k < nTail; k++)
                {
                    if (TailX[k] == j && TailY[k] == i)
                        mvprintw(i, j, "o"); // print tail of snake
                }
            }
        
    }
    mvprintw (23, 0, "Score %d", score);
    refresh();
}

    void Input()

    {

      keypad(stdscr, true);
      halfdelay(1);

      int c = getch();

      switch(c)

      {

        case KEY_LEFT:
          dir = LEFT;
          break;
        case KEY_RIGHT:
          dir = RIGHT;
          break;
        case KEY_UP:
          dir = UP;
          break;
        case KEY_DOWN:
          dir = DOWN;
          break;

        case 'Q':
          gameOver = true;
          break;

      }

    }

    void Logic()

    {
      int prevX = TailX[0];
      int prevY = TailY[0];
      int prev2X, prev2Y;
      TailX[0] = x;
      TailY[0] = y;



      for (int i = 1; i < nTail; i++)

      {

        prev2X = TailX[i];
        prev2Y = TailY[i];
        TailX[i] = prevX;
        TailY[i] = prevY;
        prevX = prev2X;
        prevY = prev2Y;

      }

      switch(dir)

      {

        case LEFT:
          x--;
          break;
              
        case RIGHT:
          x++;
          break;
              
        case UP:
          y--;
          break;

        case DOWN:
          y++;
          break;

        default:
          break;

      }

      if(x > width || x < 1 || y < 1 || y > height)
        gameOver = true;

        if(x == fruitX && y == fruitY)
      {
        score++;
          fruitX = (rand() % width)+1;
          fruitY = (rand() % height)+1;
        nTail++;
      }

      for (int i = 0; i < nTail; i++)

        if (TailX[i] == x && TailY[i] == y)

        {
          gameOver = true;
        }

    }

    int main()

    {
      Setup();
      Draw();

      while(!gameOver)

      {
        Draw();
        Input();
        Logic();

      }

      getch();
      endwin();
      return 0;

    }

I tried adding libcurses.tbd and libncurses.tbd to Frameworks and Libraries, which got me past my initial errors of unknown variables, however the code is still not running.

The message I am receiving from the compiler is "Error opening terminal: unknown. Program ended with exit code: 1"

Please let me know how to install ncurses! I have attempted to follow the steps listed here https://ports.macports.org/port/ncurses/ but don't know where to copy and paste the "sudo port install ncurses" that it tells me to. Please help I have only used the existing libraries in Xcode and am very confused with how to install a new one.

If it makes any difference, my Mac system is macOS Big Sur v11.5.2 and I am working on Xcode v12.5.1

thank you in advance!

0 Answers
Related