April 2019
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
2930     

Calendar Calendar


Need help guys, any input will be greatly appreciated!

Go down

Need help guys, any input will be greatly appreciated! Empty Need help guys, any input will be greatly appreciated!

Post  Catcher on Sun Mar 18, 2012 1:33 am

Hey guys,
Just want to say that I'm a terrible programmer, I can understand the purpose of all the elements we have learnt over the module this year, why they work and why we use them, but when it comes to implementing them and making them all work together I'm more than useless.

So i have been adapting the code I had already had working to try and add some more interesting features to my maze game. Whilst doing this though I've managed to balls everything up.

I have my maze to be displayed as:
Code:
char map[15][40]=// maze design within array
      {
         "#######################################",
         "#P        #            #            #",
         "######## ### ########## ## ############",
         "#  #  #    #          #  #          #",
         "# ### ############ ######  ######### ##",
         "#                                    #",
         "#################################### ##",
         "#                        #        #  #",
         "#### ################## ##  #### ##  #",
         "#    #              # F #  #    #  #",
         "#    # ####### ############ ########  #",
         "#    # #    # #                      #",
         "## ### ### ### ################ #######",
         "#      #                              #",
         "#######################################"
      };

And my player movement function and check function as:
Code:
//=============| User Input Function |=============
char enter()
{
char move;
do
{
    cout << "W(up), S(down), A(left), D(right)" << endl;
    cin >> move;
} while ((move != 'W') && (move != 'w') && (move != 'S') && (move != 's')
        && (move != 'A') && (move != 'a') && (move != 'D') && (move != 'd'));
return move;
}
//=============| Player Movement Within Array |=============
void Pmove(char move, char map[15][40], int *ud, int *rl)
{
   if (((move == 'W') || (move == 'w')) && (map[15][40] != '#'))
     {
        (*ud)--;
      }
   if (((move == 'S') || (move == 's')) && (map[15][40] != '#'))
     {
        (*ud)++;
      }
   if (((move == 'D') || (move == 'd')) && (map[15][40] != '#'))
     {
        (*rl)++;
      }
   if (((move == 'A') || (move == 'a')) && (map[15][40] != '#'))
     {
        (*rl)--;
      }
}

//=============| Check Movement is within the Array |=============
void check(int *rowP, int *colP)
{
   if (((*rowP) <= -1) || ((*rowP) == '#'))
   {
      (*rowP)++ ;
   };
if (((*rowP) >= 15) || ((*rowP) == '#'))
   {
      (*rowP)--;
   };
if (((*colP) <= -1) || ((*colP) == '#'))
   {
      (*colP)++;
   };
if (((*colP) >= 40) || ((*colP) == '#'))
   {
      (*colP)--;
   };
}

But neither of the if statements i have attempted stop the player from passing straight through the walls. I would've thought the parts checking whether the space is a # would accomplish this. Is there a better way to do it? I know it will have to be some kind of comparative test used with a logical operator to ensure all the conditions are met, but i have most likely been using the wrong data type throughout and there is no means of comparing such things.

You guys are all so much better at programming than i am, and i just don't have the right kind of mind for it (more of an engineer) i would appreciate any help i can get, anything that would make me realise what I've done wrong and why it is wrong so that i can learn from it would be just fantastic.

thank you so much
Catch
Catcher
Catcher

Posts : 6
Join date : 2012-02-27
Age : 26
Location : Inches from my screen. Inches.

View user profile http://in-the-skies.deviantart.com/

Back to top Go down

Need help guys, any input will be greatly appreciated! Empty Re: Need help guys, any input will be greatly appreciated!

Post  lee on Sun Mar 18, 2012 1:57 am

I just had a brief look at my programming sucks too but I think the if statement still goes if there is a #. Try != '#' in the if statement


ie

if (((*rowP) <= -1) || ((*rowP) != '#'))
lee
lee

Posts : 22
Join date : 2012-02-23

View user profile

Back to top Go down

Need help guys, any input will be greatly appreciated! Empty Re: Need help guys, any input will be greatly appreciated!

Post  Catcher on Sun Mar 18, 2012 2:12 am

Thanks Lee,
Still no success, I've tried it with the OR, AND and NOT operators but no combination seems to work Sad

So frustrating!
Catch
Catcher
Catcher

Posts : 6
Join date : 2012-02-27
Age : 26
Location : Inches from my screen. Inches.

View user profile http://in-the-skies.deviantart.com/

Back to top Go down

Need help guys, any input will be greatly appreciated! Empty Re: Need help guys, any input will be greatly appreciated!

Post  ElliottH on Sun Mar 18, 2012 2:27 am

if (((move == 'W') || (move == 'w')) && (map[15][40] != '#'))
when you do this bit, you aren't checking the space in the array you're moving to, should be something like :

if (((move == 'W') || (move == 'w')) && (map]*ud]]*rl] != '#'))

but then you're still only checking the current space you're in, not the space being moved to.

mine works by moving, checking it and then moving back if it's wrong, hope this helps. we're in g46 atm now though if you are getting uber stuck Smile


Last edited by ElliottH on Sun Mar 18, 2012 2:29 am; edited 1 time in total (Reason for editing : fixed coloured bits)
ElliottH
ElliottH
Admin

Posts : 16
Join date : 2012-02-23

View user profile

Back to top Go down

Need help guys, any input will be greatly appreciated! Empty Re: Need help guys, any input will be greatly appreciated!

Post  ElliottH on Sun Mar 18, 2012 2:30 am

coloured bits messed up a bit there but it should be
(map[*ud][*rl] != '#')
ElliottH
ElliottH
Admin

Posts : 16
Join date : 2012-02-23

View user profile

Back to top Go down

Need help guys, any input will be greatly appreciated! Empty Re: Need help guys, any input will be greatly appreciated!

Post  Catcher on Sun Mar 18, 2012 2:51 am

Code:
(((move == 'W') || (move == 'w')) && (map[15][40] != '#'))
The bit of this is kind of irrelivant, I've just included it as another attempt. But the
Code:
void check(int *rowP, int *colP)
part undoes the previous functions movement if the space it is now in is invalid.

E.g.
  1. The first function moves the player into a place by incrementing the array, but the display isn't updated yet so you don't see you've moved.
  2. The check function sees if the new space is allowed, i.e. is it a monster, is it a wall, etc.
  3. If the conditions aren't met it undoes the increment and moves the player back to where he was.
  4. Then the display is updated.


Problem is that it doesn't recognise the wall as not allowed, you can pass straight through it. I don't know why though. Probably because the walls are chars and the comparison operators only work on numbers? I'm not sure...

Been here for ages, i must've tried everything. Sad

Thanks man
Catch
Catcher
Catcher

Posts : 6
Join date : 2012-02-27
Age : 26
Location : Inches from my screen. Inches.

View user profile http://in-the-skies.deviantart.com/

Back to top Go down

Need help guys, any input will be greatly appreciated! Empty Re: Need help guys, any input will be greatly appreciated!

Post  ElliottH on Sun Mar 18, 2012 3:05 am

ahhh yours is confusingg, pointers and other scary bits, sorry i cant help Sad
here is mine:
Code:

char move;
   move = getch();
   
   if (move == 'w')
   {
      playerY--;
      if (movecheck() == 0)
      {
         cout << "Can't move through walls!" << endl;
         playerY++;
         return 0;
      }
      else if (movecheck() == 2)
      {return 1;}
      else
      {
      tempprev = tempnow;
      tempnow = maps[playerY][playerX];
      maps[playerY][playerX] = 'O';
      maps[playerY + 1][playerX] = tempprev;
      return 0;
      }
   }

int levels::movecheck()
{
   if (maps[playerY][playerX] == '#')
      {return 0;}
   else if (maps[playerY][playerX] == 'E')
      {return 2;}
   else
      {return 1;}
}

this is just the movement for up, and the movecheck function
ElliottH
ElliottH
Admin

Posts : 16
Join date : 2012-02-23

View user profile

Back to top Go down

Need help guys, any input will be greatly appreciated! Empty Re: Need help guys, any input will be greatly appreciated!

Post  Charlie on Sun Mar 18, 2012 3:14 am

I think the problem with moving and then checking is that # character will already have been replaced by the player, so when you're calling your

Code:
void check(int *rowP, int *colP)

function it doesn't see the # it sees the P and so it doesn't do anything. It would probably be better to check if the space you're trying to move into is free and then move into it, as opposed to moving then checking.

It looks like you've already tried to do that with your

Code:
if (((move == 'W') || (move == 'w')) && (map[15][40] != '#'))

checks, and that doesn't work at the moment because you're checking the map[15][40] character to see if it's a # as opposed to the character you're trying to move to. Something roughly like the following should work instead:

Code:
if (((move == 'W') || (move == 'w')) && (map[*rowP-1][*colP] != '#'))

// by subtracting 1 to the current row value you're
// checking to see that the space above is available
Charlie
Charlie

Posts : 16
Join date : 2012-02-23

View user profile

Back to top Go down

Need help guys, any input will be greatly appreciated! Empty Re: Need help guys, any input will be greatly appreciated!

Post  Catcher on Sun Mar 18, 2012 3:43 am

Thanks man,
I think you might be onto something there, it should work i just need to get the right variables in the right places..
Thanks so much

if anyone else has any ideas I'm still willing to hear them Smile

Catch
Catcher
Catcher

Posts : 6
Join date : 2012-02-27
Age : 26
Location : Inches from my screen. Inches.

View user profile http://in-the-skies.deviantart.com/

Back to top Go down

Need help guys, any input will be greatly appreciated! Empty Re: Need help guys, any input will be greatly appreciated!

Post  Catcher on Sun Mar 18, 2012 9:37 am

Didn't quite work but I'm gonna get Karsten (the legend) to have a look at some point Very Happy
Catcher
Catcher

Posts : 6
Join date : 2012-02-27
Age : 26
Location : Inches from my screen. Inches.

View user profile http://in-the-skies.deviantart.com/

Back to top Go down

Need help guys, any input will be greatly appreciated! Empty Re: Need help guys, any input will be greatly appreciated!

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum