Determining if a number is either a multiple of ten or within a particular set of ranges

后端 未结 13 1471
Happy的楠姐
Happy的楠姐 2021-01-31 07:01

I have a few loops that I need in my program. I can write out the pseudo code, but I\'m not entirely sure how to write them logically.

I need -

if (num is          


        
13条回答
  •  天涯浪人
    2021-01-31 07:15

    As others have pointed out, making the conditions more concise won't speed up the compilation or the execution, and it doesn't necessarily help with readability either.

    It can help in making your program more flexible, in case you decide later that you want a toddler's version of the game on a 6 x 6 board, or an advanced version (that you can play all night long) on a 40 x 50 board.

    So I would code it as follows:

    // What is the size of the game board?
    #define ROWS            10
    #define COLUMNS         10
    
    // The numbers of the squares go from 1 (bottom-left) to (ROWS * COLUMNS)
    // (top-left if ROWS is even, or top-right if ROWS is odd)
    #define firstSquare     1
    #define lastSquare      (ROWS * COLUMNS)
    // We haven't started until we roll the die and move onto the first square,
    // so there is an imaginary 'square zero'
    #define notStarted(num) (num == 0)
    // and we only win when we land exactly on the last square
    #define finished(num)   (num == lastSquare)
    #define overShot(num)   (num > lastSquare)
    
    // We will number our rows from 1 to ROWS, and our columns from 1 to COLUMNS
    // (apologies to C fanatics who believe the world should be zero-based, which would
    //  have simplified these expressions)
    #define getRow(num)   (((num - 1) / COLUMNS) + 1)
    #define getCol(num)   (((num - 1) % COLUMNS) + 1)
    
    // What direction are we moving in?
    // On rows 1, 3, 5, etc. we go from left to right
    #define isLeftToRightRow(num)    ((getRow(num) % 2) == 1)
    // On rows 2, 4, 6, etc. we go from right to left
    #define isRightToLeftRow(num)    ((getRow(num) % 2) == 0)
    
    // Are we on the last square in the row?
    #define isLastInRow(num)    (getCol(num) == COLUMNS)
    
    // And finally we can get onto the code
    
    if (notStarted(mySquare))
    {
      // Some code for when we haven't got our piece on the board yet
    }
    else
    {
      if (isLastInRow(mySquare))
      {
        // Some code for when we're on the last square in a row
      }
    
    
      if (isRightToLeftRow(mySquare))
      {
        // Some code for when we're travelling from right to left
      }
      else
      {
        // Some code for when we're travelling from left to right
      }
    }
    

    Yes, it's verbose, but it makes it clear exactly what's happening on the game board.

    If I was developing this game to display on a phone or tablet, I'd make ROWS and COLUMNS variables instead of constants, so they can be set dynamically (at the start of a game) to match the screen size and orientation.

    I'd also allow the screen orientation to be changed at any time, mid-game - all you need to do is switch the values of ROWS and COLUMNS, while leaving everything else (the current square number that each player is on, and the start/end squares of all the snakes and ladders) unchanged. Then you 'just' have to draw the board nicely, and write code for your animations (I assume that was the purpose of your if statements) ...

提交回复
热议问题