Card game - war - questions

This is a discussion on Card game - war - questions within the C++ Programming forums, part of the General Programming Boards category; Hi, cprogramming forum! I am working on a really basic war card game in my free time and was hoping ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Michigan, USA
    Posts
    1

    Card game - war - questions

    Hi, cprogramming forum!

    I am working on a really basic war card game in my free time and was hoping you folks could help me! Currently, I just have a basic counter and working on the tie system. However, currently the tie system continue to repeats the three cards, for instance
    Code:
    Player 1 || Player 2
    17            17
    17            17
    17            17
    I was thinking this could be because of the card shuffle itself, but I think it might be just my bad do loop handling. I know my code is messy, but I understand it

    I use gedit with g++ compiler. I have used Kate in the past (so the lines are more organized and nested)

    Thanks for the help.

    Source Download
    Code:
    //Written by Kevin Murphy 2012 //War card game is where two people play and if one card is greater that player gets the card. If they both tie, depending on how one plays, the two players throw down three more cards and the fourth car decides who gets that stash!  #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;  int main() {   system("clear");   int cards[52][2];//Creates 2 arrays for cards   int x,y,currentcard,currentsuit,random,temp,player1=0,player2=0,numberofcards,ties=0,tie_cards,tie_count=0;   string next;   for (x=0;x<52;x++)   for (y=0;y<2;y++)   {     cards[x][y] = x + 1; //Fills both arrays with 52 "cards"   } // //Randomize the cards //Deck 1   for (x=0;x<52;x++)   {     random=rand()%52;//Random numbers of 52, set to random     temp=cards[random][0];//sets cards random (the array random) into a temp.     cards[random][0]=cards[x][0];//flips     cards[x][0]=temp;//the cards   } //Deck 2   for (x=0;x<52;x++)   {     random=rand()%52;//Random numbers of 52, set to random     temp=cards[random][1];//sets cards random (the array random) into a temp.     cards[random][1]=cards[x][1];//flips     cards[x][1]=temp;//the cards   } //Randomize the cards // do {   for (x=0;x<52;x++)   {   //usleep(250000);   system("clear");   cout<<"||Player 1: "<<player1<<"||Player 2: "<<player2<<"||Ties:"<<ties<<"||"<<endl;   cout<<"||"<<cards[x][0]<<"||"<<cards[x][1]<<"||"<<endl;//Prints out the cars (from each deck)   if (cards[x][0]>cards[x][1])     {    player1++;   numberofcards--;   }   if (cards[x][0]<cards[x][1])     {    player2++;   numberofcards--;   }     if (cards[x][0]==cards[x][1])     {    do   {   cout<<"||"<<cards[x][0]<<"||"<<cards[x][1]<<"||"<<endl;//Prints out the cars (from each deck)   numberofcards--;   tie_count++;   } while (tie_count<3);   cout<<"||"<<cards[x][0]<<"||"<<cards[x][1]<<"||"<<endl;//Prints out the cars (from each deck)   ties++;   }   } } while (numberofcards<52); return 0; }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    In the following code:
    Code:
      do
      {
      cout<<"||"<<cards[x][0]<<"||"<<cards[x][1]<<"||"<<endl;//Prints out the cars (from each deck)
      numberofcards--;
      tie_count++;
      } while (tie_count<3);
    You will print the same three cards because you never change any value you are using for printing.

    Jim

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Wrong forum (C++ is next door) and you've posted just on big line of code.

    Code:
    //Written by Kevin Murphy 2012
    //War card game is where two people play and if one card is greater that player gets the card. If they both tie, depending on how one plays, the two players throw down three more cards and the fourth car decides who gets that stash! 
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    
    int main()
    {
      system("clear");
      int cards[52][2];//Creates 2 arrays for cards
      int x,y,currentcard,currentsuit,random,temp,player1=0,player2=0,numberofcards,ties=0,tie_cards,tie_count=0;
      string next;
      for (x=0;x<52;x++)
      for (y=0;y<2;y++)
      {
        cards[x][y] = x + 1; //Fills both arrays with 52 "cards"
      }
    //
    //Randomize the cards
    //Deck 1
      for (x=0;x<52;x++)
      {
        random=rand()%52;//Random numbers of 52, set to random
        temp=cards[random][0];//sets cards random (the array random) into a temp.
        cards[random][0]=cards[x][0];//flips
        cards[x][0]=temp;//the cards
      }
    //Deck 2
      for (x=0;x<52;x++)
      {
        random=rand()%52;//Random numbers of 52, set to random
        temp=cards[random][1];//sets cards random (the array random) into a temp.
        cards[random][1]=cards[x][1];//flips
        cards[x][1]=temp;//the cards
      }
    //Randomize the cards
    //
    do
    {
      for (x=0;x<52;x++)
      {
      //usleep(250000);
      system("clear");
      cout<<"||Player 1: "<<player1<<"||Player 2: "<<player2<<"||Ties:"<<ties<<"||"<<endl;
      cout<<"||"<<cards[x][0]<<"||"<<cards[x][1]<<"||"<<endl;//Prints out the cars (from each deck)
      if (cards[x][0]>cards[x][1])  
      { 
      player1++;
      numberofcards--;
      }
      if (cards[x][0]<cards[x][1])  
      { 
      player2++;
      numberofcards--;
      }  
      if (cards[x][0]==cards[x][1])  
      { 
      do
      {
      cout<<"||"<<cards[x][0]<<"||"<<cards[x][1]<<"||"<<endl;//Prints out the cars (from each deck)
      numberofcards--;
      tie_count++;
      } while (tie_count<3);
      cout<<"||"<<cards[x][0]<<"||"<<cards[x][1]<<"||"<<endl;//Prints out the cars (from each deck)
      ties++;
      }
      }
    } while (numberofcards<52);
    return 0;
    }
    Some problems:
    -) Awful indentation
    -) In your tie-loop (line 59-64) you never change the cards.
    -) You don't initialize numberofcards.
    -) You decrement numberofcards several times and thus the condition in your main do-while-loop is probably wrong.

    Bye, Andreas

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Moved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    - You have two loops for initializing decks, but it could be combined into one loop wrapped with for(d = 0; d < 2; d ++).
    - You should include <string> if you're using std::string. Also, try to use <cstdlib> instead of <stdlib.h>, <ctime> instead of <time.h>, etc. The .h versions are from C.
    - You may want to avoid typing 52 all through your code and instead have say "const int CARDS = 52;".
    - You can use std::setw() from <iomanip> to get your vertical bars "||" to line up (assuming they aren't lined up now).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. War card game
    By bigzcoder in forum C Programming
    Replies: 3
    Last Post: 04-19-2012, 09:38 PM
  2. Crazy 8s Card Game
    By danmcgrail in forum C Programming
    Replies: 1
    Last Post: 03-12-2012, 08:06 PM
  3. Card game war
    By Dr Saucie in forum C Programming
    Replies: 3
    Last Post: 02-11-2010, 10:25 PM
  4. Card game help
    By aaroroge in forum Game Programming
    Replies: 9
    Last Post: 07-16-2005, 06:37 PM
  5. card game
    By rugger78 in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2004, 03:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21