Commit 85f3ffb2 authored by Rachel Wil Sha Singh's avatar Rachel Wil Sha Singh 💬
Browse files

Debugging lab

parent 480aa238
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="DebuggingLab" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/DebuggingLab" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/DebuggingLab" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Extensions>
<code_completion />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>
# depslib dependency file v1.0
1598286590 source:/home/wilsha/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Source files/DebuggingLab/Game.cpp
"Game.hpp"
"Utilities.hpp"
<cstdlib>
<ctime>
<iostream>
<string>
<iomanip>
<fstream>
1598286556 /home/wilsha/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Source files/DebuggingLab/Game.hpp
"Map.hpp"
1598286631 /home/wilsha/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Source files/DebuggingLab/Map.hpp
<string>
1598284225 /home/wilsha/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Source files/DebuggingLab/Utilities.hpp
<string>
1598284126 source:/home/wilsha/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Source files/DebuggingLab/main.cpp
"Game.hpp"
1598286715 source:/home/wilsha/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Source files/DebuggingLab/Map.cpp
"Map.hpp"
<iostream>
<vector>
1598284220 source:/home/wilsha/TEACHING/PRIVATE/cs-235-object-oriented-programming-using-cpp/2020/Lab/Source files/DebuggingLab/Utilities.cpp
"Utilities.hpp"
<iostream>
#include "Game.hpp"
#include "Utilities.hpp"
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
Game::Game()
{
// Seed the random number generator
srand( time( NULL ) );
// Set the starting program state
m_gameState = MAIN_MENU;
m_score = 0;
LoadGame();
}
Game::~Game()
{
SaveGame();
}
void Game::Run()
{
GameState nextState = m_gameState;
while ( nextState != QUIT )
{
if ( nextState == MAIN_MENU ) { nextState = MainMenu(); }
else if ( nextState == GAME ) { nextState = Gameplay(); }
}
cout << "Game saved." << endl;
}
GameState Game::MainMenu()
{
ClearScreen();
cout << "TTTTT RRR EEEE ZZZZZ OOOO RRR OOOO" << endl;
cout << " T R R E Z O O R R O O" << endl;
cout << " T RRR EEEE ZZ O O RRR O O" << endl;
cout << " T R R E Z O O R R O O" << endl;
cout << " T R R EEEE ZZZZZ OOOO R R OOOO" << endl;
cout << "------------------------------------------" << endl;
cout << "1. Play" << endl;
cout << "2. Instructions" << endl;
cout << "3. Quit" << endl;
int choice = GetIntInput( 1, 3 );
if ( choice == 1 )
{
return GAME;
}
else if ( choice == 3 )
{
return QUIT;
}
else
{
Instructions();
return MAIN_MENU;
}
}
GameState Game::Gameplay()
{
bool done = false;
m_map.GenerateMap();
while ( !done )
{
ClearScreen();
m_map.Draw();
DrawHud();
string choice = GetStringInput();
m_map.MovePlayer( choice );
if ( choice == "QUIT" )
{
done = true;
}
if ( m_map.PlayerCollectTreasure() )
{
m_score++;
return YouWin();
}
else if ( m_map.GoblinGetPlayer() )
{
return YouLose();
}
}
return MAIN_MENU;
}
GameState Game::YouWin()
{
ClearScreen();
cout << "-------------------------------------------------" << endl;
cout << "- Y O U W I N ! -" << endl;
cout << "- -" << endl;
cout << "- You have found " << m_score << " treasures. \t\t\t-" << endl;
cout << "-------------------------------------------------" << endl;
cout << " Continue to the next level? (y/n): ";
string input = GetStringInput();
if ( input == "n" )
{
return MAIN_MENU;
}
else
{
return GAME;
}
}
GameState Game::YouLose()
{
ClearScreen();
cout << "-------------------------------------------------" << endl;
cout << "- O H N O ! -" << endl;
cout << "- -" << endl;
if ( m_score == 0 )
{
cout << "- The goblin got you, but you have no treasure! -" << endl;
}
else
{
m_score--;
cout << "- The goblin stole one of your treasures! -" << endl;
}
cout << "-------------------------------------------------" << endl;
cout << " Continue to the next level? (y/n): ";
string input = GetStringInput();
if ( input == "n" )
{
return MAIN_MENU;
}
else
{
return GAME;
}
}
void Game::Instructions()
{
cout << "Use the [W, A, S, D] keys to move around the labyrinth." << endl;
cout << "Find treasure and collect it to win." << endl;
cout << endl;
cout << "Go back? (y/n): ";
GetStringInput();
}
void Game::DrawHud()
{
cout << endl;
cout << "-------------------------------------------------" << endl;
cout << "- GOAL: Move yourself (@) to SCORE: -" << endl;
cout << "- collect the treasure ($)! " << m_score << "\t-" << endl;
cout << "- Avoid the goblin (&)! -" << endl;
cout << "- -" << endl;
cout << "- " << m_map.MoveUpKey() << ": Move NORTH -" << endl;
cout << "- " << m_map.MoveLeftKey() << ": Move WEST " << m_map.MoveDownKey() << ": Move SOUTH " << m_map.MoveRightKey() << ": Move EAST -" << endl;
cout << "- -" << endl;
cout << "- Or type QUIT to quit. -" << endl;
cout << "-------------------------------------------------" << endl;
}
void Game::SaveGame()
{
ofstream output( "save.txt" );
output << m_score;
}
void Game::LoadGame()
{
ifstream input( "save.txt" );
input >> m_score;
}
#ifndef _GAME_HPP
#define _GAME_HPP
#include "Map.hpp"
enum GameState { MAIN_MENU, GAME, QUIT };
class Game
{
public:
Game();
~Game();
void Run();
private:
GameState m_gameState;
Map m_map;
int m_score;
GameState MainMenu();
GameState Gameplay();
GameState YouWin();
GameState YouLose();
void Instructions();
void DrawHud();
void SaveGame();
void LoadGame();
};
#endif
#include "Map.hpp"
#include <iostream>
#include <vector>
using namespace std;
Map::Map()
: MAP_WIDTH(20), MAP_HEIGHT(10), EMPTYROOM(' '), WALL('+'),
MOVE_UP('w'), MOVE_DOWN('s'), MOVE_LEFT('a'), MOVE_RIGHT('d')
{
}
void Map::GenerateMap()
{
// Fill in all the rooms
for ( int y = 0; y < MAP_HEIGHT; y++ )
{
for ( int x = 0; x < MAP_WIDTH; x++ )
{
m_tiles[x][y] = WALL;
}
}
vector<Coords> roomCoords;
// Choose random rooms
int roomCount = rand() % 5 + 5;
for ( int i = 0; i < roomCount; i++ )
{
Coords c;
c.x = rand() % 18 + 1;
c.y = rand() % 8 + 1;
m_tiles[c.x][c.y] = EMPTYROOM;
roomCoords.push_back( c );
}
// Connect all the rooms
for ( unsigned int i = 0; i < roomCoords.size() - 1; i++ )
{
int x = roomCoords[i].x;
int y = roomCoords[i].y;
int targetX = roomCoords[i+1].x;
int targetY = roomCoords[i+1].y;
while ( x != targetX )
{
m_tiles[x][y] = EMPTYROOM;
if ( x < targetX ) { x++; }
else { x--; }
}
while ( y != targetY )
{
m_tiles[x][y] = EMPTYROOM;
if ( y < targetY ) { y++; }
else { y--; }
}
}
// Place the player and treasure
int randX = rand() % MAP_WIDTH;
int randY = rand() % MAP_HEIGHT;
while ( m_tiles[randX][randY] != EMPTYROOM )
{
randX = rand() % MAP_WIDTH;
randY = rand() % MAP_HEIGHT;
}
m_player.x = randX;
m_player.y = randY;
randX = rand() % MAP_WIDTH;
randY = rand() % MAP_HEIGHT;
while ( !( m_tiles[randX][randY] == EMPTYROOM
&& randX != m_treasure.x && randY != m_treasure.y ) )
{
randX = rand() % MAP_WIDTH;
randY = rand() % MAP_HEIGHT;
}
m_treasure.x = randX;
m_treasure.y = randY;
randX = rand() % MAP_WIDTH;
randY = rand() % MAP_HEIGHT;
while ( !( m_tiles[randX][randY] == EMPTYROOM
&& randX != m_treasure.x && randY != m_treasure.y
&& randX != m_player.x && randY != m_player.y ) )
{
randX = rand() % MAP_WIDTH;
randY = rand() % MAP_HEIGHT;
}
m_enemy.x = randX;
m_enemy.y = randY;
}
void Map::Draw()
{
cout << endl;
for ( int y = 0; y < MAP_HEIGHT; y++ )
{
cout << " ";
for ( int x = 0; x < MAP_WIDTH; x++ )
{
if ( x == m_player.x && y == m_player.y )
{
cout << "@";
}
else if ( x == m_treasure.x && y == m_treasure.y )
{
cout << "$";
}
else if ( x == m_enemy.x && y == m_enemy.y )
{
cout << "&";
}
else
{
cout << m_tiles[x][y];
}
}
cout << endl;
}
}
void Map::MovePlayer( string direction )
{
char firstLetter = direction[0];
firstLetter = tolower( firstLetter );
if ( firstLetter == MOVE_UP )
{
if ( m_player.y - 1 >= 0 && m_tiles[m_player.x][m_player.y - 1] == EMPTYROOM )
{
m_player.y--;
}
}
else if ( firstLetter == MOVE_DOWN )
{
if ( m_player.y + 1 < 10 && m_tiles[m_player.x][m_player.y + 1] == EMPTYROOM )
{
m_player.y++;
}
}
else if ( firstLetter == MOVE_RIGHT )
{
if ( m_player.x + 1 < 20 && m_tiles[m_player.x + 1][m_player.y] == EMPTYROOM )
{
m_player.x++;
}
}
else if ( firstLetter == MOVE_LEFT )
{
if ( m_player.x - 1 >= 0 && m_tiles[m_player.x - 1][m_player.y] == EMPTYROOM )
{
m_player.x--;
}
}
MoveGoblin();
}
void Map::MoveGoblin()
{
int x = m_enemy.x;
int y = m_enemy.y;
int direction = rand() % 4;
if ( direction == 0 )
{
x += 1;
}
else if ( direction == 1 )
{
x -= 1;
}
else if ( direction == 2 )
{
y += 1;
}
else
{
y -= 1;
}
if ( m_tiles[x][y] == EMPTYROOM )
{
m_enemy.x = x;
m_enemy.y = y;
}
}
bool Map::PlayerCollectTreasure()
{
return ( m_player.x == m_treasure.x && m_player.y == m_treasure.y );
}
bool Map::GoblinGetPlayer()
{
return ( m_player.x == m_enemy.x && m_player.y == m_enemy.y );
}
char Map::MoveUpKey()
{
return MOVE_UP;
}
char Map::MoveDownKey()
{
return MOVE_DOWN;
}
char Map::MoveLeftKey()
{
return MOVE_LEFT;
}
char Map::MoveRightKey()
{
return MOVE_RIGHT;
}
#ifndef _MAP_HPP
#define _MAP_HPP
#include <string>
using namespace std;
struct Coords
{
int x, y;
};
class Map
{
public:
Map();
void GenerateMap();
void Draw();
void MovePlayer( string direction );
void MoveGoblin();
bool PlayerCollectTreasure();
bool GoblinGetPlayer();
char MoveUpKey();
char MoveDownKey();
char MoveLeftKey();
char MoveRightKey();
private:
const int MAP_WIDTH;
const int MAP_HEIGHT;
const char EMPTYROOM;
const char WALL;
const char MOVE_UP;
const char MOVE_DOWN;
const char MOVE_LEFT;
const char MOVE_RIGHT;
char m_tiles[20][10];
Coords m_player;
Coords m_treasure;
Coords m_enemy;
};
#endif
#include "Utilities.hpp"
#include <iostream>
using namespace std;
void ClearScreen()
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
// The terminal clear command on Windows is "cls"
system( "cls" );
#else
// The terminal clear command on Linux/Mac/Unix is "clear"
system( "clear" );
#endif
}
int GetIntInput( int minimum, int maximum )
{
cout << ">> ";