0

I am new in c++ and Im trying to program a simply game program and I dont know how to make an input without pressing enter. In the function InputHandle I used the cin >> move, but this make my code dependant of pressing enter key. I tried to use the bib curses.h and use the getch() function, but this does not work:

__fpurge(stdin);
move = getch();
putchar(move)

other solution I tried was:

WINDOW *w;

    w = initscr();
    timeout(3000);
    move = getch();
    

the terminal read without pressing enter but the map board was printed wrong.

Here i have my entire code, Thanks.

#include <algorithm>
#include <iterator>
#include <memory>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <random>
#include <stdlib.h>
#include <curses.h>
#include <math.h>
#include <stdio_ext.h>

using namespace std;



//Struct Player
typedef struct {
    bool chave;
}inventario;

//Dragon Struct
typedef struct {
    int posX;
    int posY;
}dragon;


//clear the terminal every turn
void Clear(){
    system("clear");//clear console
}


//controls
void InputHandle(char move, int &pX, int &pY, vector<vector<char>> &v, inventario inv){
    cin >> move;
    int panterior = pX;
    
    switch(move){
            case 'w':
            if( pY-1 >= 0 ){
                if(v[pY-1][pX] != 'X' ){//if it is not the limit or it is not the wall(X) move player 
                pY--;
                }
            }
            break;

            case 's':
            if( pY+1 <= 9 ){
                if(v[pY+1][pX] != 'X'){
                pY++;
                }
            }
            break;

            case 'd':
            if( pX+1 <= 9){
                if(v[pY][pX+1] == 'X'){
                panterior = pX;
                }
                if(v[pY][pX+1] == ' '){
                pX++;
                break;
                }
                if(v[pY][pX+1] == 'E' && inv.chave){
                    v[5][9]  = ' ';  
                }
                if(v[pY][pX+1] == 'E' && !inv.chave){
                panterior = pX;
                }
                if(v[pY][pX+1] == 'K'){
                pX++;
                }
                   
            }
            break;

            case 'a':
            if(pX-1 >= 0 ){
                if(v[pY][pX-1] != 'X'){
                pX--;
                }
            }
    }
}


void Display(int &pX, int &pY, vector<vector<char>> v){
for(int i=0;i<v.size();i++){
    for(int j=0;j<v[i].size();j++){
        if(j==pX && i==pY){//put player's 'H' 
            cout<<"H ";
            continue;    
        }
        cout<<v[i][j]<<" ";
    }
    cout<<endl;
}
}

//lab
vector<vector<char>> board()
{
    vector<vector<char>> board
    {
        {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', 'X', 'X', ' ', 'X', ' ', 'X', ' ', 'X'},
        {'X', 'D', 'X', 'X', ' ', 'X', ' ', 'X', ' ', 'X'},
        {'X', ' ', 'X', 'X', ' ', 'X', ' ', 'X', ' ', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', 'X', ' ', 'E'},
        {'X', ' ', 'X', 'X', ' ', 'X', ' ', 'X', ' ', 'X'},
        {'X', ' ', 'X', 'X', ' ', 'X', ' ', 'X', ' ', 'X'},
        {'X', ' ', 'X', 'X', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}
    };
    return board;
    
}


//function to randomize the key
vector<vector<char>> keyGen(vector<vector<char>> v){
    int rand1, rand2;

    while(1){
    std::random_device rd;
    std::mt19937 eng(rd());
    std::uniform_int_distribution<int> distr(0, 9);
    rand1 = distr(eng);
    std::mt19937 eng2(rd());
    rand2 = distr(eng2);
    
    if(v[rand1][rand2] == ' '){
        v[rand1][rand2] = 'K';
        break;
    }
    }
    return v;
}


bool CheckWinCondition(int &pX, int &pY, inventario in){
if(pX==9 && pY ==5 && in.chave == true){
    return true;
}
else{
    return false;
}

}

//ver se perde
bool CheckLoseCondition(int &pX, int &pY, dragon dr)
{
    if((pX-1 == dr.posX && pY == dr.posY )|| 
       (pX+1 == dr.posX && pY == dr.posY )||
       (pX == dr.posX && pY+1 == dr.posY )||
       (pX == dr.posX && pY-1 == dr.posY )){
        return true;
    }
    else{
        return false;
    }
    
}

//DRAGON MOVEMENT

int main()
{
    
    vector<vector<char>> level, levelKey;
    int playerPosX=1, playerPosY=1;
    char movement, state;
    inventario player;
    dragon drogo;
    player.chave = false;
    level = board();
    levelKey = keyGen(level);


    cout<<endl<<"Hello, welcome to The Maze and The Dragon"<<endl<<"You awake still shattered from your last encounter..."<<endl
    <<"all is still fuzzy in your mind as you don't have any recollection on how you got to where you are..."<<endl
    <<"you can see a poorly-lite corridor and feel the damp smell of closed quarters."<<endl
    <<"Where are your things? You are still dressed in your leather clothes but no weapons or armour. Where are you? What dangers lie beyond?... "<<endl
    <<"A tinkle in the back of your head tells you that you must leave this place as fast as you can..."<<endl<<endl
    <<"Use WASD to walk with your hero (H) to pick up the key(K) to run from the dragon(D)"<<endl
    <<"Wanna Begin?(Y/N)"<<endl<<endl;

    


    cin >> state;
    Clear();



if(state == 'Y' || state == 'y')
{

    while(!CheckWinCondition(playerPosX,playerPosY, player)){//if player is not in the finish, loop 
        
        for(int i=0;i<levelKey.size();i++){
            for(int j=0;j<levelKey[i].size();j++){
                if(levelKey[i][j] == 'D'){
                    drogo.posY = i;
                    drogo.posX = j;
                }       
            }
        }
        
        if(CheckLoseCondition(playerPosX,playerPosY, drogo)) //  TA MAL, VER ISSO
        {
            cout<<"YOU ARE DEAD"<<endl<<endl;
            return 0;
        } 
        if(levelKey[playerPosY][playerPosX] != 'K'){
            Display(playerPosX, playerPosY,levelKey);//Show current maze
            InputHandle(movement, playerPosX, playerPosY,levelKey,player);//take input 
            Clear();//Clear the screen
    
        }   
        else if (levelKey[playerPosY][playerPosX] = 'K'){
        
        levelKey[playerPosY][playerPosX] = ' ';
        player.chave = true;
        
        //cout<<player.chave<<endl<<endl;
        cout<<"You found the key, go to the door and run from the dragon"<<endl<<endl; 
        }   
    }   
    cout<<"*******Congrats, you finished the maze!*******"<<endl<<endl; 
}

else if(state == 'N' || state == 'n'){

    exit;
}
   
    return 0;
}
thaidy_04
  • 9
  • 3
  • You'll need to use something in addition to C++. What the additional thing is depends on what platform you are on. Since you are on Ubuntu (I take it), you could use **ncurses** for a command line program. – Eljay Oct 30 '21 at 18:19
  • You need to use a library like ncurses. – Martin York Oct 30 '21 at 19:43

0 Answers0