<commit_before>/*
 * File:   main.cpp
 * Author: hsk81
 *
 * Created on June 13, 2016, 10:44 AM
 */

#include <ncurses.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>

#include "main.h"

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

int main(int argc, char** argv) {

    srand(time(NULL));

    initscr();
    curs_set(0);
    keypad(stdscr, TRUE);
    nodelay(stdscr, TRUE);

    start_color();
    init_pair(1, COLOR_BLACK, COLOR_WHITE);
    init_pair(2, COLOR_WHITE, COLOR_BLACK);

    int gamespeed = 25; // milli seconds
    int direction = DIR_RIGHT;

    struct SnakeNode *snake = NULL;
    snake_create(&snake);
    struct Food food;
    food.eaten = 1;

    do {
        food_create(&food, snake);
        paint(snake, food);
        sleep(gamespeed);
        refresh();
    } while (simulate(&snake, &direction, &food));

    snake_destroy(&snake);
    endwin();

    return 0;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void paint(struct SnakeNode *snake, struct Food food) {
    struct SnakeNode *node = snake;

    attron(COLOR_PAIR(1));
    while (node != NULL) {
        mvaddch(node->y, node->x, ' ');
        node = node->next;
    }
    attroff(COLOR_PAIR(1));

    attron(COLOR_PAIR(2)|A_BOLD);
    mvaddch(food.y, food.x, '#');
    attroff(COLOR_PAIR(2)|A_BOLD);
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

int simulate(struct SnakeNode **snake, int *direction, struct Food *food) {

    switch(getch()) {
        case KEY_RIGHT:
            if (*direction != DIR_LEFT) *direction = DIR_RIGHT;
            break;
        case KEY_LEFT:
            if (*direction != DIR_RIGHT) *direction = DIR_LEFT;
            break;
        case KEY_UP:
            if (*direction != DIR_UP) *direction = DIR_DOWN;
            break;
        case KEY_DOWN:
            if (*direction != DIR_DOWN) *direction = DIR_UP;
            break;
        default:
            break;
    }

    snake_move(snake);
    snake_teleport(snake, direction);

    int game_over = !snake_bitten(*snake);
    if (game_over) {
        paint(*snake, *food);
        sleep(2000);
    }

    int food_eaten = (*snake)->x == (*food).x && (*snake)->y == (*food).y;
    if (food_eaten) {
        (*food).eaten = 1;
        snake_grow(snake);
    }

    return game_over;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void food_create(struct Food *food, struct SnakeNode *snake) {

    if (food->eaten) do {
        food->x = rand() % 48 + 1;
        food->y = rand() % 18 + 1;
    } while (snake_fed(snake, food));

    food->eaten = 0;
}

///////////////////////////////////////////////////////////////////////////////<commit_msg>food: terminal centric<commit_after>/*
 * File:   main.cpp
 * Author: hsk81
 *
 * Created on June 13, 2016, 10:44 AM
 */

#include <ncurses.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>

#include "main.h"

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

int main(int argc, char** argv) {

    srand(time(NULL));

    initscr();
    curs_set(0);
    keypad(stdscr, TRUE);
    nodelay(stdscr, TRUE);

    start_color();
    init_pair(1, COLOR_BLACK, COLOR_WHITE);
    init_pair(2, COLOR_WHITE, COLOR_BLACK);

    int gamespeed = 25; // milli seconds
    int direction = DIR_RIGHT;

    struct SnakeNode *snake = NULL;
    snake_create(&snake);
    struct Food food;
    food.eaten = 1;

    do {
        food_create(&food, snake);
        paint(snake, food);
        sleep(gamespeed);
        refresh();
    } while (simulate(&snake, &direction, &food));

    snake_destroy(&snake);
    endwin();

    return 0;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void paint(struct SnakeNode *snake, struct Food food) {
    struct SnakeNode *node = snake;

    attron(COLOR_PAIR(1));
    while (node != NULL) {
        mvaddch(node->y, node->x, ' ');
        node = node->next;
    }
    attroff(COLOR_PAIR(1));

    attron(COLOR_PAIR(2)|A_BOLD);
    mvaddch(food.y, food.x, '#');
    attroff(COLOR_PAIR(2)|A_BOLD);
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

int simulate(struct SnakeNode **snake, int *direction, struct Food *food) {

    switch(getch()) {
        case KEY_RIGHT:
            if (*direction != DIR_LEFT) *direction = DIR_RIGHT;
            break;
        case KEY_LEFT:
            if (*direction != DIR_RIGHT) *direction = DIR_LEFT;
            break;
        case KEY_UP:
            if (*direction != DIR_UP) *direction = DIR_DOWN;
            break;
        case KEY_DOWN:
            if (*direction != DIR_DOWN) *direction = DIR_UP;
            break;
        default:
            break;
    }

    snake_move(snake);
    snake_teleport(snake, direction);

    int game_over = !snake_bitten(*snake);
    if (game_over) {
        paint(*snake, *food);
        sleep(2000);
    }

    int food_eaten = (*snake)->x == (*food).x && (*snake)->y == (*food).y;
    if (food_eaten) {
        (*food).eaten = 1;
        snake_grow(snake);
    }

    return game_over;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void food_create(struct Food *food, struct SnakeNode *snake) {

    int row, col;
    getmaxyx(stdscr, row, col);

    if (food->eaten) do {
        food->x = 0.90 * (rand() % col) + 0.05 * col;
        food->y = 0.90 * (rand() % row) + 0.05 * row;
    } while (snake_fed(snake, food));

    food->eaten = 0;
}

///////////////////////////////////////////////////////////////////////////////
{'fraction_non_alphanumeric': 0.34127740705433746, 'fraction_numerical': 0.012074992055926279, 'mean_word_length': 1.1736878453038675, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1383280', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1800, 'n_words': 390}