Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使这个snake和ladder java程序一轮一轮地运行_Java - Fatal编程技术网

如何使这个snake和ladder java程序一轮一轮地运行

如何使这个snake和ladder java程序一轮一轮地运行,java,Java,嗨,伙计们,我已经编写了一个蛇和梯子的游戏,它工作得很好,但是,有一个问题我不知道如何解决。目前,游戏是通过连续滚动骰子来运行的,我无法控制每一轮。我正在考虑如何逐轮控制游戏,这样我就可以手动设置每轮的骰子值并进行一些测试。请给我一些帮助,谢谢 package snakeandladder; /** * * @author cguo */ public class tale { int taleIndex; int nextTale; String agentTale; St

嗨,伙计们,我已经编写了一个蛇和梯子的游戏,它工作得很好,但是,有一个问题我不知道如何解决。目前,游戏是通过连续滚动骰子来运行的,我无法控制每一轮。我正在考虑如何逐轮控制游戏,这样我就可以手动设置每轮的骰子值并进行一些测试。请给我一些帮助,谢谢

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;
}
}
============================================================================board.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();
    }
}
}

嗯,如果你真的使用对象的话,会有很多更优雅的解决方案

也就是说,只需在命令行中输入一些内容,说明您要手动控制游戏:

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

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

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

  • 如果有一个合适的OO设计,测试会更容易,而不是一堆更新类(静态)变量的静态方法
  • 测试代码应该在单独的类中
  • 看看JUnit测试框架
public static void main(String[] args) {
    if (args.length == 1 && args[0].equals("debug"))
        SMLD();
    else
        SML();