Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 trying to make BlackJack
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Viper




PostPosted: Wed Nov 09, 2005 9:31 am   Post subject: trying to make BlackJack

i have no idea what so ever on what is wrong Sad
heres my code(keep in mind im still new to C++

DevC++<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

code:
#include<iostream>
#include<ctime>
#include<windows.h>

main()
{
int playerHand[4],comHand[4],deck[51];
int i,a;
/*........................................................Deck maker*/
for(i=0;i<52;++i){
deck[i]=i;
}
/*...........................................end deck maker\hand maker*/
for (i=0;i<4;++i){
srand((unsigned)time(NULL));
playerHand[i]=deck[rand()%51];
}
for(i=0;i<4;++i){
srand((unsigned)time(NULL));
comHand[i]=deck[rand()%51];
}
for (i=0;i<4;++i){
for (a=0;a<4;++a){
    if (comHand[a]==playerHand[i]){
                                  ++comHand[a];
                                  }
                                  }
                                  }
/*...........................................end hand maker\Display*/
std::cout<<"Player Hand\n";
Sleep(1000);
for (i=0;i<2;++i){
    if (i==1){
             std::cout<<"Computer Hand\n";
             }
    for (a=0;a<4;++a){
        std::cout<<playerHand[a];}
        }
}
Sponsor
Sponsor
Sponsor
sponsor
Flikerator




PostPosted: Wed Nov 09, 2005 9:37 am   Post subject: (No subject)

Put it in code tags, makes it easier to read Laughing

Why is the deck 51 and not 52? EDIT - Oh yah, arrays are dif in C++ then Turing. They start at 0 Evil or Very Mad

EDIT-

put this at the end of your program to see the output, instead of having a delay. It asks the user for input, and stores it no where.

std::cin.get();
Viper




PostPosted: Wed Nov 09, 2005 9:39 am   Post subject: (No subject)

the deck has 52 it just starts at 0
Tony




PostPosted: Wed Nov 09, 2005 9:41 am   Post subject: (No subject)

but
Quote:

for(i=0;i<52;++i)

so aren't you starting your counter at 1?
wtd




PostPosted: Wed Nov 09, 2005 1:34 pm   Post subject: (No subject)

You declare:

code:
deck[52]


And you get an array with 52 slots... that just happens to start at 0 and end at 51.

code:
deck[51]


Gets you an array with only space for 51 cards.
wtd




PostPosted: Wed Nov 09, 2005 1:54 pm   Post subject: (No subject)

Also, for the love of Bjarne Stroutrop... use some decent variable names!

c++:
#include<iostream>
#include<ctime>

const int HAND_SIZE = 4;

int main()
{
   int player_hand[HAND_SIZE],
       dealer_hand[HAND_SIZE],
       deck[52];

   bool cards_taken[52];

   int i, a;

   // Initialize deck
   for (int current_card_index = 0;
            current_card_index < 52;
          ++current_card_index)
   {
      deck[current_card_index] = current_card_index;
      cards_taken[current_card_index] = false;
   }

   // Initialize player hand
   for (int current_card_index = 0;
            current_card_index < HAND_SIZE;
          ++current_card_index)
   {
      // Seed random number generator
      srand((unsigned)time(NULL));

      int selected_card;

      // Pick a card.  Do so until you've chosen one that
      // hasn't already been selected.
      do
      {
         selected_card = rand() % 52;
      }
      while (cards_taken[selected_card]);

      // Yay!  You got a good one
      player_hand[current_card_index] = selected_card;
      cards_taken[selected_card] = true;
   }

   // Initialize dealer hand
   for (int current_card_index = 0;
            current_card_index < HAND_SIZE;
          ++current_card_index)
   {
      // Seed random number generator
      srand((unsigned)time(NULL));

      int selected_card;

      // Pick a card.  Do so until you've chosen one that
      // hasn't already been selected.
      do
      {
         selected_card = rand() % 52;
      }
      while (cards_taken[selected_card]);

      // Yay!  You got a good one
      dealer_hand[current_card_index] = selected_card;
      cards_taken[selected_card] = true;
   }

   // Display

   std::cout << "Player Hand" << std::endl;

   for (int current_card_index = 0;
            current_card_index < HAND_SIZE;
          ++current_card_index)
   {
      std::cout << "   " << player_hand[current_card_index]
                << std::endl;
   }

   std::cout << "Dealer Hand" << std::endl;

   for (int current_card_index = 0;
            current_card_index < HAND_SIZE;
          ++current_card_index)
   {
      std::cout << "   " << dealer3_hand[current_card_index]
                << std::endl;
   }
}
Geminias




PostPosted: Wed Nov 09, 2005 2:23 pm   Post subject: (No subject)

who writes all these codes you post wtd? you? lol
wtd




PostPosted: Wed Nov 09, 2005 2:48 pm   Post subject: (No subject)

Yes, I wrote that code.
Sponsor
Sponsor
Sponsor
sponsor
Flikerator




PostPosted: Thu Nov 10, 2005 7:26 pm   Post subject: (No subject)

wtd wrote:
Yes, I wrote that code.


Pwned! When did you get so good at Coding anyway wtd?
wtd




PostPosted: Thu Nov 10, 2005 7:28 pm   Post subject: (No subject)

Flikerator wrote:
wtd wrote:
Yes, I wrote that code.


Pwned! When did you get so good at Coding anyway wtd?


I'm good? Nawww... decent, but not good. There are many many people who are way beyond me. Smile

And I've taught myself how to code, by watching people who are better than me, seeing what they're doing differently, and then trying to understand why they're doing things differently.
[Gandalf]




PostPosted: Thu Nov 10, 2005 9:00 pm   Post subject: (No subject)

Phaw, "CompSci God".

Guess who "people who are better than me" are? You Smile.
wtd




PostPosted: Thu Nov 10, 2005 9:04 pm   Post subject: (No subject)

[Gandalf] wrote:
Phaw, "CompSci God".


Nawww... that just means I like to post.
md




PostPosted: Thu Nov 10, 2005 9:31 pm   Post subject: (No subject)

back on the original topic... kinda... did anyone else notice the horrible formating in the original code?! Seems to me that the first thing people should be taught is how to write readable code.
wtd




PostPosted: Thu Nov 10, 2005 9:56 pm   Post subject: (No subject)

Indeed.

Indentation is your friend. Don't use tab, though, since it doesn't render consistently across different computers.
Flikerator




PostPosted: Fri Nov 11, 2005 5:02 pm   Post subject: (No subject)

Cornflake wrote:
back on the original topic... kinda... did anyone else notice the horrible formating in the original code?! Seems to me that the first thing people should be taught is how to write readable code.


Im just getting used to indenting myself. Since turing had a built in indentation [F2] I rarely did it myself.

Are there any text editors with a C/C++/C# Indentation in them, that you know of?
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 22 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: