Jump to content
  • Advertisement

ICanC

Member
  • Content Count

    39
  • Joined

  • Last visited

Community Reputation

147 Neutral

About ICanC

  • Rank
    Member

Personal Information

  • Interests
    Programming

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. thanks all, slightly different to fallout, I just wanted to get the number of letters that exist in the answer, irrespective of where they are. I came up with this, which seems to work, but is probably over complicated : void computer::terminal::get_chars_correct(std::string guess) { int count{ 0 }; std::vector<char> pass; for (char c : answer) { pass.push_back(c); } auto check_match = [](const char pc, const char c) {return pc == c; }; std::vector<char> counted{}; std::vector<char>::iterator it; for (char c : guess) { it = pass.begin(); while (it != pass.end()) { bool b = check_match(*it, c); if (b) { if (std::find(counted.begin(), counted.end(), c) == counted.end()) //check that this letter hasn't already been accounted for { ++count; counted.push_back(c); } } ++it; } } chars_correct = count; }
  2. thanks Zakwayda, I appreciate the feedback very much. Regarding my question, there is no code actually doing the check yet, as I'm unsure as to how to go about doing it. Basiaclly if the user input is 'bars' and the correct answer is 'mars' I need the program to say 3 characters match, but I don't know how best to write a function to count this, although thinking about it again now, possibly I'll have a look at passing a function the above in 2 vectors of chars, iterate through the answer for each character in the user input, and count the matches... I've probably just answered my own question
  3. just functions to clear the screen and print characters slower like an old terminal #define CLEAR system("cls") void print(std::string s) { for (auto& c : s) { std::cout << c; std::this_thread::sleep_for(std::chrono::milliseconds(5)); } }
  4. I'm trying to make a terminal hacking game the same as fallout, but I'm struggling to get the number of characters matching between the user input and the correct password. I've tried searching but so far I haven't found any simple solutions. Please can someone suggest a way? I've also included the full class code below for any tips on improving it void computer::terminal::create_pass() { int lower, upper; switch (diff) { case difficulty::easy: lower = 2; upper = 4; break; case difficulty::medium: lower = 5; upper = 7; break; case difficulty::hard: lower = 8; upper = 12; break; } int a = rand_eng(lower, upper); fill_vec(a); this->fill_addresses(a); this->fill_characters(); int vec_size = passwds.size(); int b = rand_eng(0, vec_size-1); answer = passwds[b]; return; } void computer::terminal::fill_addresses(int amount) { int ran{}; std::string text{}; for (int i = 0; i <= amount; i++) { ran = rand_eng(100, 999); text = "0xF" + std::to_string(ran); bool dupe{ false }; std::vector<std::string>::iterator it = adds.begin(); while (it != adds.end()) { if (*it == text) { dupe = true; break; } ++it; } if (dupe == false) { adds.push_back(text); } else this->fill_addresses(amount); } return; } void computer::terminal::fill_characters() { std::vector<char> temp = { '?', '/', '\\', '-', '_', '[', ']', '$', '%', '&', '*', '"', '<', '#', '>' }; int ran{}; for (int i = 0; i <= 14; i++) { ran = rand_eng(0, 14); chars.push_back(temp[ran]); } return; } void computer::terminal::fill_vec(int amount) { std::vector<std::string> temp{ "biscuit", "jupiter", "mars", "wellington", "statue", "radio", "sponge", "universe", "capture", "penny", "thunder", "hurricane", "knight", "bishop", "queen", "king"}; int r{ 0 }; for (int i = 0; i <= amount; i++) { bool dupe{ false }; r = rand_eng(0, 15); std::string s = temp[r]; auto it = passwds.begin(); while (it != passwds.end()) { if (*it == s) { dupe = true; break; } ++it; } if (dupe == false) { passwds.push_back(s); } } return; } void computer::terminal::login(bool first_login) { CLEAR; std::cout << "ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL\nENTER PASSWORD NOW\n\n" << tries << " ATTEMPT(S) LEFT: "; for (int i = 1; i <= tries; i++) { std::cout << "| "; } std::cout << "\n\n"; std::random_device shuffle_device; for (int i = 0; i != passwds.size(); ++i) { print(adds[i]); std::cout << '\t'; int before = rand_eng(0, 10); int end = rand_eng(0, 10); std::shuffle(chars.begin(), chars.end(), std::mt19937(shuffle_device())); for (int i = 0; i <= before; i++) { std::cout << chars[i]; } print(passwds[i]); if (answer == passwds[i]) print("THIS IS THE ANSWER"); std::shuffle(chars.begin(), chars.end(), std::mt19937(shuffle_device())); for (int i = 0; i <= end; i++) { std::cout << chars[i]; } std::cout << '\n'; } print("\n\nCHARACTERS MATCH: "); std::cout << chars_correct; if (first_login) guess(); else return; } bool computer::terminal::guess_valid(const std::string& guess) { auto validity_check = [&guess](const std::string& pw) { return guess == pw; }; return std::any_of(passwds.begin(), passwds.end(), validity_check); } void computer::terminal::check_valid(std::string& s) { while (!guess_valid(s)) { print("\n\nINVALID PASSWORD"); std::this_thread::sleep_for(std::chrono::seconds(1)); std::cout << "\n\n> "; std::cin.clear(); std::getline(std::cin, s); } } void computer::terminal::guess() { std::cout << "\n\n> "; std::string s{}; std::cin.ignore(); std::getline(std::cin, s); check_valid(s); while (tries > 0) { if (pass_correct(s)) { print("\n\nPASSWORD ACCEPTED"); std::this_thread::sleep_for(std::chrono::seconds(1)); return; } else { print("\n\nWRONG PASSWORD"); std::this_thread::sleep_for(std::chrono::seconds(1)); std::vector<std::string>::iterator it = passwds.begin(); while (it != passwds.end()) { if (*it == s) { passwds.erase(it); break; } ++it; } tries--; std::cout << "\n\n"; login(false); } std::cout << "\n\n> "; std::cin.clear(); std::getline(std::cin, s); check_valid(s); } CLEAR; print("\n\nTERMINAL LOCKED. PLEASE CONTACT YOUR SYSTEM ADMINISTRATOR"); std::this_thread::sleep_for(std::chrono::seconds(3)); }
  5. I'm using enums for the first time to and I'm wondering how best to use them for classes. At the moment I'm encapsulating the meaning within classes (and duplicating for classes that will share it) - but I've run into a problem when assigning a enum type: class mast { enum signal { E, G, H, HPLUS, FOURG }; private: signal capability; signal current_broadcast; bool power{ true }; public: mast(); ~mast() = default; signal getCapability(); signal getBroadcast(); void printCapability(); void printBroadcast(); void setCapability(signal s) {capability = s;} void setBroadcast(signal s) { current_broadcast = s; } bool getPower() { return power; } void setPower(bool b) { power = b; } }; /*====================================================================================*/ class phone { enum signal { E, G, H, HPLUS, FOURG }; private: std::map<std::string,std::string> contacts; std::map<std::string, std::string> messages; signal signal_strength; bool wifi; public: phone(); ~phone() = default; void boot(mast& m); void addContact(std::pair<std::string,std::string>); bool removeContact(std::string); void getContacts() { for (auto& it : contacts) { std::cout << "\nName: " << it.first << "\nNumber: " << it.second << std::endl; for (int i = 0; i < 20; ++i) std::cout << "-"; } } void setSignal(signal s) { signal_strength = s; } void setWifi(bool b) { wifi = b; } bool getWifi() { return wifi; } void searchWifi() {}; }; //========================================================================================== Are my 2 classes, however in the phone constructor (see error line below) I get the following errors : error C3867: 'mast::getBroadcast': non-standard syntax; use '&' to create a pointer to member error C2440: '=': cannot convert from 'mast::signal (__thiscall mast::* )(void)' to 'phone::signal' #include "phone.h" #include "mast.h" #include "rand_eng.h" phone::phone() { wifi = false; } void phone::boot(mast& m) { signal_strength = m.getBroadcast; // THIS FLAGS ERROR } void phone::addContact(std::pair<std::string, std::string> entry) { contacts.insert(entry); } bool phone::removeContact(std::string entry) { std::map<std::string, std::string>::iterator it; it = contacts.find(entry); if (it != contacts.end()) { contacts.erase(it); return true; } return false; } Any by the way the code for mast::getBroadcast is: mast::signal mast::getBroadcast() { return current_broadcast; } I've also tried putting the enum definition outside of the classes but it still says error C2440: '=': cannot convert from 'signal (__thiscall mast::* )(void)' to 'signal'
  6. ICanC

    blackjack game

    I'm a complete amateur but just in case you know less than I do, here is some example code from a text based blackjack I attempted to make which may give you some ideas: class blackjack { private: std::vector<card> deck; int minBet; int maxBet; int decks; public: blackjack(int minBet, int maxBet, int decks); ~blackjack() {}; void shuffle(); void reshuffle(); int getValue(std::vector<card> hand) const; card deal(); void play(std::shared_ptr<player> p); bool check_for_blackjack(std::vector<card> hand); bool bust(std::vector<card> hand); }; blackjack::blackjack(int minBet, int maxBet, int decks) { this->minBet = minBet; this->maxBet = maxBet; this->decks = decks; this->reshuffle(); } void blackjack::shuffle() { static std::random_device rd; std::shuffle(this->deck.begin(), this->deck.end(), std::mt19937(rd())); } void blackjack::reshuffle() { this->deck.clear(); for (int i = 1; i < 14; ++i) { for (int j = 1; j < 5; ++j) { if (i == 1) { card c1("Ace", i, j); this->deck.push_back(c1); } else if (i == 11) { card c1("Jack", i - 1, j); this->deck.push_back(c1); } else if (i == 12) { card c1("Queen", i - 2, j); this->deck.push_back(c1); } else if (i == 13) { card c1("King", i - 3, j); this->deck.push_back(c1); } else { card c1(std::to_string(i), i, j); this->deck.push_back(c1); } } } } card blackjack::deal() { card dealCard = this->deck[this->deck.size() - 1]; this->deck.pop_back(); return dealCard; } int blackjack::getValue(std::vector<card> hand) const { int value = 0; for (card &c : hand) { value += c.value; } return value; } bool blackjack::check_for_blackjack(std::vector<card> hand) { int value = 0; for (auto &i : hand) { value += i.value; } if (value == 21) { return true; } else return false; } bool blackjack::bust(std::vector<card> hand) { int value = 0; for (auto &i : hand) { value += i.value; } if (value > 21) { return true; } else return false; } /*-------------------------------------------------------------------------------------------------------*/ class card { public: std::string name; int value; int suit; card(std::string name, int value, int suit); card() {}; ~card() {}; std::string getSuit() const; void display() const; }; card::card(std::string name, int value, int suit) { this->name = name; this->value = value; this->suit = suit; } void card::display() const { print(this->name); if (suit == 1) { print(" of Hearts\n"); } if (suit == 2) { print(" of Diamonds\n"); } if (suit == 3) { print(" of Clubs\n"); } if (suit == 4) { print(" of Spades\n"); } } std::string card::getSuit() const { if (this->suit == 1) { return "Hearts"; } if (this->suit == 2) { return "Diamonds"; } if (this->suit == 3) { return "Clubs"; } if (this->suit == 4) { return "Spades"; } else return "Something went wrong"; }
  7. the stock class now looks like this, but it generates the same error: #pragma once #include <iostream> #include <iomanip> #include <string> class stock { private: std::pair<std::string,float> company; public: stock(std::string name, float price); ~stock() = default; void display() const { std::cout << company.first << std::endl << std::fixed << std::setprecision(2) << company.second << std::endl; } const float getPrice() { return company.second; } const std::string getName() { return company.first; } bool operator==(stock n) { return this->company.first == n.company.first; } }; stock::stock(std::string name, float price) { company.first = name; company.second = price; }
  8. I've decided to change the code slightly and work with a vector of pairs instead. However now I'm having problems getting std::find to work again, please see the following code: void sell_stock(std::shared_ptr<stock> shares, int quantity) { std::vector<std::pair<stock, int>>::iterator it = std::find(std::begin(investments), std::end(investments), shares->getName()); if (it == std::end(investments)) { std::cout << "\n\nERROR - person.sell_stock tried to sell shares not in portfolio" << std::endl; char exit; std::cin >> exit; } else { investments.erase(it); cash += (shares->getPrice() * quantity); } } }; // ==================================================================================================================================== and the overload in the stock class : bool operator==(std::pair<std::string,float> n) { return this->company.first == n.first; } gives me error: c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(3579): error C2678: binary '==': no operator found which takes a left-hand operand of type 'std::pair<stock,int>' (or there is no acceptable conversion) 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\random(2557): note: could be 'bool std::operator ==(const std::bernoulli_distribution &,const std::bernoulli_distribution &)' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(284): note: or 'bool std::operator ==(const std::error_condition &,const std::error_condition &) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(278): note: or 'bool std::operator ==(const std::error_condition &,const std::error_code &) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(272): note: or 'bool std::operator ==(const std::error_code &,const std::error_condition &) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(266): note: or 'bool std::operator ==(const std::error_code &,const std::error_code &) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\exception(330): note: or 'bool std::operator ==(const std::exception_ptr &,std::nullptr_t) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\exception(325): note: or 'bool std::operator ==(std::nullptr_t,const std::exception_ptr &) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\exception(320): note: or 'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(3579): note: while trying to match the argument list '(std::pair<stock,int>, const std::string)' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(3592): note: see reference to function template instantiation '_InIt std::_Find_unchecked1<_InIt,_Ty>(_InIt,const _InIt,const _Ty &,std::false_type)' being compiled
  9. Thanks, fixed it searching for shares->getName() instead of the object itself and adding: bool operator==(std::string n) { return this->name == n; } not too sure how to compare 2 objects yet, so just looking at the name will do for now
  10. 1>------ Build started: Project: stock-simulator, Configuration: Debug Win32 ------ 1>main.cpp 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\memory(1772): warning C4244: 'argument': conversion from 'double' to 'float', possible loss of data 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\memory(1836): note: see reference to function template instantiation 'std::_Ref_count_obj<_Ty>::_Ref_count_obj<const char(&)[9],double>(const char (&)[9],double &&)' being compiled 1> with 1> [ 1> _Ty=stock 1> ] 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\memory(1837): note: see reference to function template instantiation 'std::_Ref_count_obj<_Ty>::_Ref_count_obj<const char(&)[9],double>(const char (&)[9],double &&)' being compiled 1> with 1> [ 1> _Ty=stock 1> ] 1>c:\users\jonathan\documents\projects\stock-simulator\main.cpp(22): note: see reference to function template instantiation 'std::shared_ptr<stock> std::make_shared<stock,const char(&)[9],double>(const char (&)[9],double &&)' being compiled 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(3579): error C2678: binary '==': no operator found which takes a left-hand operand of type 'stock' (or there is no acceptable conversion) 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(284): note: could be 'bool std::operator ==(const std::error_condition &,const std::error_condition &) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(278): note: or 'bool std::operator ==(const std::error_condition &,const std::error_code &) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(272): note: or 'bool std::operator ==(const std::error_code &,const std::error_condition &) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(266): note: or 'bool std::operator ==(const std::error_code &,const std::error_code &) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\exception(330): note: or 'bool std::operator ==(const std::exception_ptr &,std::nullptr_t) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\exception(325): note: or 'bool std::operator ==(std::nullptr_t,const std::exception_ptr &) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\exception(320): note: or 'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\memory(1621): note: or 'bool std::operator ==<stock>(std::nullptr_t,const std::shared_ptr<stock> &) noexcept' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(3579): note: while trying to match the argument list '(stock, const std::shared_ptr<stock>)' 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(3592): note: see reference to function template instantiation '_InIt std::_Find_unchecked1<_InIt,_Ty>(_InIt,const _InIt,const _Ty &,std::false_type)' being compiled 1> with 1> [ 1> _InIt=stock *, 1> _Ty=std::shared_ptr<stock> 1> ] 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(3601): note: see reference to function template instantiation '_InIt std::_Find_unchecked<stock*,_Ty>(const _InIt,const _InIt,const _Ty &)' being compiled 1> with 1> [ 1> _InIt=stock *, 1> _Ty=std::shared_ptr<stock> 1> ] 1>c:\users\jonathan\documents\projects\stock-simulator\person.hpp(22): note: see reference to function template instantiation '_InIt std::find<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,std::shared_ptr<_Ty>>(_InIt,const _InIt,const std::shared_ptr<_Ty> &)' being compiled 1> with 1> [ 1> _InIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<stock>>>, 1> _Ty=stock 1> ] 1>Done building project "stock-simulator.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  11. #pragma once #include <string> #include <algorithm> #include "stock.hpp" class person { private: std::string name; float cash; std::vector<stock> investments; public: person(std::string name, float cash) { this->name = name; this->cash = cash; } ~person() = default; void buy_stock(std::shared_ptr<stock> shares, int quantity) { cash -= (shares->getPrice() * quantity); //quantity TODO investments.push_back(*shares); } void sell_stock(std::shared_ptr<stock> shares, int quantity) { std::vector<stock>::iterator it = std::find(std::begin(investments), std::end(investments), shares); if (it == std::end(investments)) { std::cout << "\n\nERROR - person.sell_stock tried to sell shares not in portfolio" << std::endl; char exit; std::cin >> exit; } else { ptrdiff_t index = std::distance(std::begin(investments), it); investments.erase(it); cash += (shares->getPrice() * quantity); } } }; The void sell_stock function won't compile but I can't understand the compiler errors that come out, please can someone advise? Is it something to do with the fact I don't have an overload for ==? by the way the ptrdiff_t index = std::distance I know is doing nothing note: see reference to function template instantiation '_InIt std::_Find_unchecked<stock*,_Ty>(const _InIt,const _InIt,const _Ty &)' being compiled 1> with 1> [ 1> _InIt=stock *, 1> _Ty=std::shared_ptr<stock> 1> ]
  12. ICanC

    C++ and SDL loop

    I'm a C++ and SDL beginner and I'm trying to make my first 2d game (blackjack) but I'm struggling to get an elegant loop in place to control the images, animations and input. in my rush and excitement to get graphics on the screen I ended up with a massive function handling everything, which just 2 'player decisions' in, has become a monstrous unmaintainable mess, e.g.: bool decision_one = true; bool decision_two = false; card playercard3 = this->deal(); while (decision_one) { SDL_PollEvent(&e); if (e.type == SDL_KEYDOWN) { if (e.key.keysym.sym == SDLK_h) { while (playersource3.x != playerdestination3.x) { playersource3.x += 10; playersource3.y += 10; SDL_BlitSurface(background, NULL, screenSurface, NULL); SDL_BlitSurface(this->getImage(playercard1.image_number), NULL, screenSurface, &playersource1); SDL_BlitSurface(redback, NULL, screenSurface, &dealersource1); SDL_BlitSurface(this->getImage(playercard2.image_number), NULL, screenSurface, &playersource2); SDL_BlitSurface(this->getImage(dealercard2.image_number), NULL, screenSurface, &dealersource2); SDL_BlitSurface(this->getImage(playercard3.image_number), NULL, screenSurface, &playersource3); SDL_UpdateWindowSurface(window); decision_one = false; decision_two = true; } } else if (e.key.keysym.sym == SDLK_s) { card dealercard1 = this->deal(); SDL_BlitSurface(this->getImage(dealercard1.image_number), NULL, screenSurface, &dealersource1); SDL_UpdateWindowSurface(window); decision_one = false; } } SDL_Delay(80); } So I'm trying break it up into smaller chunks like this: class card { private: std::string name; int value; int suit; public: int image_number; SDL_Rect source; SDL_Rect dest; int vel; card(std::string name, int value, int suit, int image_number); card() {}; ~card() {}; void display() const; }; class blackjack { private: std::vector<card> deck; int minBet; int maxBet; int decks; public: blackjack(int minBet, int maxBet, int decks); ~blackjack() {}; SDL_Surface* getImage(int i); void play_hand(std::shared_ptr<player> p); void getinput(std::shared_ptr<player> p); void create_card(card c, int sourceX, int sourceY, int destX, int destY, int vel); void animate_card(card c); void shuffle(); void reshuffle(); void render(); card deal(); void play(std::shared_ptr<player> p); }; card blackjack::deal() { card dealCard = this->deck[this->deck.size() - 1]; this->deck.pop_back(); return dealCard; } void blackjack::create_card(card c, int sourceX, int sourceY, int destX, int destY, int vel) { SDL_Surface* newCard = this->getImage(c.image_number); entities.push_back(*newCard); c.source.x = sourceX; c.source.y = sourceY; c.dest.x = destX; c.dest.y = destY; c.vel = vel; } void blackjack::animate_card(card c) { while (c.source.x != c.dest.x) { c.source.x += c.vel; c.source.x += c.vel; for (auto i : entities) { SDL_Surface *s = &i; SDL_BlitSurface(background, NULL, screenSurface, NULL); SDL_BlitSurface(s, NULL, screenSurface, //HOW TO GET IMAGE POS HERE?); SDL_UpdateWindowSurface(window); } } } void blackjack::render() { } void blackjack::play(std::shared_ptr<player> p) { this->shuffle(); init_graphics(); this->getinput(p); } The main problem I'm struggling to get my head around is rendering all current images in the game and animating them until they reach their destination. I've created a global variable std::vector<SDL_Surface> entities to hold all images in a hand, and I've gotten to the animate card function above but can't understand how i'm going to pass in the location of all the sdl_surface entities for the blitsurface function - any advise on a simple way to add images, animate them from a source to a dest, and keep track of all of this in one 'render' function would be great
  13. ICanC

    C++ comparing 2 floats

    thanks haegarr, that fixed it. and thanks Bregma, my logic was backwards, when writing that code I was wrongly thinking that 'return 0' indicates a 'true' and successful end to the program, when it is infact true values that indicate failure
  14. hi all, I'm testing with floats for the first time and have discovered that you can't compare 2 floats due to rounding errors. I've read online that you must use an epsilon and test for less than, however I don't seem to be able to get it working with the below code. - when running the code below it it is looping through 6 floats testing for equality with f entered by the player, but even if I put something totally different from the options, the message "not a valid position" is never executed void locks::try_lock() { print("\nEnter lock position: "); float f; cin >> f; int fexists = 1; float epsilon = 0.0000001; // Not sure what the best value to put here is? for (float &a : this->answers) { if ((a - f) < epsilon) { fexists = 0; } } if (fexists == 1) { cout << endl << f << " is not a valid position\n\n"; Sleep(2000); return; } }
  15. ICanC

    C++ buffer or vector issue

    Thanks for all the replies. I've fixed the iterator issue, and the actual cause of the problem was something to do with the messages not being properly conditioned, adding in a few ifs below fixed the issue void merchant::buyfrom(character *a) { print("\nItem to buy: "); string item1; getline(cin, item1); vector<item>::iterator it; unsigned vector_size = this->items.size(); int check = 0; // to handle merchant does not have msg with not enough gold msg for (it = this->items.begin(); it != this->items.end();) { if (it->name == item1) { if (it->price > a->gold) { print("\n\nYou don't have enough gold to buy the "); cout << it->name; check = 1; break; } print("\n\nYou bought "); std::cout << item1; print(" for "); std::cout << it->price; print(" gold"); a->inventory.push_back(*it); a->gold -= it->price; this->gold += it->price; this->items.erase(it); break; } else { ++it; } } if (this->items.size() == vector_size && check == 0) { print("\n\nMerchant does not have a "); std::cout << item1; print(" to sell"); } print("\n\n\n1. Back\n\n\n> "); char c; std::cin >> c; } void merchant::sellto(character *a) { print("\nItem to sell: "); string item1; getline(cin, item1); vector<item>::iterator it; unsigned vector_size = a->inventory.size(); for (it = a->inventory.begin(); it != a->inventory.end();) { if (it->name == item1) { print("\n\nYou sold "); std::cout << item1; print(" for "); std::cout << it->price; print(" gold"); this->items.push_back(*it); a->gold += it->price; this->gold -= it->price; a->inventory.erase(it); break; } else { ++it; } } if (a->inventory.size() == vector_size) { print("\nYou do not have "); std::cout << item1; print(" to sell"); } /* int increase = (it->price * 1.2+1); vector<item>::iterator it2; for (it2 = this->items.begin(); it2 != this->items.end(); ++it2) { if (it2->name == it->name) { it2->price = increase; } } */ print("\n\n\n1. Back\n\n\n> "); char c; std::cin >> c; }
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!