Program Runs But Doesn't Work How I Want It To

Hi, I'm try to simulate a fight between two people. Two xs are to walk across the screen and perform some mathematical calculations to decide who wins the fight. The program runs fine but seems to totally ignore the part where it is supossed to figure out who wins. I looked over it several times and couldn't find anything wrong. I'm sorry if my code is messy or confusing but please have a look at it. I added notes so its easier to understand.
________________________________________________________________________________



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
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <time.h>
#include <windows.h>

using namespace std;

int sleep();

int main()

{

 srand(time(NULL));

    short int width = 50; //width of the board
    short int heigth = 20; //heigth of the board

    short int win1 = 0;
    short int win2 = 0;

    short int x = 1; // x axis for first person
    short int y = 1; // y axis for first person
    short int a = 50; // x axis for second person
    short int b = 1; // y axis for second person

    short int att1 = 0; //attack for first person
    short int def1 = 0; //defense for first person
    short int agl1 = 0; //agility for first person
    short int hp1 = 100; //hp for first person

    short int att2 = 0; //attack for second person
    short int def2 = 0; //defense for second person
    short int agl2 = 0; //agility for second person
    short int hp2 = 100; //hp for second person

    short int result1 = 0; //result #1 for net damage done after attack and defense factors are taken in
    short int result2 = 0; //result #1 for net damage done after attack and defense factors are taken in


    //drawing the board

    for ( short int z = 0; z > -1; z++ ) //creating a infinate loop
	{

	for ( short int j = 0; j <= heigth; j++ ) //heigth of the board

	{

	for ( short int i = 0; i <= width; i++ ) //width of the board

	{

    if ( a == x ) { //if both the xs are in the same spot on the board

do{

    att1 = rand() % 10 + 1;
    att2 = rand() % 10 + 1;

    def1 = rand() % 5 + 1;
    def2 = rand() % 5 + 1;

    agl1 = rand() % 10 + 1;
    agl2 = rand() % 10 + 1;

    if ( agl2 > 1 ) { //there is a one in ten chance that agility will dodge the attack

    if ( def2 >= att1 ) { //if the defense is greater than the attack, then the result damage is 0
    result1 = 0;
    }

    if ( def2 < att1 ) { //if the defense is less than the attack, then the result damage is the defference
    result1 = att1 - def2;
    }

    hp2 = hp2 - result1; //calculate the new hp

    }

    if ( agl1 > 1 ) { //same thing with the other person

    if ( def1 >= att2 ) {
    result2 = 0;
    }

    if ( def1 < att2 ) {
    result2 = att2 - def1;
    }

    hp1 = hp1 - result2;

    }

    if ( hp1 < 0 ) {
    hp1 = 0;
    }

    if ( hp2 < 0 ) {
    hp2 = 0;
    }

    }while( hp1 != 0 && hp2 != 0 ); //keep doing it until one of them reaches 0 hp

    if ( hp1 = 0 ) {
    win2 = 1;
    }

    if ( hp2 = 0 ) {
    win1 = 1;
    }
Sleep(1000); //testing if the lines above that calculate the damage done is happening ( its not )
    }

        else if ( x != a && win1 != 1 && i == x && j == y ) { //only print a x if their not next to eachother and one of them isn't dead

		cout<<"x";
		}

        else if ( x != a && win2 != 1 && i == a && j == b ) { //same as above

		cout<<"x";
		}

		else { //otherwise print this, the _ is what makes up the board

			cout<< '_';

		}

	}
		cout<< "\n";
	}

    system("CLS");

    x++; //moves the first x

    a = a - 1; //moves the second x

	}

return 0;

    }
Last edited on
Please format your code with [code][/code] tags (the <> format option). Have you tried to debugging it to see what path the execution is taking?
1
2
3
//drawing the board

for ( short int z = 0; z > -1; z++ ) //creating a infinate loop 


Why do you need an infinite loop to draw the board? infinite loops are generally a bad idea.

You also need some functions.

@Zhuge

I'm sorry, I'm a beginner so I don't know what you mean by the tags and format option. As far as debugging, I have tested the hp of both people by printing it to a text file every time they hit each other. It shows that they are fighting and both their hp is getting lower and lower, but they both end up being zero in the end.

Example taken from text document:

96 98
94 96
88 96
88 96
88 88
88 85
85 85
85 85
83 85
77 81
73 81
69 81
68 81
60 75
60 75
60 67
60 66
57 66
54 66
49 61
41 53
33 53
33 47
32 45
30 43
30 39
27 39
27 31
27 24
24 21
24 16
20 10
19 10
13 7
11 0
0 0
0 0
0 0

and then it goes on to print a ton more 0s
Idk if that is what you meant by debugging to see what path the execution is taking though.

@TheIdeasMan

I'm pretty sure the infinite loop is needed to constantly update the board and show the characters moving. I would probably eventually change this to some other condition but I'm leaving it at that for now. However, if there is another way that works the same, I'm open for suggestions. And also what parts of the program should I be making functions for?
Last edited on
I think that using the text file narrowed it down to the fact that the problem is within the chunk of code that does the calculations.
The code tags button looks like this <> and is on the right under format: Select your code then press the <> button. You can also do quotes, like below, and program output.

I'm pretty sure the infinite loop is needed to constantly update the board and show the characters moving
.

If you want to loop, put the code in a while loop, don't have an infinite one - it's unnecessary and very bad. Use a while loop in conjunction with a bool variable to help decide when the loop terminates.

Functions help split up your code into smaller jobs, so instead of having 200 lines of code in one go, you can split it up. This is especially handy for reuse of code. Example Drawing the board could be a function.

What I do is write down the methodology (all the steps to carry out) as comments. This helps organise your thoughts and get things happening logically. You can start with very general ideas first, then go back and refine them with more detail. You can repeat this process until you have everything sorted out.

Then go back and write code that does what's written in the comment, leave the comments there as they serve as documentation.

The comments can also help you see which code should go into a function.
I think I just need redo some parts in a more organized way and add some functions. Just structuring it better may fix the problem. I'll also change the loop. Thanks for the help :).
Topic archived. No new replies allowed.