添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

So I have finished the UFO project and got it fully working, but I wanted to add a bit more to it and have been working on trying to create a pool of codewords to use from a function vector.

github.com

AceKokuren/LearnCoding/blob/ufo/Learn Cpp/UFO Game/ufo.cpp

#include <iostream>
#include <vector>
#include "ufo_functions.hpp"
int main() {
    greet();
    std::string codeword, answer;
    int misses = 0;
    std::vector<char> incorrect;
    bool guess = false;
    char letter;
    solutions(codeword, answer);
    std::cout << codeword << " " << answer;
    while (misses < 7 && answer != codeword) {
        std::cout << "\nPlease enter your guess: ";
        std::cin >> letter;
        for (int i = 0; i < codeword.length(); i++) {
  This file has been truncated. show original
    

AceKokuren/LearnCoding/blob/ufo/Learn Cpp/UFO Game/ufo_functions.cpp

#include <iostream>
#include <vector>
#include <ctime>
// Define functions
void solutions(std::string codeword, std::string answer) {
  std::vector<std::string> codewords{"chicken", "abduction", "cheese", "humans"};
  srand(time(NULL)); 
  int random = rand() % 2;
  switch (random) {
    case 0:
     codeword = codewords[0];
     for (int i = 0; i < codeword.size(); i++) {
      answer = "_";
     std::cout << codeword << " " << answer << std::endl; 
     break;
  This file has been truncated. show original
    

AceKokuren/LearnCoding/blob/ufo/Learn Cpp/UFO Game/ufo_functions.hpp

void greet();
void display_misses(int misses);
void end_game (std::string codeword, std::string answer);
void display_status (std::vector<char> incorrect, std::string answer);
void solutions(std::string codeword, std::string answer);

That is what I have but when executed, the program throws up the win condition instantly, and the answer only displays one underscore, but I want the answer to be the size of the codeword length.

I know it is something in the function but I can’t crack it.

Thanks.

acekokuren:
    std::string codeword, answer; //both codeword and answer are declared here, and never changed
    int misses = 0;
    std::vector<char> incorrect;
    bool guess = false;
    char letter;
    solutions(codeword, answer); //this calls your solutions function, but the function doesn't return anything, so after it prints a few things to the console, nothing else happens
    std::cout << codeword << " " << answer; //This is only printing the space because codeword and answer have not been initialized you can see this by adding something that you can see. Try adding the line below:
  std::cout << "codeword: " << codeword << " answer: " << answer;
    while (misses < 7 && answer != codeword) { //since neither has been changed, answer does equal codeword, so this loop never runs

It looks like perhaps you want your solutions function to return values for codeword and answer to be assigned to.

So, what you need is for the solutions function to return 2 values. One to be assigned to codeword, and one to be assigned to answer. That can be a little tricky. There a a few ways to do it. Probably the simplest would be to return an array or vector with 2 values. Another way would be to use a struct. You’ll have to google that to see how to set it up and use it. If you returned an array, you could initialize an empty array of size 2, assign the result of calling the function to the array:

gameValues = solutions(); //no arguments or parameters needed
codeword = gameValues[0];
answer = gameValues[1];

Also your solutions function doesn’t need a switch at all. You can simply assign the codeword by it’s index from your vector of possible codewords, and then use a single for loop to assign the _'s. You probably also want to use += instead of = when assigning _'s, so that you add them on. You only get one _ because you repeatedly do answer = "_";. You are only reassigning the same value of a single _ each time instead of adding an additional one.

I modified your code a bit to make a working version of what you are attempting. I used a struct. If you really, really want to see it, you can go to this repl. I’d strongly suggest you try it on your own first using an array. Feel free to ask additional questions. I may not be able to get to them until tomorrow, but perhaps someone else will chime in.

So I have managed to create the code that does what I want it to do, however, this is in the body of main rather than in a function.

// Creates a vector of random solutions, and creates _'s in answer = to codeword length
    std::vector<std::string> codewords{"chicken", "abduction", "cheese", "humans"};
    srand(time(NULL));
    int random = rand() % codewords.size();
    codeword = codewords[random];
    for (int i = 0; i < codeword.size(); i++) {
        answer += "_";

I have tried several times to place this block of code in a function and then call the function but no matter how I write it I can’t seem to get the function to return the values correctly.

If you were to put codeword and answer in a vector, then return that vector to a vector variable in your main function, you could assign the elements to variables in your main function:

//something like:
std::vector<std::string> gameVals = solutions(); //assuming the code above is in a function called solutions
// the solutions function would have to return a vector such as {"chicken", "_______"}
std::string codeword = gameVals[0];
std::string answer = gameVals[1];
              

Ok so I have managed to get a working the function working, along with calling it, but I feel like I should be able to streamline it in the function a bit. Just let me know if there is a way to reduce the lines or if this is the only way to make it work

std::vector<std::string> solutions() {
  std::vector<std::string> gameval;
  std::string codeword, answer;
  std::vector<std::string> codewords{"chicken", "abduction", "cheese", "humans"};
  srand(time(NULL));
  int random = rand() % codewords.size();
  codeword = "";
  codeword = codewords[random];
  for (int i = 0; i < codeword.size(); i++) {
    answer += "_";
  gameval.push_back(codeword);
  gameval.push_back(answer);
  return gameval;

And this is how I’m calling it in main

std::vector <std::string> gameval = solutions();
     codeword = gameval[0];
     answer = gameval[1];
              

Yeah, you could reduce the number of lines a bit if you wanted to. You could, for example, declare your vector gameval right before you return it, and initialize it on the same line like so:

std::vector<std::string> solutions() {
  std::string codeword, answer;
  std::vector<std::string> codewords{"chicken", "abduction", "cheese", "humans"};
  srand(time(NULL));
  int random = rand() % codewords.size();
  codeword = codewords[random];
  for (int i = 0; i < codeword.size(); i++) {
    answer += "_";
  std::vector<std::string> gameval {codeword, answer};
  return gameval;
              

Thank you for your help, I know have the fully working game with the function.

This is my full solution to the project!

github.com

AceKokuren/LearnCoding/blob/master/Learn Cpp/UFO Game/ufo.cpp

#include <iostream>
#include <vector>
#include <ctime>
#include "ufo_functions.hpp"
int main() {
    greet();
    bool play_again = true;
    while (play_again) {
     std::string codeword, answer;
     int misses = 0;
     std::vector<char> incorrect;
     bool guess = false;
     char letter;
     std::string yes_no;
  This file has been truncated. show original
    

AceKokuren/LearnCoding/blob/master/Learn Cpp/UFO Game/ufo_functions.hpp

void greet();
void display_misses(int misses);
void end_game (std::string codeword, std::string answer);
void display_status (std::vector<char> incorrect, std::string answer);
std::vector<std::string> solutions();

AceKokuren/LearnCoding/blob/master/Learn Cpp/UFO Game/ufo_functions.cpp

#include <iostream>
#include <vector>
#include <ctime>
// Define functions
void greet() {
   std::cout << "=====================\n";
   std::cout << "    UFO: The Game\n";
   std::cout << "=====================\n";
   std::cout << "Instructions: Save your friend from alien\n";
   std::cout << "abduction by guessing the letters\n";
   std::cout << "in the codeword!" << std::endl;  
std::vector<std::string> solutions() {
  std::string codeword, answer;
  std::vector<std::string> codewords{"chicken", "abduction", "cheese", "humans"};
  srand(time(NULL));
  int random = rand() % codewords.size();
  codeword = codewords[random];
  This file has been truncated. show original