【问题标题】:Java - Why don't my mice move correctly?Java - 为什么我的鼠标不能正确移动?
【发布时间】:2015-10-19 16:55:38
【问题描述】:

我目前正在开发一个程序,该程序可以显示田野中小动物的运动。小动物用一个字符标记(例如:“M”代表鼠标)并且每个都有自己的移动方式。我遇到问题的方法是应该让我的鼠标小动物移动的方法。

  • 它们应该沿对角线(向北然后向东或向西。向东,然后向北或向南)移动一定次数,然后改变对角线方向。
    • 鼠标沿对角线移动的次数由myCount 控制。如果myCount 小于myTarget,那么鼠标应该沿对角线移动。当myCount 等于myTarget 时,鼠标会改变方向。然后将myCount 设置回 0。我希望我的老鼠在改变对角线方向之前沿对角线移动 8 次。
  • 但是,相反我的老鼠有时会沿对角线移动一点点,但它们最终总是会向上然后向下或向左然后向右移动大约 5 次。

Here 是我的程序输出的样子。 (S 是不动的石头,在这种情况下它们被认为是另一种生物)。

界面:

public interface Critter
{
   public static final int NORTH = 0;
   public static final int WEST = 1;
   public static final int SOUTH = 2;
   public static final int EAST = 3;
   public static final int CENTER = 4;

   public char getChar();
   public int getMove(CritterInfo theInfo);
}

抽象类:

public abstract class AbstractCritter implements Critter
{
   private char myCritterName;

   public AbstractCritter(final char theChar)
   {
      myCritterName = theChar;
   }
   public char getChar()
   {
      return myCritterName;
   }
}

我遇到问题的方法:

import java.util.*;
public class Mouse extends AbstractCritter
{
   private int myDirection;
   private int myCount;
   private boolean myFirst;
   private int myTarget;
   private Random myRand;
   private int myCheck;

   public Mouse()
   {
      super('M');
      myRand = new Random();
      myDirection = myRand.nextInt(4);
      myCount = 0;
      myFirst = false;
      myTarget = 16;
      myCheck = 0;
   }
   public int getMove(CritterInfo theInfo)
   {
      if (myCount == myTarget)
      {
         myCount = 0;
         int direction = myRand.nextInt(4);
         myFirst = false;

      }
      int direction = myDirection;
      int direction2 = myRand.nextInt(4);
      myCount++;

      if (!myFirst)
      {

         myCheck = myRand.nextInt(2);
         if (myDirection == NORTH || myDirection == SOUTH)
         {
            if (myCheck == 0)
            {
               direction2 = EAST;
            }
            else
            {
               direction2 = WEST;
            }
         }
         else if (myDirection == EAST || myDirection == WEST)
         {
            if (myCheck == 0)
               {
                  direction2 = NORTH;
               }
               else
               {
                  direction2 = SOUTH;
               }
         }
         myFirst = true;
      }

      if (myCount % 2 == 0)
      {
         return direction;
      }
      else
      {
         return direction2;
      }
   }
}

【问题讨论】:

    标签: java methods interface abstract-class


    【解决方案1】:

    如果您创建一个enum 来保存您的鼠标可能处于的两种不同状态,那么它就会变得非常非常容易混淆。每 8 步在两种状态之间切换一次,然后使用 enum 本身告诉你应该使用哪些移动规则。

    逻辑的关键部分是这里的这一行:

    int offset = 2 - ((index / 2) * 2);
    

    如果方向是NORTH, SOUTH,索引将为0,1,如果方向为EAST, WEST,索引将为2,3。除然后乘以 2 使 NORTH, SOUTH0EAST, WEST2。然后我们用2 -那个值来翻转它,然后我们随机加上1再次选择一个正交方向。

    代码如下:

    import java.util.*;
    public class Mouse extends AbstractCritter {
      private static final int[] allDirections = { NORTH, SOUTH, EAST, WEST };
      private static final Random myRand = new Random();
    
      private final int myTarget = 16;
      private int myCount = 0;
      private int firstDirection;
      private int secondDirection;
    
      public Mouse() {
        super('M');
        randomizeDirection();
      }
    
      public int getMove(CritterInfo info)
      { 
        myCount++;
        if(myCount == myTarget) {
          randomizeDirection();
          myCount = 0;
        }
        return myCount % 2 == 0 ? firstDirection : secondDirection;
      }
    
      private void randomizeDirection() {
        int index = myRand.nextInt(4);
        firstDirection = allDirections[index];
        int offset = 2 - ((index / 2) * 2); // this turns 0, 1 -> 2; 2, 3 -> 0
        secondDirection = allDirections[offset + myRand.nextInt(2)];
      } 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多