Whizzy 0 Newbie Poster

As requested by a friend in email, I wrote a a simple attack routine in a 20d ad&d style. As you all can see, I'm not a programmer. I dabble. I was trying to show how the attack is calculated, nothing much more than that. Though this code works, it is my hope that a real programmer will see what I did, and rewrite it so it is better coded.

#include <cstdlib>
#include <iostream>
#include <ctime>
#include "windows.h"
using namespace std;
/* 
 This routine was requested from a friend.
 Though I am not a programmer, and this could probably have been written
 100 times better from a real programmer, it does show the *basics* for
 an ad&d style attack on a monster. 
 
 Roll a 20 sided die (atdie)
 check monsters armor rating (monac)
 if atdie is greater > then destroy monster ac first
 work on monsters hit points 
 
 You can also incorporate other factors into this...
 Players physical attributes, agility, level and so on along with
 monsters agility, and so forth. It cam be as detailed and in depth
 as you like. 
 
 Of course, you should also allow the monster to attack you back.
*/




int wepdam=5;  // weapon damage
int exp; // players experience points
int monac=4; // Monsters armor rating points
int monhits=40; // hit points to kill monster
int monmaxdam=13; // variable I threw in
int monexp=50; // monster experience value
int mongold=20; // monster gold value
int gold; // players gold

// attack monsters armor
int attackac (int attdie,int atdamage,int atmonac){
         atdamage=atdamage/2;
         if (atmonac==0){
                       cout <<"Returning from attack ac function"<<endl;
                       return(atmonac);
                       }
         if (attdie==atmonac){
                           cout << "The monster blocked your strike."<<endl;
                           atdamage=0;
                           return(atdamage);
                           }
         if (atmonac>atdamage){
                      atmonac=atmonac-atdamage;
                      cout<<"You have damaged your opponnents ac."<<endl;
                      return(atmonac);
                      }
         if (atmonac<=atdamage){
                            cout << "You have destroyed your opponnents armor."<<endl;
                            atmonac=0;
                            return(atmonac);
                            }
                            }
                            
// monster dies
int mondie(){
exp=exp+monexp;
gold=gold+mongold;
cout << "Congrats on killing the creature."<<endl;

}                            
                            
// here's where the real hack and slash code starts...
int main(int argc, char *argv[])
{
    srand ( time(NULL) );
    for (int i=1;i<50;i++){
        int atdie=rand()%20+1;
        int damage=rand()%wepdam+1;

                
             Sleep(500);
             cout <<"You now have "<<exp<<" experience points and " << gold << " gold"<< endl;
             cout << "\n\nMonster has "<<monhits<<" hit points."<<endl;

if (monac>0){
             cout << "Monster has "<<monac <<" ac points."<<endl;
             }
             
if (monac>0){
             monac=attackac(atdie,damage,monac);
             }
                          
if (atdie==1){
              cout << "Complete miss. You suck."<<endl;
              damage=0;
              }
              
if (atdie==20){
               damage=damage*2;
               cout << "Critical hit! Way to go!" << endl;
               exp+=10;
               monhits=monhits-damage;
               cout << "You strike for " << damage << " points!"<< endl;
               }
                          
if (atdie!=1 && atdie!=20) {
if (atdie<monmaxdam) {
            cout << "The creature has dodged your attack."<< endl;
            }
if (atdie==monmaxdam){
                  cout << "The monster blocked your strike."<<endl;
                  damage=0;
                  }
                  
if (atdie>monmaxdam) {
                     monhits=monhits-damage;
                     cout << "You strike for " << damage << " points!"<< endl;
                     exp=exp+rand()%wepdam;
                     if (monhits<=0){
                                     monhits=0;
                                     cout << "The creature falls to the ground, lifeless." << endl;
                                     mondie();
                                     i=51; // backwards ass way to end the loop
                                     }
                                     }
                                     }
                                     }
                                     system("PAUSE");
    return EXIT_SUCCESS;
}

Ok guys... have at it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.