我怎样才能使这个蛇和梯子java程序逐轮运行

时间:2011-04-16 23:47:47

标签: java

嗨,大家好我编写了一个蛇和梯子游戏,它运行正常,然而,有一个问题我不知道如何解决。目前,游戏是通过持续滚动模具来运行的,而且我无法控制每一轮。我正在考虑如何逐轮控制游戏,所以我可以手动设置每轮的模具值并进行一些测试。请给我一些帮助,谢谢。

package snakeandladder;

  /**
  *
  * @author cguo
   */
public class tale {
int taleIndex;
int nextTale;
String agentTale;
String taleType;
 /**
 * tale constructor
 * @param index
 * @param type
 * @param next
 * @param agent
 */
 public tale(int index, String type, int next, String agent)
 {
    taleIndex=index;
    nextTale=next;
    taleType=type;
    agentTale=agent;
}
 /**
 * print function
 */
public void printTale()
{
    if(agentTale==null)
    {
        if(taleType!=null)
           System.out.print("(" + taleIndex + " (" + taleType + " " + nextTale + " ))");
        else
           System.out.print("(" + taleIndex+ ")");
    }
    else
    {
      if(taleType!=null)
        System.out.print("("+taleIndex+" ("+taleType+" "+nextTale+" )"+ agentTale+")");
      else
        System.out.print("(" + taleIndex+ " "+agentTale+ ")");
    }
    }
/**
 *
 * @return the tale index
 */
public int getIndex()
{
    return taleIndex;
}
/**
 * set the tale type
 * @param type
 */
public void settype(String type)
{
    taleType=type;
}
/**
 *
 * @return the tale type as a string
 */
public String getType()
{
    return taleType;
}

public int getNext()
{
    return nextTale;
}

public void setNext(int next)
{
    nextTale=next;
}

public void setAgent(String agent)
{
    agentTale=agent;
}

public String getAgent()
{
    return agentTale;
}
}

==============================================板的.java

 package snakeandladder;

/**
 *
 * @author cguo
 */
import java.util.Random;

public class board {
static Random rand = new Random();
static tale[] gameBoard=new tale[37];
static String agentIndex;
public board(String agent)
{
    agentIndex=agent;

    gameBoard[0]=new tale(0,"D1",0,null);

    gameBoard[1]=new tale(1,"START",1,"A1");
    gameBoard[2]=new tale(2,null,2,null);
    gameBoard[3]=new tale(3,"L",16,null);
    gameBoard[4]=new tale(4,null,4,null);
    gameBoard[5]=new tale(5,"L",7,null);
    gameBoard[6]=new tale(6,null,6,null);
    gameBoard[7]=new tale(7,null,7,null);
    gameBoard[8]=new tale(8,null,8,null);
    gameBoard[9]=new tale(9,null,9,null);
    gameBoard[10]=new tale(10,null,10,null);
    gameBoard[11]=new tale(11,null,11,null);
    gameBoard[12]=new tale(12,"S",2,null);
    gameBoard[13]=new tale(13,null,13,null);
    gameBoard[14]=new tale(14,"S",11,null);
    gameBoard[15]=new tale(15,"L",25,null);
    gameBoard[16]=new tale(16,null,16,null);
    gameBoard[17]=new tale(17,"S",4,null);
    gameBoard[18]=new tale(18,"L",20,null);
    gameBoard[19]=new tale(19,null,19,null);
    gameBoard[20]=new tale(20,null,20,null);
    gameBoard[21]=new tale(21,"L",32,null);
    gameBoard[22]=new tale(22,null,22,null);
    gameBoard[23]=new tale(23,null,23,null);
    gameBoard[24]=new tale(24,null,24,null);
    gameBoard[25]=new tale(25,null,25,null);
    gameBoard[26]=new tale(26,null,26,null);
    gameBoard[27]=new tale(27,null,27,null);
    gameBoard[28]=new tale(28,null,28,null);
    gameBoard[29]=new tale(29,null,29,null);
    gameBoard[30]=new tale(30,null,30,null);
    gameBoard[31]=new tale(31,"S",19,null);
    gameBoard[32]=new tale(32,null,32,null);
    gameBoard[33]=new tale(33,null,33,null);
    gameBoard[34]=new tale(34,null,34,null);
    gameBoard[35]=new tale(35,"S",22,null);
    gameBoard[36]=new tale(36,"GOAL",36,null);


}

static public boolean goalChecker()
{
    tale tale1=gameBoard[1];
    for(int i=1;i<=36;i++)
    {
        //System.out.println(gameBoard[i].getAgent());
        if(gameBoard[i].getAgent()!=null)
            tale1=gameBoard[i];
    }

        if(tale1.getAgent()!=null&&tale1.getType()!=null&&((tale1.getType()).equals("GOAL")))
        return true;
    else
        return false;
}

static public int getAgentPo()
{
    int position=1;
    tale tale1=gameBoard[1];
    for(int i=1;i<=36;i++)
    {
        if(gameBoard[i].getAgent()!=null)
        {
            tale1 = gameBoard[i];
            position=i;
        }
    }

    return position;
}

static public void move(int current,int step)
{
    int next=current+step;
    if(next>=36)
        next=36;
    gameBoard[current].setAgent(null);
    gameBoard[next].setAgent(agentIndex);
    String cuType=gameBoard[next].taleType;
    if(cuType!=null&&cuType.equals("L"))
    {
        int extra=gameBoard[next].getNext();
        extra=extra-next;
        printBoard();
        System.out.println("still moving");
        move(next,extra);
    }
    if(cuType!=null&&cuType.equals("S"))
    {
        int extra2=gameBoard[next].getNext();
        extra2=extra2-next;
        printBoard();
        System.out.println("still moving");
        move(next,extra2);
    }

    printBoard();

}

static public void ladderMove(int current,int step)
{
    int next=current+step;
    if(next>=36)
        next=36;
    gameBoard[current].setAgent(null);
    gameBoard[next].setAgent(agentIndex);
    String cuType=gameBoard[next].taleType;
    if(cuType!=null&&cuType.equals("L"))
    {
        int extra=next+(int)(Math.random()*((35 - next) + 1));


        extra=extra-next;
        System.out.println("moving "+extra +" steps");
        printBoard();
        System.out.println("still moving");
        move(next,extra);
    }
    if(cuType!=null&&cuType.equals("S"))
    {
        int extra2=gameBoard[next].getNext();
        extra2=extra2-next;
        printBoard();
        System.out.println("still moving");
        move(next,extra2);
    }

    printBoard();

}
  //avoid any ladder which can lead to another ladder or snake head
      static public void sladderMove(int current,int step)
   {
    int next=current+step;
    if(next>=36)
        next=36;
    gameBoard[current].setAgent(null);
    gameBoard[next].setAgent(agentIndex);
    String cuType=gameBoard[next].taleType;
    if(cuType!=null&&cuType.equals("L"))
    {
        int extra=next+(int)(Math.random()*((35 - next) + 1));
        if(gameBoard[extra].getType()==null)
        {


        extra=extra-next;
        System.out.println("moving "+extra +" steps");
        printBoard();
        System.out.println("still moving");
        move(next,extra);
        }
        else
        {
            System.out.println("prefer moving one step");
            move(next,1);
            printBoard();
        }
    }
    if(cuType!=null&&cuType.equals("S"))
    {
        int extra2=gameBoard[next].getNext();
        extra2=extra2-next;
        printBoard();
        System.out.println("still moving");
        move(next,extra2);
    }

    printBoard();

}

static public void printBoard()
{
    for(int i=0;i<=36;i++)
    {
        gameBoard[i].printTale();
    }
}
}

=============================================== ===================== driver.java

   package snakeandladder;

   import java.util.Random;
   /**
    *
   * @author cguo
   */
   public class Driver {

   /**
   * @param args the command line arguments
   */

    static Random rand = new Random();
    static String player="A1";
public static void main(String[] args) {


    board gameboard=new board(player);
    /*while(board.goalChecker()!=true)
    {
        System.out.println("\n");
        System.out.println("agent "+player+" is playing");
        System.out.println("rolling dice");
        int dice=rand.nextInt(6)+1;
        System.out.println("rolled "+dice+" and moving");
        board.move(board.getAgentPo(),dice);

    }*/
    SML();

    System.out.println("\n");
    System.out.println("game finished");



}

static void SRP()
{
            while(board.goalChecker()!=true)
    {
        System.out.println("\n");
        System.out.println("agent "+player+" is playing");
        System.out.println("rolling dice");
        int dice=rand.nextInt(6)+1;
        System.out.println("rolled "+dice+" and moving");
        board.move(board.getAgentPo(),dice);

    }
}
static void ML()
{
    while(board.goalChecker()!=true)
    {
        System.out.println("\n");
        System.out.println("agent "+player+" is playing");
        System.out.println("rolling dice");
        int dice=rand.nextInt(6)+1;
        System.out.println("rolled "+dice+" and moving");
        board.ladderMove(board.getAgentPo(),dice);

    }
}

static void SML()
{
    while(board.goalChecker()!=true)
    {
        System.out.println("\n");
        System.out.println("agent "+player+" is playing");
        System.out.println("rolling dice");
        int dice=rand.nextInt(6)+1;
        System.out.println("rolled "+dice+" and moving");
        board.sladderMove(board.getAgentPo(),dice);

    }
}

}

2 个答案:

答案 0 :(得分:1)

如果您实际使用的是对象,那么会有更多优雅的解决方案...

话虽这么说,只需在命令行传递一些内容,表示你想手动控制游戏:

public static void main(String[] args) {
    if (args.length == 1 && args[0].equals("debug"))
        SMLD();
    else
        SML();

SMLD()方法中,您可以从命令行获取输入并手动控制游戏。

答案 1 :(得分:0)

如果您担心测试和可测试性,那么:

  • 使用正确的OO设计而不是一堆更新类(静态)变量的静态方法,测试会更容易。
  • 测试代码应该在不同的类中。
  • 查看JUnit测试框架。