For this assignment, you will take your robot simulation from last week and add
ID: 656100 • Letter: F
Question
For this assignment, you will take your robot simulation from last week and add the NCurses library to it for output. You may use Josh's CursWin wrapper for this as well. The links for the files are located at the CursWin home page. Specifically, there should be a window showing the "map" with the robots on it, a window showing the individual robot stats, and a window for the user to enter commands.
Specific requirements
Add the NCurses library to the Robot World project
Implement windows (or areas) for the following output: The map, the robots, and a player-interaction window.
Heres my code for the robot world:
#include <cstlib>
#include <iostream>
#include <ctime>
#include "robot.h"
#include "world.h"
using namespace std;
int main(int argc, char** argv) {
world a_world;
srand(time(NULL));
a_world.set_up();
do{
a_world.update();
a_world.draw();
cin.ignore();
}while(1);
return 0;
}
#ifndef ROBOT_H
#define ROBOT_H
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
class robot {
public:
robot(int);
void refresh() {moved = 0;}
void draw();
void print();
void move(int &, int &);
private:
int direction;
int energy;
int ID;
int moved;
void turnLeft();
void turnRight();
void forward(int &, int &);
};
#endif /* ROBOT_H */
#include "robot.h"
void robot::turnLeft() {
energy--;
direction =(direction + 3)%4;
}
void robot::turnRight() {
energy--;
direction = (direction + 1)%4;
}
void robot::forward(int &x, int&y) {
energy -= 2;
if (direction == 0)
y--;
if (direction == 2)
y++;
if (direction ==1)
x++;
if (direction ==3)
x--;
}
robot::robot(int id){
energy = 50;
ID = id;
moved = 0;
direction = 0;
}
void robot::draw(){
cout << "#";
}
void robot::print(){
cout << "ID" <
}
moved = 1;
}
#ifndef WORLD_H
#define WORLD_H
#include "robot.h"
const int HEIGHT = 10;
const int WIDTH = 10;
class world {
public:
void set_up();
void draw();
void update();
private:
int terrain[WIDTH][HEIGHT];
robot *bots[WIDTH][HEIGHT];
};
#endif /* WORLD_H */
#include "world.h"
#include "robot.h"
void world::set_up() {
for (int y = 0; y < HEIGHT; y++){
for (int x = 0; x < WIDTH; x++){
bots[x][y] = NULL;
terrain[x][y] = rand()%4;
}
}
bots[2][2] = new robot(1);
}
void world::draw(){
for (int y = 0; y < HEIGHT; y++){
for (int x = 0; x< WIDTH; x++){
if (bots[x][y] == NULL)
switch(terrain[x][y]){// the function prints the terrain
case 0: // by adding 45 to the value and printing it as a character
cout << "~";
case 1:
cout << "T";
case 2:
cout << "M";
case 3:
cout << "P";
}
else
bots[x][y] ->draw();// draws the robot at the correct location in the world
}
cout << endl;
}
for (int y = 0; y < HEIGHT; y++)
for (int x = 0; x < WIDTH; x++)
if (bots[x][y] != NULL){
bots[x][y] ->print(); // prints the robots
bots[x][y] ->refresh(); // refreshes the robots
cout <<" ";
}
}
void world::update(){
int tempx, tempy;
robot *temp;
for (int y = 0; y < HEIGHT; y++){
for (int x = 0; x < WIDTH; x++){
if (bots[x][y] != NULL){
tempx = x;
tempy = y;
bots[x][y] ->move(tempx,tempy);
if(tempx < 0 || tempx >= WIDTH)
tempx = x;
if(tempy <0 || tempy >= HEIGHT)
tempy = y;
if(bots[tempx][tempy] == NULL){
temp = bots[x][y]; // sets the pointer temp to point to the robot thats moving
bots[x][y] = NULL; // pointer at the robots old location is set to null
bots[tempx][tempy] = temp; // pointer at the new location points to the robot
}
}
}
}
}
Can use any Ncurses library
Explanation / Answer
#include #include "CursWin.h" //#include "StdCursWin.h" using namespace curs; int CursWin::numWindows = 0; CursWin::CursWin(bool init, const char* win_name) { InitCurses(); InitColor(); strcpy(windowName,win_name); if (!(strcmp(windowName,"output"))) { upperLeftRow = 0; upperLeftCol = 0; lowerRightRow = LINES-3; lowerRightCol = COLS-1; } else { upperLeftRow = LINES-3; upperLeftCol = 0; lowerRightRow = LINES-1; lowerRightCol = COLS-1; } isPopUp = false; restore = NULL; DrawBoundingBox('-','|','+'); writeEnabled = true; scrolling = true; curRowPos = curColPos = 0; headerWidth = footerWidth = 0; maxRowPos = lowerRightRow - (upperLeftRow+2); maxColPos = lowerRightCol - (upperLeftCol+2); sv.expectingStreamParam = false; sv.precision = 2; sv.justified = LEFT_JUSTIFIED; sv.last = flush; sv.field_enabled = false; sv.paramNum = 1; sv.invalid_input = false; sv.immErrorHandling = false; sv.min_filter = sv.max_filter = sv.fail_value = 0.0; sv.force_range = sv.min_active = sv.max_active = false; sv.filter_active = 0; CursErrorOut = NULL; for (int i = 0; i < 1000; i++) { InputStream[i] = ''; } numWindows++; } CursWin::CursWin() { if (!numWindows) { InitCurses(); } InitColor(); strcpy(windowName,"unnamed window"); upperLeftRow = lowerRightRow = upperLeftCol = lowerRightCol = 0; writeEnabled = false; scrolling = true; curRowPos = curColPos = 0; headerWidth = footerWidth = 0; maxRowPos = lowerRightRow - (upperLeftRow+2); maxColPos = lowerRightCol - (upperLeftCol+2); sv.expectingStreamParam = false; sv.precision = 2; sv.justified = LEFT_JUSTIFIED; sv.last = flush; sv.field_enabled = false; sv.paramNum = 1; sv.invalid_input = false; sv.immErrorHandling = false; sv.min_filter = sv.max_filter = sv.fail_value = 0.0; sv.force_range = sv.min_active = sv.max_active = false; sv.filter_active = 0; isPopUp = false; restore = NULL; CursErrorOut = NULL; for (int i = 0; i < 1000; i++) { InputStream[i] = ''; } numWindows++; } CursWin::CursWin(int ulr, int ulc, int lrr, int lrc, bool pop) { if (!numWindows) { InitCurses(); } InitColor(); strcpy(windowName,"unnamed window"); upperLeftRow = ulr; upperLeftCol = ulc; lowerRightRow = lrr; lowerRightCol = lrc; isPopUp = pop; if (isPopUp) { int ySize = (lrr - ulr) + 1, xSize = (lrc - ulc) + 1; restore = new char*[ySize]; for (int r = 0; rRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.