Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- //Board is a data struture which will represent the playing board
- class Board{
- //using an arraylist as the datastructure of the board
- ArrayList<Box> sl = new ArrayList<Box>();
- //this method will choose where each of the ladders/snakes will be on the board
- //taking into account *even spacing ****need to edit to make sure that the top of a ladder
- // doesnt == top of snake too!
- int[] getArrayOfSpecial(){
- int holding=0,low=4,high=12,i=1;
- //rng to figure out how spaced until the next ladder/snake
- Random rnf = new Random();
- holding=rnf.nextInt(high-low)+low;
- int [] tempHold = new int [20];
- //filling an array with the locations of specials
- while(tempHold[i-1]<90){
- holding=rnf.nextInt(high-low) + low;
- tempHold[i]=holding+tempHold[i-1];
- i++;
- }
- int [] finArray = new int [tempHold.length];
- finArray=tempHold;
- //returning all the speical locations in an array
- return finArray;
- }
- //method to fill the board with boxes and specials
- ArrayList<Box> fillBoard(){
- int[]specialAddress=getArrayOfSpecial();
- int x=0;
- for(int j=0;j<100;j++){
- if(j==specialAddress[x]){
- getSpecial(specialAddress,j,x);
- x++;
- }
- else getBox(j);
- }
- return sl;
- }
- Box getBox(int j){
- Box b = new Box(j,false);
- return b;
- }
- //creating the ladder or snake
- Special getSpecial(int [] array,int j, int x){
- boolean isSnake=getSnake(array,x);
- Special s = new Special(j,getOtherEnd(j,isSnake),isSnake,true);
- return s;
- }
- //rng to pick if the next special will be ladder of snake
- boolean getSnake(int [] array,int x){
- Random rnSnake = new Random();
- int answer = rnSnake.nextInt(2)+1;
- if(x<2){
- return false;
- }
- else if(x>array.length-2){
- return true;
- }
- else return answer==1;
- }
- //method to make sure the other end of the snake or ladder doesnt leave the board dimensions
- int getOtherEnd(int j, boolean var){
- int otherHalf=0;
- Random rng = new Random();
- while(otherHalf<99 && otherHalf>1){
- int yAxis=rng.nextInt(3)+1;
- int xAxis=rng.nextInt(14);
- xAxis=xAxis-7;
- yAxis=yAxis*10;
- otherHalf=yAxis+xAxis;
- if(var==true){
- otherHalf=otherHalf-j;
- }
- else otherHalf=otherHalf+j;
- }
- return otherHalf;
- }
- }
- class Box{
- boolean specialObject;
- int squareNum;
- Box(){}
- Box(int squareNum0,boolean specialObject0){
- squareNum0=squareNum;
- specialObject0=specialObject;
- }
- }
- //this class is an extension of box it is either a ladder or snake depending on the variable
- class Special extends Box{
- boolean isSnake;
- int otherEnd;
- Special(){}
- Special(int squareNum0, int otherEnd0, boolean isSnake0, boolean specialObject0){
- super(squareNum0,specialObject0);
- otherEnd0=otherEnd;
- isSnake0=isSnake;
- }
- }
- class Die{
- int dieValue;
- int rollDie(){
- Random rn = new Random();
- dieValue=rn.nextInt(6);
- System.out.println("Dice Rolled: "+dieValue);
- return dieValue;
- }
- }
- class PlayerPiece{
- int boardLocation;
- }
- class SnakeLadder123{
- public static void main (String [] args){
- System.out.println("Main");
- }
- static void playGame(){
- System.out.println("Game has begun");
- PlayerPiece Player1 = new PlayerPiece();
- PlayerPiece Player2 = new PlayerPiece();
- Board b= new Board();
- ArrayList gameBoard=new ArrayList<>();
- gameBoard=b.fillBoard();
- Die dice = new Die();
- }
- PlayerPiece turn(PlayerPiece p,Die d,ArrayList<Box> g){
- d.rollDie();
- p.boardLocation=p.boardLocation+d.dieValue;
- Box b = g.get(p.boardLocation);
- if(b.specialObject==true){
- //some tiny trouble here with accessing special variables as a box but mehhh almost finished
- if(b.isSnake==true){
- System.out.println("Snake! :(");
- p.boardLocation=b.otherEnd;
- if(d.dieValue==6){
- turn(p,d,g);
- }
- return p;
- }
- else{
- System.out.println("Ladder! :)");
- p.boardLocation=b.otherEnd;
- if(d.dieValue==6){
- turn(p,d,g);
- }
- return p;
- }
- }
- else if(b.specialObject==false){
- if(d.dieValue==6){
- turn(p,d,g);
- }
- return p;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement