943,865 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4812
  • C++ RSS
Mar 23rd, 2004
0

game code help

Expand Post »
i found this off some site and when i build it in vis c++, the errors are with the two random functions. will someone tell me why and how i can fix this. the random on line 15 and randomize on line 36. undeclared identifiers.

C++ Syntax (Toggle Plain Text)
  1. /* Jason Cordes */
  2. /* COMBAT.CPP */
  3. /* 8/95 */
  4.  
  5. #include <stdlib.h> // random, randomize
  6. #include <fstream.h> // file i/o
  7. #include <string.h> // string commands (copy, concatenate, etc...)
  8. #include <ctype.h> // toupper (100 bytes)
  9. #include <conio.h> // clearscr(), getch()
  10.  
  11. int roll(int die=6, int throws=1, int modifier=0)
  12. {
  13. int total=0, index;
  14. for(index=1;index<=throws;index++)
  15. { total += random(die); total++; }
  16. total += modifier;
  17. return(total);
  18. }
  19.  
  20. void main()
  21. {
  22. int hp,mhp,THAC,mTHAC,AC,mAC,hitroll;
  23. int damage,mdamage,ALIVE,temp;
  24.  
  25.  
  26. hp = 32;
  27. mhp = 64;
  28. THAC = 12;
  29. AC = 5;
  30. mTHAC = 15;
  31. mAC = 4;
  32. damage = 10;
  33. mdamage = 8;
  34. ALIVE = 1;
  35.  
  36. randomize();
  37. cout << "\n\nStarting combat...";
  38. do{
  39. cout << "\n\nYour hp: " << hp;
  40. cout << "\nIt's hp: " << mhp << endl << endl;
  41. getch();
  42.  
  43.  
  44. cout << endl << " Attacking...";
  45. hitroll = roll(20);
  46. cout << "\nYou rolled a " << hitroll;
  47. if(hitroll >= (THAC-mAC))
  48. { cout << "\nHIT!";
  49. temp = roll(damage);
  50. mhp -= temp;
  51. cout << " You did " << temp << " point(s) of damage.";
  52. if(mhp <= 0)
  53. { ALIVE = 0;
  54. cout << "\nThe Skeleton is dead. You gain 10 xp.";} }
  55.  
  56. else
  57. { cout << "\nYou missed."; }
  58.  
  59. if(ALIVE != 0)
  60. {
  61. hitroll = roll(20);
  62. cout << "\nIt rolled a " << hitroll;
  63. if(hitroll >= (mTHAC-AC))
  64. { cout << "\n\nThe skeleton swings wildly at you...";
  65. cout << "\nHIT!";
  66. temp = roll(damage);
  67. hp -= temp;
  68. cout << " IT did " << temp << " point(s) of damage.";
  69. if(hp <= 0)
  70. { ALIVE = 0;
  71. cout << "\nYou are dead. Maybe someone will ressurect you.";} }
  72.  
  73. else
  74. cout << "\nIT missed."; }
  75.  
  76.  
  77. }while(hp > 0 && mhp > 0);
  78. cout << "\nHim: " << mhp << " You: " << hp << endl;
  79. }
Last edited by cscgal; Mar 23rd, 2004 at 10:13 pm.
Similar Threads
Reputation Points: 14
Solved Threads: 0
Newbie Poster
popo_prince is offline Offline
12 posts
since Mar 2004
Mar 23rd, 2004
0

Re: game code help

Just a few things about this code, whoever wrote doesn't really understand completely what is going on. The header fstream is for file i/o, which isn't used in this program at all, also cout, and cin are used so the iostream header must be included. For the random functions I think what is needed is randomize = srand (seed the random number generator) and random(die) can be reproduced from
C++ Syntax (Toggle Plain Text)
  1. (rand() % die) + 1

Also some compilers don't care about this, but some do, cout, and cin are contained in the namespace std, so you may need to fully qualify them i.e.
C++ Syntax (Toggle Plain Text)
  1. std::cout << ...
or put a
C++ Syntax (Toggle Plain Text)
  1. using namespace std;
at the top of your program.

Doing this, I still could not get it to compile because VS.net 2003 gives a fatal compile error.
Reputation Points: 12
Solved Threads: 2
Newbie Poster
Steu is offline Offline
11 posts
since Mar 2004
Apr 29th, 2008
0

Re: game code help

As the author of the code, I would offer in my own self defense that this code was written in 1994 before C++ was ansi compliant.

if anyone has questions about this code, 1. delete it and ask me what you really want to do and I'll give you something much cleaner to look at.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JasonCordes is offline Offline
1 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Sorting on Class members
Next Thread in C++ Forum Timeline: My simple game: what would i use





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC