Digital
and Interactive Games 2015 – Week 2, Session 2
void Infantry
putItem (slotNumber, GameObject) {
Inventory myInventory;
myInventory.putItem(1, anAxe);
myInventory.putItem(2, myFood);
void Inventory::putItem(slotNumber, GameItem) {
switch(slotNumber) {
case 1:
slot1 = theItem;
break;
case 2:
slot2 = theItem;
break;
etc
}
}
put Item (slotNumber, GameObject) {
switch (slotNumber) {
case 1:
break
}
}
GameItem Inventory::takeItem (int, slotNumber) {
return;
}
GameItem takeItem(int slotNumber)
return slot1;
GameItem Inventory::takeItem (int slotNumber)
if (slotNumber == 1)
return slot1;
}
void Infantry
putItem (slotNumber, GameObject) {
Inventory myInventory;
myInventory.putItem(1, anAxe);
myInventory.putItem(2, myFood);
void Inventory::putItem(slotNumber, GameItem) {
switch(slotNumber) {
case 1:
slot1 = theItem;
break;
case 2:
slot2 = theItem;
break;
etc
}
}
put Item (slotNumber, GameObject) {
switch (slotNumber) {
case 1:
break
}
}
GameItem Inventory::takeItem (int, slotNumber) {
return;
}
GameItem takeItem(int slotNumber)
return slot1;
GameItem Inventory::takeItem (int slotNumber)
if (slotNumber == 1)
return slot1;
}
How
to check if the slot is taken.
…
Inventory.h
public:
Inventory();
~Inventory();
void putItem (int, gameItem);
GameItem takeItem(int);
bool isEmptySlot;
Inventory.cpp
bool Inventory::isEmptySlot (int, slotNumber) {
if (slotNumber == 1) {
slot1
return;
}
}
GameItem.h
class GameItem
string itemType;
int itemAmount;
bool bIsEmpty { true };
public:
GameItem();
~GameItem();
void setItemType(int);
string getItemType();
bool isEmpty();
};
…
GameItem myWeapon;
– myWeapon.setItemType(2);
…
GameItem.cppp
bIsEmpty = false;
switch (ItemTypeNumber) {
string GameItem::getItemType() {
return ItemType;
bool GameItem::isEmpty() {
return bIsEmpty;
}
Inventory.cpp
bool Inventory::isEmptySlot (int slotNumber) {
bool bEmpty { false };
if (slotNumber == 1) {
bEmpty = slot1, isEmpty())
}
return bEmpty;
}
C++ Primer – Chapter 3
New Project – stringExperiment
int main() {
string firstString;
return 0;
}
string from C++ Standard Library
– not the string from C
using std::string
using std::cout
int main() {
string firstString; // default initialisation
cout << firstString
system(“pause”)
return 0;
}
String can be seen as characters
<code>
…
Inventory.h
public:
Inventory();
~Inventory();
void putItem (int, gameItem);
GameItem takeItem(int);
bool isEmptySlot;
Inventory.cpp
bool Inventory::isEmptySlot (int, slotNumber) {
if (slotNumber == 1) {
slot1
return;
}
}
GameItem.h
class GameItem
string itemType;
int itemAmount;
bool bIsEmpty { true };
public:
GameItem();
~GameItem();
void setItemType(int);
string getItemType();
bool isEmpty();
};
…
GameItem myWeapon;
– myWeapon.setItemType(2);
…
GameItem.cppp
bIsEmpty = false;
switch (ItemTypeNumber) {
string GameItem::getItemType() {
return ItemType;
bool GameItem::isEmpty() {
return bIsEmpty;
}
Inventory.cpp
bool Inventory::isEmptySlot (int slotNumber) {
bool bEmpty { false };
if (slotNumber == 1) {
bEmpty = slot1, isEmpty())
}
return bEmpty;
}
C++ Primer – Chapter 3
New Project – stringExperiment
int main() {
string firstString;
return 0;
}
string from C++ Standard Library
– not the string from C
using std::string
using std::cout
int main() {
string firstString; // default initialisation
cout << firstString
system(“pause”)
return 0;
}
String can be seen as characters
<code>
#include
<iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string firstString; // default initialisation
string secondString { “This is a string” };
string thirdString [10, '#'];
cout << firstString << endl;
cout << secondString << endl;
cout << thirdString << endl;
cin >> firstString;
cout << firstString << endl;
system(“pause”);
return 0;
}
</code>
a while loop
while (std::getline(cin, firstString)) {
if (firstString = “exit”) break;
cout << firstString << endl;
}
Basis for the main loop of the text version of the game.
Complete code here:
<code>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string firstString; // default initialisation
string secondString { “This is a string” };
string thirdString [10, '#'];
cout << firstString << endl;
cout << secondString << endl;
cout << thirdString << endl;
cin >> firstString;
cout << firstString << endl;
system(“pause”);
return 0;
}
</code>
a while loop
while (std::getline(cin, firstString)) {
if (firstString = “exit”) break;
cout << firstString << endl;
}
Basis for the main loop of the text version of the game.
Complete code here:
<code>
#include
<iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string firstString; // default initialisation
string secondString { “This is a string” };
string thirdString [10, '#'];
cout << firstString << endl;
cout << secondString << endl;
cout << thirdString << endl;
cin >> firstString;
while (std::getline(cin, firstString)) {
if (firstString = “exit”) break;
cout << firstString << endl;
}
system(“pause”);
return 0;
}
</code>
Place Commands in the loop
cout << “Command: “;
Complete code here:
<code>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string firstString; // default initialisation
string secondString { “This is a string” };
string thirdString [10, '#'];
cout << firstString << endl;
cout << secondString << endl;
cout << thirdString << endl;
cin >> firstString;
while (std::getline(cin, firstString)) {
if (firstString = “exit”) break;
cout << firstString << endl;
}
system(“pause”);
return 0;
}
</code>
Place Commands in the loop
cout << “Command: “;
Complete code here:
<code>
#include
<iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string firstString; // default initialisation
string secondString { “This is a string” };
string thirdString [10, '#'];
cout << firstString << endl;
cout << secondString << endl;
cout << thirdString << endl;
cin >> firstString;
while (std::getline(cin, firstString)) {
if (firstString = “exit”) break;
cout << firstString << endl;
cout << “Command: “;
}
system(“pause”);
return 0;
}
</code>
beneath break point”
if (!firstString.empty()) {
cout << firstString << endl;
cout << “Command ”;
}
Complete code here:
<code>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string firstString; // default initialisation
string secondString { “This is a string” };
string thirdString [10, '#'];
cout << firstString << endl;
cout << secondString << endl;
cout << thirdString << endl;
cin >> firstString;
while (std::getline(cin, firstString)) {
if (firstString = “exit”) break;
cout << firstString << endl;
cout << “Command: “;
}
system(“pause”);
return 0;
}
</code>
beneath break point”
if (!firstString.empty()) {
cout << firstString << endl;
cout << “Command ”;
}
Complete code here:
<code>
#include
<iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string firstString; // default initialisation
string secondString { “This is a string” };
string thirdString [10, '#'];
cout << firstString << endl;
cout << secondString << endl;
cout << thirdString << endl;
cin >> firstString;
while (std::getline(cin, firstString)) {
if (firstString = “exit”) break;
if (!firstString.empty()) {
cout << firstString << endl;
cout << “Command ”;
}
cout << firstString << endl;
cout << “Command: “;
}
system(“pause”);
return 0;
}
</code>
Text comparison is case sensitive
Manipulating strings
beneath cout << firstString << endl;
for (auto c: firstString )
cout << c << “ ” << endl;
Complete code here:
<code>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string firstString; // default initialisation
string secondString { “This is a string” };
string thirdString [10, '#'];
cout << firstString << endl;
cout << secondString << endl;
cout << thirdString << endl;
cin >> firstString;
while (std::getline(cin, firstString)) {
if (firstString = “exit”) break;
if (!firstString.empty()) {
cout << firstString << endl;
cout << “Command ”;
}
cout << firstString << endl;
cout << “Command: “;
}
system(“pause”);
return 0;
}
</code>
Text comparison is case sensitive
Manipulating strings
beneath cout << firstString << endl;
for (auto c: firstString )
cout << c << “ ” << endl;
Complete code here:
<code>
#include
<iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string firstString; // default initialisation
string secondString { “This is a string” };
string thirdString [10, '#'];
cout << firstString << endl;
cout << secondString << endl;
cout << thirdString << endl;
cin >> firstString;
while (std::getline(cin, firstString)) {
if (firstString = “exit”) break;
if (!firstString.empty()) {
cout << firstString << endl;
for (auto c: firstString )
cout << c << “ ” << endl;
cout << “Command ”;
}
cout << firstString << endl;
cout << “Command: “;
}
system(“pause”);
return 0;
}
</code>
In for loop, comment out existing line and add block.
c = toupper(c);
cout << c;
Complete code here:
<code>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string firstString; // default initialisation
string secondString { “This is a string” };
string thirdString [10, '#'];
cout << firstString << endl;
cout << secondString << endl;
cout << thirdString << endl;
cin >> firstString;
while (std::getline(cin, firstString)) {
if (firstString = “exit”) break;
if (!firstString.empty()) {
cout << firstString << endl;
for (auto c: firstString )
cout << c << “ ” << endl;
cout << “Command ”;
}
cout << firstString << endl;
cout << “Command: “;
}
system(“pause”);
return 0;
}
</code>
In for loop, comment out existing line and add block.
c = toupper(c);
cout << c;
Complete code here:
<code>
#include
<iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string firstString; // default initialisation
string secondString { “This is a string” };
string thirdString [10, '#'];
cout << firstString << endl;
cout << secondString << endl;
cout << thirdString << endl;
cin >> firstString;
while (std::getline(cin, firstString)) {
if (firstString = “exit”) break;
if (!firstString.empty()) {
cout << firstString << endl;
for (auto c: firstString ) {
// cout << c << “ ” << endl;
c = toupper(c);
cout << c;
}
cout << “Command ”;
}
cout << firstString << endl;
cout << “Command: “;
}
system(“pause”);
return 0;
}
</code>
Size of string
(below for loop)
cout << endl;
cout << firstString.size() << endl;
Vector
New Project – Vector experiment
Not a type.
– It's a class template.
#include <iostream>
#include <vector>
using std::vector;
int main () {
return 0;
}
Not a type
We need to supply a type.
Int main () {
vector <int> first vector:
return 0;
}
firstVector → Nothing, it's pointing to nothing.
Vector <int> firstVector;
firstVector.push_back(10);
The integer is pushed to the back of the vector
firstVector.push_back(10);
firstVector.push_back(5);
using std::vector
using std::cout
using std::endl;
int main() {
vector <int> firstVector;
firstVector.push_back(10)
firstVector.push_back(5);
cout << firstVector[0] << endl;
return 0;
}
cout << firstVector[0] << endl;
for (auto anInt; firstVector) {
cout << anInt << endl;
}
Vector – one of the most efficient tools in C++
(for loop)
vector <int> slotAmount { 0, 0, 0, 0, 0, 0, 0, 0 };
More ways to initialise vector
vector <int> slotAmount { 0, 0, 0, 0, 0 , 0, 0, 0 }'
vector <int> slotAmount2 (8, 0);
cout << slotAmount, size() << endl;
…
Reopen GameItem
Inventory.h
#define INVENTORY_H
#include <vector>
#include “GameItem.h”
class Inventory() {
std::vector <GameItem> slotItem;
}
Inventory.cpp
Inventory::Inventory()
{
GameItem anItem;
slotItem.push_back(anItem);
}
…
for loop
GameItem anItem;
for (auto i = 0; i != 0; i++)
slot.push_back(anItem);
…
// put an item into a slot
// slot number from 1 – 7
void Inventory::putItem(int slotNumber, GameItem) {
slotItem[slotNumber] = theItem;
}
bool Inventory::isEmptySlot(int slotNumber) {
return slotItem[slotNumber].isEmpty();
}
main.cpp
#include “hero.h”
#include “Inventory.h”
int main() {
Inventory myInventory();
return 0;
}
myInventory.putItem(0, anItem);
Inventory.cpp
#include <iostream>
voidInventory::putItem(int slotNumber, GameItem) {
std::cout << “putting ”;
std::cout << theItem.getItemType();
std::cout << “ into slot ”;
std::cout << slotNumber”
std::cout << std::endl;
slotItem[slotNumber] = theItem;
Should be commented out in production version.
Main.cpp
anItem.getItemType(2);
myInventory.putItem(1, anItem);
#cplusplus #gamedesign #gameprogramming #programming #tafe
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string firstString; // default initialisation
string secondString { “This is a string” };
string thirdString [10, '#'];
cout << firstString << endl;
cout << secondString << endl;
cout << thirdString << endl;
cin >> firstString;
while (std::getline(cin, firstString)) {
if (firstString = “exit”) break;
if (!firstString.empty()) {
cout << firstString << endl;
for (auto c: firstString ) {
// cout << c << “ ” << endl;
c = toupper(c);
cout << c;
}
cout << “Command ”;
}
cout << firstString << endl;
cout << “Command: “;
}
system(“pause”);
return 0;
}
</code>
Size of string
(below for loop)
cout << endl;
cout << firstString.size() << endl;
Vector
New Project – Vector experiment
Not a type.
– It's a class template.
#include <iostream>
#include <vector>
using std::vector;
int main () {
return 0;
}
Not a type
We need to supply a type.
Int main () {
vector <int> first vector:
return 0;
}
firstVector → Nothing, it's pointing to nothing.
Vector <int> firstVector;
firstVector.push_back(10);
The integer is pushed to the back of the vector
firstVector.push_back(10);
firstVector.push_back(5);
using std::vector
using std::cout
using std::endl;
int main() {
vector <int> firstVector;
firstVector.push_back(10)
firstVector.push_back(5);
cout << firstVector[0] << endl;
return 0;
}
cout << firstVector[0] << endl;
for (auto anInt; firstVector) {
cout << anInt << endl;
}
Vector – one of the most efficient tools in C++
(for loop)
vector <int> slotAmount { 0, 0, 0, 0, 0, 0, 0, 0 };
More ways to initialise vector
vector <int> slotAmount { 0, 0, 0, 0, 0 , 0, 0, 0 }'
vector <int> slotAmount2 (8, 0);
cout << slotAmount, size() << endl;
…
Reopen GameItem
Inventory.h
#define INVENTORY_H
#include <vector>
#include “GameItem.h”
class Inventory() {
std::vector <GameItem> slotItem;
}
Inventory.cpp
Inventory::Inventory()
{
GameItem anItem;
slotItem.push_back(anItem);
}
…
for loop
GameItem anItem;
for (auto i = 0; i != 0; i++)
slot.push_back(anItem);
…
// put an item into a slot
// slot number from 1 – 7
void Inventory::putItem(int slotNumber, GameItem) {
slotItem[slotNumber] = theItem;
}
bool Inventory::isEmptySlot(int slotNumber) {
return slotItem[slotNumber].isEmpty();
}
main.cpp
#include “hero.h”
#include “Inventory.h”
int main() {
Inventory myInventory();
return 0;
}
myInventory.putItem(0, anItem);
Inventory.cpp
#include <iostream>
voidInventory::putItem(int slotNumber, GameItem) {
std::cout << “putting ”;
std::cout << theItem.getItemType();
std::cout << “ into slot ”;
std::cout << slotNumber”
std::cout << std::endl;
slotItem[slotNumber] = theItem;
Should be commented out in production version.
Main.cpp
anItem.getItemType(2);
myInventory.putItem(1, anItem);
#cplusplus #gamedesign #gameprogramming #programming #tafe
No comments:
Post a Comment