I am making a tic tac toe game, and have completed it. Now I am trying to implement a library to my code and I chose ncurses, because it seems easier to add and will allow me to change the color of the input for the game. However, I can't seem to overlay the colors and background into my tic tac toe code. My question is - how do I implement the ncurses library to my code? I (where?) Or does anyone know of a library that I can use that's minimal and east to code into what I have already written? Any help will be appreciated!

Main file:

#include <cstdlib>
#include<string>
#include <ctime>
#include "board.h"
#include "players.h"
#include <ncurses.h>

using namespace std;

/**
 * Controls main function of the game
 */
int main() {
    string name;
    cout << "What's your name: ";
    cin >> name;
    cout << "Nice to meet you, "<< name << ". Welcome to Tic Tac Toe!" << endl;
    Player game;
    game.play();
};

/the board. h file

#ifndef BOARD_H
#define BOARD_H

const int ROWS = 3;
const int COLUMNS = 3;

class Board {
protected:
    char board[ROWS][COLUMNS];
public:
    Board();
    virtual void play() = 0;
    void computermove();
    void playermove();
    void draw();
    void set(int row, int col, char symbol);
    char get(int row, int col);
    bool is_horizontal(int row, char sym);
    bool is_vertical(int col, char sym);
    bool is_diagonal(char sym);
    char checkwin(char symbol);
    bool valid();
};


// player.h is :
#ifndef PLAYERS_H
#define PLAYERS_H
#include "board.h"


class Player : public Board {
public:
    void play();
};
#endif


My .cpp code for players / board:

#include "board.h"
#include "players.h"
#include <ncurses.h>
#include <iostream>
using namespace std;

/**
 * Constructor for Board class
 */
Board::Board() {
    for (int row = 0; row  < ROWS; row++) {
        for (int col = 0; col < COLUMNS; col++) {
             board[row][col] = '0';
        }
    }
}

/**
 * Draws the grid for the game
 */
void Board::draw() {
    cout << "\t     |     |     " << endl;
    cout << "\t  " << board[0][0] << "  |  " << board[0][1] << "  |  " << board[0][2] << endl;
    cout << "\t     |     |     " << endl;
    cout << "\t-----|-----|-----" << endl;
    cout << "\t     |     |     " << endl;
    cout << "\t  " << board[1][0] << "  |  " << board[1][1] << "  |  " << board[1][2] << endl;
    cout << "\t     |     |     " << endl;
    cout << "\t-----|-----|-----" << endl;
    cout << "\t     |     |     " << endl;
    cout << "\t  " << board[2][0] << "  |  " << board[2][1] << "  |  " << board[2][2] << endl;
    cout << "\t     |     |     " << endl;
    //attron(COLOR_PAIR(1));
}

/**
 * Sets the data stored at the given index to a symbol
 * @param row is the number of rows
 * @param col is the number of COLUMNS
 * @param symbol is letter that user is assigned
 */
void Board::set(int row, int col, char symbol) {
    board[row][col] = symbol;
}

/**
 * Gets the data stored at index
 * @param row is the number of rows
 * @param col is the number of COLUMNS
 * @return the placement of symbol
 */
char Board::get(int row, int col) {
    return board[row][col];
}

/**
 * Checks if horizonatally there is a winner
 * @param row is the number of rows
 * @param symbol is letter that user is assigned
 * @return the win if horizontal
 */
bool Board::is_horizontal(int row, char sym) {
    for (int col = 0; col < COLUMNS; col++) {
        if (sym != board[row][col])
            return false;
    }
    return true;
}

/**
 * Checks if vertically there is a winner
 * @param row is the number of rows
 * @param symbol is letter that user is assigned
 * @return the win if vertical
 */
bool Board::is_vertical(int col, char sym) {
    for (int row = 0; row < ROWS; row++) {
        if (sym != board[row][col])
            return false;
    }
    return true;
}

/**
 * Checks if diagonally there is a winner
 * @param symbol is letter that user is assigned
 * @return the win if diagonal
 */
bool Board::is_diagonal(char sym) {
    bool ascending = sym == board[0][0] &&
    sym == board[1][1] && sym == board[2][2];
    bool descending = sym == board[2][0] &&
    sym == board[1][1] && sym == board[0][2];
    if (ascending || descending)
        return true;
    else
        return false;
}

/**
 * Checks if there is a win
 * @param symbol is letter that user is assigned
 * @return the win if there is any
 */
char Board::checkwin(char symbol) {
    if (is_diagonal(symbol))
        return symbol;
    for (int index= 0; index < ROWS; index++) {
        if (is_horizontal(index, symbol))
            return symbol;
        else if (is_vertical(index, symbol))
            return symbol;
    }
    return '0';
}

/**
 * Checks if game should continue
 * @return if game is still valid
 */
bool Board::valid() {
    for (int row = 0; row < ROWS; row++) {
        for (int col = 0; col < COLUMNS; col++) {
            if (board[row][col] == '0') {
                return true;
            }
        }
    }
    return false;
}

/**
 * Allows computer to play after user has played
 */
void Board::computermove() {
    if (valid()) {
    cout << endl << "Computer is thinking..." << endl;
//    attron(COLOR_PAIR(4));
    srand(time(0));
//    endwin();
    int row = rand() % 3, column = rand() % 3;
    while (board[row][column] != '0') {
        row = rand() % 3, column = rand() % 3;
    }
    board[row][column] = 'R';
    }
}

/**
 * Allows user to input postion to play
 */
void Board::playermove() {
    int row = 0, column = 0;
    cout << "Make a Move :)" << endl;
    cout << "Enter Row Coordinates: ";
    cin >> row;
    cout << "Enter Column Coordinates: ";
    cin >> column;
    while (board[row][column] != '0') {
        cout << "Sorry, try again." << endl;
        cout << "Enter Row Coordinate: ";
        cin >> row;
        cout << "Enter Column Coordinate: ";
        cin >> column;
    }
    board[row][column] = 'X';
}


///Lastly, the player cpp file:

#include "board.h"
#include "players.h"
#include "ncurses.h"
#include <iostream>

using namespace std;

/**
 * Allows user to play the game
 */
void Player::play() {
    while (valid()) {
        draw();
        playermove();
        if (checkwin('X') == 'X') {
            draw();
            cout << "CONGRATULATIONS, YOU WON!";
            attron(COLOR_PAIR(1));
            break;
        }
        draw();
        computermove();
        if (checkwin('R') == 'R') {
            draw();
            cout << "Uh oh, computer won. Try again next time!" << endl;
            break;
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.