Computer Science Canada

trying to make BlackJack

Author:  Viper [ 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];}
        }
}

Author:  Flikerator [ Wed Nov 09, 2005 9:37 am ]
Post 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();

Author:  Viper [ Wed Nov 09, 2005 9:39 am ]
Post subject: 

the deck has 52 it just starts at 0

Author:  Tony [ Wed Nov 09, 2005 9:41 am ]
Post subject: 

but
Quote:

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

so aren't you starting your counter at 1?

Author:  wtd [ Wed Nov 09, 2005 1:34 pm ]
Post 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.

Author:  wtd [ Wed Nov 09, 2005 1:54 pm ]
Post 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;
   }
}

Author:  Geminias [ Wed Nov 09, 2005 2:23 pm ]
Post subject: 

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

Author:  wtd [ Wed Nov 09, 2005 2:48 pm ]
Post subject: 

Yes, I wrote that code.

Author:  Flikerator [ Thu Nov 10, 2005 7:26 pm ]
Post subject: 

wtd wrote:
Yes, I wrote that code.


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

Author:  wtd [ Thu Nov 10, 2005 7:28 pm ]
Post 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.

Author:  [Gandalf] [ Thu Nov 10, 2005 9:00 pm ]
Post subject: 

Phaw, "CompSci God".

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

Author:  wtd [ Thu Nov 10, 2005 9:04 pm ]
Post subject: 

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


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

Author:  md [ Thu Nov 10, 2005 9:31 pm ]
Post 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.

Author:  wtd [ Thu Nov 10, 2005 9:56 pm ]
Post subject: 

Indeed.

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

Author:  Flikerator [ Fri Nov 11, 2005 5:02 pm ]
Post 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?

Author:  wtd [ Fri Nov 11, 2005 5:14 pm ]
Post subject: 

Flikerator wrote:
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?


Almost certainly, but get used to doing it yourself.

Author:  [Gandalf] [ Fri Nov 11, 2005 7:31 pm ]
Post subject: 

Please do. For me, one of the most important things about coding is indenting well. On your own.

If you wish to not follow my advice, I don't see how you could have a hard time looking, I haven't seen an editor that doesn't have auto-indent.

Author:  md [ Fri Nov 11, 2005 9:51 pm ]
Post subject: 

The only auto-indentation I use is the ones where each new line has the same number of blank spaces before the first character as the line before. It's really handy as even though I keep track of my own indentation putting 8 or 12 spaces at the front of each line slows typing quite a bit...

Author:  wtd [ Fri Nov 11, 2005 10:06 pm ]
Post subject: 

Cornflake wrote:
The only auto-indentation I use is the ones where each new line has the same number of blank spaces before the first character as the line before. It's really handy as even though I keep track of my own indentation putting 8 or 12 spaces at the front of each line slows typing quite a bit...


So only use 3 or 4 spaces as your unit of indentation.

Author:  [Gandalf] [ Fri Nov 11, 2005 10:29 pm ]
Post subject: 

If only they made a convention, using tab would be a lot easier Sad.

Author:  md [ Fri Nov 11, 2005 10:57 pm ]
Post subject: 

wtd wrote:
Cornflake wrote:
The only auto-indentation I use is the ones where each new line has the same number of blank spaces before the first character as the line before. It's really handy as even though I keep track of my own indentation putting 8 or 12 spaces at the front of each line slows typing quite a bit...


So only use 3 or 4 spaces as your unit of indentation.


4 is my unit, however there are times when I get to 3 levels of indentation, and occasionally even 4. 3*4 is 12 Wink Also many editors can be set up to replace tabs with a set number of spaces... and if they can't it's not hard to write a short program to change it all over for you...

Author:  wtd [ Fri Nov 11, 2005 11:26 pm ]
Post subject: 

Cornflake wrote:
wtd wrote:
Cornflake wrote:
The only auto-indentation I use is the ones where each new line has the same number of blank spaces before the first character as the line before. It's really handy as even though I keep track of my own indentation putting 8 or 12 spaces at the front of each line slows typing quite a bit...


So only use 3 or 4 spaces as your unit of indentation.


4 is my unit


Excellent. I was worried you meant you used 8 or even twelve spaces for one unit of indentation. I've encountered this before.

Try viewing that code in an 80x25 console.


: