Little Game

Posts 1–15 of 19 · Page 1 of 2
Little Game
Well to practise my coding in C + + I started to create games, I have created this one

Instructions

You Set up your warriors in 3 values
Enemy Set up his warriors

You attack, choose value from 0-9
Enemy attack from 0-9

You have 3 soldiers

If he hits you a soldier it loses 1 HP, soldiers have 3 HP
*etc*

If all soldiers are dead you Lose.

Code:
#include <ctime> 
#include <cstdlib>
#include <iostream>
using namespace std;
int main(){
		
		int vida1 = 3;
		int vida2 = 3;	
		int amigo1 = 2;
		int amigo2 = 2;
		int amigo3 = 2;	
		int enemy1 = 2;
		int enemy2 = 2;
		int enemy3 = 2;
		int map1[ 10 ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		int map2[ 10 ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		int map3[ 10 ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		int hit1;
		time_t t;
		time(&t);
		srand(t);
		int warrior1;
		int warrior2;
		int warrior3;
		int gameover = 2;
		
		cout << "----------------------------" << endl;
		cout << "| Welcome To WarriorKiller |" << endl;
		cout << "----------------------------" << endl;
		cout << "Choose Position Of Warrior 1  -> ";
		cin >> warrior1;
		cout << "Choose Position Of Warrior 2  -> ";
		cin >> warrior2;
		cout << "Choose Position Of Warrior 3  -> ";
		cin >> warrior3;
		

		map1[warrior1-1] = warrior1;
		map2[warrior2-1] = warrior2;
		map3[warrior3-1] = warrior3;

		cout << "----------------------------" << endl;
		cout << "|       Warrior Board       |" << endl;
		cout << "----------------------------" << endl;
		
		cout << "------------------------------" << endl;
		for (int i=0;i<10;++i){
		 cout << "|" << map1[i] << "|";
		}
		cout << endl;
		for (int y=0;y<10;++y){
		 cout << "|" << map2[y]<< "|";
		}
		cout << endl;
		for (int x=0;x<10;++x){
		 cout << "|" << map3[x]<< "|";
		}
		cout << endl;
		cout << "------------------------------" << endl;
		

		
		do{
		int hit2 = rand() % 9 + 1;
		int warrior1x = rand() % 9 + 1;
		int warrior2x = rand() % 9 + 1;
		int warrior3x = rand() % 9 + 1;
		cout << "----------------------------" << endl;
		cout << "|   !  Attacking Time  !   |" << endl;
		cout << "----------------------------" << endl;
		cout << "Select Number To Attack (0 - 9) ->  ";
		cin >> hit1;
		if (hit1 == warrior1x){
			cout << "You Attacked Enemy Warrior 1 !" << endl;
			--enemy1;
		} else if (hit1 == warrior2x) {
			cout << "You Attacked Enemy Warrior 2 !" << endl;
			--enemy2;
		}else if (hit1 == warrior3x) {
			cout << "You Attacked Enemy Warrior 3 !" << endl;
			--enemy3;
		
		}else {
			cout << "You Miss !" << endl;
		}
			cout << "Enemy Attacks ->  ";
			cout << hit2 << endl;
			
		if (hit2 == warrior1){
			cout << "Enemy Attacked Warrior 1 !" << endl;
			--amigo1;
		}else if (hit2 == warrior2){
			cout << "Enemy Attacked Warrior 2 !" << endl;
			--amigo2;
		}else if (hit2 == warrior3){
			cout << "Enemy Attacked Warrior 3 !" << endl;
			--amigo3;
		} else {
			cout << "Enemy Miss !" << endl;
		}
		if (amigo1 == 0){
			--vida1;
		}else if ( amigo2 == 0){
			--vida1;
		}else if (amigo3 == 0){
			--vida1;
		}
		if (enemy1 == 0){
			--vida2;
		}else if (enemy2 == 0){
			--vida2;
		}else if (enemy3 == 0){
			--vida2;
		}
		if (vida1 == 0){
			gameover = 1;
		}else if (vida2 == 0){
			gameover = 1;
		}
		}while(gameover == 2);
		cout << "GAME OVER" << endl;
		system("PAUSE");
}
Any tip to improve code will be accepted ;D
switch statements are in this case an improvement.

making gameover a boolean and declaring more variables at once. (int warrior1, warrior2, warrior3
ultilize OOP

-create a friend class
-- attack function
-enemy class
-- attack function
*This gonna help make your code look nice cause all you have to call is amigo1.attack(); or enemy1.attack();. Plus if you wanted to add more features like maybe "use potion" or "retreat", its gonna be much easier.

OR you could make the attack function global so you don't need to have it in there twice, since its essentially the same.
Sometimes I make ex :

Code:
int main(){
attack1;
}
void attack1(){
cout << "BLABLA" << endl;
}
sometimes I get the error that the attack1 is undefined ._.
you need to declare it first
put

void attack1(); at the top to tell the compiler that there is a function named attack following
Oh thanks

/msgtshrt
pst. make a text based rpg next its great OOP practice.


hint: create new header and source files for your soldiers and enemies that way your main function will looks simple and sexy
Nice release you dshould post a screen shot of the game itself.
You also forgot the brackets next to attack1.
no OOP whatsoever..
Yeah, it's so messy and does not utilise the power of Object Oriented Programming. It's like DOS/Batch, C++ style.

Also, usage of: system("pause") is a big no no. It's inefficient and uses a crap load of resources just for 1 little command.
It's a very expensive and resource heavy function call. It's like using a bulldozer to open your front door. It works, but the key is cleaner, easier, cheaper. What system() does is:
* suspend your program
* call the operating system
* open an operating system shell (relaunches the O/S in a sub-process)
* the O/S must now find the PAUSE command
* allocate the memory to execute the command
* execute the command and wait for a keystroke
* deallocate the memory
* exit the OS
* resume your program
There are much cleaner ways included in the language itself that make all this unnessesary.
A better alternative would be something like this:
[highlight=c++]std::cout << "Press any key to continue...";
std::cin.get();[/highlight]
i made the game, with all the edits people said, works for me and it's a funny game
can u post the code with all the edits please, thanks
yh funny game
has anyone got the source code for it (for the better one~)?
Posts 1–15 of 19 · Page 1 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us