Game of the "Horse"
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Game of the "Horse"

  1. #1
    Join Date
    Feb 2011
    Location
    Cosenza,Italy
    Posts
    62

    Game of the "Horse"

    Hello Guys! Here in Italy there is a card game (with the napoletan cards) called "Gioco dell'asino"..
    The card in a Napoletan deck are divided in 4 seeds and in 10 figures (total=40 cards). The rules are:
    ->Discard a "horse" (number 9 figure) from the deck;
    ->Distribute the remaining 39 cards to 2 Players;
    ->The Players have to discard two cards for each pair of cards with the seem figure (ex. two "10" or 2 "8" or 4 "2" etc";
    ->So the players remains with some figures; in each turn the player must draw a card from the opponent and discard (if possible) his pair of cards;
    ->The player who finish cards wins, and the one that remains with the "horse" loses.

    Now i tried to do a C++ program that represents the two players that, after discarding cards, choose randomly one card form the opponent and (if possible) discard the pair of cards (counter-2) or nothing (counter +1). The only thing that should appear on the monitor is the Player who wins but it doesn't do anything. Anyone helps me? The code:

    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    void distribuisci(bool C1[][10],int &,bool [][10],int&);
    void eliminacarta(bool [][10],int&);
    void scegli(bool[][10],int&,bool [][10],int &);
    int main()
    {
    	srand(time(0));
    	bool C1[4][10]={{0}};
    	bool C2[4][10]={{0}};
    	int cont1=0,cont2=0;
    	distribuisci(C1,cont1,C2,cont2);
    	
    	eliminacarta(C1,cont1);
    	eliminacarta(C2,cont2);
    	
    	do
    	   scegli(C1,cont1,C2,cont2);
    	while(cont1!=0 and cont2!=0);
    	if(cont1==0)
    		cout<<"il giocatore 1 ha vinto!\n";
    	if(cont2==0)
    		cout<<"il giocotare 2 ha vinto!\n";
    
    	return 0;
    }
    
    void distribuisci(bool C1 [][10],int & cont1, bool C2 [][10],int & cont2)
    {
    	int r,c;
    	
    	for(int i=0;i<19;i++)
    	{
    		do
    	{
    	    
    		r=rand()%4;
    	    c=rand()%10;
    	}
    		while(C1[r][c]!=0 or (r==0 and c==9));
    		 C1[r][c]=true;
    		cont1++;
    	}	 
    	for(int i=0;i<20;i++)
    	{
    		do{
    			r=rand()%4;
    			c=rand()%10;
    		}while(C2[r][c]!=0 or C1[r][c]!=0 or (r==0 and c==9));
    		C2[r][c]=true;
    		cont2++;
    	}
    
    	
    }
    void scegli (bool C1[][10],int &cont1,bool C2[][10],int &cont2)
    {
    	int r,c;
    	do{
    		r=rand()%4;
    		
    		c=rand()%10;
    	}
    		while(C1[r][c]==false or (r==0 and c==9));
    	C2[r][c]=true;
        C1[r][c]=false;
    		cont2++;
                   cont1--;
                eliminacarta(C2,cont2);
    	
    	do{
    		r=rand()%4;
    		c=rand()%10;
    	}
    	while(C2[r][c]==false or (r==0 and c==9));
    	C1[r][c]=true;
    	C2[r][c]=false;
    	cont1++;
            cont2--;
    	eliminacarta(C1,cont1);
    
    }
    
    void eliminacarta( bool C[][10],int &cont)
    { 
    	
    	int carte=0;
    	int passaggi=0;
    	for(int j=0;j<10;j++)
    		for(int i=0;i<4;i++)
    		{
    			if(C[i][j]==true)
    				carte++;
    			if(carte==1)
    			passaggi++;
    	        if(carte==2)
    			{
    				cont-=2;
    			    C[i][j]=false;
                    C[i-passaggi][j]=false;
    			    carte=0;
    			    passaggi=0;
    			}
    			
    	}
    }
    Last edited by Falko-tux; February 21st, 2011 at 01:27 PM.

  2. #2
    Join Date
    Oct 2008
    Posts
    1,131

    Re: Game of the "Horse"

    ... uhm, there are much simpler ways of writing the same program; is there a reason why you are not using STL ?

    anyway, note that the lines "rand()%3" and "rand()%9" return a number between 0-2 and 0-8 respectively, is this your intent ?

  3. #3
    Join Date
    Feb 2011
    Location
    Cosenza,Italy
    Posts
    62

    Re: Game of the "Horse"

    Yes, you're right i forgot that &#37; operator goes from 0 to value-1 but the program still doesn't work!..anyway what is STL? I'm at the beginning of c++ programming at University!

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,292

    Re: Game of the "Horse"

    Quote Originally Posted by Falko-tux View Post
    choose randomly one card form the opponent and (if possible) discard the pair of cards (counter-2) or nothing (counter +1).
    When you draw a card from the opponent, and it turns out it is a pair, the new amount of cards in your hand will actually be counter-1.

    STL is a library (provided by the C++ language), which contains sets of standard and useful objects. In particular, you have the container vector, which is a dynamically resizeable array. Using the STL usually means easier to write and maintain code, and less bugs.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Feb 2011
    Location
    Cosenza,Italy
    Posts
    62

    Re: Game of the "Horse"

    yes, but in my university we have studied only arrays, so i have to write the program with basic elements
    Anyway the counter is -2 beacause:
    ->I take a card (counter +1)
    ->if discard a pair of cards I put 2 cards out of my hands (counter -2)

  6. #6
    Join Date
    Feb 2011
    Location
    Cosenza,Italy
    Posts
    62

    Re: Game of the "Horse"

    "0" or "false" it's the same,right?

  7. #7
    Join Date
    Apr 2008
    Posts
    726

    Re: Game of the "Horse"

    Quote Originally Posted by Falko-tux View Post
    "0" or "false" it's the same,right?
    yes, mostly.

  8. #8
    Join Date
    Feb 2011
    Location
    Cosenza,Italy
    Posts
    62

    Re: Game of the "Horse"

    ok thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center