Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: C++ Help  (Read 1223 times)

0 Members and 1 Guest are viewing this topic.

PoeFacedKilla

Prussian Killer Bee
C++ Help
« on: January 21, 2014, 09:03:25 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 269
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;

}
Logged
the Indyboard - User Generated Social Forum | Now With Even More Discussion!
Poe, The Independent Programmer
  • Zelda Shrine

PoeFacedKilla

Prussian Killer Bee
Re: C++ Help
« Reply #1 on: January 21, 2014, 09:11:06 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 269
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;

}
Logged
the Indyboard - User Generated Social Forum | Now With Even More Discussion!
Poe, The Independent Programmer
  • Zelda Shrine

PoeFacedKilla

Prussian Killer Bee
Re: C++ Help
« Reply #2 on: January 21, 2014, 09:17:52 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 269
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;

}
Logged
the Indyboard - User Generated Social Forum | Now With Even More Discussion!
Poe, The Independent Programmer
  • Zelda Shrine
Re: C++ Help
« Reply #3 on: January 21, 2014, 03:56:19 pm »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6590
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.
Logged
Re: C++ Help
« Reply #4 on: January 21, 2014, 05:22:59 pm »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
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.
Logged
Quote
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.

But if my friends and I gather, wear black robes, and say  "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
  • Development Blog
Pages: [1]   Go Up

 


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



Page created in 0.104 seconds with 32 queries.