Am I a bad coder?
Of course you are. Go back to Java.
 33%  [ 1 ]
What are you talking about? This is decent code.
 66%  [ 2 ]
Total Votes : 3

I have some code here that is Java code ported to C++. I was wondering if it could be ~~fixed~~ optimized.

I know I'm a novice, so the code is kinda trash anyway.

https://paste.gnome.org/2JLPw0h2H
I haven't gone over it carefully, but a few errors stood out to me straight away.

Think of your game's map as a grid on a piece of paper, like for a game of Battleship. This grid is 100 squares wide and 50 squares tall. The biggest problem with this code is that it often tries to look for or draw a square that is off the paper.

The player starts in the wrong place
Imagine your paper grid. Since it's 50 squares tall, you can number the rows from 0 to 49.

For most levels, the code says startY = 50. It's trying to place the player in row 50. But since the rows only go up to 49, row 50 doesn't exist and (probably) the program immediately crashes.

Code:
if ( levelSelection == 1){
  tilemap.map = level1_map;
  startX = 46;
  startY = 50; // Problem is here
}


Major logical error in map Loading

Code:
for (int i = 0; i < 100; i++){
  for (int j = 0; j < 50; j++){
    if (tilemap.map[i+(j*50)] == 0x00){
      map[i][j] = 'X';
    // ...
    }
  }
}

This formula i + (j * 50) assumes the map is 50 tiles wide. However, the loop for i goes from 0 to 99. This means the code is trying to read from a map that is conceptually 50 tiles wide but is accessing it as if it were 100 tiles wide.

Maybe you were thinking of declaring it as map[50][100] instead of [100][50]? If so, then inside the loops, you'd have to use map[j][i] instead of map[i][j] (with the indices swapped). That would make it consistent with the loops and the formuĊ‚a.

There are no boundary checks in the code, which implies the map data is designed with walls around the edges. Thanks to this, wall collision keeps the player from going out of bounds (am I right?). But if the map is loaded incorrectly and is missing these edge walls, any movement can lead to a crash as soon as the player crosses the boundary.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement