RPG Battles

I have been working at this little program for sometime. I feel it is finally ready for ridicule, and by that I mean is it any good. Bad, Good Enough, Good you know that sort of thing. To start off with will I be able to ever add it in with the finished produced of this ginormous thing at the end of this thread link?http://www.programmingforums.org/thread48146.html

Well here it is. Do not just look at it compile & run it.
/*
Written By: Smjprogrammer 6/5/2014
Changes made on  6/14/2014
Changes made on 12/21/2014
Changes made on  1/30/2015

The purpose of this program is to eventually add it into a simple console based RPG. 
Weather it can be or not is yet to be determined.
*/

#include <iostream>
#include <ctime>
#include <Windows.h>
using std::cout;

class RPG_Battle_Simulation
{
public:
	RPG_Battle_Simulation()
	{
		srand((unsigned int)time(nullptr));// Seed the random number generator.
		SetConsoleTitle(TEXT("RPG Battle"));// Title.
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0A);// Text Color.
	}// Constructor Function.

	void Battle()
	{
		int enemy = 8, enemy_attack, player = 8, player_attack;

		for (;;)
		{
			enemy_attack = rand() % 3;
			player_attack = rand() % 3;

			if (enemy <= 0)
			{
				cout << "You defeated the enemy!\n";
				break;
			}
			else if (player <= 0)
			{
				cout << "Game Over.\a\n";
				break;
			}

			// HP stands for health points.
			cout << "Enemy  HP: " << enemy << ' ' << "Damage Taken: " << player_attack << '\n';
			cout << "Player HP: " << player << ' ' << "Damage Taken: " << enemy_attack << '\n';

			enemy -= player_attack;
			player -= enemy_attack;

			system("pause & cls");
		}
	}// Turn Based Battle System Simulator Function.
};

int main()
{
	RPG_Battle_Simulation play;

	play.Battle();
}// main.cpp

Comments

  • Nothing much to criticize.

    int enemy = 8, enemy_attack, player = 8, player_attack;
    
    I would break this up to at least two sets. Chaining declarations can get hard to read, and even more-so when you mix in assignments.
    int enemy, player, enemy_attack, player_attack; 
    enemy = player = 8;
    

    Also, you should be aware of the design decision to process both attacks in the same game tick. By doing so, you give the player a slightly better chance of winning (ties on the final round which result in both players health being below zero will allow the player to win). More importantly, should you wish to extend this game to include multiple fights, tracking the health across that fight will be necessary, unless your game design is such that you want each fight to start at full health. In the prior case, you could end up with a zombie player running around with 0 health, only to die immediately at the start of the next fight. Not really a criticism, just something I noticed.
    system("pause & cls");
    
    As you may be aware, calling system is non-portable and can also be a security risk if implemented improperly. Not a concern for a small console program such as this, but generally should be avoided.
  • I agree with the system calls, and just have never found anything better. I do however know that this will never be anything past console stuff, so I feel there is nothing much to worry about. You give a lot of great points on design. I have not hasted any of it because I feel it will come in good time. I have run the program a lot, and I almost always lose maybe just bad luck. However if we are going to change anything lets change the tick count first. How do we make that more equal playing ground?
  • Its less about the tick and more about when the checks are performed. I was a bit unclear about that in my previous post.

    To remove the tie case, each health calculation and death check needs to happen directly after the attack was made.

    i.e.

    player1 attack
    player2 health check
    player2 attack
    player1 health check

    What you have looks like this:

    player1 attack
    player2 attack
    player2 health check
    player1 health check

    If player2 health check fails, the battle ends and the player1 check is never performed leading to the slight bias in a tie case.

    The alternative solution presents a new problem, however. Now, the leading player has an advantage by striking first. This is common in any turn based game. Once players have stats such as "speed" you can use that to determine who goes first. Until then, you'll want to select it randomly.
  • I have been leaving stats out of it for two reasons. One this is just all for fun, and two it would just make things look out of control in a text based game.
Sign In or Register to comment.