I need help with my Projects Knight fighting

I'm having trouble writing a main() It needs to include
1. ask 10 questions
2. create two knights
3. Have each knight display.
4. have the two knights joust until one is unhorsed or one is exhausted
A. While loop: ask 4 questions in the condition of the while statement:
1. knight1 is not exhausted and
2. knight2 is not exhausted and
3. knight1 is on horse and
4. knight2 is on horse
B. Inside while loop, have knights attack each other, unhorse when appropriate.
C. Inside while loop, have each knight display.
D. (end of loop. Back up to 4A)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<iostream>
#include<string>
#include"Random.h"

using namespace std;
   
class Weapon
   {
   private:	
       int hit_chance;
       int stamina_required;	
 	   string weapon_type;
   public:
      void display (void);
      Weapon(string type, int sr, int hc);	
	  bool did_you_hit();
	  int get_stamina_required(void);

  };
	
Weapon:: Weapon(string type, int sr, int hc)
    :hit_chance(hc), stamina_required(sr), weapon_type(type)
  {
  }

 void Weapon::display(void)
 {
    std::cout<< "hit chance: " << hit_chance << endl;
    std::cout<< "stamina required: " << stamina_required << endl;
  }
 int Weapon::get_stamina_required(void)
 {
   return stamina_required;
}

 bool Weapon:: did_you_hit()

{
   Random r(1,100);
   int prob=r.get();
   if (hit_chance<=prob)
    {
        did_you_hit()==false;
    }
    else
     did_you_hit()==true;
  }
  

 class Knight
 {
       private:
          string name;
		  int stamina;	
		  
       public:
          Knight(string n, int stam, const Weapon);
          Weapon weapon_in_hand;
		  bool attack();
          bool on_horse();		
		        
  };

 Knight::Knight(string n, int stam, const Weapon)
	:name(n), stamina(stam)
 {
 }

  bool Knight::on_horse()
  {
    if (stamina<=0)
   {
	  std::cout << name << " is exhausted." << endl;
      return false;
   }
  
    else
    return true;
  }

  
bool Knight::attack(const Weapon &w)
{ stamina -= w.get_stamina_required();
   did_you_hit; 
}
{					 
if did_you_hit() 
{
    stamina -= get_stamina_required();
}
else
 {
	 stamina -=  stamina_required;
}
}

 int main()

 {
   Knight k1;
   cout << "What is the first knights name?" << endl;
   cin >> k1.name;
   cout << "What is" << k1.name << "stamina?" << endl;
   cin >> k1.stamina;
   cout << "What is knights weapon?" << endl;
   cin >> k1.weapon;
   cout << "What is the weapon's hit chance?" << endl;
   cin >> w1.hit_chance;
   cout << "What is the weapon's stamina required to use?" << endl;
   cin >> w1.sr;

   Knight k2;	
   cout << "What is the second knights name?" << endl;
   cin >> k2.name;
   cout << "What is" << name << "stamina?" << endl;
   cin >> k2.stamina;
   cout << "What is knights weapon?" << endl;
   cin >> k2.weapon;
   cout << "What is weapon's hit chance?" << endl;
   cin >> w2.hc;
   cout << "What is weapon's stamina required?" << endl;
   cin >> w2.sr;
 

 
   while(k1==on_horse)
   {
     k1.attack;
     if(did_you_hit==false)
     {
       k2.attack;
     }
     else
 
   }
 
 }	


But my is not working

this is random.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef _RANDOM_H_
#define _RANDOM_H_
// File:  Random.h
// Version: 1.4
// class declaration

class Random {
  public:
    Random(int low, int high); //constructor
    int get(void) const;
  private:
    int low;
    int diff;
    static void seed(void);
    static int seedcount;
};
#endif 
Last edited on
.
Last edited on
.
Last edited on
Umm what is line 5 supposed to be doing? You are saying "address of did you hit function" maybe you mean

1
2
3
4
if( did_you_hit() )
{
    stamina -= get_stamina_required();
}
it gives me undefined did_you_hit() and get_stamina_required()
anyone?
How can I make it works from line 82 to the end? it has a lot of error right now
it gives me undefined did_you_hit() and get_stamina_required()
did_you_hit() and get_stamina_required() are methods on the Weapon class. You're calling them as if they were methods of Knight.

Any time you find yourself writing duplicated code, that's usually an indication that you'd be better off writing a function or method to do that. So, for lines 100 - 122, you'd be better off writing a single function or method for inputting the data for a single knight, and then calling it twice.

Lines 100 - 122 should also be giving you a lot of compiler errors about attempting to access private data members. Do you understand the difference between private members and public members?

Also, you need to be sure you understand the correct syntax for calling a method. Lines 84, 93, 128 and 131 all look like you probably intended to call some methods, but you're not using the write syntax for a call.
Topic archived. No new replies allowed.