Skip to content
Snippets Groups Projects
Commit 6811360b authored by Lina Tunell's avatar Lina Tunell
Browse files

Robot simulator

parents
No related branches found
No related tags found
No related merge requests found
File added
PLACE,1,2,EAST
MOVE
MOVE
LEFT
MOVE
REPORT
\ No newline at end of file
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "tabletop.h"
using namespace std;
TableTop execute_command(string command, TableTop table){
string dir = table.get_dir();
int x, y;
tie(x, y) = table.get_pos();
//cout << "Command " << command << endl;
if (command == "REPORT"){
//cout << "Reporting" << endl;
cout << table.get_details() << endl;
}
if (command.at(0) == 'M'){
if (dir.at(0) == 'N') {
if (y+1<=4){ y=y+1;}
} else if (dir.at(0) == 'S') {
if (y-1>=0){ y=y-1;}
} else if (dir.at(0) == 'W') {
if (x-1>=0){ x=x-1;}
} else if (dir.at(0) == 'E') {
if (x+1<=4){ x=x+1;}
}
}
if (command.at(0) == 'L'){
if (dir.at(0) == 'N') {
dir = "WEST";
} else if (dir.at(0) == 'S') {
dir = "EAST";
} else if (dir.at(0) == 'W') {
dir = "SOUTH";
} else if (dir.at(0) == 'E') {
dir = "NORTH";
}
}
if (command == "RIGHT"){
if (dir.at(0) == 'N') {
dir = "EAST";
} else if (dir.at(0) == 'S') {
dir = "WEST";
} else if (dir.at(0) == 'W') {
dir = "NORTH";
} else if (dir.at(0) == 'E') {
dir = "SOUTH";
}
}
TableTop new_table(dir, x, y);
//cout << new_table.get_details() << endl;
return new_table;
}
tuple<bool, bool> check_placement(TableTop table_top_info, bool start_status, bool new_place_ok){ //Kolla så att starten är giltig
int x, y;
tie(x, y) = table_top_info.get_pos();
if (x < 0 || x > 4 || y < 0 || y > 4){
new_place_ok=false;
} else {new_place_ok=true;}
//Så fort någon placering är ok kan programmet ta in annan input än PLACE
if (new_place_ok){start_status=true;}
return make_tuple(start_status,new_place_ok);
}
TableTop place(string place_command){
stringstream ss(place_command);
string temp;
// Variabler att spara i
int x, y;
string dir;
getline(ss, temp, ','); // Ignorera "PLACE"
getline(ss, temp, ','); // Hämta ut informationen
x = stoi(temp);
getline(ss, temp, ',');
y = stoi(temp);
getline(ss, dir, ',');
TableTop table(dir, x, y);
return table;
}
robot.h 0 → 100644
#ifndef ROBOT_H
#define ROBOT_H
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <tuple>
#include "tabletop.h"
using namespace std;
TableTop execute_command(string command, TableTop table);
tuple<bool, bool> check_placement(TableTop table_top_info, bool start_status, bool new_place_ok);
TableTop place(string place_command);
#endif // ROBOT_H
\ No newline at end of file
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "tabletop.h"
#include "robot.h"
using namespace std;
int robot_simulator(string input_file) {
bool start_status=false;
bool new_place_ok=false;
string input;
TableTop table_pos_info;
TableTop table_suggestion;
// Läs från denna fil
ifstream file_to_read_from(input_file);
// Läs rad för rad
while (getline (file_to_read_from, input)) {
if (input.at(0) == 'P'){
table_suggestion = place(input);
tie(start_status,new_place_ok) = check_placement(table_suggestion, start_status, new_place_ok);
if (new_place_ok){table_pos_info=table_suggestion;}
}
if (start_status){
table_pos_info = execute_command(input,table_pos_info);
}
}
file_to_read_from.close();
return 0;
}
int main() {
robot_simulator("input.txt");
}
#ifndef ROBOT_SIMULATOR_H
#define ROBOT_SIMULATOR_H
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <tuple>
#include "tabletop.h"
using namespace std;
int robot_simulator(string input_file);
#endif // ROBOT_SIMULATOR_H
// tabletop.cpp
#include "tabletop.h"
#include <sstream>
#include <tuple>
#include <string>
// Konstructor
TableTop::TableTop() : facing("NORTH"), x(0), y(0) {}
TableTop::TableTop(std::string facing, int x, int y) : facing(facing), x(x), y(y) {}
// Function to return variables as a formatted string
std::string TableTop::get_details() const {
std::ostringstream details;
//details << "X: " << x << ", Y: " << y << " Facing: " << facing;
details << x << "," << y << "," << facing;
return details.str();
}
std::tuple<int, int> TableTop::get_pos() const {
return std::make_tuple(x, y);
}
std::string TableTop::get_dir() const {
return facing;
}
// tabletop.h
#ifndef TABLETOP_H
#define TABLETOP_H
#include <string>
class TableTop {
private:
std::string facing;
int x;
int y;
public:
TableTop();
TableTop(std::string facing, int x, int y); // Constructor
std::string get_details() const; // Function to return variables as a string
std::tuple<int, int> get_pos() const;
std::string get_dir() const;
};
#endif // TABLETOP_H
test.cpp 0 → 100644
#include "robot.h"
#include "tabletop.h"
using namespace std;
bool test_place_function(){
bool works = false;
string place_command = "PLACE,2,3,NORTH";
TableTop table = place(place_command);
if (table.get_dir() == "NORTH" && table.get_pos() == make_tuple(2, 3)){
works=true;
}
cout << "Test place function: " << works << endl;
return works;
}
bool test_move_out_of_table(){
bool works = false;
string place_command = "PLACE,3,3,NORTH";
string move_command = "MOVE";
TableTop table = place(place_command);
table = execute_command(move_command,table);
table = execute_command(move_command,table);
table = execute_command(move_command,table);
table = execute_command(move_command,table);
if (table.get_dir() == "NORTH" && table.get_pos() == make_tuple(3, 4)){
works=true;
}
cout << "Test move out of table: " << works << endl;
return works;
}
bool test_invalid_placecment(){
bool works = false;
bool new_place_ok;
bool start_status;
string place_command = "PLACE,6,3,NORTH";
TableTop table = place(place_command);
tie(start_status,new_place_ok) = check_placement(table, start_status, new_place_ok);
if (new_place_ok==false){
works=true;
}
cout << "Test invalid placement: " << works << endl;
return works;
}
bool test_turn(){
bool works = false;
int tests_ok = 0;
string turn_left_command = "LEFT";
string turn_right_command = "RIGHT";
string place_command = "PLACE,3,3,NORTH";
TableTop table = place(place_command);
table = execute_command(turn_left_command,table);
if (table.get_dir() == "WEST"){
tests_ok+=1;
}
table = execute_command(turn_left_command,table);
if (table.get_dir() == "SOUTH"){
tests_ok+=1;
}
table = execute_command(turn_left_command,table);
if (table.get_dir() == "EAST"){
tests_ok+=1;
}
table = execute_command(turn_left_command,table);
if (table.get_dir() == "NORTH"){
tests_ok+=1;
}
table = execute_command(turn_right_command,table);
if (table.get_dir() == "EAST"){
tests_ok+=1;
}
table = execute_command(turn_right_command,table);
if (table.get_dir() == "SOUTH"){
tests_ok+=1;
}
table = execute_command(turn_right_command,table);
if (table.get_dir() == "WEST"){
tests_ok+=1;
}
if (tests_ok==7){
works = true;
}
cout << "Test turn: " << works << endl;
return works;
}
int main() {
test_place_function();
test_move_out_of_table();
test_invalid_placecment();
test_turn();
}
\ No newline at end of file
#test1 Move out to table
PLACE,3,3,NORTH
MOVE
MOVE
MOVE
MOVE
LEFT
MOVE
REPORT
Output: 2,4,WEST
#test2 Invalid initial placement
PLACE,6,3,NORTH
MOVE
MOVE
REPORT
Output:
#test3 Invalid second placement
PLACE,3,3,SOUTH
MOVE
MOVE
PLACE,6,3,NORTH
RIGHT
REPORT
Output: 3,1,WEST
#test4 Commands before PLACE
LEFT
MOVE
PLACE,3,6,NORTH
PLACE,3,3,NORTH
MOVE
RIGHT
RIGHT
REPORT
MOVE
MOVE
PLACE,3,6,NORTH
MOVE
LEFT
MOVE
REPORT
Output: 3,4,SOUTH 4,1,EAST
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment