Monday, February 22, 2016

Basic shapes in Ncurses

First of all I will give you a brief introduction about the Ncurses.
It is a special library in C++ where you need to get downloaded from internet.It is a GUI toolkit provided that the output is not given out from normal terminal(terminal independent).It provides functions to move the cursor, create windows, produce colors, play with mouse and provide several GUI features.Hope you'll search for more details :)

Color Codes
COLOR_BLACK           0
COLOR_RED                1
COLOR_GREEN          2
COLOR_YELLOW       3
COLOR_BLUE             4
COLOR_MAGENTA   5
COLOR_CYAN            6
COLOR_WHITE         7


1)
#include<ncurses.h>
Output
 int main(){
initscr();
start_color();
init_pair(1,3,4);
attrset(COLOR_PAIR(1));
int row=5,no=1;
for(int i=1;i<=5;i++){//i=no of rows
move(row,10);
for(int j=1;j<=no;j++){
//j=no of columns
printw("* ");
}
no+=2;
//each line increment by 2 *

row++;
}
move(0,0);
getch();
endwin();
return 0;

}


2)
#include<ncurses.h>
Output
 int main(){
initscr();
start_color();
init_pair(1,3,4);
attrset(COLOR_PAIR(1));
int row=5,no=9;
for(int i=1;i<=5;i++){//i=no of rows
move(row,10);
for(int j=1;j<=no;j++){
//j=no of columns
printw("* ");
}
no-=2;
//each line decrement by 2 *
row++;
}
move(0,0);
getch();
endwin();
return 0;

}


3)
Output
#include<ncurses.h>
int main(){
initscr();
start_color();
init_pair(1,3,4);
attrset(COLOR_PAIR(1));
int row=5,no=1,col=10;
for(int i=1;i<=5;i++){
move(row,col);
for(int j=1;j<=no;j++){
printw("*");
}
no+=1;
row++;
col--;//columns reduces by 1
}
move(0,0);
getch();
endwin();
return 0;

}


4)
Output
#include<ncurses.h>
int main(){
initscr();
start_color();
init_pair(1,3,4);
attrset(COLOR_PAIR(1));
int row=5,no=5,col=10;
for(int i=1;i<=5;i++){
move(row,col);
for(int j=1;j<=no;j++){
printw("*");
}
no-=1;
row++;
col++;
}
move(0,0);
getch();
endwin();
return 0;

}


5)
Output
#include<ncurses.h>
int main(){
        initscr();
        start_color();
        init_pair(1,3,4);
        attrset(COLOR_PAIR(1));
        int row=5,no=1,col=10;
        for(int i=1;i<=5;i++){
                move(row,col);
                for(int j=1;j<=no;j++){
                        printw("*");
                }
                no+=2;
                row++;
                col--;
}
move(0,0);
getch();
endwin();
return 0;

}


6)
Output
#include<ncurses.h>
int main(){
initscr();
start_color();
init_pair(1,3,4);
attrset(COLOR_PAIR(1));
int row=5,no=7,col=10;
for(int i=1;i<=5;i++){
move(row,col);
for(int j=1;j<=no;j++){
printw("*");
}
no-=2;
row++;
col++;
}
move(0,0);
getch();
endwin();
return 0;
}


7)

Output
#include<ncurses.h>
int main(){
initscr();
start_color();
init_pair(1,3,4);
attrset(COLOR_PAIR(1));
int row=5;
for(int i=1;i<=5;i++){//no of rows
move(row,10);
for(int j=1;j<=9;j++){//no of columns
printw("*");
}

row++;

}
move(0,0);
getch();
endwin();
return 0;
}


8)Drawing a square upon another square
Output
#include<ncurses.h>
int main(){
initscr();
start_color();
init_pair(1,3,4);
init_pair(2,0,5);
attrset(COLOR_PAIR(1));
int row=5;
for(int i=1;i<=5;i++){//no of rows
move(row,10);
for(int j=1;j<=9;j++){//no of columns
printw("*");
}

row++;

}
attrset(COLOR_PAIR(2));//inner box
        int row1=6;
        for(int i=1;i<=3;i++){//no of rows
                move(row1,11);
                for(int j=1;j<=7;j++){//no of columns
                        printw("$");
                }

                row1++;
 }
move(0,0);
getch();
endwin();
return 0;
}


9)
Output
#include<ncurses.h>
int main(){
initscr();
start_color();
init_pair(1,3,4);
attrset(COLOR_PAIR(1));
int row=5,col=10,c=1;
for(int i=1;i<=5;i++){//no of rows
move(row,col);
for(int j=1;j<=c;j++){//no of columns
printw("*");
}

if(i>1){//when no of rows>1 thz gets executed
c+=4;
                        col-=2;
                  }
               else{
                   c+=2;
                   col-=1;
                   }
row++;//common to both cases
}
move(0,0);
getch();
endwin();
return 0;
}




10) Diamond shape
Output
#include <ncurses.h>
using namespace std;
int main(){
     initscr();
     start_color();
     init_pair(1,3,4);
     attrset(COLOR_PAIR(1));
     int row=5,col=10,c=1;
     for(int i=1;i<=11;i++){//no of rows
        move(row,col);
         for(int j=1;j<=c;j++){//no of columns
               printw("*");
        }
          //conditions for increments & decrements
          if (i>9) {//For lower part of the diamond
            c -= 2;
            col += 1;
        }else if (i > 5) {
            c -= 4;
            col += 2;   
        }else if (i > 1) {//
For upper part of the diamond
                        c += 4;
                        col -= 2;
                }else {
                        c += 2;
                        col -= 1;
                }
                    
       row++;//common to both cases
     }
    move(0,0);
    getch();
    endwin();
    return 0;

}



11) Printing a frame (using single nested loop)
Output
#include <ncurses.h>
using namespace std;
int main(){
     initscr();
     start_color();
     init_pair(1,3,4);
     attrset(COLOR_PAIR(1));
     int row=2,col=5,c=1;
     for(int i=1;i<=8;i++){//no of rows
        move(row,col);
         for(int j=1;j<=10;j++){//no of columns
            if (i==1 ||i == 8)//To print border
                printw("*");
            else if (j>1 && j<10) {//Not to print inside of the frame
                    move (row,col+9);
                    printw("*");
                    j=10;
            }else
                printw("*");//the rest (leftmost vertical line)      
        }

                   
       row++;//increment for rows
     }
    move(0,0);
    getch();
    endwin();
    return 0;
}




*highlighted conditions can be written in many ways.

2 comments: