Thread: understanding void functions

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    15

    understanding void functions

    Hi all, I have an assignment that needs me to simulate a game of beetle.
    Beetle (game) - Wikipedia, the free encyclopedia
    It's just a simple dice roll game, one number represents one part of the beetle and my professor wants us to use a function like this:
    void drawBeetle( string parts );
    This function should draw the current state of the beetle on the screen after the diceroll. (same function should be used for human and computer players). The input parameter parts indicate which parts should be drawn.
    I've been reading up on void functions, and I have an idea of how they work (not really), but i'm still a little confused by the logic of the assignment. So far all I did was write a function for a diceroll.
    Is the idea to draw the pieces of the beetle in main, and then have the final picture in the void function?
    I'm not really asking anyone to write any code, just hope anyone could help out with how the program should flow. Thanks in advance, hope someone can help.

    edit: Sorry, just realized the title sounds a bit misleading. Yes, I'm asking for help on an assignment, no I do not want the answer.
    Last edited by jtieu; 02-25-2011 at 02:40 PM.

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    i would have one function to display the beetle like this
    Code:
    void drawbeetle(int no_of_parts)
    {
         // cout the parts
    }
    but it seems that your teacher wants you to learn about strings.
    if that is the case you will need to have it so that your diceroll function adds the text into the string, this can be made easier by using a string array as shown
    Code:
    int diceroll = rand % max parts;
    switch (diceroll)
    {
         case 1:
               your_string[incrementor] = "part1";
               break;
         case 2:
      //       ...... and so on
    }
    then you need to make your drawbeetle
    Code:
    void drawbeetle(your_string[], int string_max)
    {
          for(int a = 0; a < string_max; a++)
                cout << your_string[a] << endl;
    }

    also, do you output part 1, part 2, or leg, head, or something like
    | |
    / \
    | * * |
    \ /
    and so on
    Last edited by bobknows; 02-26-2011 at 09:07 PM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    Thank you for the help! I think I understand what you're saying, but I only have my void displaying the beetle. This is is shortened version of what I wrote so far(dice roll = 1). I'm not too comfortable yet with switch statements, so I made a bunch of (if else) statements in place, and i have letters in place of the beetle picture.
    My question now, is that I have to make a computer opponent. Again, I'm not really looking for code, but a hint as to how I would implement it. Would I need a new function? Thanks in advance.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    void drawbeetle( string parts);
    int dice();
    
    int main () 
    {
    	string parts (14, ' ');
    	char turn = 'r';
    	int roll;
    	cout << "Press [R/r] to roll.\n";
    	cin >> turn;
    	srand(time(0));
    	int one = 1;
    	
    	while (turn == 'r')
    	{	
    		roll = dice();
    		cout << "You rolled a " <<roll<<".\n";
    
    		else if (roll == 1)
    		{
    			if ((five == 2) && (six == 2) && (one == 1)) 
    			{
    				parts[12] = 'e';
    				cout << "You rolled a eye\n";
    				drawbeetle(parts);
    				one = one+1;
    				cin >>turn;
    			}
    			else if (one == 2)
    			{
    				parts[13] = 'e';
    				cout << "You rolled a eye\n";
    				drawbeetle(parts);
    				one = one+1;
    				cin >>turn;
    			}
    			else if (five == 1) 
    			{
    				cout << "Sorry. You gain nothing this round.\n";
    				cin >>turn;
    			}
    			else 
    			{
    				cout << "Sorry. You gain nothing this round.\n";
    				cin >>turn;
    			}
    		}
    		if (parts == "bhwwllllllaaee") 
    		{
    			cout << "You Win!!!!!!";
    			break;
    		}
    	}
    }
    void drawbeetle(string parts)
    {
    		cout <<parts<<"\n";
    }
    int dice()
    { 
    	int diceroll;
    	diceroll =  (rand()% 6-1+1) + 1;
    	return diceroll;			  
    }

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
    else if (roll == 1)
    you can't use "else if" without using an "if" statement first. And, there are no variables "five" and "size".
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    nice job, just a few things you need to do,

    take out the else like above,

    move most of your main function into a void function called taketurn with one argument to tell if it is a human or a computer like so:
    Code:
    void taketurn(bool ishuman)
    char turn;
    int roll;
    if(ishuman){
    cout << "Press r to roll.\n";
    cin >> turn;
    }
    int one = 1;
    while (turn == 'r')
    {	
    	roll = dice();
                     if(ishuman)
    	cout << "You rolled a " <<roll<<".\n";
                      else cout << "the computer rolled a << roll << endl;
    
    // and so on, using if statements to cout if it is human or computer
    }
    make your parts string into two strings: my_parts, and comp_parts
    use if statements as before to tell whether to add to my_parts or _comp parts

    change your srand to the start fo main and so its like this
    Code:
     srand(time(NULL));
    this makes it work better & is recognised more

    also, you need to increment the place you are putting your parts into like this
    Code:
    for(int a = 0; a < 14; a++){
         taketurn(1);   // takes the humans turn
         taketurn(0);   // takes the computers turn
    
    }
    and there are several more changes but make these first, then ill se where you are

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    I wish I read your post before I finished up my program.
    So I decided to keep everything in main. It was 5 in the morning, so I was getting desparate. It looks something like this:
    Code:
     else if (roll == 1)
    		{
    			if ((five == 2) && (six == 2) && (one == 1)) 
    			{
    				parts[12] = 'e';
    				cout << "You rolled a eye\n";
    				drawbeetle(parts);
    				one = one+1;
    				    
    			}
    			else if (one == 2)
    			{
    				parts[13] = 'e';
    				cout << "You rolled a eye\n";
    				drawbeetle(parts);
    				one = one+1;
    				    
    			}
    			else if (five == 1) 
    			{
    				cout << "Sorry. You gain nothing this round.\n";
    				drawbeetle(parts);
    				    
    			}
    			else 
    			{
    				cout << "Sorry. You gain nothing this round.\n";
    				drawbeetle(parts);
    				    
    			}
    		}
    else if (croll == 1)
    	{
    		if ((cfive == 2) && (csix == 2) && (cone == 1)) 
    		{
    			cparts[12] = 'e';
    			cout << "I rolled a eye\n";
    			drawbeetle(cparts);
    			cone = cone+1;
    			cout << "Your turn. Press [R/r] to roll\n";
    			cin >>turn;
    			
    		}
    		else if (cone == 2)
    		{
    			cparts[13] = 'e';
    			cout << "I rolled a eye\n";
    			drawbeetle(cparts);
    			cone = cone+1;
    			cout << "Your turn. Press [R/r] to roll\n";
    			cin >>turn;
    		}
    		else if (cfive == 1) 
    		{
    			cout << "I gain nothing this round.\n";
    			drawbeetle (cparts);
    			cout << "Your turn. Press [R/r] to roll\n";
    			cin >>turn;
    		}
    		else 
    		{
    			cout << "I gain nothing this round.\n";
    			drawbeetle( cparts);
    			cout << "Your turn. Press [R/r] to roll\n";
    			cin >>turn;
    		}
    	}
    The idea was to have me roll first, then the computer follows immediately after me, and prompts the user to roll again (cin>>turn) and I'll put a pause function in there somewhere so that the random function will work properly.
    That's just for diceroll == 1. Imagine that for rolls 1-6. I think there's about 40 if else statements in my program by my last count.
    So aside from this eventually being an eyesore for my teacher to read, It manages to work somewhat fine. I do like your logic better though, bobknows. Unfortunately, I've spent my allotted time for this project. If I were to later implement your void functions to learn how it works, will I be able to get rid of most of the if-else statements, and just have two void functions (one for human, one for comp), or am I completely understanding your solution wrong? Thanks in advance for the help

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    you get it prety welll

    the only thing i would wonder is would it be better to have one function with several if/else statements to allow for a human and comp using it or just having two seperate functions?

    this is a hard question because when you have both it is easier to reuse one of them but it takes more memory in you original program. i like using if/else if the functio is longer than 8-9 lines aso it saves space but otherwise i just make two.

    what do you think?

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    I wasn't aware that having both would take up more memory. It would make it easier for someone else to copy and paste the code if they ever wanted to use it though. And it does save a lot of space in the actual program so you wouldn't have to read as much. In the end, it probably would've been much better for me to have split it into two separate functions.
    And again, Thanks for all the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why do I get screen flicker?
    By javalurnin in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2009, 08:24 AM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM