Author Topic: In serious need of C++ help!  (Read 813 times)

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
In serious need of C++ help!
« on: May 08, 2005, 04:02:36 am »
..................................................................................................................................................................................
I spent the past couple of hours (i\'m still a n00b) making a program that plays blackjack with you.

I got up to the part where the player draws the cards. Before implementing the code which will allow the dealer to draw the cards I decided to see how it works so far. And (not to my surprise) it did\'nt work. I solved all the errors I could, but I have no clue what the rest are talking about!

Here is the code:
P.S. Ignore the periods, I only put those there so the code does\'nt overlap on forum.
Code: [Select]

..................................................................................................................................................................................
// A program that plays Black Jack with you.


#include
#include
#include
using namespace std;
#define SCORE_SYSTEM scoreSystem(pickedCard, score, score2)
#define CHECK check(pickedCard, usedCards, r)
#define DEAL deal(deck, usedCards, r, playerScore, playerScore2, dealerScore, dealerScore2, score, score2)

int main()
{
  int cash, bet, pot;
 int score, score2;
  int playerScore = 0, playerScore2 = 0;
  int dealerScore = 0, dealerScore2 = 0;
  int pickedCard;
 int deck[51];
   int usedCards[51];
  int r;
  char question[15];

  int scoreSystem(pickedCard, score, score2) {  //Scoring algorithm [begining]
        score = 1, score2 = 1; //Set scores 1&2
     if(pickedCard >= 48 && pickedCard <= 51) score = 1, score2 = 11; //If ace is selected
       else { //If ace is not selected
         for(int a=0, int b=0; b<48;) {
              score++, score2++;
              b = a+3;
                if(pickedCard >= a && pickedCard <= b) break;
               a += 4;
         }
       }
       return score, score2;
   } // Scoring algorithm [end]

    int check(pickedCard, usedCards, r) { //Checking [begining]
     for(int i=0; i<52; i++) {
           if(pickedCard == usedCards[i]) {r=1; break;} // if card was used
            else { // if card was not used
              r=0;
            }
       }
       for (int x=0; x<52; x++) { // Keep Track of picked cards.
           if(x==99) {usedCards[i]=pickedCard; break;}
         else;
       }
       return r;
   }// Checking [end]
 
    int deal(deck, usedCards, r, playerScore, playerScore2, //Deal hands
            dealerScore, dealerScore2, score, score2) {     //[begining]
        for(int i=0;i<=2;){ //Deal to player
            pickedCard = rand()%51;
         CHECK; // Check if card was already picked
          if(r) continue;
         else;
           SCORE_SYSTEM;
          i++;
            playerScore += score;
           playerScore2 += score2;
     }
       for(int x=0;x<=2;){ //Deal to dealer
            pickedCard = rand()%51;
         CHECK; // Check if card was already picked
          if(r) continue;
         else SCORE_SYSTEM; x++;
         dealerScore += score; // Add to player score
            dealerScore2 += score2;
     }
       return playerScore, playerScore2, dealerScore, dealerScore2;
    }




   for(int i=0; i<52; i++) deck[i]=i;  //Load up deck

  cout << \"******************************\\n\";
    cout << \"*Game:          BlackJack     *\\n\"; // Header
 cout << \"*Version:       Alpha Test    *\\n\";
   cout << \"*Developer: Egor Poblaguev*\\n\";
   cout << \"******************************\\n\\n\\n\";

  for(;;) {  // Begin Game
        srand(GetTickCount()); //Generate random seed
       for(int i=0; i<52; i++) usedCards[i]=99; // Set usedCards to default
   
        cash = 100;     // Set initial cash
     pot = bet * 2;  // Define pot

       cout << \"*****************\\n\";
     cout << \"Total cash: \" << cash << \"\\n\";
      cout << \"Place bet: \";
        cin >> bet;
     cout << \"*****************\\n\\n\";
   
        DEAL; // Deal hands

     do { // Player\'s Turn
            cout << \"*************************\\n\";
         cout << \"Your Score: \" << playerScore << \" || \" << playerScore2 << \"\\n\"; // Display Score
          cout << \"Current Pot: \" << pot << \"\\n\"; // Display current pot
           cout << \"Draw again? (\'y\' or \'n\') \";
          gets(question);
         cout << \"\\n*************************\\n;
                if(question == \'y\') {
                 for(;;){ //Deal to player
                       pickedCard = rand()%51;
                     CHECK; // Check if card was already picked
                      if(r) continue; //If card was used
                      else SCORE_SYSTEM;
                      playerScore += score;
                       playerScore2 += score2;
                     break;
                  }
               }
               else;
       }while(question != \'n\');
  }
}


And here is what the compiler is telling me:

Code: [Select]

..................................................................................................................................................................................
Compiling...
BlackJack.cpp
G:\\C++Projects\\BlackJack\\BlackJack.cpp(23) : error C2448: \'\' : function-style initializer appears to be a function definition
G:\\C++Projects\\BlackJack\\BlackJack.cpp(27) : warning C4518: \'int \' : storage-class or type specifier(s) unexpected here; ignored
G:\\C++Projects\\BlackJack\\BlackJack.cpp(27) : warning C4228: nonstandard extension used : qualifiers after comma in declarator list are ignored
G:\\C++Projects\\BlackJack\\BlackJack.cpp(37) : error C2448: \'\' : function-style initializer appears to be a function definition
G:\\C++Projects\\BlackJack\\BlackJack.cpp(52) : error C2448: \'\' : function-style initializer appears to be a function definition
G:\\C++Projects\\BlackJack\\BlackJack.cpp(55) : error C2065: \'check\' : undeclared identifier
G:\\C++Projects\\BlackJack\\BlackJack.cpp(58) : error C2065: \'scoreSystem\' : undeclared identifier
G:\\C++Projects\\BlackJack\\BlackJack.cpp(98) : error C2065: \'deal\' : undeclared identifier
G:\\C++Projects\\BlackJack\\BlackJack.cpp(106) : error C2001: newline in constant
G:\\C++Projects\\BlackJack\\BlackJack.cpp(107) : error C2143: syntax error : missing \';\' before \'if\'
G:\\C++Projects\\BlackJack\\BlackJack.cpp(107) : error C2446: \'==\' : no conversion from \'int\' to \'char *\'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
G:\\C++Projects\\BlackJack\\BlackJack.cpp(107) : error C2040: \'==\' : \'char [15]\' differs in levels of indirection from \'int\'
G:\\C++Projects\\BlackJack\\BlackJack.cpp(119) : error C2446: \'!=\' : no conversion from \'int\' to \'char *\'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
G:\\C++Projects\\BlackJack\\BlackJack.cpp(119) : error C2040: \'!=\' : \'char [15]\' differs in levels of indirection from \'int\'
Error executing cl.exe.

BlackJack.obj - 12 error(s), 2 warning(s)
« Last Edit: May 14, 2005, 10:34:55 pm by RussianVodka »



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.

MaidenIndigo

  • Hydlaa Citizen
  • *
  • Posts: 222
  • 藍子
    • View Profile
(No subject)
« Reply #1 on: May 08, 2005, 04:38:39 am »
Um...unless I\'m really missing something here, your functions are in int main().  I don\'t think functions can be in int main()...then again, I\'m a novice and I\'ve seen some pretty weird stuff that\'s apparently legal.

Code: [Select]
G:\\C++Projects\\BlackJack\\BlackJack.cpp(107) : error C2446: \'==\' : no conversion from \'int\' to \'char *\'This should be (question[variable] == \'n\'), not (question == \'n\').  You can\'t compare an array variable to a primitive data type (legally, though some compilers allow it), you have the include the index of whatever it is you\'re comparing.  question[variable] is a char data type, or more specifically a pointer to a char in memory, while question is a placeholder for a bunch of pointers...that made little sense, but for the time being keep in mind that (char question) != (char question[]).  Why would you want to compare the entire array to a char constant, anyway?  Doesn\'t that ruin the point of an array...? (unless I\'m missing something)

/me waits for someone more skilled to debunk this entire post

~Indi
« Last Edit: May 08, 2005, 04:39:56 am by MaidenIndigo »

Seytra

  • Forum Addict
  • *
  • Posts: 2052
  • No system can compensate lack of common sense.
    • View Profile
(No subject)
« Reply #2 on: May 08, 2005, 04:59:04 am »
The main problem is that you try to define functions within another function (the main() function, in this case), as MaidenIndigo pointed out correctly. Thus, you must move them out. The rest have been some general syntax errors which you would probably have been able to fix yourself, but I wanted to make sure the code at least compiles so I fixed them, too. These fixes are what seems to be appropriate after only glancing at it, but might not enable the code to actually work, which I couldn\'t test for lack of the remaining code. :)

Anyway, here is a compiling version:
Code: [Select]

// A program that plays Black Jack with you.





#include
//#include
#include
using namespace std;
#define SCORE_SYSTEM scoreSystem(pickedCard, score, score2)
#define CHECK check(pickedCard, usedCards, r)
#define DEAL deal(deck, usedCards, r, playerScore, playerScore2, dealerScore, dealerScore2, score, score2)

    int scoreSystem(int pickedCard, int score, int score2) {  //Scoring algorithm [begining]
        score = 1, score2 = 1; //Set scores 1&2
     if(pickedCard >= 48 && pickedCard <= 51) score = 1, score2 = 11; //If ace is selected
       else { //If ace is not selected
         for(int a=0, b=0; b<48;) {
              score++, score2++;
              b = a+3;
                if(pickedCard >= a && pickedCard <= b) break;
               a += 4;
         }
       }
       return score, score2;
   } // Scoring algorithm [end]


    int check(int pickedCard, int usedCards[], int r) { //Checking [begining]
       for(int i=0; i<52; i++) {
           if(pickedCard == usedCards[ i ]) {r=1; break;} // if card was used
          else { // if card was not used
              r=0;
            }
       }
       for (int x=0; x<52; x++) { // Keep Track of picked cards.
           if(x==99) {usedCards[x]=pickedCard; break;}
         else;
       }
       return r;
   }// Checking [end]
 
    int deal(int deck[], int usedCards[], int r, int playerScore, int playerScore2, //Deal hands
            int dealerScore, int dealerScore2, int score, int score2) {     //[begining]
int pickedCard;
     for(int i=0;i<=2;){ //Deal to player
            pickedCard = rand()%51;
         CHECK; // Check if card was already picked
          if(r) continue;
         else;
           SCORE_SYSTEM;
          i++;
            playerScore += score;
           playerScore2 += score2;
     }
       for(int x=0;x<=2;){ //Deal to dealer
            pickedCard = rand()%51;
         CHECK; // Check if card was already picked
          if(r) continue;
         else SCORE_SYSTEM; x++;
         dealerScore += score; // Add to player score
            dealerScore2 += score2;
     }
       return playerScore, playerScore2, dealerScore, dealerScore2;
    }



int main()
{
    int cash, bet, pot;
 int score, score2;
  int playerScore = 0, playerScore2 = 0;
  int dealerScore = 0, dealerScore2 = 0;
  int pickedCard;
 int deck[51];
   int usedCards[51];
  int r;
  char question[15];












  for(int i=0; i<52; i++) deck[ i ]=i;  //Load up deck


    cout << \"******************************\\n\";
    cout << \"*Game:          BlackJack     *\\n\"; // Header
 cout << \"*Version:       Alpha Test    *\\n\";
   cout << \"*Developer: Egor Poblaguev*\\n\";
   cout << \"******************************\\n\\n\\n\";


  for(;;) {  // Begin Game
        srand(GetTickCount()); //Generate random seed
       for(int i=0; i<52; i++) usedCards[ i ]=99; // Set usedCards to default
 
        cash = 100;     // Set initial cash
     pot = bet * 2;  // Define pot


       cout << \"*****************\\n\";
     cout << \"Total cash: \" << cash << \"\\n\";
      cout << \"Place bet: \";
        cin >> bet;
     cout << \"*****************\\n\\n\";
   
        DEAL; // Deal hands


     do { // Player\'s Turn
            cout << \"*************************\\n\";
         cout << \"Your Score: \" << playerScore << \" || \" << playerScore2 << \"\\n\"; // Display Score
          cout << \"Current Pot: \" << pot << \"\\n\"; // Display current pot
           cout << \"Draw again? (\'y\' or \'n\') \";
          gets(question);
         cout << \"\\n*************************\\n\";
                if(strcmp(question,\"y\")==0) {
                 for(;;){ //Deal to player
                       pickedCard = rand()%51;
                     CHECK; // Check if card was already picked
                      if(r) continue; //If card was used
                      else SCORE_SYSTEM;
                      playerScore += score;
                       playerScore2 += score2;
                     break;
                  }
               }
               else;
       }while(strcmp(question, \"n\")!=0);
 }
}


Output of a diff (only to show what I changed)

Code: [Select]

9c9
< //#include
---
> #include
22c22,47
<  int scoreSystem(int pickedCard, int score, int score2) {  //Scoring algorithm [begining]
---
>
> int main()
>
> {
>
>   int cash, bet, pot;
>
>  int score, score2;
>
>   int playerScore = 0, playerScore2 = 0;
>
>   int dealerScore = 0, dealerScore2 = 0;
>
>   int pickedCard;
>
>  int deck[51];
>
>    int usedCards[51];
>
>   int r;
>
>   char question[15];
>
>
>
>   int scoreSystem(pickedCard, score, score2) {  //Scoring algorithm [begining]
30c55
<          for(int a=0, b=0; b<48;) {
---
>          for(int a=0, int b=0; b<48;) {
50c75
<    int check(int pickedCard, int usedCards[], int r) { //Checking [begining]
---
>   int check(pickedCard, usedCards, r) { //Checking [begining]
66c91
<           if(x==99) {usedCards[x]=pickedCard; break;}
---
>             if(x==99) {usedCards[ i ]=pickedCard; break;}
77c102
<    int deal(int deck[], int usedCards[], int r, int playerScore, int playerScore2, //Deal hands
---
>    int deal(deck, usedCards, r, playerScore, playerScore2, //Deal hands
79c104
<             int dealerScore, int dealerScore2, int score, int score2) {     //[begining]
---
>            dealerScore, dealerScore2, score, score2) {     //[begining]
81d105
< int pickedCard;
125,152d148
< int main()
<
< {
<
<  int cash, bet, pot;
<
<  int score, score2;
<
<   int playerScore = 0, playerScore2 = 0;
<
<   int dealerScore = 0, dealerScore2 = 0;
<
<   int pickedCard;
<
<  int deck[51];
<
<    int usedCards[51];
<
<   int r;
<
<   char question[15];
<
<
<
<
<
<
<
214c210
<            cout << \"\\n*************************\\n\";
---
>            cout << \"\\n*************************\\n;
216c212
<                if(strcmp(question,\"y\")==0) {
---
>                 if(question == \'y\') {
240c236
<         }while(strcmp(question, \"n\")!=0);
---
>         }while(question != \'n\');



Also, MaidenIndigo is correct in saying that the use of a string to hold a single letter is a waste, but there are some other things that I\'d avoid (break statements, for example), and maybe you have plans with that string?

Just a few hints:

The use of macros for function calls is not a good idea IMO, because it takes away the freedom to use different parameters for each call.

Also, you should #define all constants that will be used more than once (like the deck size), so that you won\'t have to change many occurances, likely missing one, causing debugging pain to yourself.

Anyway, hope this helps!

Edit: the forum should really disable smilies in [ code ] [ / code ] statements...

Edit 2: Also, it is highly likely that \"return var1,var2;\" isn`t going to do what you want (return two values): you will need references or pointers for that. The way it is now, only the rightmost value will be returned, the other(s) discarded.

Thus, you need to change your function to something like this:
Code: [Select]

int scoreSystem(int pickedCard, int &score, int &score2)


This will make the assignments inside the function work on the variable in the calling function (main()), instead on a copy, as is the default behaviour.

Thus, you don\'t need the return value for that and can either change it ti void or use it for something else.

edit 3: added spaces in array subscripts like [ i ] so the board won\'t interprete them as as italic formatting.
« Last Edit: May 10, 2005, 06:18:50 pm by Seytra »

Barneygumball

  • Wayfarer
  • *
  • Posts: 5
    • View Profile
(No subject)
« Reply #3 on: May 14, 2005, 07:48:18 am »
lol i don\'t have one single clue what all that means but

i know i would actually go buy a program or download one

just wish it was html i could help u heaps when it comes to that

Seytra

  • Forum Addict
  • *
  • Posts: 2052
  • No system can compensate lack of common sense.
    • View Profile
(No subject)
« Reply #4 on: May 14, 2005, 08:45:27 pm »
This looks like either an assignment or the start of a hobby coder. Both are commendable and would be badly served by just downloading a program.
Without such homegrown programs, there would be no programs to download in the first place.
Well, with Software / Idea patents (movie), there won\'t be any soon, but that is another, sad, story.

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
(No subject)
« Reply #5 on: May 14, 2005, 10:33:59 pm »
Yes, actualy many professional coders/programmers that work for big businesses should wory about software patants (as they exist now). I\'m not gona get into how, but the lack of these restrictions will let programmers make more money.

http://www.linuxcommand.org/rantings.php  <-- read that to find out more.


Back on topic, a I\'ve been working on this program, and am slmost finished. I\'ve compeletly rewriten the code, now it should be more comprehandable.

But I have a question... As you can see in the code, the part that decides who won is a if-else-if latter, and it\'s prety damn long. I was wondering if there is a better way of doing this? And \'switch\' statements won\'t work there.

The thing I\'m woried about is in the \"dealer\'s turn\" code (labeled)

Code:

Code: [Select]

// Black Jack test-2


#include
#include
using namespace std;



int usedCards[3][12];
int score, score2; // Uneversal score for both ace value.


void defultLoad() {// Set usedCards to Defult

   for(int a; a<4; a++) {
     for(int b; b<13; b++){
          usedCards[a][b] = 0;
}}} // Card defulter [end]


int checkCard(int suit, int value) { //Checks if Card\'s were used
  if(usedCards[suit][value]==0) {
     if(value >8) score = score2 = 10; //If picture card
     if(value == 0) { //If ace
           score = 1;
          score2 = 11;
        }
       else {score = score2 = value;} //If number card

     usedCards[suit][value] = 1;
     return 1;
   }
   else return 0;
}



int main()
{
  int cash, bet=0, pot;           // Create Constants cash, bet, and pot.
 int playerScore, playerScore2;  // Player\'s score for both values of an ace.
 int dealerScore, dealerScore2;  // Dealer\'s score for both values of an ace.
 int pickedSuit, pickedValue;    // Used to draw cards.
  int winAward;                   // Used to determine winner.
    char question[15] = \"y\";

  cash = 100;  //Set initial Cash
 playerScore = playerScore2 = dealerScore = dealerScore2 = 0; //Set all scores to zero

   cout << \"*******************************\\n\";
   cout << \"*Game:      BlackJack     \\n\"; // Header
  cout << \"*Version:   Alpha Test 2  \\n\";
    cout << \"*Developer: Egor Poblaguev\\n\";
    cout << \"*******************************\\n\\n\\n\";
 


    /*~~~~~~~~~~~Begin Game~~~~~~~~~~~*/
    for(;;){
        winAward = 0; // Set to defult.
     srand(GetTickCount());
     

        cout << \"*****************\\n\"; // Ask for bet.
     cout << \"Total cash: \" << cash << \"\\n\";
      for(;;)
     { // Beting phase [begin]
           cout << \"Place bet: \";
            cin >> bet;
         if(bet>cash) cout << \"CAN NOT EXCED CASH!\\n\";
          else {cash -= bet; break;}
      } // Beting phase [end]
     cout << \"*****************\\n\\n\";
        pot = bet * 2;  //Define pot.

       for(int dP=0; dP<2; dP++) { // Deals to player [begin]
          for(;;){
                pickedSuit = rand()%3;
              pickedValue = rand()%12;
                if(checkCard(pickedSuit, pickedValue)==1) break;
                else; //Check if card was used
          }
           playerScore += score; //Add to score
            playerScore2 += score2;
     } // Deals to player [end]

      for(int dD=0; dD<2; dD++) { // Deals to dealer [begin]
          for(;;){
                pickedSuit = rand()%3;
              pickedValue = rand()%12;
                if(checkCard(pickedSuit, pickedValue)==1) break;
                else; //Check if card was used
          }
           dealerScore += score; //Add to score
            dealerScore2 += score2;
     } // Deals to dealer [end]



      /*~~~~~~~~~~~Player\'s Turn~~~~~~~~~~~*/
      do{//Initiate palyer\'s turn
          cout << \"*************************\\n\";
         cout << \"Your Score: \" << playerScore << \" || \" << playerScore2 << \"\\n\"; // Display Score
          cout << \"Current Pot: \" << pot << \"\\n\"; // Display current pot
           cout << \"Draw again? (\'y\' or \'n\') \";
          cin >> question;
            cout << \"\\n*************************\\n\";
                if(strcmp(question,\"y\")==0) { //If answered \"yes\"
                   for(;;){
                    pickedSuit = rand()%3;
                  pickedValue = rand()%12;
                    if(checkCard(pickedSuit, pickedValue)==1) break;
                    else; //Check if card was used
                  }
           playerScore += score; //Add to score
            playerScore2 += score2;
             }
               if(playerScore>21) {// PLAYER BUST
                  cout << \"\\n\\n\\n***PLAYER BUST***\\n\\n***DEALER WINS!\\n\\n\\n\";
                   winAward = 2;
                   break;
              }
       }while(strcmp(question,\"n\")!=0);//End Player\'s turn


       
        /*~~~~~~~~~~~Dealer\'s Turn~~~~~~~~~~~*/
      do{//Initiate dealer\'s turn
          if(winAward==2) break; // if PLAYER BUST

            cout << \"*************************\\n\";
         cout << \"Dealer Score: \" << dealerScore << \" || \" << dealerScore2 << \"\\n\"; // Display Score
            cout << \"Current Pot: \" << pot << \"\\n\"; // Display current pot
           cout << \"\\n*************************\\n\";

            if(dealerScore>21){ // DEALER BUST
              cout << \"\\n\\n\\n***DEALER BUST***\\n\\n***PLAYER WINS!***\\n\\n\\n\";
                winAward = 1;
               break;
          }
           else if((dealerScore || dealerScore2)==21 && (playerScore || playerScore2)==21) { //If tie
              cout << \"\\n\\n\\n***TIE***\\n\\n\\n\";
                winAward = 3;
               break;
          }
           else if((playerScore2 && dealerScore2) < 22){ // If *Score2 are less then 21
                if (playerScore2 > dealerScore2){//If player wins
                   cout << \"\\n\\n\\n***PLAYER WINS!***\\n\\n\\n\";
                   winAward =1;
                    break;
              }
               else { // If dealer wins
                    cout << \"\\n\\n\\n***DEALER WINS!***\\n\\n\\n\";
                   winAward =2;
                    break;
              }
           }
           else if((playerScore2 > 21) && (dealerScore2 < 22)){ //READ THE IF STETEMENT!
               if (playerScore > dealerScore2){//If player wins
                    cout << \"\\n\\n\\n***PLAYER WINS!***\\n\\n\\n\";
                   winAward =1;
                    break;
              }
               else { // If dealer wins
                    cout << \"\\n\\n\\n***DEALER WINS!***\\n\\n\\n\";
                   winAward =2;
                    break;
              }
           }
           else if((playerScore2 <22) && (dealerScore2 >21)) { //READ THE IF STETEMENT!
                if(playerScore2 > dealerScore){ //If player wins
                    cout << \"n\\n\\n***PLAYER WINS!***\\n\\n\\n\";
                   winAward =1;
                    break;
              }
               else { //If dealer wins
                 cout << \"\\n\\n\\n***DEALER WINS!***\\n\\n\\n\";
                   winAward =2;
                    break;
              }
           }
           else if((playerScore2 && dealerScore2) > 21){ // If *Score2 is greater than 21
              if(playerScore>dealerScore){
                    cout << \"n\\n\\n***PLAYER WINS!***\\n\\n\\n\";
                   winAward =1;
                    break;
              }
               else { //If dealer wins
                 cout << \"\\n\\n\\n***DEALER WINS!***\\n\\n\\n\";
                   winAward =2;
                    break;
              }
           }
           else;


           if((playerScore>dealerScore || playerScore>dealerScore2) && dealerScore2 != 21){ //Pick card if
             for(;;){//Draw Card
                 pickedSuit = rand()%3;
                  pickedValue = rand()%12;
                    if(checkCard(pickedSuit, pickedValue)==1) break;
                    else; //Check if card was used
              }
               dealerScore += score; //Add to score
                dealerScore2 += score2;
         }

       }while((playerScore>dealerScore || playerScore>dealerScore2) //Condition for dealer drawing
             && dealerScore < 22 && dealerScore2 != 21); //End Dealer\'s Turn
      }

       return 0;
       }
« Last Edit: May 14, 2005, 10:35:09 pm by RussianVodka »



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.

Cha0s

  • Veteran
  • *
  • Posts: 1860
    • View Profile
(No subject)
« Reply #6 on: May 14, 2005, 11:26:49 pm »
I\'ve made a BlackJack game before (in Java, though) and the if-else is pretty much unavoidable. The only suggestion I\'d make is to put it in its own method called findWinner or something that returns an int representing the winner (or a boolean if you want ;) ). That way you say something like:
Code: [Select]

if (findWinner() == PLAYER){ //PLAYER is a constant (const int PLAYER = 0; see, I know some C++ ;) ) representing the player
//do player win stuff
}
else{
//do dealer win stuff
}


Hopefully that should make things easier to follow. ;)

EDIT: Actually, you\'ll have to do it with ints probably if you want different results for black jack vs. straight win (there\'s usually a difference in how the betting works. 2X for blackjack I think).
« Last Edit: May 14, 2005, 11:51:56 pm by Cha0s »
Cha0s
Mac OS X Forum Moderator
In-Game Roleplay Forum Moderator
Please search and skim existing threads before posting!

ramlambmoo

  • Hydlaa Notable
  • *
  • Posts: 567
    • View Profile
(No subject)
« Reply #7 on: May 15, 2005, 05:19:52 am »
Uh, i tried your second lot of code, and it didnt really work, because it didnt reset the variable that holds the players score before it started the new round.  So say you got 17 and held, and then the next round the first card you get is a 5, then it says the total of your cards is 22 and you go bust... ill fix the code up a bit and post it.

EDIT****

Your code is really..confusing.  I dont understand why you have playerScore and PlayerScore2, as well.  Plus even if you win it dosnt add the money to your account.  And i have no idea how you coded the bit where the dealer has his turn.  I\'d suggest breaking the code up into functions, especially the parts like picking a card, etc.  

EDIT2****
Oh, i see, playerScore2 is for the ace values.  Gotcha.

EDIT3****

Ok i fixed up the code, i did a MAJOR simplification of the code that works out what the dealer is meant to do, i dunno how you made it so complicated but here it goes:

Code: [Select]

// Black Jack test-2


#include

#include

using namespace std;


int usedCards[3][12];

int score, score2; // Uneversal score for both ace value.

int cash, bet=0, pot;// Why dont we make them global instead? Global is gooood :)


void defultLoad() {// Set usedCards to Defult



 for(int a; a<4; a++) {

     for(int b; b<13; b++){

          usedCards[a][b] = 0;

}}} // Card defulter [end]

void Playerwins() {
     cout << \"Dealer has gone bust!!  Player wins!! Woot!!\" << endl;
     cash += bet*2;
}
     
int  DealerDesicion(int dealerScore, int dealerScore2, int playerScore, int playerScore2){
     int dealer_score,player_score;
     if (dealerScore2 < 22) {             // Work out which value we should use, i.e wwhich one is the highest but still u nder 21
         dealer_score = dealerScore2;
     } else {
         dealer_score = dealerScore;
     }
     if (playerScore2 < 22) {            // Same here.  Work out whether playerScore or playerScore2 is the better score.
         player_score = playerScore2;
     } else {
         player_score = playerScore;
     }
     if (dealer_score > 21){               // If we busted, then return 0
         return 0;
     }
     if (dealer_score >= player_score){    // If dealer is beating player, then hold, and dealer wins
         return 1;
     } else {
         return 2;                         // If dealer isnt beating player, then take another card!!
     }
     
}


int checkCard(int suit, int value) { //Checks if Card\'s were used

  if(usedCards[suit][value]==0) {

     if(value >8) score = score2 = 10; //If picture card

     if(value == 0) { //If ace

           score = 1;

          score2 = 11;

        }

       else {score = score2 = value;} //If number card



     usedCards[suit][value] = 1;

     return 1;

   }

   else return 0;

}


int main()

{

  int playerScore, playerScore2;  // Player\'s score for both values of an ace.

 int dealerScore, dealerScore2;  // Dealer\'s score for both values of an ace.

 int pickedSuit, pickedValue;    // Used to draw cards.

  int winAward;                   // Used to determine winner.
 
    int i,j;                        // Random variables to control loops

   char question[15] = \"y\";



  cash = 100;  //Set initial Cash

 playerScore = playerScore2 = dealerScore = dealerScore2 = 0; //Set all scores to zero



   cout << \"*******************************\\n\";

   cout << \"*Game:      BlackJack     \\n\"; // Header

  cout << \"*Version:   Alpha Test 2  \\n\";

    cout << \"*Developer: Egor Poblaguev\\n\";

    cout << \"*******************************\\n\\n\\n\";

 


    /*~~~~~~~~~~~Begin Game~~~~~~~~~~~*/

    for(;;){

        winAward = 0; // Set to defult.

     srand(GetTickCount());

     


        cout << \"*****************\\n\"; // Ask for bet.

     cout << \"Total cash: \" << cash << \"\\n\";

      for(;;)

     { // Beting phase [begin]

           cout << \"Place bet: \";

            cin >> bet;

         if(bet>cash) cout << \"CAN NOT EXCED CASH!\\n\";

          else {cash -= bet; break;}

      } // Beting phase [end]

     cout << \"*****************\\n\\n\";

        pot = bet * 2;  //Define pot.

       playerScore = playerScore2 = dealerScore = dealerScore2 = 0; // *** NEW  Set all scores to zero

        for(int dP=0; dP<2; dP++) { // Deals to player [begin]

          for(;;){

                pickedSuit = rand()%3;

              pickedValue = rand()%12;

                if(checkCard(pickedSuit, pickedValue)==1) break;

                else; //Check if card was used

          }

           playerScore += score; //Add to score

            playerScore2 += score2;

     } // Deals to player [end]



      for(int dD=0; dD<2; dD++) { // Deals to dealer [begin]

          for(;;){

                pickedSuit = rand()%3;

              pickedValue = rand()%12;

                if(checkCard(pickedSuit, pickedValue)==1) break;

                else; //Check if card was used

          }

           dealerScore += score; //Add to score

            dealerScore2 += score2;

     } // Deals to dealer [end]







      /*~~~~~~~~~~~Player\'s Turn~~~~~~~~~~~*/

      do{//Initiate palyer\'s turn

          cout << \"*************************\\n\";

         cout << \"Your Score: \" << playerScore << \" || \" << playerScore2 << \"\\n\"; // Display Score

          cout << \"Current Pot: \" << pot << \"\\n\"; // Display current pot

           cout << \"Draw again? (\'y\' or \'n\') \";

          cin >> question;

            cout << \"\\n*************************\\n\";

                if(strcmp(question,\"y\")==0) { //If answered \"yes\"

                   for(;;){

                    pickedSuit = rand()%3;

                  pickedValue = rand()%12;

                    if(checkCard(pickedSuit, pickedValue)==1) break;

                    else; //Check if card was used

                  }

           playerScore += score; //Add to score

            playerScore2 += score2;

             }

               if(playerScore>21) {// PLAYER BUST

                  cout << \"\\n\\n\\n***PLAYER BUST***\\n\\n***DEALER WINS!\\n\\n\\n\";

                   winAward = 2;

                   break;

              }

       }while(strcmp(question,\"n\")!=0);//End Player\'s turn



       
        /*~~~~~~~~~~~Dealer\'s Turn~~~~~~~~~~~*/

      do{//Initiate dealer\'s turn

          if(winAward==2) break; // if PLAYER BUST



            cout << \"*************************\\n\";

         cout << \"Dealer Score: \" << dealerScore << \" || \" << dealerScore2 << \"\\n\"; // Display Score

            cout << \"Current Pot: \" << pot << \"\\n\"; // Display current pot

           cout << \"\\n*************************\\n\";

            i = DealerDesicion(dealerScore,dealerScore2,playerScore,playerScore2);
           
            if(i==0){          // If the dealer has busted, then player wins
                Playerwins();
                break;
            }
            if(i==1){          // If the dealer is beating the player or has equal value, dealer wins
                cout << \"Dealer wins!!! You suck!!\" << endl;
                break;
            }
            if(i==2){          // If dealer has lower score then player, he/she MUST take another card to try and beat player

               for(;;){//Draw Card

                 pickedSuit = rand()%3;

                  pickedValue = rand()%12;

                    if(checkCard(pickedSuit, pickedValue)==1) break;

                    else; //Check if card was used

              }

               dealerScore += score; //Add to score

                dealerScore2 += score2;

         }
           
        }while(1);
        }


     return 0;

       }



Oh and sorry about the way i set up my code, is a tiny bit messy, but thats just the way i like it.
« Last Edit: May 15, 2005, 06:13:38 am by ramlambmoo »

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
(No subject)
« Reply #8 on: May 15, 2005, 06:39:30 pm »
Yes, I know about the glitch where the scores don\'t go back to zero, it worked fine before I added the if-else-if ladder.

Also the programm isn\'t finished, so nothing happens with the money you bet.

I was planing on having 3 values of winAward, and then using an \'if\' statement to determine the winner.

I think it was if winAward is 1, then player wins (and gets the pot), if 2 dealer wins (and money disapears), and if 3 it is a draw (and pot remains).



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.

ramlambmoo

  • Hydlaa Notable
  • *
  • Posts: 567
    • View Profile
(No subject)
« Reply #9 on: May 16, 2005, 03:36:19 am »
Quote
Yes, I know about the glitch where the scores don\'t go back to zero, it worked fine before I added the if-else-if ladder.

Also the programm isn\'t finished, so nothing happens with the money you bet.

I was planing on having 3 values of winAward, and then using an \'if\' statement to determine the winner.

I think it was if winAward is 1, then player wins (and gets the pot), if 2 dealer wins (and money disapears), and if 3 it is a draw (and pot remains).


Ah, well firstly, i fixed all these problems in my code, if you want to try compiling it.  Plus i really cleared up the dealer section, you should definatly look at that, even if you want to fix the other things yourself.
And lastly, if the dealer and the player get the same score, i thought the dealer automatically wins.  Well at least thats the way i\'ve always seen it played, because otherwise the casino dosnt have an advantage.
« Last Edit: May 16, 2005, 03:36:55 am by ramlambmoo »