5 Replies - 1949 Views - Last Post: 27 April 2011 - 06:47 PM Rate Topic: -----

#1 LostSignal13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-April 11

Snake Game Help

Posted 27 April 2011 - 05:25 PM

Hi guys,

So I'm writing a Snake game for my Computer Science class in college. All of my tests for this SnakeMove class are passing except for my generateSnake test. I'm trying to generate the tail for the snake within the game every time a piece of food is consumed. The tail is also supposed to generate two pieces of snake per every piece of food eaten.

Every time I run the test for generateSnake, I get the error "junit.framework.AssertionFailedError: Expected <2> but was <1>."

Any help at all would be much appreciated.

P.S. My professor also does not want us to use arraylists for this part keep in mind.


 public class SnakeMove {
	Coordinate move;
	
	public SnakeMove(Coordinate move) {
		this.move = move;
	}
		
	public Coordinate getMove(){
		return move;
	}
	
	public SnakeState moveSnake(SnakeState state){
		//moves the snake
		Coordinate[] snake = new Coordinate[state.snake.length]; //new snake
		snake[0] = new Coordinate(move.getRow(), move.getColumn()); //moves the head of the snake to the new position
		for (int i = 1; i < state.snake.length; i += 1){
			snake[i] = state.getSnake()[i - 1]; //moves the rest of the snake
		}
		SnakeState newState = new SnakeState(state.getBoard(), snake, state.getSnakeFood(), state.getTimeTick()); //creates new SnakeState using moved snake
		if (state.getBoard()[move.getRow()][move.getColumn()] == 'o'){
			//checks to see if the snake consumed food
			newState = consumeFood(newState); //generates newFood and longer snake
		}
		return newState;
	}
	
	public SnakeState consumeFood(SnakeState state){
		//generates newFood and longer snake
		SnakeState newState = generateSnake(state);
		newState = generateSnake(newState);
		newState = generateFood(newState);
		SnakeState result = new SnakeState(newState.getBoard(), newState.getSnake(), newState.getSnakeFood(), newState.getTimeTick());
		return result;
	}
	
	public SnakeState generateFood(SnakeState state){
		//generates new snake food after food has been eaten
	 	Coordinate[] Food = new Coordinate[state.snakeFood.length + 1]; //new Food Coordinate[]
    	for (int i = 0; i < state.snakeFood.length; i++) {
    		//fills in Food with state.snakeFood Coordinates
    		Food[i] = state.getSnakeFood()[i];
    	}	
    	Coordinate newFood = new Coordinate((int)(Math.random() * 14), (int)(Math.random() * 14)); //randomly generates newFood Coordinates
    	while (state.board[newFood.getRow()][newFood.getColumn()] != ' '){
    		//will run until newFood Coordinates are valid
        	newFood = new Coordinate((int)(Math.random() * 14), (int)(Math.random() * 14));
    	}
    	for (int i = 0; i < state.snakeFood.length; i++) {
    		//finds the food that was eaten
    		if (Food[i].getRow() == state.snake[0].getRow() && Food[i].getColumn()
    			== state.snake[0].getColumn()){
    			Food[i] = newFood; //replaces eaten food with newFood   	
    		}
    	}	
        SnakeState newState = new SnakeState(state.getBoard(), state.getSnake(), Food, state.getTimeTick());
        Coordinate newFood2 = new Coordinate((int)(Math.random() * 14), (int)(Math.random() * 14));
        while (newState.board[newFood2.getRow()][newFood2.getColumn()] != ' '){
            newFood2 = new Coordinate((int)(Math.random() * 14), (int)(Math.random() * 14));
        	}
    	Food[Food.length - 1] = newFood2;
    	newState = new SnakeState(state.getBoard(), state.getSnake(), Food, state.getTimeTick());
    	return newState;	
    }
	
	public SnakeState generateSnake(SnakeState state) {
		//Generates snake after food is eaten
		Coordinate [] snakeTail = {state.getSnake()[state.snake.length - 1]};
		SnakeState tailState = new SnakeState(state.getBoard(), snakeTail, state.getSnakeFood(), state.getTimeTick());
		SnakeAI newAI = new SnakeAI(tailState); //new AI instance
		SnakeMove[] positions = newAI.getAllMoves(tailState); //getAllMoves for the position of snakeTail
		int randomNum1 = (int)(Math.random() * 3);  //chooses position randomly
		SnakeMove newsnakeTail = positions[randomNum1]; 
		while (state.getBoard()[positions[randomNum1].move.getRow()][positions[randomNum1].move.getColumn()] != ' '){
			//will run until the chosen snakeTail is valid
			randomNum1 = (int)(Math.random() * 3);
			newsnakeTail = positions[randomNum1];
		}
		Coordinate[] newSnake = new Coordinate[state.getSnake().length + 1]; //increases snake length by 1
    	for (int i = 0; i < state.snake.length; i++) {
    		//generates newSnake
    		newSnake[i] = new Coordinate(state.snake[i].getRow(), state.snake[i].getColumn()); 
    		
    	}
    	newSnake[newSnake.length - 1] = new Coordinate(newsnakeTail.move.getRow(), newsnakeTail.move.getColumn()); //adds snakeTail to snake
    	SnakeState newState = new SnakeState(state.getBoard(), newSnake, state.getSnakeFood(), state.getTimeTick()); //new SnakeState with newSnake
		return newState;
	}
	
	public char[][] makeBoard(SnakeState state){
		//makes a board using current state
		char[][] board = new char[15][15]; //empty board
		for (int i = 0; i < state.board.length; i++){
			//fills in empty board with previous state
			for (int j = 0; j < state.board.length; j++){
				board[i][j] = state.getBoard()[i][j];
			}		
		}
		for (Coordinate coordinate: state.getSnake()){
			//puts snake on board
			board[coordinate.getRow()][coordinate.getColumn()] = 'x';
		}
		for (Coordinate coordinate: state.getSnakeFood()){
			//puts snakeFood on board
			board[coordinate.getRow()][coordinate.getColumn()] = 'o';
		}
		return board;		
	}	
	
	public SnakeState make(SnakeState state){
		//makes new SnakeState after move is made
		SnakeState newState = moveSnake(state);
		char[][] board = new char[15][15]; //empty board
		int time = state.getTimeTick() + 1; //timeTick increase by 1
		for (int i = 0; i < state.board.length; i++){
			//fills in empty board with previous state
			for (int j = 0; j < state.board.length; j++){
				board[i][j] = state.getBoard()[i][j];
			}		
		}
		for (Coordinate coordinate: newState.getSnake()){
			//puts snake on board
			board[coordinate.getRow()][coordinate.getColumn()] = 'x';
		}
		for (Coordinate coordinate: newState.getSnakeFood()){
			//puts snakeFood on board
			board[coordinate.getRow()][coordinate.getColumn()] = 'o';
		}
		newState = new SnakeState(board, newState.getSnake(), newState.getSnakeFood(), time); //new state after move
		return newState;
	}
	
	public boolean isValid(SnakeState state) {
		boolean result;
		if (state.board[move.getRow()][move.getColumn()] != 'x' && state.board[move.getRow()][move.getColumn()] != '-'
			&& state.board[move.getRow()][move.getColumn()] != '|' && state.board[move.getRow()][move.getColumn()] != '+') {
			result = true;
		}
		else {
			result = false;		
		}
		return result;
	}
	
	
	
	

}



Is This A Good Question/Topic? 0
  • +

Replies To: Snake Game Help

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Snake Game Help

Posted 27 April 2011 - 05:43 PM

Please the exact error message with the stack trace
Post also all your class (with the import statements) so we can match the line numbers with the error stack

If it has do do with your SnakeState class you will also have to post it as for your Coordinate class
Was This Post Helpful? 0
  • +
  • -

#3 LostSignal13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-April 11

Re: Snake Game Help

Posted 27 April 2011 - 06:04 PM

SnakeState:

public class SnakeState {
	char board[][];
	Coordinate[] snake;
	Coordinate[] snakeFood;
	int timeTick;
	
	public SnakeState(char board[][], Coordinate[] snake, Coordinate[] snakeFood, int timeTick){
		this.board = board;
		this.snake = snake;
		this.snakeFood = snakeFood;
		this.timeTick = timeTick;
	}
	
	public Coordinate[] getSnake(){
		return snake;
	}
	
	public char[][] getBoard(){
		return board;
	}
	
	public Coordinate[] getSnakeFood(){
		return snakeFood;
	}
	
	public int getTimeTick(){
		return timeTick;
	}
}



Snake Coordinate
public class Coordinate {
	int row;
	int column;
	
	public Coordinate(int row, int column){
		this.row = row;
		this.column = column;
	}
	
	public int getRow(){
		return row;
	}
	
	public int getColumn(){
		return column;
	}
	
	
}



Snake AI

public class SnakeAI {
	SnakeState snake;
	
	public SnakeAI(SnakeState snake){
		this.snake = snake;
	}
	    
	  public SnakeMove[] getAllMoves(SnakeState state) {
		  Coordinate head = state.snake[0];
		  Coordinate up = new Coordinate(head.getRow(), head.getColumn() + 1);
		  Coordinate down = new Coordinate(head.getRow(), head.getColumn() - 1);
		  Coordinate left = new Coordinate(head.getRow() - 1, head.getColumn());
		  Coordinate right = new Coordinate(head.getRow() + 1, head.getColumn());
		  SnakeMove[] allMoves = {new SnakeMove(up),new SnakeMove(down), new SnakeMove(left), new SnakeMove(right)};
	      return allMoves;
	    }
	  
	  public SnakeMove move(){
		  SnakeMove[] moves = getAllMoves(snake);
		  int randomNum = (int)Math.random() * 4;
		  SnakeMove newMove = moves[randomNum];
		  while (newMove.isValid(snake) == false){
			  randomNum = (int)Math.random() * 4;
			  newMove = moves[randomNum];
		  }
		  return newMove;
	  }
	  
}



Error Message
junit.framework.AssertionFailedError: expected:<2> but was:<1>
	at junit.framework.Assert.fail(Assert.java:47)
	at junit.framework.Assert.failNotEquals(Assert.java:280)
	at junit.framework.Assert.assertEquals(Assert.java:64)
	at junit.framework.Assert.assertEquals(Assert.java:198)
	at junit.framework.Assert.assertEquals(Assert.java:204)
	at SnakeTests.test_generateSnake(SnakeTests.java:64)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at junit.framework.TestCase.runTest(TestCase.java:164)
	at junit.framework.TestCase.runBare(TestCase.java:130)
	at junit.framework.TestResult$1.protect(TestResult.java:106)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.framework.TestResult.run(TestResult.java:109)
	at junit.framework.TestCase.run(TestCase.java:120)
	at junit.framework.TestSuite.runTest(TestSuite.java:230)
	at junit.framework.TestSuite.run(TestSuite.java:225)
	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)




Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Snake Game Help

Posted 27 April 2011 - 06:14 PM

Error is originating from TestCase and TestUnit

So it's an Assert error for an invalid condition. We will have to see what you based your Assert on.


Did you tried it from a simple main() ?

P.S.
and before rewriting basic class like Coordinate from scratch check the API.
awt.Point and/or awt.Dimension would have done the job for you
Was This Post Helpful? 0
  • +
  • -

#5 LostSignal13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-April 11

Re: Snake Game Help

Posted 27 April 2011 - 06:34 PM

I've tried printing it out from the main() and it continually prints out the snake just around the food. The snake never manages to sprout a tail at all.

I've only been programming in java for about 2 1/2 months now so I forget to check the API sometimes.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Snake Game Help

Posted 27 April 2011 - 06:47 PM

View PostLostSignal13, on 27 April 2011 - 08:34 PM, said:

I've tried printing it out from the main() and it continually prints out the snake just around the food. The snake never manages to sprout a tail at all.

And you are using JUnit... it can be your conditions that are false
Just try it from main() without JUnit and we'll see what happens
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1