Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

scala - How do I use these functions to check for bounties

As the player moves they can collect bounties. I am trying to use the below functions to collect the bounties and also check for bounties in a rectangle between the current position and the save position (if the player has covered 9 spaces in this rectangle then all of the bounties in the rectangle are collected and added to the score.

def checkBounty() {
   if(bounties (playerX)(playerY) != null) {
     val bounty: Int => Int = bounties(playerX)(playerY)
    
     score += bounty(score)
     bounties(playerX)(playerY) == null
    }
     
  } 

The checkBounty() function is supposed to check if the current position is a bounty (a bounty exists if the cell is not set to null). If a bounty does exist, it increases the score, and then erases the bounty (sets it back to null).

def checkBounties() {
   
    if ((((playerX - saveX) + 1).abs * ((playerY - saveY) + 1).abs) >= 9) {

     for (x <- saveX to playerX; y <- saveY to playerY) {

      checkBounty();
    }
    saveX = -1
    saveY = -1
  }

  }

The checkBounties () function checks if the rectangle defined by the current position and saved position covers nine or more positions. If yes, it collects bounties in it, increases the score, and erases the bounties.

However, when called they don't seem to be working correctly.

I don't think checkBounties() correctly checks the cells which I iterate through using the loop, however I'm not quite sure how to fix this. Any help would be appreciated.

EDIT - My issue is that the tests expect a checkBounty() with no parameters and using playerX and playerY as the coorindates I sm checking. I need to find a way to leave checkBounty() as it is and be able to call something similar, but with parameters for x and y, from checkBounties().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Without a complete example I still don't know if this is the only issue (besides the == I commented about), but this is likely the main problem:

Edit: given the restriction that we can't change the signature of checkBounty(), you can fake having function parameters just be creating some internal class variables:

class Game(/*...*/) {
  private var checkBountyX: Int = 0;
  private var checkBountyY: Int = 0;


   def checkBounty() {
     if(bounties (checkBountyX)(checkBountyY) != null) {
       val bounty: Int => Int = bounties(checkBountyX)(checkBountyY)
      
       score += bounty(score)
       bounties(checkBountyX)(checkBountyY) = null
      }
       
    } 
  //...
  def checkBounties() {
    for (x <- saveX to playerX; y <- saveY to playerY) {
      checkBountyX = x;
      checkBountyY = y;
  
      checkBounty(x, y);
    }
  }

}

This is not great code IMO, but given the requirements it should work. If you have some kind of test that has to access checkBounty(), you may have to make the vars public or constructor parameters.

Also, just an aside: using null in Scala is strongly discouraged. Once you get this working, I'd recommend looking into using Option[Int] instead of just Int for the bounties.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...