how can I make this snake and ladder java program running round by round
Hi guys I have coded a snake and ladder game, it works fine, however, there is one question I don't know how to settle. currently, the game is running by rolling the die continuously, and I don't have control over each round. I am thinking about how to control the game round by round, so I can manually set the die value for each round and do some test. please give me some help, thank you.
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.pri开发者_高级运维ntln(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);
}
}
}
Well, there'd be a lot more elegant solutions if you were actually using objects ...
That being said, just pass in something on the command line that says you want to manually control the game:
public static void main(String[] args) {
if (args.length == 1 && args[0].equals("debug"))
SMLD();
else
SML();
In your SMLD()
method you'd take input from the command line and control the game manually.
If you are concerned about testing and testability then:
- Testing would be easier you had a proper OO design rather than a bunch of static methods updating class (static) variables.
- Test code should be in separate classes.
- Look at the JUnit test framework.
精彩评论