初学者C ++随机数生成器 [英] Beginner C++ Random Number Generator

查看:168
本文介绍了初学者C ++随机数生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者,我希望有一个随机数生成器的代码,但我的代码不符合我希望它遵循的...请帮助! 。这是到目前为止...请帮助任何更正,改进等。



I am a beginner and I want to have a code with a random number generator, but my code does not follow what I would like it to follow.... PLEASE HELP! . This is what have so far... please help with any corrections, improvements, etc.

#include <iostream>;
#include <cstdlib> //drand function
#include <ctime> // time function
#include <windows.h> //Sleep function
using namespace std;
/* *****************
Oscar, Zamora
1/20/13
This program was made
so that people can 
understand and 
experience how video
games are made using
the language of C++
***************** */

//start( float radius); //Function Prototype
//float volumes( float radius); // Function Prototype

int main()
{
int num;
char choicei = 'i';
char choiceh = 'h';
char choices = 's';
char choicec = 'c';
char choiceb = 'b';

// Suspend program for 2 seconds

Sleep(2000);
//seed random number ganerator using the system clock
srand(static_cast<unsigned int> (time(0)));
//generate a random number between 1 and 13
num = rand() % 13 + 1;

// Welcome user to the introduction screen
cout << " BLACKJACK\n\n\n";
cout << " Thank youdsfafafd for playing this game!\n\n";
cout << " Press 'b' to Start the game\n";
cout << " Press 'i' for intructions on the game\n";
cout << " Press 'c' for credits on the game\n";

// Show the instructions
if (choicei == 'i')
{
  cout << " The game you will be playing is called 'Blackjack' or 'twenty-one'.\n This is a multiplayer games in which one of the players want to get cards";
}

if (choicec == 'c')
{
  cout << " The writer, producer, developer, and artist of this game is Oscar Zamora";
}

if (choiceb == 'b')
{
  do
  {
    //Tell the user what card they got
    cout << " You got" << num;

    // Add up all the user"s cards and print
    // Ask them if the y want to hit or stay
    cout << " Would you like to get hit ('h') or stay ('s')";

    //If they want to hit, loop back to generating a random number again otherwise let the computer get a hand

  }while (choices == 's');
}
}

推荐答案

您似乎对许多事情感到困惑,这些事情大多与程序流程和编程逻辑有关:



1.您初始化了一些 char 变量并且永远不会改变它们,但是,la你测试它们的价值。显然,您打算将用户输入与存储在这些变量中的值进行比较?从程序的其余部分来看,这些变量根本不需要,或者应该被定义为常量并用作比较输入的值。 (但请参阅下面的第3点)



2.初始化num一次,然后永远不要更改它。显然你的意图是每次用户请求新卡时重新计算它,i。即你需要在主循环中调用随机数函数。



3.你反复向用户打印指令,请求输入,但你从来没有读过那个输入!你甚至没有定义一个变量来保存输入。



我建议你按如下方式重写该程序:



You appear to be confused about a number of things, most related to program flow and programming logic:

1. You intialize a number of char variables and never change them, but then, later, you test their value. Apparently what you intended to do is compare the user input against the values you stored in these variables? Judging by the rest of the program, these variables aren''t needed at all, or should be defined as constant and used as the value to compare the input against. (but see point 3 below)

2. You initialize num once, and then never change it. Apparently your intention is to recalculate it every time the user requests a new card, i. e. you need to call the random number function within the main loop.

3. You repeatedly print out instructions to the user, requesting input, but you never read that input! You didn''t even define a variable to hold that input.

I suggest you rewrite that program as follows:

// recognized input token list:
const char begin_game = 'b';
const char end_game = 'e';
const char instructions = 'i';
const char credits = 'c';
const char deal_a_card = 'd'; // wouldn't that be more appropriate than 'hit'?
const char stay_hand = 's';

void print_intro() {
   // print your introductory text here ...
}

void print_instructions() {
   // print your instructions here
}

void print_credits() {
   // print your credits here ...
}

void deal() {
   // generate a new card, print it, and store for later evaluation ...
}

void stay() {
   // player is done, now generate the hand for the computer ...
}

bool process_input(char choice) {
   bool continue = true; // assume that you wish to continue later
   if (choice == end_game) {
      continue = false;
   }
   else { 
      switch (choice) { // now react to the input
         case begin_game:
            // initialize scores and hands ...
            break;
         case credits:
            print_credits();
            break;

         // and all the other cases, you get the picture ...

         default:
            // print some error message (invalid input)
      }
   }
   return continue;
}

int main() {
   int card=0; // use a name that describes the meaning rather than the type
   char choice=' ';

   intro();

   do { // this is the main game loop
      print_instructions(); // request input and print options
      choice = getc(); // read user input
   while (process_input(choice));

   return 0;
} 





我故意为琐碎的任务介绍了很多功能 - 故意!它将使主循环更短,并且您将有更多的时间来稍后添加更多命令或更改现有命令,而不会浪费大量时间来挖掘代码。当你去参加更复杂的游戏时,这也会很有用。



主游戏循环非常简单:

1.呈现用户有一些选择

2.获取用户输入

3.处理输入

重复直到处理返回false



I deliberately introduced a whole lot of functions for trivial tasks - on purpose! It will make the main loop much shorter, and you will have an easier time to later add more commands or change existing ones, without losing a lot of time digging through your code. This will also be helpful when you go for more complex games.

The main game loop is pretty simple:
1. present the user with some choices
2. get the user input
3. process the input
Repeat until processing returns false





你可以在c ++中使用rand函数





i = rand();


You can use rand function in c++


i = rand();


这篇关于初学者C ++随机数生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆