weird bug

when I run the game and answer the questions the first battle has weird symbols to the right and the y axis is inverted even when I flip it around.

[code]
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
begin:
string name;
string surname;
int ehp = 100;
int edefense = 0;
int eattack = 1;
int positionx = 5;
int positiony = 1;
int epositionx = 5;
int epositiony = 10;
int hp = 100;
int defense = 0;
int attack = 1;
int money = 100;
int maxmana = 100;
int mana = 100;
bool male;
string answer;
int badgood = 0;
int luck;

cout << "enter your warriors first name" << endl;
cin >> name;
cout << "now their lastname" << endl;
cin >> surname;
system ("CLS");
cout << "long intro unable to fit into forum submission"<< endl;
system ("pause");
system ("CLS");
cout << "to begin this game you will be asked some basic questions, please answer all multiple choice questions in capitol letters" << endl;
cout << "are they a male?" << endl;
cin >> answer;
if (answer == "yes")
{
male = "1";
attack = attack + 1;
}

if (answer == "no")
{

male = "0";
defense = defense + 1;
}

system("CLS");


cout << "your enemy falls at your feet and offers his life" << endl;
cout << "A. you kill him with your trusty sword" << endl;
cout << "B. you make him your slave" << endl;
cout << "C. you free him" << endl;

cin >> answer;

if (answer == "A")
{
badgood = -10;
attack = attack + 1;
}

if (answer == "B")
{
badgood = -5;
defense = defense + 1;
}

if (answer == "C")
{
badgood = 10;
}
system("CLS");

cout << "you are stopped on the street by a mysterious hooded figure and asked for money, what do you do?" << endl;
cout << "A. you give it to them" << endl;
cout << "B. you say no" << endl;
cout << "C. you give it to them asking for something in return" << endl;
cin >> answer;
if (answer == "B")
{
badgood = badgood - 1;
}

if (answer == "A")
{
badgood = badgood + 3;
}

luck = rand() % 100 + 1;

if (answer == "C" && luck > 50 )
{
cout << endl << "they cast a strange spell on you making you feel powerfull";
defense = defense + 1;
attack = attack + 1;
hp = hp + 1;
maxmana = maxmana + 1;
mana = mana + 1;
}

system ("CLS");

cout << "thank you for answering these questions in future versions, there will be more questions to better determine your character" << endl ;

system("pause");

system("CLS");

cout << "please look in your manual for controls" << endl ;

system("pause");

system("CLS");

cout << "you hear Cordata VIII begin the games and you remember who you must kill" << endl ;

system("pause");

system("CLS");

battle:

positionx = 5;
positiony = 1;

epositionx = 5;
epositiony = 10;

battleturn:

char arena [10] [10] = {0};

arena [positionx] [positiony] = '@';

arena [epositionx] [epositiony] = 'e';

cout << arena [1] [1] << arena [2] [1] << arena [3] [1] << arena [4] [1] << arena [5] [1] << arena [6] [1] << arena [7] [1] << arena [8] [1] << arena [9] [1] << arena [10] [1] << endl << arena [1] [2] << arena [2] [2] << arena [3] [2] << arena [4] [2] << arena [5] [2] << arena [6] [2] << arena [7] [2] << arena [8] [2] << arena [9] [2] << arena [10] [2] << endl << arena [1] [3] << arena [2] [3] << arena [3] [3] << arena [4] [3] << arena [5] [3] << arena [6] [3] << arena [7] [3] << arena [8] [3] << arena [9] [3] << arena [10] [3] << endl << arena [1] [4] << arena [2] [4] << arena [3] [4] << arena [4] [4] << arena [5] [4] << arena [6] [4] << arena [7] [4] << arena [8] [4] << arena [9] [4] << arena [10] [4] << endl << arena [1] [5] << arena [2] [5] << arena [3] [5] << arena [4] [5] << arena [5] [5] << arena [6] [5] << arena [7] [5] << arena [8] [5] << arena [9] [5] << arena [10] [5] << endl << arena [1] [6] << arena [2] [6] << arena [3] [6] << arena [4] [6] << arena [5] [6] << arena [6] [6] << arena [7] [6] << arena [8] [6] << arena [9] [6] << arena [10] [6] << endl << arena [1] [7] << arena [2] [7] << arena [3] [7] << arena [4] [7] << arena [5] [7] << arena [6] [7] << arena [7] [7] << arena [8] [7] << arena [9] [7] << arena [10] [7] << endl << arena [1] [8] << arena [2] [8] << arena [3] [8] << arena [4] [8] << arena [5] [8] << arena [6] [8] << arena [7] [8] << arena [8] [8] << arena [9] [8] << arena [10] [8] << endl << arena [1] [9] << arena [2] [9] << arena [3] [9] << arena [4] [9] << arena [5] [9] << arena [6] [9] << arena [7] [9] << arena [8] [9] << arena [9] [9] << arena [10] [9] << endl << arena [1] [10] << arena [2] [10] << arena [3] [10] << arena [4] [10] << arena [5] [10] << arena [6] [10] << arena [7] [10] << arena [8] [10] << arena [9] [10] << arena [10] [10] << endl;

cin >> answer;

if(answer == "w") positiony = positiony + 1;
if(answer == "s") positiony = positiony - 1;
if(answer == "a") positionx = positionx - 1;
if(answer == "d") positionx = positionx + 1;

system("CLS");

if(hp > 0 && ehp > 0) goto battleturn;
if(hp < 0) goto begin;

}
[code/]
I did not get your question but I have a suggestion in movement

include the header file :
#include <conio.h>
replace cin>>answer; with
answer=getch();

the program will be like

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
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <string>
#include <cstdlib>
#include <conio.h>   //<<<<<< added this

using namespace std;

int main()
{
begin:
string name;
string surname;
int ehp = 100;
int edefense = 0;
int eattack = 1;
int positionx = 5;
int positiony = 1;
int epositionx = 5;
int epositiony = 10;
int hp = 100;
int defense = 0;
int attack = 1;
int money = 100;
int maxmana = 100;
int mana = 100;
bool male;
string answer;
int badgood = 0;
int luck;

cout << "enter your warriors first name" << endl;
cin >> name;
cout << "now their lastname" << endl;
cin >> surname;
system ("CLS");
cout << "long intro unable to fit into forum submission"<< endl;
system ("pause");
system ("CLS");
cout << "to begin this game you will be asked some basic questions, please answer all multiple choice questions in capitol letters" << endl;
cout << "are they a male?" << endl;
cin >> answer;
if (answer == "yes")
{
male = "1";
attack = attack + 1;
}

if (answer == "no")
{

male = "0";
defense = defense + 1;
}

system("CLS");


cout << "your enemy falls at your feet and offers his life" << endl;
cout << "A. you kill him with your trusty sword" << endl;
cout << "B. you make him your slave" << endl;
cout << "C. you free him" << endl;

cin >> answer;

if (answer == "A")
{
badgood = -10;
attack = attack + 1;
}

if (answer == "B")
{
badgood = -5;
defense = defense + 1;
}

if (answer == "C")
{
badgood = 10;
}
system("CLS");

cout << "you are stopped on the street by a mysterious hooded figure and asked for money, what do you do?" << endl;
cout << "A. you give it to them" << endl;
cout << "B. you say no" << endl;
cout << "C. you give it to them asking for something in return" << endl;
cin >> answer;
if (answer == "B")
{
badgood = badgood - 1;
}

if (answer == "A")
{
badgood = badgood + 3;
}

luck = rand() % 100 + 1;

if (answer == "C" && luck > 50 )
{
cout << endl << "they cast a strange spell on you making you feel powerfull";
defense = defense + 1;
attack = attack + 1;
hp = hp + 1;
maxmana = maxmana + 1;
mana = mana + 1;
}

system ("CLS");

cout << "thank you for answering these questions in future versions, there will be more questions to better determine your character" << endl ;

system("pause");

system("CLS");

cout << "please look in your manual for controls" << endl ;

system("pause");

system("CLS");

cout << "you hear Cordata VIII begin the games and you remember who you must kill" << endl ;

system("pause");

system("CLS");

battle:

positionx = 5;
positiony = 1;

epositionx = 5;
epositiony = 10;

battleturn:

char arena [10] [10] = {0};

arena [positionx] [positiony] = '@';

arena [epositionx] [epositiony] = 'e';

cout << arena [1] [1] << arena [2] [1] << arena [3] [1] << arena [4] [1] << arena [5] [1] << arena [6] [1] << arena [7] [1] << arena [8] [1] << arena [9] [1] << arena [10] [1] << endl << arena [1] [2] << arena [2] [2] << arena [3] [2] << arena [4] [2] << arena [5] [2] << arena [6] [2] << arena [7] [2] << arena [8] [2] << arena [9] [2] << arena [10] [2] << endl << arena [1] [3] << arena [2] [3] << arena [3] [3] << arena [4] [3] << arena [5] [3] << arena [6] [3] << arena [7] [3] << arena [8] [3] << arena [9] [3] << arena [10] [3] << endl << arena [1] [4] << arena [2] [4] << arena [3] [4] << arena [4] [4] << arena [5] [4] << arena [6] [4] << arena [7] [4] << arena [8] [4] << arena [9] [4] << arena [10] [4] << endl << arena [1] [5] << arena [2] [5] << arena [3] [5] << arena [4] [5] << arena [5] [5] << arena [6] [5] << arena [7] [5] << arena [8] [5] << arena [9] [5] << arena [10] [5] << endl << arena [1] [6] << arena [2] [6] << arena [3] [6] << arena [4] [6] << arena [5] [6] << arena [6] [6] << arena [7] [6] << arena [8] [6] << arena [9] [6] << arena [10] [6] << endl << arena [1] [7] << arena [2] [7] << arena [3] [7] << arena [4] [7] << arena [5] [7] << arena [6] [7] << arena [7] [7] << arena [8] [7] << arena [9] [7] << arena [10] [7] << endl << arena [1] [8] << arena [2] [8] << arena [3] [8] << arena [4] [8] << arena [5] [8] << arena [6] [8] << arena [7] [8] << arena [8] [8] << arena [9] [8] << arena [10] [8] << endl << arena [1] [9] << arena [2] [9] << arena [3] [9] << arena [4] [9] << arena [5] [9] << arena [6] [9] << arena [7] [9] << arena [8] [9] << arena [9] [9] << arena [10] [9] << endl << arena [1] [10] << arena [2] [10] << arena [3] [10] << arena [4] [10] << arena [5] [10] << arena [6] [10] << arena [7] [10] << arena [8] [10] << arena [9] [10] << arena [10] [10] << endl;

answer=getch(); //<<<< added this 

if(answer == "w") positiony = positiony + 1;
if(answer == "s") positiony = positiony - 1;
if(answer == "a") positionx = positionx - 1;
if(answer == "d") positionx = positionx + 1;

system("CLS");

if(hp > 0 && ehp > 0) goto battleturn;
if(hp < 0) goto begin;
}



Now the movement is smooth and clear
Last edited on
Possibly the longest line of code I've seen on these forums at 1805 characters. The program begs for the use of for-loops.

The weird characters are most likely the result of trying to access elements outside of the array arena[10][10], valid subscripts are in the range 0-9 and 0-9 so accessing arena[x][10] or arena[10][x] is an error.
thanks but my movement is still inverted
Well, either change the way you process the key-presses, or change the way the arena is displayed.
@klay2

This..
1
2
3
4
if(answer == "w") positiony = positiony + 1;
if(answer == "s") positiony = positiony - 1;
if(answer == "a") positionx = positionx - 1;
if(answer == "d") positionx = positionx + 1;


should look like this...
1
2
3
4
if(answer == "w") positiony = positiony--; // Subtracts 1, from positiony
if(answer == "s") positiony = positiony++; // Adds 1, to positiony
if(answer == "a") positionx = positionx++; // Adds 1, to positionx
if(answer == "d") positionx = positionx--; // Subtracts 1, from positionx 


Remember, that the left side of the arena, starts with 0, so to go left, you subtract 1 from your position, and add 1, to go right. Same with up and down. Top of screen is 0. If you're going south, or down, you add 1 and subtract 1, when going up.

Last edited on
yes Chervil I think you are right, in line 146 you can do a logic for it with a loop to be in 1 line and few characters

Something of this style perhaps:
1
2
3
4
    const int rows = 10;
    const int cols = 10;

    char arena [rows] [cols];


1
2
3
4
5
6
7
    // Display the board
    for (int i=0; i<rows; i++)
    {
        for (int j=0; j<cols; j++)
            cout << arena[i][j];
        cout << endl;
    }


But will it be slower then the regular cout<<""; ? because for 2 loops it will acquire some space in registers
Last edited on
The difference in execution time is trivial. But if you want to speed it up, treat each row as a character string, and output the whole line in one step.
1
2
3
4
5
6
7
8
9
10
11
    // Define and fill the array
    const int rows = 10;
    const int cols = 10;
    char arena [rows][cols+1]; // extra column for null terminator
    
    for (int i=0; i<rows; i++)
    {
        for (int j=0; j<cols; j++)
            arena[i][j] = '*'; // fill array with '*'
        arena[i][cols] = 0;    // Null Terminator
    }


Output it like this:
1
2
    for (int i=0; i<rows; i++)
        cout << arena[i] << endl;


or like this:
1
2
    for (int i=0; i<rows; i++)
        puts(arena[i]);


You could even define the entire board as one long single-dimension array with newline characters and a single null terminator. Then the output reduces to a simple puts(arena);. The downside is you have to calculate the subscripts like this: arena[y*rowlength + x] where rowlength is the number of character per row. At that stage it starts to look like the basis for a class where the details are handled internally and the code which uses it becomes very simple.
Last edited on
execution is trivial in this type, but in my first post about diamond program before I learn about recursive function, execution time was very noticeable
going to try to compare the different outputs when I get home
Topic archived. No new replies allowed.