Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed Hey guys I need some help with my snek program on c++.

+2 votes
asked Aug 25, 2020 by ZX228 (400 points)
closed Aug 25, 2020 by ZX228
//Snek
//08-18-2020
#include <stdio.h>
#include <iostream>
#include <curses.h>
#include <chrono>
#include <thread>
#include <unistd.h> //usleep(milliseconds);
using namespace std;
int level_num;
static int base[ 20 ][ 30 ] = {
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
{4, 0, 0, 0, 0, 0, 0, 0, 0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3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}
};
string direction;
int length = 1;
int head_pos[2] = {10, 16};
string level = "easy";
int dist_moved = 0;
bool alive = true;
bool touching_self = false;
namespace setup {
    void ask_level() {
        cout << "Easy, medium, or hard?" << endl;
        cin >> level;
        while ((level != "easy") && (level != "medium") && (level != "hard")) {
            cout << "\aPlease enter only \"easy\", \"medium\", or \"hard\" (no caps)." << endl;
            cin >> level;
        }
        if (level == "easy") {level_num = 1;}
        if (level == "medium") {level_num = 2;}
        if (level == "hard") {level_num = 3;}
        for (int z = 25; z > 0; z--) {
            cout << "\033[F                                                               ";
        }
        cout << "\r";
        base[10][16] = 2;
    }
    void set_up() {
        for (int r = 0; r < 20; r++) {
            for (int c = 0; c < 30; c++) {
                if (base[ r ][ c ] == 5) {cout << " |";}
                if (base[ r ][ c ] == 4) {cout << "| ";}
                if (base[ r ][ c ] == 3) {cout << "--";}
                if (base[ r ][ c ] == 2) {cout << "[]";}
                if (base[ r ][ c ] == 1) {cout << "{}";}
                if (base[ r ][ c ] == 0) {cout << "  ";}
            }
        cout << endl;
        }
    }
}
char c = 'w';
char d;
namespace sense {
    using namespace sense;
    void change_direction() {
        if ((c == 'd') && (direction != "left")) {direction = "right";}
        if ((c == 'a') && (direction != "right")) {direction = "left";}
        if ((c == 'w') && (direction != "down")) {direction = "up";}
        if ((c == 's') && (direction != "up")) {direction = "down";}
    }
    void sense_keypress() {
        if ((c == 'a') || ((c == 'd') || ((c == 's') || ((c == 'w')) {d = c;}
        initscr();
        timeout(100);
        c = getch();
        endwin();
        if ((c != 'd') && (c != 'a') && (c != 'w') && (c != 's')) {c = d;}
        cout << c;
        if ((c == 'd') && (direction != "left")) {direction = "right";}
        if ((c == 'a') && (direction != "right")) {direction = "left";}
        if ((c == 'w') && (direction != "down")) {direction = "up";}
        if ((c == 's') && (direction != "up")) {direction = "down";}
        cout << direction;
    }
}
int doit = (100000 - (level_num^10));
int dist_2 = 0;
namespace run {
    using namespace run;
    using namespace setup;
    using namespace this_thread; // sleep_for, sleep_until
    using namespace chrono; //nanoseconds, system_clock, seconds
    using namespace sense;
    using namespace setup;
    void move_right() {
        if ((head_pos[1] < 28) && (touching_self == false) && (dist_moved == dist_2)) {
            if (length == 1) {base[head_pos[0]][head_pos[1]] = 0;}
            dist_2++;
            head_pos[1]++;
            base[head_pos[0]][head_pos[1]] = 2;
        }
    }
    void move_left() {
        if ((head_pos[1] > 1) && (touching_self == false) && (dist_moved == dist_2)) {
            if (length == 1) {base[head_pos[0]][head_pos[1]] = 0;}
            dist_2++;
            head_pos[1]--;
            base[head_pos[0]][head_pos[1]] = 2;
        }
    }
    void move_up() {
        if ((head_pos[0] > 1) && (touching_self == false) && (dist_moved == dist_2)) {
            if (length == 1) {base[head_pos[0]][head_pos[1]] = 0;}
            dist_2++;
            head_pos[0]--;
            base[head_pos[0]][head_pos[1]] = 2;
        }
    }
    void move_down() {
        if ((head_pos[0] < 18) && (touching_self == false) && (dist_moved == dist_2)) {
            if (length == 1) {base[head_pos[0]][head_pos[1]] = 0;}
            dist_2++;
            head_pos[0]++;
            base[head_pos[0]][head_pos[1]] = 2;
        }
    }
    void move() {
        usleep(doit);
        sense_keypress();
        if (direction == "right") {
            move_right();
            usleep(10000);
            dist_moved++;
        }
        if (direction == "left") {
            move_left();
            usleep(10000);
            dist_moved++;
        }
        if (direction == "up") {
            move_up();
            usleep(10000);
            dist_moved++;
        }
        if (direction == "down") {
            move_down();
            usleep(10000);
            dist_moved++;
        }
        for (int z = 25; z > 0; z--) {
            cout << "\033[F                                                               ";
        }
        cout << "\r";
        for (int r = 0; r < 20; r++) {
            for (int c = 0; c < 30; c++) {
                if (base[ r ][ c ] == 5) {cout << " |";}
                if (base[ r ][ c ] == 4) {cout << "| ";}
                if (base[ r ][ c ] == 3) {cout << "--";}
                if (base[ r ][ c ] == 2) {cout << "[]";}
                if (base[ r ][ c ] == 1) {cout << "{}";}
                if (base[ r ][ c ] == 0) {cout << "  ";}
            }
        cout << endl;
        }
    }
}
using namespace this_thread; // sleep_for, sleep_until
using namespace chrono; //nanoseconds, system_clock, seconds
using namespace setup;
using namespace sense;
using namespace run;
int main() {
    ask_level();
    set_up();
    while (alive == true) {
        move();
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...