Java - 为什么我的鼠标不能正确移动?

Java - Why don't my mice move correctly?

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

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);
}

摘要class:

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;
      }
   }
}

如果您创建一个 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)];
  } 
}