Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Rotating -1 to bigger number  (Read 2570 times)

0 Members and 1 Guest are viewing this topic.

TehKyle

  • Newbie
  • *
  • Posts: 8
    • View Profile
Rotating -1 to bigger number
« on: May 25, 2011, 05:13:53 am »
Modulus operator works fine if you add up, but how do you deal with -1 going back to 7 without special case code line (if possible)

Code: [Select]
               if (Event.Key.Code == sf::Key::Z){
                    TileSelect--;
                    TileSelect = TileSelect % 7;
                }
                if (Event.Key.Code == sf::Key::X){
                    TileSelect++;
                    TileSelect = TileSelect % 7;
                }

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Rotating -1 to bigger number
« Reply #1 on: May 25, 2011, 05:56:04 am »
I'm not certain on what you're trying to do. Can't you just do "IF num > 0 THEN num--"?

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Rotating -1 to bigger number
« Reply #2 on: May 25, 2011, 06:17:11 am »
Code: [Select]
if (Event.Key.Code == sf::Key::Z){
//                    TileSelect--;
                    TileSelect += 7 - 1;
                    TileSelect = TileSelect % 7;
                }

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Rotating -1 to bigger number
« Reply #3 on: May 25, 2011, 10:16:44 am »
A bitwise AND (& 7) would keep the value at 0-7, if I'm understanding you correctly.
{much better code}

TehKyle

  • Newbie
  • *
  • Posts: 8
    • View Profile
Rotating -1 to bigger number
« Reply #4 on: May 25, 2011, 12:54:47 pm »
Quote from: "Disch"
Code: [Select]
if (Event.Key.Code == sf::Key::Z){
//                    TileSelect--;
                    TileSelect += 7 - 1;
                    TileSelect = TileSelect % 7;
                }

Thanks, I totally forgot about that. Your code was off by one though, I don't know why you added -1. I used const int MAX_TILE which contained 7 and now it works as intended.

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Rotating -1 to bigger number
« Reply #5 on: May 25, 2011, 10:39:37 pm »
Quote
A bitwise AND (& 7) would keep the value at 0-7, if I'm understanding you correctly.


& 7 is equivilent to %8

He wants % 7 which does not have a & equivilent.

Quote
Thanks, I totally forgot about that. Your code was off by one though, I don't know why you added -1.


I added the -1 because you wanted to subtract 1:

Code: [Select]
x = (x + 7) % 7;  // does not change x:
// ex:  x=2
//  (2 + 7) % 7
//  9 % 7
//  2

 

anything