Rate my "Dungeon Crawl"

Simple moving program. Is there a way to make it shorter? (I'm talking about that stuff in"define".. How to make computer remember my current location?)
Also, don't judge me about system("CLS"), I SWEAR I WON'T USE IT IN SERIOUS PROJECTS.

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
#include <iostream>
 #include <conio.h>
 #include <windows.h>
#define longcode  if (x[1]==" o ") a=1;\
if (x[2]==" o ") a=2;\
if (x[3]==" o ") a=3;\
if (x[4]==" o ")a=4;\
if (x[5]==" o ")a=5;\
if (x[6]==" o ")a=6;\
f (x[7]==" o ")a=7;\
if (x[8]==" o ")a=8;\
 if (x[9]==" o ")a=9;\
 if (x[10]==" o ")a=10;\
 if (x[11]==" o ")a=11;\
 if (x[12]==" o ")a=12;\
 if (x[13]==" o ")a=13;\
 if (x[14]==" o ")a=14;\
 if (x[15]==" o ")a=15;\
 if (x[16]==" o ")a=16;\
 if (x[17]==" o ")a=17;\
 if (x[18]==" o ")a=18;\
 if (x[19]==" o ")a=19;\
 if (x[20]==" o ")a=20;\
 if (x[21]==" o ")a=21;\
 if (x[22]==" o ")a=22;\
 if (x[23]==" o ")a=23;\
 if (x[24]==" o ")a=24;\
 if (x[25]==" o ")a=25;
using namespace std;

int main()
{
string x[10000];
int t=0,a=0,y;
do {x[t]=" . "; t++;} while (t<=30);
t=0;
x[1]= " o ";
do {t++; cout << x[t]; if (t%5==0) cout << endl;} while (t<=24);
t=0;
cout << "hello." << endl;
x[1]=" . ";
a=1;
while (true)
 {
  char y=_getch();

  if ((y=='s') and (a<21))
  {   system("CLS");
      if (t==0) x[t+6]=" o ";
      else if (t>0) x[t+5]=" o ";
      t=0;
      while (t<=24)
   {
         t++;
         cout << x[t]; if (t%5==0) cout << endl;
   }  longcode;
      t=a;
      x[t]=" . ";
  }

    if ((y=='w') and (a>=6))
  {   system("CLS");
      x[t-5]=" o ";
      t=0;
      while (t<=24)
   {
         t++;
         cout << x[t]; if (t%5==0) cout << endl;
   }  longcode;
      t=a;
      x[t]=" . ";
  }

      if ((y=='a') and (a!=1) and (a!=6) and (a!=11) and (a!=16) and (a!=21))
  {   system("CLS");
      x[t-1]=" o ";
      t=0;
      while (t<=24)
   {
         t++;
         cout << x[t]; if (t%5==0) cout << endl;
   }  longcode;
      t=a;
      x[t]=" . ";
  }

     if ((y=='d') and (a!=5) and (a!=10) and (a!=15) and (a!=20) and (a!=25))
  {   system("CLS");
      x[t+1]=" o ";
      t=0;
      while (t<=24)
   {
         t++;
         cout << x[t]; if (t%5==0) cout << endl;
   }  longcode;
      t=a;
      x[t]=" . ";
  }
 if (a==25) {cout << "YOU WON!"; break;}

 }
  cin.ignore(); cin.get();
}

Last edited on
still need help
still don't know how to get rid of that thing in define.. basically with that I wouldn't even need to use arrays
Rather than use a define... use a separate function.

1
2
3
4
5
6
7
8
// bad
#define longcode  if (x[1]==" o ") a=1;\
if (x[2]==" o ") a=2;\
if (x[3]==" o ") a=3;\
...

// to call it:
longcode;


1
2
3
4
5
6
7
8
9
10
11
12
// better, but still not great:
int getAFromX(int* x, int defaultval)
{
  if(x[1] == " o ")  return 1;
  if(x[2] == " o ")  return 2;
  //...

  return defaultval;
}

// to call it:
a = getAFromX(x,a);


Note I named this function poorly ("getAFromX") because I have no idea what this code is trying to do because your variable names do not reflect what the variables represent. You really should put more thought into your names. One letter variable names and stuff like "longcode" makes it very hard to know what's actually going on here.

Anyway... this can be improved even further by using a loop:

1
2
3
4
5
6
7
8
9
10
11
// even better

int getAFromX(int* x, int defaultval)
{
    for(int i = 1; i <= 25; ++i)
    {
        if(x[i] == " o ")
            return i;
    }
    return defaultval;
}


General rule of thumb: If you find yourself copy/pasting code over and over, you're probably doing something wrong.


Other things I noticed:

- Your indentation is inconsistent, which also hinders readability

- You have way too many special cases with magic numbers. Like this line:
if ((y=='a') and (a!=1) and (a!=6) and (a!=11) and (a!=16) and (a!=21))
What is the significance of 1,6,11,16,21? And what is the significance of the a or y variables for that matter?
I'm sorry, I really should have made myself more clear.
As you probably understood already, this program makes a 5x5 board, where a object moves with the press of "wasd" keys.

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
#include <iostream>
 #include <conio.h>
 #include <windows.h> 
#define longcode  if (square[1]==" o ") a=1;\
if (square[2]==" o ") a=2;\
if (square[3]==" o ") a=3;\
if (square[4]==" o ")a=4;\
if (square[5]==" o ")a=5;\
if (square[6]==" o ")a=6;\
if (square[7]==" o ")a=7;\
if (square[8]==" o ")a=8;\
 if (square[9]==" o ")a=9;\
 if (square[10]==" o ")a=10;\
 if (square[11]==" o ")a=11;\
 if (square[12]==" o ")a=12;\
 if (square[13]==" o ")a=13;\
 if (square[14]==" o ")a=14;\
 if (square[15]==" o ")a=15;\
 if (square[16]==" o ")a=16;\
 if (square[17]==" o ")a=17;\
 if (square[18]==" o ")a=18;\
 if (square[19]==" o ")a=19;\
 if (square[20]==" o ")a=20;\
 if (square[21]==" o ")a=21;\
 if (square[22]==" o ")a=22;\
 if (square[23]==" o ")a=23;\
 if (square[24]==" o ")a=24;\
 if (square[25]==" o ")a=25;\
// longcode makes PC memorize what is my current spot in the grid. 
//If a equals 23 that means I'm in the 23rd square. 
//Code basically works like this:  
//I press " d ", that means object is going down the grid .
// Now I set value squareCo, which is coordinate of my current location, to be squareCo+5, because that square is 5 squares away from me (counting to the right). 
//To make PC memorize where I am I call the "longcode", which checks if any of the squares are " o ", which mean if I'm in one of the squares.
// If I am, it sets "a" value to the square number, for example, if i'm in 2nd square, that means a will equal to 2.
// I need to use separate variable for that, because loops mess with my squareCo, always making it's value 25, so I use a help, because a doesn't change.
using namespace std;

int main()
{
string square[10000]; // I have no idea what to set in the [] so I always type a high number there
int squareCo=0,a=0,y; // y is basically my button (wasd) , a is coordinate helper 
do {square[squareCo]=" . "; squareCo++;} while (squareCo<=30); // I set " . " to every value of square, that means grid will be made of " . "
squareCo=0; // square coordination goes to the beginning , so I can make a new grid
square[1]= " o "; // my current "location"
do {squareCo++; cout << x[squareCo]; if (squareCo%5==0) cout << endl;} while (squareCo<=24); // I make  the grid visible
squareCo=0; // again going back to the beginning
cout << "hello." << endl;
square[1]=" . "; // as I know with next press character will move somewhere, I immediately set starting value to " . " 
// which means that  the character is somewhere else
a=1; // setting my helper to the beginning position too
while (true)
 {
  char y=_getch(); // waiting for my button press

  if ((y=='s') and (a<21)) // as you probably understand, y button press, which lets you move,
// and a<21 means that if you are in 21th square you can't move down anymore.
  {   system("CLS"); // a shameful function, I know
      if (squareCo==0) square[squareCo+6]=" o "; // if my coordinate was 0 from the start, I will move 6 squares forwards
      else if (squareCo>0) square[squareCo+5]=" o "; // else I will move only 5
      squareCo=0; // as I have to paint the grid I set this to zero
      while (squareCo<=24) // painting the grid
   {
         squareCo++;
         cout << square[squareCo]; if (squareCo%5==0) cout << endl;
   }  longcode; // checks where I am now and sets "a" value to the same as the square which I'm standing in right now (as for "I'm" I mean the character, which is " o ")
      squareCo=a; // because square coordinate was made to 25 by the loop, I make PC "remember" where I'm currently standing with "a" help, because "a" doesn't change.
      square[squareCo]=" . "; // as I know I will move somewhere I immediately set my current location to " . ", that means " o " is somewhere else
  }

    if ((y=='w') and (a>=6))
  {   system("CLS");
      square[squareCo-5]=" o ";
      squareCo=0;
      while (squareCo<=24)
   {
         squareCo++;
         cout << square[squareCo]; if (squareCo%5==0) cout << endl;
   }  longcode;
      squareCo=a;
      square[squareCo]=" . ";
  }

      if ((y=='a') and (a!=1) and (a!=6) and (a!=11) and (a!=16) and (a!=21))
  {   system("CLS");
      square[squareCo-1]=" o ";
      squareCo=0;
      while (squareCo<=24)
   {
         squareCo++;
         cout << square[squareCo]; if (squareCo%5==0) cout << endl;
   }  longcode;
      squareCo=a;
      square[squareCo]=" . ";
  }

     if ((y=='d') and (a!=5) and (a!=10) and (a!=15) and (a!=20) and (a!=25)) // that means if you are somewhere on the edge, like 10th square, you can't move right anymore.
  {   system("CLS");
      square[squareCo+1]=" o ";
      squareCo=0;
      while (squareCo<=24)
   {
         squareCo++;
         cout << square[squareCo]; if (squareCo%5==0) cout << endl;
   }  longcode;
      squareCo=a;
      square[squareCo]=" . ";
  }
 if (a==25) {cout << "YOU WON!"; break;}

 }
  cin.ignore(); cin.get();
}


Also sorry if my english is too difficult to understand. I'm bad at topics like this :)
Last edited on
Topic archived. No new replies allowed.