mercredi 20 mars 2019

Using srand(time(NULL)) gives same output each time my program is run (not in loop)

I've been following lessons in Codecademy to learn C++. I've come to the last project where I learned to make a program that works like hangman. I wanted to add a list of words from which the program chooses a new word each time the program is run. (line 5)

#include "ufo.hpp"

int main() {

  srand (time(NULL));

  std::cout << "=============\n";
  std::cout << "UFO: The Game\n";
  std::cout << "=============\n";
  std::cout << "Instructions: save your friend from alien\nabduction by guessing the letters in the\ncodeword.\n";

  for (int i = 0; i < codeword.size(); i++) {
      answer.append("_");
  }

  while (answer != codeword && misses <= 6) {
      take_turn();
  }

  if (answer == codeword) {
      std::cout << "Hooray! You saved the person and earned a medal of honor!\n";
  } else {
      std::cout << "Oh no! The UFO just flew away with another person!\n";
      std::cout << "The codeword was: " << codeword << "\n";
  }

  return 0;
}

In the code above, I used srand(time(NULL)) in the main function. The rand_number variable can be found in the following header: (line 11)

#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>

void display(int misses);

// Declare variables

std::vector<std::string> codewords = {"camera", "computer", "table", "desk"};
int rand_number = rand() % codewords.size();

std::string codeword = codewords[rand_number];
std::string answer;
int misses = 0;
std::vector<char> incorrect;
bool guess = false;
char letter;

// Define take_turn()

void take_turn() {

  display(misses);

  std::cout << "Please enter your guess: ";
  std::cin >> letter;

  for (int i = 0; i < codeword.size(); i++) {
    if (letter == codeword[i]) {
      answer[i] = letter;
      guess = true;
    }
  }

  if (guess) {
    std::cout << "Correct!\n";
  } else {
    std::cout << "Incorrect! The tractor beam pulls the person in further.\n";
    incorrect.push_back(letter);
    misses++;
  }

  std::cout << "==========================\n";

  std::cout << "Incorrect Guesses:\n";
  for (int i = 0; i < incorrect.size(); i++) {
    std::cout << incorrect[i] << " ";
  }

  std::cout << "\nCodeword:\n";

  for (int i = 0; i < answer.size(); i++) {
    std::cout << answer[i] << " ";
  }

  std::cout << "\n";

  guess = false;
}



void display(int misses) {

if (misses == 0 || misses == 1) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /     \\        (  Send help! ) \n";
std::cout << "             /   0   \\      / `-----------'  \n";
std::cout << "            /  --|--  \\    /                 \n";
std::cout << "           /     |     \\                     \n";
std::cout << "          /     / \\     \\                   \n";
std::cout << "         /               \\                   \n";

} else if (misses == 2) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /  0  \\        (  Send help! ) \n";
std::cout << "             / --|-- \\      / `-----------'  \n";
std::cout << "            /    |    \\    /                 \n";
std::cout << "           /    / \\    \\                    \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

} else if (misses == 3) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /--|--\\        (  Send help! ) \n";
std::cout << "             /   |   \\      / `-----------'  \n";
std::cout << "            /   / \\   \\    /                \n";
std::cout << "           /           \\                     \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

} else if (misses == 4) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /  |  \\        (  Send help! ) \n";
std::cout << "             /  / \\  \\      / `-----------' \n";
std::cout << "            /         \\    /                 \n";
std::cout << "           /           \\                     \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

} else if (misses == 5) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              / / \\ \\        (  Send help! )\n";
std::cout << "             /       \\      / `-----------'  \n";
std::cout << "            /         \\    /                 \n";
std::cout << "           /           \\                     \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

} else if (misses == 6) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /     \\        (  Send help! ) \n";
std::cout << "             /       \\      / `-----------'  \n";
std::cout << "            /         \\    /                 \n";
std::cout << "           /           \\                     \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

}
}

I haven't been coding for too long so tips are always welcome




Aucun commentaire:

Enregistrer un commentaire