© Glenn Rowe 20041 AC21001 - Lab 3 An adventure game.

Slides:



Advertisements
Similar presentations
Chapter 10 Input/Output Streams
Advertisements

© Glenn Rowe AC Lab 2 A simple card game (twenty- one)
Object Oriented Programming Elhanan Borenstein Lecture #12 copyrights © Elhanan Borenstein.
Lab#1 (14/3/1431h) Introduction To java programming cs425
File I/O Finished off Colin Cherry standing in for Dr. Lin.
1 Lecture 31 Handling Selected Topics Again!! File I/O Special Lectures.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Stacks.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Program Input and the Software Design Process ROBERT REAVES.
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
1)Never start coding unless you understand the task! 2)Gather requirements first. This means identify the problem and ask questions about it. Now you kind.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
OOP Languages: Java vs C++
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
ITM 352 © Port,KazmanFile I/O - 1 File I/O in PHP Persistent Data.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Java and C++, The Difference An introduction Unit - 00.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
Description, Classes, Interfaces, Hierarchy, Specifics George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Inheritance Version 1.1. Topics Inheritance Constructors and Inheritance Function Over-riding (Redefinition) The Stringstream class Shadowing Variables.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
File I/O ifstreams and ofstreams Sections 11.1 &
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
1 CS161 Introduction to Computer Science Topic #13.
FILE HANDLING IN C++.
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
1 firstlastnextprevioushome richard jones COMP103A Introduction to Computer Science 1 Richard Jones G2.04
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Protectedly Inherited Base Class  When the access specifier of the base class in the derived class definition is protected, the base class is protectedly.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Overview of C++ Templates
M1G Introduction to Programming 2 5. Completing the program.
More on OO Programming Our programs will no longer just be a main method or a main which calls a collection of other methods –instead, a program will be.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
Introduction to the Object-oriented Data Protocol © Dr. David Workman COP4331 School of EE and CS February 4, 2010.
Learners Support Publications Working with Files.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Lecture 14 Arguments, Classes and Files. Arguments.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
World of Wokcraft The very best in Single pan cooking themed fantasy gaming!
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Motivation for Generic Programming in C++
C++ CSCE 343.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CS 215 Final Review Ismail abumuhfouz Fall 2014.
CS3340 – OOP and C++ L. Grewe.
Objectives Identify the built-in data types in C++
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Lecture 5A File processing Richard Gesick.
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
File I/O in C++ I.
STL and Example.
File I/O in C++ I.
Presentation transcript:

© Glenn Rowe AC Lab 3 An adventure game

© Glenn Rowe Adventure games Player runs a character that explores a map  can fight 'monsters'  collect treasure  solve puzzles Final goal  defeat 'boss' character  obtain final treasure, etc.

© Glenn Rowe Adventure game: requirements Single location One player character One monster (dragon) Goal: kill the dragon Actions:  drink (a potion to restore health)  wear (suit of armour)  attack (attack the dragon)  status (show current state of player)  quit (quit game)

© Glenn Rowe Aims of lab (coding) Inheritance Polymorphism File access

© Glenn Rowe Class structure Player can carry items of various types  potions  armour All items have properties in common (e.g. description, weight, etc) Define Item base class for common properties Define derived classes for each item type  Potion class  Armour class

© Glenn Rowe Item class Base class for all item types Create data field for  description (string)  can add other fields in extended version of game Provide constructors  zero-parameter  one-parameter Provide interface methods (get / set) Provide ToString() method

© Glenn Rowe Potion class Inherits Item Add a 'health' data field  amount of health gained when potion drunk Override ToString() to print out customized description for potion Provide constructors Provide interface methods for health

© Glenn Rowe Armour class Inherits Item Add a 'protection' data field  reduction in damage when wearing armour Override ToString() to print out customized description for armour Provide constructors Provide interface methods for protection

© Glenn Rowe Derived class methods A derived class may have methods not overridden from base class  e.g. Armour has getProtection() If using polymorphism, must cast the pointer to derived class in order to access such methods Item* myItem = new Armour(); ((Armour *)myItem)->getProtection();

© Glenn Rowe Creature class Define Creature class  base class for all creature and player types Data fields  name (string)  health (int) - when reduced to zero, creature dies Use Creature class for dragon  or define a Dragon class that inherits Creature Provide constructors, interface methods Write Attack() method for dealing with attacks by creature (see later)

© Glenn Rowe Player class Player class represents player's character Inherits Creature class Additional data fields:  carriedItems (array of Item* pointers)  wornArmour  others as you see fit Initial items read from disk file (see later) Provide constructors, interface methods Override Attack() method

© Glenn Rowe Carried items - using polymorphism 00FE34D Item **carriedItems Declare carriedItems array pointer (of type Item **). [Or can use a vector from STL)

© Glenn Rowe Carried items - using polymorphism 00FE34D Item **carriedItems 00FE45700FE51900FE623[…] new Item*[numItems] Create an array of Item* pointers of the right size (size read from disk file).

© Glenn Rowe Carried items - using polymorphism 00FE34D Item **carriedItems 00FE45700FE51900FE623[…] new Item*[numItems] Each Item* pointer can point to an Item object or to object derived from Item (uses polymorphism) ItemPotion ArmourPotion

© Glenn Rowe File access in C++ Can read player's items from a text file Typical file format:  First line: number of items to be read  Remaining lines: details for each item  E.g. potion;carrot;5  (item type);(item name);(item value[s]) Need method of reading in text… …and of 'parsing' details for each item

© Glenn Rowe Reading from a file #include ifstream dataStream; dataStream.open("ItemData.txt"); if (!dataStream) { cout << "Can't open file\n"; } dataStream >> numItems; C++: Player.cpp Must include the fstream library to allow I/O to/from files.

© Glenn Rowe Reading from a file #include ifstream dataStream; dataStream.open("ItemData.txt"); if (!dataStream) { cout << "Can't open file\n"; } dataStream >> numItems; C++: Player.cpp Create an ifstream for input from a file. (Use ofstream for writing to a file.)

© Glenn Rowe Reading from a file #include ifstream dataStream; dataStream.open("ItemData.txt"); if (!dataStream) { cout << "Can't open file\n"; } dataStream >> numItems; C++: Player.cpp Open the file by calling the open( ) method from the ifstream object.

© Glenn Rowe Reading from a file #include ifstream dataStream; dataStream.open("ItemData.txt"); if (!dataStream) { cout << "Can't open file\n"; } dataStream >> numItems; C++: Player.cpp If there is a problem opening the file (e.g. file doesn't exist), dataStream is NULL. Should always test this after opening.

© Glenn Rowe Reading from a file #include ifstream dataStream; dataStream.open("ItemData.txt"); if (!dataStream) { cout << "Can't open file\n"; } dataStream >> numItems; C++: Player.cpp Once opened, an ifstream object can be used just like 'cin', so can use it to read data from file.

© Glenn Rowe Reading from a file #include ifstream dataStream; dataStream.open("ItemData.txt"); if (!dataStream) { cout << "Can't open file\n"; } dataStream >> numItems; // Code to read in data for // items C++: Player.cpp Read in the data for each of your numItems items and store in your array or vector.

© Glenn Rowe Parsing an input line Must 'parse' (interpret) an item's properties  potion;carrot;5 Contains 3 separate pieces of info Would like to 'tokenize' the line - split it into 3 separate words Various methods exist  look up on web  write your own  or use Tokenize() method provided on lab 3 web page (uses STL’s vector)

© Glenn Rowe Tokenize( ) method void Player::Tokenize(const string& str, vector & tokens, const string& delimiters) 'str' is the string to be tokenized (input line from file) 'tokens' is a vector of strings containing the separate words after tokenizing  Declare ‘tokens’ before calling Tokenize() 'delimiters' is a list of symbols used to separate words (; in the input line above)

© Glenn Rowe Game class Controls play of the game Each turn:  Print prompt: (e.g. "What now? >")  Accept player's command  Test for errors  Call appropriate method to run command

© Glenn Rowe Commands: status Prints out status of Player and Dragon Call methods from Player and Dragon class to do this e.g. ToString( )

© Glenn Rowe Commands: drink Used to drink a potion Should print out a numbered list of potions being carried by player Accept number of potion to be drunk Increase player's health by value of potion Delete potion from player's carriedItems list

© Glenn Rowe Commands: wear Used to wear or swap a suit of armour Print numbered list of armours being carried Accept number of suit to be worn Set player's protection value according to value of armour (Protection value used in attacks)

© Glenn Rowe Command: quit Quit the game May print an "Are you sure?" prompt first

© Glenn Rowe Commands: attack Run one round of combat between player & dragon Use your own attack system if you like Simple version:  Randomly determine who goes first  For each combatant: Randomly determine if they hit the opponent If they hit, subtract damage done from opponent's health For hits on player, adjust damage because of armour Check if opponent is dead

© Glenn Rowe main( ) main( ) should do no more than create an instance of Game Then call Game's play( ) method