Public Group

#### Archived

This topic is now archived and is closed to further replies.

# my baseball game....

This topic is 5306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

okay, if anyone has time helping me debug or see where i did things kinda screwy let me know. having a book in front of me and not a prof does not help much.... hopefully this is not too cryptic...but when this is ran it becomes the game you can never lose...hehe...LOL (maybe i should make a living at programming slot machines in vegas?) source:
/* baseball DOS!!! */

/* main.cpp */

#include <iostream>
#include <time.h>
#include <stdlib.h>

void ShowScreen (void);
short GetNumber (void);
short GenerateNumber (void);
void ComparedNumber (short OffenseNumber, short DefenseNumber);
void ShowOuts (short Outs);
void ShowHits (short Hits);

main ()

{
short OffenseNumber;
short DefenseNumber;
short Hits;
short Outs;
Hits = 0;
Outs = 0;

ShowScreen ();
/* play loop, either play gets to 4th base, or computer gets to 3 outs */
do
{
GetNumber ();
GenerateNumber ();
void ComparedNumber (short OffenseNumber, short DefenseNumber);
void ShowOuts (short Outs);
void ShowHits (short Hits);

if (Hits > 0);
{
std::cout << "You have won!  Try Again!";
std::cout << std::endl << std::endl;
}
}
while (Outs < 3);

std::cout << "You have acquired 3 outs....Game Over!!! ";
std::cout << std::endl << std::endl;

return 0;
}

/* function definitions */

void ShowScreen (void)
{
std::cout << "Welcome to BASEBALL for DOS!  Here are the insctructions:";
std::cout << std::endl << std::endl;

std::cout << "Your opponent will ask you to pick a number between (1-5).  If you guess correctly ";
std::cout << "then you will have a 'hit'.  If you guess incorrectly then you will have an 'out'.";
std::cout << std::endl << std::endl;

std::cout << "Use number keys [1] - [5] for your guesses, it's that easy!  When you can guess (4) ";
std::cout << "time without losing more then twice you win!";
std::cout << std::endl << std::endl;

std::cout << "Correct guesses = Hit!" << std::endl;
std::cout << "Incorrect guesses = Out!" << std::endl;
std::cout << "3 outs = You Lose!" << std::endl;
std::cout << "4 hits in less then 3 outs = You Win" << std::endl;
std::cout << std::endl;
std::cout << "Eventually, I will add innings, and create a whole game..." << std::endl;

}

short GetNumber (void)
{
short OffenseNumber;
std::cout << "Batter Up! Guess a number between [1] and [5]: ";
std::cin >> OffenseNumber;

/* in case player hit a key other then 1, 2, or 3 */
if ((OffenseNumber == 1) || (OffenseNumber == 2) || (OffenseNumber == 3) || (OffenseNumber == 4) || (OffenseNumber == 5))
{
return OffenseNumber;
}
else
{
return 1;
}
}

short GenerateNumber (void)
{
short DefenseNumber;

srand (time (NULL));
DefenseNumber = (rand () % 4) + 1;

return DefenseNumber;
}

void ComparedNumber (short OffenseNumber, short DefenseNumber)
{
short Outs;
short Hits;
if (OffenseNumber != DefenseNumber)
{
(Outs + 1);
}
else
{
(Hits + 1);
}
}

void ShowOuts (short Outs)
{
if (Outs = 0);
{
std::cout << "You have 0 outs...batter up" << std::endl;
}
if (Outs = 1);
{
std::cout << "You have 1 out...batter up" << std::endl;
}
if (Outs = 2);
{
std::cout << "You have 2 outs...batter up" << std::endl;
}
}

void ShowHits (short Hits)
{
if (Hits = 1);
{
std::cout << "You are on first base...batter up" << std::endl;
}
if (Hits = 2);
{
std::cout << "You are on second base...batter up" << std::endl;
}
if (Hits = 3);
{
std::cout << "You are on third base...batter up" << std::endl;
}
if (Hits = 4);
{
std::cout << "You have made it home!" << std::endl;
}
}

note: i did not link a header file cause im not ready for that yet. i tried, but i just want to get the whole concept down with funcions..and i wasn't about to involve pointers, even though that was my goal when i sat down to program this [edited by - deadsexy on January 6, 2004 1:47:02 AM] [edited by - deadsexy on January 6, 2004 1:51:10 AM]

##### Share on other sites
An if statement does not need a semi-colon right after the test. Where you have statements like
if (Outs = 0);
change it to say
if (Outs = 0)
Also you are using the '=' operator to test for equality, when it is the assignment operator. The way you have now, in each if test you are assigning outs to 1, then to 2 then to 3, so those cases will always be true.

Another suggestion would be to use a switch. They just clean up those if statements a bit. If you haven't come across switch statements yet don't worry.

Also, and I'm not too sure about this, is it necessary to use shorts? Is there any performance or other benefits from using them over ints?

BTW nice to see a spin-off of the usual "guess a number".

[edited by - vangelis on January 6, 2004 2:00:51 AM]

##### Share on other sites
You''ve got a classic bug happening .

In ifs, make sure you do == instead of just =

##### Share on other sites
Just a handy tip, it''s easier and cleaner to use the C++ style comments for single lined comments. Instead of doing this:

/* main.cpp */

Do this:

// main.cpp

##### Share on other sites
i changed the if statements....and revamped a little of the code. i can''t believe i made that mistake! LOL

i am going to try using "switch" and see what that does. this really is a spin off of the "guess the number" game, you are right. hehe....i just wanted to create a challenge and see if i could get my own program with just the resources i have and small knowledge.

already i am feeling more confident of my basic knowledge. enough to keep going after pointers & arrays. i want to master the simple programs like this one...

thanks for your helps...!!

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 29
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631740
• Total Posts
3001959
×