- Details
- Parent Category: Programming Assignments' Solutions
We Helped With This C++ Programming Homework: Have A Similar One?
Assignment Description
COMP282 Assignment 2 –
Objective-C Programming
• This assignment is similar to the previous one, the chief differences being the use of Objective-C and some changes to the menu options.
• The assignment is worth 35% of the total mark for COMP282
Further information on how the work is assessed is given below.
Submission Instructions
• Make sure your name and student ID number are at the head of each of your source files.
• Zip your XCode project folder.
• Submit your zip file using the departmental electronic submission system
The deadline for this assignment is Monday 8 May, 12 noon
Please note, however, that Assignment 3 (worth 35%) will be circulated several days before that deadline. I will leave it to you to decide how best to split your time over the two assignments.
• Penalties for late submission apply in accordance with departmental policy as set out in the student handbook, which can be found at: http://www.csc.liv.ac.uk/student/ugpdfhandbook.pdf
• Please note that the new regulations state that late work will be subject to a penalty for each 24-hour period following the submission deadline. This includes weekends!
• Note also that the markers will assess exactly what you submit. It is up to you to make sure you package your files properly. No attempt will be made by the markers to track down missing files.
The problem
Create an interactive program implementing some of the features of a simple adventure game.
Start by defining a ‘Game_character’ base class. Each character has a name (string) and a strength (integer in the range 1 to 10). These should be defined as properties.
Sub-classes of Game_character are ‘Friend’ and ‘Foe’. These both inherit the name and strength properties. In addition, Friend has an ‘intelligence’ variable (integer, range 1 to 10) and a ‘spell’ variable (string), while Foe has a ‘wrath’ variable (integer, 1 to 10).
Your program should offer the following menu options to the user:
1) Create friend
2) Create foe
3) Display all characters
4) Meet
5) Edit character
6) Sort by strength
7) Quit
In implementing this, you should make use of the following:
• A separate class for handling the collection of characters (e.g. in a mutable array)
• Fast enumeration (see lecture notes)
• A ‘description’ method to return a string about each friend or foe that can be printed elsewhere
If the ‘Meet’ option is chosen, two characters are selected at random. If one is a friend and the other a foe, they will do battle as described in the previous assignment. If both are friends, then each will be given the average of the two intelligence values (so, if one friend has intelligence of 6, and the other is 10, both will end up with a score of 8). Similarly, if both are foes, they get the average of the ‘wrath’ values.
If the ‘Edit character’ option is selected, the system should show you a list and prompt you to select a character whose details you can then alter. If you don’t want to change a field (e.g. name), you should just be able to press the enter key.
The ‘Sort by strength’ option should sort the characters into ascending order of strength. For this you will need to do some research of your own. (See, for example, Chapter 15 of Kochan)
COMP282 Assignment 2 Marking
This assignment is to assess the following learning outcome;
To introduce the notion of object orientation and illustrate the differences between message-based and method-based object-oriented approaches, through the introduction of two Object-oriented variants of C; namely C++ and Objective-C 2.0.
In particular, this assignment tests that you can take advantage of the features of the Objective-C language that were demonstrated in lectures. This includes using the Objective C input / output techniques, the use of classes and properties, inheritance, function overriding and use of Foundation Framework features including collections and fast enumeration.
This Assignment contributes 35% towards the total mark for COMP282.
Assignment Code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Foe.cpp
* Author: Marlon Guerra
*
* Created on March 26, 2017, 10:26 AM
*/
#include "Foe.h"
#include "Game_character.h"
using namespace std;
//accessors
int Foe::get_wrath() const {
return wrath;
}
//mutators
void Foe::set_wrath(int wrath) {
if(wrath<0 || wrath>10) throw("Invalid wrath exception");
this->wrath = wrath;
}
void Foe::print_character(){
cout << this->get_name() << " (Strength: " << this->get_strength()
<< ", Wrath: " << wrath << ")";
}
Foe::~Foe() {
}
Assignment Code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Friend.cpp
* Author: author
*
* Created on March 26, 2017, 9:59 AM
*/
#include "Friend.h"
#include "Game_character.h"
//accessors
int Friend::get_intelligence() const {
return intelligence;
}
string Friend::get_spell() const {
return spell;
}
//mutators
void Friend::set_intelligence(int intelligence) {
if(intelligence<0 || intelligence>10) throw("Invalid intelligence exception");
this->intelligence = intelligence;
}
void Friend::set_spell(string spell) {
this->spell = spell;
}
void Friend::print_character(){
cout << this->get_name() << " (Strength: " << this->get_strength()
<< ", Intelligence: " << intelligence
<< ", Spell: " << spell << ")";
}
//destructor
Friend::~Friend() {
}
Assignment Code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Game_character.cpp
* Author: author
*
* Created on March 26, 2017, 9:33 AM
*/
#include "Game_character.h"
#include <iostream>
#include <string>
using namespace std;
//constructors
Game_character::Game_character() {
}
Game_character::Game_character(string name, int strength) {
if(strength<0 || strength>10) throw("Invalid strength exception");
this->name = name;
this->strength = strength;
}
Game_character::Game_character(const Game_character& orig) {
}
//accessors
string Game_character::get_name() const {
return name;
}
int Game_character::get_strength() const {
return strength;
}
//mutators
void Game_character::set_name(string name) {
this->name = name;
}
void Game_character::set_strength(int strength) {
if(strength<0 || strength>10) throw("Invalid strength exception");
this->strength = strength;
}
void Game_character::print_character(){
cout << name << " (Strength: " << strength << ")";
}
//destructor
Game_character::~Game_character() {
}
Assignment Code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: author
*
* Created on March 26, 2017, 7:37 AM
*/
#include <cstdlib>
#include <vector>
#include <iostream>
#include <sstream>
#include <string>
#include "Game_character.h"
#include "Friend.h"
#include "Foe.h"
using namespace std;
/**
* A helper function in getting a number input from the user
* @param prompt
* @return
*/
int getInt(string prompt){
string input;
int n;
while(true){
cout << prompt;
getline(cin, input);
stringstream myStream(input);
if (myStream >> n){
if(n>0 && n <= 10)
break;
}
cout << "Invalid number, please try again.
";
}
return n;
}
/**
* Create a friend game character
* @param game_characters
*/
void create_friend(vector<Game_character*> &game_characters){
string name;
int strength;
int intelligence;
string spell;
cout << "Enter name: ";
getline(cin, name);
strength = getInt("Enter strength: ");
intelligence = getInt("Enter intelligence: ");
cout << "Enter spell: ";
getline(cin, spell);
Friend *f = new Friend(name, strength);
f->set_intelligence(intelligence);
f->set_spell(spell);
game_characters.push_back(f);
}
/**
* Create a foe game character
* @param game_characters
*/
void create_foe(vector<Game_character*> &game_characters){
string name;
int strength;
int wrath;
cout << "Enter name: ";
getline(cin, name);
strength = getInt("Enter strength: ");
wrath = getInt("Enter wrath: ");
Foe *f = new Foe(name, strength);
f->set_wrath(wrath);
game_characters.push_back(f);
}
/**
* Displays all of the characters still on the game
* @param game_characters
*/
void display_all_characters(vector<Game_character*> &game_characters){
for( vector<Game_character*>::iterator i = game_characters.begin();
i != game_characters.end(); ++i ){
(*i)->print_character();
cout << "
";
}
}
/**
* Your program should choose two characters at random. They can be either
* friends or foes, but the same character should not be chosen to do battle
* with itself. Your program should then compare the strengths of the two
* characters. The one with the lowest strength should be deleted from your
* list. If the two strengths are equal, both characters live on to fight
* another day.
* @param game_characters
*/
void do_battle(vector<Game_character*> &game_characters){
int size = game_characters.size();
if(size > 1){
int r1 = rand() % size;
int r2 = rand() % size;
while(r1 == r2){
r2 = rand() % size;
}
Game_character *p1 = game_characters.at(r1);
Game_character *p2 = game_characters.at(r2);
string n1 = p1->get_name();
string n2 = p2->get_name();
cout << n1 << " will do battle with " << n2 << ".
";
int s1 = p1->get_strength();
int s2 = p2->get_strength();
cout << n1 << " has strength " << s1 << ". "
<< n2 << " has strength " << s2 << ".
";
if(s1<s2){
cout << n2 << " wins!
";
game_characters.erase(game_characters.begin()+r1);
delete p1;
}
else if(s2<s1){
cout << n1 << " wins!
";
game_characters.erase(game_characters.begin()+r2);
delete p2;
}else{
cout << "Its a draw!
";
}
}else{
cout << "Minimum of two game characters to do battle.
";
}
}
/*
* main function
* - displays the menu
* - calls the function corresponding to the menu item chosen
*/
int main(int argc, char** argv) {
vector<Game_character*> game_characters;
int command = 0;
do{
cout << "
Main Menu:
";
cout << "1) Create friend
";
cout << "2) Create foe
";
cout << "3) Display all characters
";
cout << "4) Do battle
";
cout << "5) Quit
";
command = getInt("What would you like to do? ");
switch(command){
case 1:
create_friend(game_characters);
break;
case 2:
create_foe(game_characters);
break;
case 3:
display_all_characters(game_characters);
break;
case 4:
do_battle(game_characters);
break;
}
}while(command!=5);
return 0;
}
Assignment Code
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* ...
*
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions ...
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the ...
*
* Contributor(s):
*/
// List of standard headers was taken in http://en.cppreference.com/w/cpp/header
#include <cstdlib> // General purpose utilities: program control, dynamic memory allocation, random numbers, sort and search
#include <csignal> // Functions and macro constants for signal management
#include <csetjmp> // Macro (and function) that saves (and jumps) to an execution context
#include <cstdarg> // Handling of variable length argument lists
#include <typeinfo> // Runtime type information utilities
#include <bitset> // std::bitset class template
#include <functional> // Function objects, designed for use with the standard algorithms
#include <utility> // Various utility components
#include <ctime> // C-style time/date utilites
#include <cstddef> // typedefs for types such as size_t, NULL and others
#include <new> // Low-level memory management utilities
#include <memory> // Higher level memory management utilities
#include <climits> // limits of integral types
#include <cfloat> // limits of float types
#include <limits> // standardized way to query properties of arithmetic types
#include <exception> // Exception handling utilities
#include <stdexcept> // Standard exception objects
#include <cassert> // Conditionally compiled macro that compares its argument to zero
#include <cerrno> // Macro containing the last error number
#include <cctype> // functions to determine the type contained in character data
#include <cwctype> // functions for determining the type of wide character data
#include <cstring> // various narrow character string handling functions
#include <cwchar> // various wide and multibyte string handling functions
#include <string> // std::basic_string class template
#include <vector> // std::vector container
#include <deque> // std::deque container
#include <list> // std::list container
#include <set> // std::set and std::multiset associative containers
#include <map> // std::map and std::multimap associative containers
#include <stack> // std::stack container adaptor
#include <queue> // std::queue and std::priority_queue container adaptors
#include <algorithm> // Algorithms that operate on containers
#include <iterator> // Container iterators
#include <cmath> // Common mathematics functions
#include <complex> // Complex number type
#include <valarray> // Class for representing and manipulating arrays of values
#include <numeric> // Numeric operations on values in containers
#include <iosfwd> // forward declarations of all classes in the input/output library
#include <ios> // std::ios_base class, std::basic_ios class template and several typedefs
#include <istream> // std::basic_istream class template and several typedefs
#include <ostream> // std::basic_ostream, std::basic_iostream class templates and several typedefs
#include <iostream> // several standard stream objects
#include <fstream> // std::basic_fstream, std::basic_ifstream, std::basic_ofstream class templates and several typedefs
#include <sstream> // std::basic_stringstream, std::basic_istringstream, std::basic_ostringstream class templates and several typedefs
#include <strstream> // std::strstream, std::istrstream, std::ostrstream(deprecated)
#include <iomanip> // Helper functions to control the format or input and output
#include <streambuf> // std::basic_streambuf class template
#include <cstdio> // C-style input-output functions
#include <locale> // Localization utilities
#include <clocale> // C localization utilities
#include <ciso646> // empty header. The macros that appear in iso646.h in C are keywords in C++
#if __cplusplus >= 201103L
#include <typeindex> // (since C++11) std::type_index
#include <type_traits> // (since C++11) Compile-time type information
#include <chrono> // (since C++11) C++ time utilites
#include <initializer_list> // (since C++11) std::initializer_list class template
#include <tuple> // (since C++11) std::tuple class template
#include <scoped_allocator> // (since C++11) Nested allocator class
#include <cstdint> // (since C++11) fixed-size types and limits of other types
#include <cinttypes> // (since C++11) formatting macros , intmax_t and uintmax_t math and conversions
#include <system_error> // (since C++11) defines std::error_code, a platform-dependent error code
#include <cuchar> // (since C++11) C-style Unicode character conversion functions
#include <array> // (since C++11) std::array container
#include <forward_list> // (since C++11) std::forward_list container
#include <unordered_set> // (since C++11) std::unordered_set and std::unordered_multiset unordered associative containers
#include <unordered_map> // (since C++11) std::unordered_map and std::unordered_multimap unordered associative containers
#include <random> // (since C++11) Random number generators and distributions
#include <ratio> // (since C++11) Compile-time rational arithmetic
#include <cfenv> // (since C++11) Floating-point environment access functions
#include <codecvt> // (since C++11) Unicode conversion facilities
#include <regex> // (since C++11) Classes, algorithms and iterators to support regular expression processing
#include <atomic> // (since C++11) Atomic operations library
#include <ccomplex> // (since C++11)(deprecated in C++17) simply includes the header <complex>
#include <ctgmath> // (since C++11)(deprecated in C++17) simply includes the headers <ccomplex> (until C++17)<complex> (since C++17) and <cmath>: the overloads equivalent to the contents of the C header tgmath.h are already provided by those headers
#include <cstdalign> // (since C++11)(deprecated in C++17) defines one compatibility macro constant
#include <cstdbool> // (since C++11)(deprecated in C++17) defines one compatibility macro constant
#include <thread> // (since C++11) std::thread class and supporting functions
#include <mutex> // (since C++11) mutual exclusion primitives
#include <future> // (since C++11) primitives for asynchronous computations
#include <condition_variable> // (since C++11) thread waiting conditions
#endif
#if __cplusplus >= 201300L
#include <shared_mutex> // (since C++14) shared mutual exclusion primitives
#endif
#if __cplusplus >= 201500L
#include <any> // (since C++17) std::any class template
#include <optional> // (since C++17) std::optional class template
#include <variant> // (since C++17) std::variant class template
#include <memory_resource> // (since C++17) Polymorphic allocators and memory resources
#include <string_view> // (since C++17) std::basic_string_view class template
#include <execution> // (since C++17) Predefined execution policies for parallel versions of the algorithms
#include <filesystem> // (since C++17) std::path class and supporting functions
#endif
Assignment Code
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* ...
*
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions ...
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the ...
*
* Contributor(s):
*/
// List of standard headers was taken in http://en.cppreference.com/w/c/header
#include <assert.h> // Conditionally compiled macro that compares its argument to zero
#include <ctype.h> // Functions to determine the type contained in character data
#include <errno.h> // Macros reporting error conditions
#include <float.h> // Limits of float types
#include <limits.h> // Sizes of basic types
#include <locale.h> // Localization utilities
#include <math.h> // Common mathematics functions
#include <setjmp.h> // Nonlocal jumps
#include <signal.h> // Signal handling
#include <stdarg.h> // Variable arguments
#include <stddef.h> // Common macro definitions
#include <stdio.h> // Input/output
#include <string.h> // String handling
#include <stdlib.h> // General utilities: memory management, program utilities, string conversions, random numbers
#include <time.h> // Time/date utilities
#include <iso646.h> // (since C95) Alternative operator spellings
#include <wchar.h> // (since C95) Extended multibyte and wide character utilities
#include <wctype.h> // (since C95) Wide character classification and mapping utilities
#ifdef _STDC_C99
#include <complex.h> // (since C99) Complex number arithmetic
#include <fenv.h> // (since C99) Floating-point environment
#include <inttypes.h> // (since C99) Format conversion of integer types
#include <stdbool.h> // (since C99) Boolean type
#include <stdint.h> // (since C99) Fixed-width integer types
#include <tgmath.h> // (since C99) Type-generic math (macros wrapping math.h and complex.h)
#endif
#ifdef _STDC_C11
#include <stdalign.h> // (since C11) alignas and alignof convenience macros
#include <stdatomic.h> // (since C11) Atomic types
#include <stdnoreturn.h> // (since C11) noreturn convenience macros
#include <threads.h> // (since C11) Thread library
#include <uchar.h> // (since C11) UTF-16 and UTF-32 character utilities
#endif