Skip to content

Instantly share code, notes, and snippets.

@adam-sas-on
Last active November 22, 2020 14:33
Show Gist options
  • Save adam-sas-on/5559163b59691addcf7c354eea06df04 to your computer and use it in GitHub Desktop.
Save adam-sas-on/5559163b59691addcf7c354eea06df04 to your computer and use it in GitHub Desktop.
A simple example to use whole terminal screen in C (same as python example but in C language).
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
/*
* https://gist.github.com/adam-sas-on/
*/
void rand_range(int *col, int *row, const int width, const int height){
double rnd;
if(col != NULL){
rnd = (double)rand()/(1. + RAND_MAX);// [0.0, 1.0);
*col = (int)floor(rnd*width);
}
if(row != NULL){
rnd = (double)rand()/(1. + RAND_MAX);// [0.0, 1.0);
*row = (int)floor(rnd*height);
}
}
void run(){
int c, row = 3, col = 0, run = 1, height, width;
const int row_begin = 3;
srand((unsigned)time(NULL));
initscr();
raw();
keypad(stdscr, TRUE);
noecho();
mvprintw(0, 4, "Random point on a board");
mvprintw(1, 3, "Press any key for the next point");
mvprintw(2, 3, "or ENTER for exit");
while(run){
getmaxyx(stdscr, height, width);
width -= 3;
height -= row_begin;
mvprintw(row, col, " ");
rand_range(&col, &row, width, height);
row += row_begin;
mvprintw(row, col, "xxx");
c = getch();
if(c == '\n' || c == KEY_ENTER || c == KEY_EXIT)
run = 0;
}
endwin();
}
int main(int ac, char**av, char**env){
run();
return 0;
}
// gcc c_curses_example.c -lm -lncurses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment