vendredi 13 novembre 2020

debug/fix up snakes and ladders code using functions & a constructor

I'm writing this snake and ladders code using functions and construction. there seems to be an issue with the code I wrote but I'm not sure what the problem is. here is the code-

public class SnakesLadders {
    private static int turn;
    private static int p1,p2;
    private static int board[]= new int[101];
    
    public SnakesLadders() { 
        this.p1=0;
        this.p2=0; 
        board();
    }


    public static void board() {
        for(int i =0;i<board.length;i++) {
            board[i]=i;
        }
        board[2]=38;//ladder
        board[7]=14;
        board[8]=31;
        board[15]=26;
        board[21]=42;
        board[28]=84;
        board[36]=44;
        board[51]=67;
        board[78]=98;
        board[87]=94;
        board[71]=91;//ladder
        board[99]=80;//snake
        board[95]=75;
        board[92]=88;
        board[89]=68;
        board[74]=53;
        board[64]=60;
        board[62]=19;
        board[46]=25;
        board[49]=11;
        board[16]=6;//snake
        for(int i =0;i<board.length;i++) {
            System.out.println(board[i]);
        }
    }
    public static void alternate(int turn) {//alternate function
        if(turn==1) {
            turn=0;
        }
        else if(turn==0) {
            turn=1;
        }
        else {
            turn=0;
        }
        }
    
    public static String play(int die1, int die2) {
      int sum= die1+die2;
      alternate(turn);
      //player and change it to plus sum of dice
      if(turn==0 && (p1+sum)<100){
          p1+=sum;  
          p1=board[p1];
          if(die1!=die2) {//afterwards alternate
             alternate(turn);
          }
          }        
      
      else{
          if(turn==1 && (p2+sum)<100){
              p2+=sum;
              p2=board[p2];
                if(die1!=die2) {//afterwards alternate
                    alternate(turn);
                  }
                  }
                }     
      
      if(p1== 100) {
          return "player 1 won";
      }
      else if(p2==100) {
          return "player 2 won";
      }
      else
        if(turn==0){
          return "Player 1 is on square "+ p1;
        }
      else{
        return "Player 2 is on square "+ p2;
        
    }
    }
}

the following test is supposed to run

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SampleTest {
    @Test
    public void exampleTests() {
        SnakesLadders game = new SnakesLadders();
        assertEquals("Player 1 is on square 38", game.play(1, 1));
        assertEquals("Player 1 is on square 44", game.play(1, 5));
        assertEquals("Player 2 is on square 31", game.play(6, 2));
        assertEquals("Player 1 is on square 25", game.play(1, 1));
    }
}

any tips/advice would be appreciated. error/test fail: expected Player 2 is on square 31, actual: Player 1 is on square 52

Aucun commentaire:

Enregistrer un commentaire