ZFGC

Resources => Coding => Topic started by: PoeFacedKilla on January 21, 2014, 09:03:25 am

Title: C++ Help
Post by: PoeFacedKilla on January 21, 2014, 09:03:25 am
Ok, So I've got a simple txt game im building and what it does is rotate turns between two players (one being the computer) and they can either choose to deliver -25 points or take -10 if the computer (randomly decides) is using its shield.  Simple enough, but every time it compiles perfect yet no matter what I enter (1 or 2) it always takes 10 from the opponent and leaves me with full health.

Code: [Select]
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{

srand(static_cast<unsigned int>(time(0)));
short int my_health = 100;
short int enemy_health = 50;
short int action;
short int turn = 1;
short int shield = 0;
short int enemy_shield = 0;
short int random_number = rand();
short int play;

while( ( my_health > 0 ) && ( enemy_health > 0 ) )
{

if( turn == 1 )
{
turn = 2;
} else
{
turn = 1;
}

switch( turn )
{

case 1:
std::cout << "1. Attack | 2. Defend; Enter: " << std::endl;
std::cin >> action;
switch( action )
{

case 1:

if( enemy_shield == 1)
{
enemy_health -= 10;
enemy_shield = 0;
} else
{
enemy_health -= 25;
}
break;

case 2:

shield = 1;
break;

default:

break;

}
system("CLS");
std::cout << "My Health: " << my_health << " " << "\t\t | \t\t Enemy Health: " << enemy_health << std::endl;

case 2:
play = (random_number % 2 ) + 1;
switch( play )
{

case 1:
if( shield == 1 )
{
my_health -= 10;
shield = 0;
} else
{
my_health -= 25;
}
system("CLS");
std::cout << "My Health: " << my_health << " " << "\t\t | \t\t Enemy Health: " << enemy_health << std::endl;

break;

case 2:
enemy_shield = 1;
system("CLS");
std::cout << "My Health: " << my_health << " " << "\t\t | \t\t Enemy Health: " << enemy_health << std::endl;
break;

default:
system("CLS");
std::cout << "My Health: " << my_health << " " << "\t\t | \t\t Enemy Health: " << enemy_health << std::endl;
break;

}


}

}

return 0;

}
Title: Re: C++ Help
Post by: PoeFacedKilla on January 21, 2014, 09:11:06 am
fixed some basic problems but now it still is happening about every other time I compile and run

Code: [Select]
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{

srand(static_cast<unsigned int>(time(0)));
short int my_health = 100;
short int enemy_health = 100;
short int action;
short int turn = 1;
short int shield = 0;
short int enemy_shield = 0;
short int random_number = rand();
short int play;

while( ( my_health > 0 ) && ( enemy_health > 0 ) )
{

if( turn == 1 )
{
turn = 2;
} else
{
turn = 1;
}

switch( turn )
{

case 1:
std::cout << "1. Attack | 2. Defend; Enter: " << std::endl;
std::cin >> action;
switch( action )
{

case 1:

if( enemy_shield == 1)
{
enemy_health = enemy_health - 10;
enemy_shield = 0;
} else
{
enemy_health = enemy_health - 25;
}
break;

case 2:

shield = 1;
break;

default:

break;

}
system("CLS");
std::cout << "My Health: " << my_health << " " << "\t\t | \t\t Enemy Health: " << enemy_health << std::endl;
break;

case 2:
play = (random_number % 2 ) + 1;
switch( play )
{

case 1:
if( shield == 1 )
{
my_health = my_health - 10;
shield = 0;
} else
{
my_health = my_health - 25;
}
system("CLS");
std::cout << "My Health: " << my_health << " " << "\t\t | \t\t Enemy Health: " << enemy_health << std::endl;

break;

case 2:
enemy_shield = 1;
system("CLS");
std::cout << "My Health: " << my_health << " " << "\t\t | \t\t Enemy Health: " << enemy_health << std::endl;
break;

default:
system("CLS");
std::cout << "My Health: " << my_health << " " << "\t\t | \t\t Enemy Health: " << enemy_health << std::endl;
break;

}
break;


}

}

return 0;

}
Title: Re: C++ Help
Post by: PoeFacedKilla on January 21, 2014, 09:17:52 am
final update, this is what I have; now it works perfectly when attacking the computer but my health doesn't go down half of the time

Code: [Select]
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{

srand(static_cast<unsigned int>(time(0)));
short int my_health = 100;
short int enemy_health = 100;
short int action;
short int turn = 1;
short int shield = 0;
short int enemy_shield = 0;
short int random_number = rand();
short int play;

while( ( my_health > 0 ) && ( enemy_health > 0 ) )
{

switch( turn )
{

case 1:
std::cout << "1. Attack | 2. Defend; Enter: " << std::endl;
std::cin >> action;
switch( action )
{

case 1:

if( enemy_shield == 1)
{
enemy_health = enemy_health - 10;
enemy_shield = 0;
} else
{
enemy_health = enemy_health - 25;
}
break;

case 2:

shield = 1;
break;

default:

break;

}
system("CLS");
std::cout << "My Health: " << my_health << " " << "\t\t | \t\t Enemy Health: " << enemy_health << std::endl;
break;

case 2:
play = (random_number % 2 ) + 1;
switch( play )
{

case 1:
if( shield == 1 )
{
my_health = my_health - 10;
shield = 0;
} else
{
my_health = my_health - 25;
}
system("CLS");
std::cout << "My Health: " << my_health << " " << "\t\t | \t\t Enemy Health: " << enemy_health << std::endl;

break;

case 2:
enemy_shield = 1;
system("CLS");
std::cout << "My Health: " << my_health << " " << "\t\t | \t\t Enemy Health: " << enemy_health << std::endl;
break;

default:
system("CLS");
std::cout << "My Health: " << my_health << " " << "\t\t | \t\t Enemy Health: " << enemy_health << std::endl;
break;

}
break;

default:
system("CLS");
std::cout << "My Health: " << my_health << " " << "\t\t | \t\t Enemy Health: " << enemy_health << std::endl;
break;



}

if( turn == 1 )
{
turn = 2;
} else
{
turn = 1;
}

}

if( my_health > enemy_health )
{

system("CLS");
std::cout << "You Win, Game Over!" << std::endl;

} else
{
system("CLS");
std::cout << "You Lose, Game Over" << std::endl;
}

std::cin.get();
return 0;

}
Title: Re: C++ Help
Post by: 4Sword on January 21, 2014, 03:56:19 pm
In terms of how you have variables defined, if your program is going to be using the variables very actively use what is closest to the "native type" - use ints instead of short ints. You save space sure by using short ints but your program performance goes down; when you compile your program and the logic is converted into lower-level instructions, the compiler will have to add specific instructions which cast the short ints into what it uses more natively.

Also, the play variable defined in the enemy actions - because you aren't changing the random_number anywhere else, the play variable is going to have the same value in it every iteration.
Title: Re: C++ Help
Post by: Cassyblanca on January 21, 2014, 05:22:59 pm
Ignoring all of the structural issues for a minute (we all have to start out somewhere, I suppose)...

Firstly, if you're still at the stage when programming that you have everything inside of the main() function, do not use any modifiers to the variable type on top of ints - you don't understand well enough what the purpose is, and you're gaining no benefits at all for doing so.

Second, you should be using >=, not > for your health checks -- right now, if your health is reduced to zero, you're still alive according to your program (until you go below).

Third, you can replace your "variable = variable - number" and "variable = variable + number" with "variable -= number" and "variable += number," respectively.

Fourth, if you've got values that are either 1 or 0 (and never anything else), use booleans. Type is 'bool', and the values can be either true or false.

Fifth, your health doesn't go down half the time because you've programmed it such that the AI shields half the time. If you want to see the results of this directly, add an output when the enemy uses a shield. If you don't want the enemy shielding half the time, change the code responsible for determining the AI's action -- maybe generate a random number between 1 and 5, and if it's 1 or 2 it'll shield.

Edit
------
Also, what Diminish said (I didn't catch that the first time around) about the assignment random_number -- you need to assign to it every iteration of your while loop.

Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved