Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ random
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');
}
}
Posted 27-Jan-13 19:22pm
Edited 28-Jan-13 11:50am
v3
Comments
Marius Bancila at 28-Jan-13 17:51pm
   
So, do you have problems or something? You're using srand/rand correctly. Do you get undesired results, or what?
Sergey Alexandrovich Kryukov at 28-Jan-13 18:04pm
   
Why Sleep?!!!!
—SA
LokoLuke at 30-Jan-13 5:57am
   
You may want to take into account that blackjack usually plays 3 decks in a game. Also you should remove previously dealt cards from your total.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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;
} 
 
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
  Permalink  
v2
Comments
Maximilien at 29-Jan-13 13:06pm
   
Nice answer; just enough explanation and code to help without spoon feeding.
H.Brydon at 30-Jan-13 2:52am
   
I agree. +5 for content and delivery. :-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1


You can use rand function in c++

 
i = rand();
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 403
1 Sergey Alexandrovich Kryukov 227
2 Richard Deeming 180
3 ChauhanAjay 170
4 Snehasish_Nandy 161
0 Sergey Alexandrovich Kryukov 6,367
1 OriginalGriff 5,588
2 CPallini 2,473
3 Richard MacCutchan 1,627
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100