SHARE
TWEET

Untitled

a guest Sep 19th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.InputMismatchException;
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. class input {
  7.     static Scanner inp;
  8.     input() {
  9.         inp=new Scanner(System.in);
  10.     }
  11. }
  12.  
  13. class GameWinnerException extends Exception{
  14.     public GameWinnerException(String message) {
  15.         super(message);
  16.     }
  17. }
  18.  
  19. class DieValueException extends Exception{
  20.     public DieValueException(String message) {
  21.         super(message);
  22.     }
  23. }
  24.  
  25. class SnakeBiteException extends Exception{
  26.     public SnakeBiteException(String message) {
  27.         super("Hiss...! I am a Snake, you go back 7 tiles!");
  28.     }
  29. }
  30.  
  31. class CricketBiteException extends Exception{
  32.     public CricketBiteException(String message) {
  33.         super("Chirp...! I am a Cricket, you go back 2 tiles!");
  34.     }
  35. }
  36.  
  37. class VultureBiteException extends Exception{
  38.     public VultureBiteException(String message) {
  39.         super("");
  40.     }
  41. }
  42.  
  43. class TrampolineBiteException extends Exception{
  44.     public TrampolineBiteException(String message) {
  45.         super("");
  46.     }
  47. }
  48.  
  49. class TrackLengthException extends Exception{
  50.     public TrackLengthException(String message) { super(""); }
  51. }
  52.  
  53. class Game{
  54.     private int length;
  55.     private ArrayList<String> track = new ArrayList<>();
  56.  
  57.  
  58.     public ArrayList<String> getTrack() {
  59.         return track;
  60.     }
  61.  
  62.     public void setTrack(ArrayList<String> track) {
  63.         this.track = track;
  64.     }
  65.  
  66.     public int getLength() {
  67.         return length;
  68.     }
  69.  
  70.     void start() throws InputMismatchException, DieValueException, SnakeBiteException, VultureBiteException,
  71.             CricketBiteException, TrampolineBiteException, GameWinnerException, TrackLengthException {
  72.         boolean ans = false;
  73.         while(!ans){
  74.             System.out.print("Enter total number of tiles on the race track (length): ");
  75.             try {
  76.                 length = input.inp.nextInt();
  77.                 if(length<10){
  78.                     throw new TrackLengthException("");
  79.                 }
  80.                 else{
  81.                     ans = true;
  82.                 }
  83.             }
  84.             catch(InputMismatchException inp) {
  85.                 System.out.println("Wrong input:");
  86.                 System.out.println("Try again");
  87.                 input.inp.next();
  88.             }
  89.             catch (TrackLengthException e){
  90.  
  91.                 System.out.println("Minimum length of the track should be 10");
  92.             }
  93.         }
  94.  
  95.         Tile snake = new Snake();
  96.         Tile vulture = new Vulture();
  97.         Tile cricket = new Cricket();
  98.         Tile trampoline = new Trampoline();
  99.         Tile white = new White();
  100.  
  101.         Map map = new Map();
  102.         map.generate(length, snake, vulture, cricket, trampoline, white);
  103.         snake.setShake_tile(false);
  104.         vulture.setShake_tile(false);
  105.         cricket.setShake_tile(false);
  106.         trampoline.setShake_tile(true);
  107.         white.setShake_tile(1);
  108.  
  109.         System.out.println("Danger: There are "+snake.getTile()+", "+cricket.getTile()+", "+vulture.getTile()+" numbers"
  110.                 + " of Snakes, Cricket, and Vultures respectively on your track!");
  111.  
  112.         System.out.println("Danger: Each Snake, Cricket, and Vultures can throw you back by "+snake.getShake_tile()+", "+
  113.                 cricket.getShake_tile()+", "+vulture.getShake_tile()+ " number of Tiles respectively!");
  114.  
  115.         System.out.println("Good News: There are "+ trampoline.getTile() +" number of Trampolines on your track!");
  116.  
  117.         System.out.println("Good News: Each Trampoline can help you advance by "+ trampoline.getShake_tile()
  118.                 +" number of Tiles");
  119.  
  120.         map.generate_race_track(track, length, snake, vulture, cricket, trampoline, white);
  121.         track.add(0,"");
  122.  
  123.  
  124.         System.out.print("Enter the User Name: ");
  125.         String name = input.inp.next();
  126.         User user = new User(name);
  127.  
  128.         System.out.println("Starting the game with "+user.getName()+ " at Tile-1");
  129.         System.out.println("Control transferred to Computer for rolling the Dice for Josh");
  130.  
  131.         boolean status= true;
  132.         while(status){
  133.             try{
  134.                 play(user, snake, vulture, cricket, trampoline, white);
  135.             }
  136.             catch (GameWinnerException e){
  137.                 status= false;
  138.                 int no_of_rolls = user.getRolls()-1;
  139.                 System.out.println(user.getName()+" wins the race in "+no_of_rolls+" rolls.");
  140.             }
  141.             catch (DieValueException e){
  142.                 System.out.println(e.getMessage());
  143.             }
  144.         }
  145.  
  146.         System.out.println("Total Snake Bites = "+snake.getBites());
  147.         System.out.println("Total Vulture Bites = "+vulture.getBites());
  148.         System.out.println("Total Cricket Bites = "+cricket.getBites());
  149.         System.out.println("Total Trampoline Bites ="+trampoline.getBites());
  150.     }
  151.  
  152.     void roll_message(User user, int roll_value){
  153.         System.out.print("[Roll-"+user.getRolls()+"]: "+user.getName()+" rolled "+ roll_value+" at Tile:"+user.getPosition()+". ");
  154.     }
  155.  
  156.     void set_position(Tile tile, User user){
  157.         tile.setBites(tile.getBites()+1);
  158.         int pos = user.getPosition()+tile.getShake_tile();
  159.         if(pos<1){
  160.             user.setPosition(1);
  161.         }
  162.         else{
  163.             user.setPosition(pos);
  164.         }
  165.     }
  166.  
  167.     void roll_message_position(User user, Tile snake, Tile vulture, Tile cricket, Tile trampoline, Tile white)
  168.             throws SnakeBiteException, VultureBiteException, CricketBiteException, TrampolineBiteException{
  169.         System.out.print("Landed on Tile: "+user.getPosition());
  170.         System.out.println();
  171.         System.out.println("Trying to shake the Tile: "+user.getPosition());
  172.         if(user.getPosition()==track.size()-1){
  173.             user.setPosition(track.size()-1);
  174.         }
  175.         else if(track.get(user.getPosition()).equals("S")){
  176.             set_position(snake, user);
  177.             throw new SnakeBiteException(" Hiss...! I am a Snake, you go back "+ snake.getShake_tile() +" tiles!");
  178.         }
  179.         else if(track.get(user.getPosition()).equals("C")){
  180.             set_position(cricket, user);
  181.             throw new CricketBiteException("Chirp...! I am a Cricket, you go back "+ cricket.getShake_tile() +" tiles!");
  182.         }
  183.         else if(track.get(user.getPosition()).equals("V")){
  184.             set_position(vulture, user);
  185.             throw new VultureBiteException("Yapping...! I am a Vulture, you go back "+ vulture.getShake_tile() +" tiles!");
  186.         }
  187.         else if(track.get(user.getPosition()).equals("T")){
  188.             trampoline.setBites(trampoline.getBites()+1);
  189.             int pos = user.getPosition()+trampoline.getShake_tile();
  190.             if(pos>track.size()-1){
  191.                 user.setPosition(user.getPosition());
  192.             }
  193.             else{
  194.                 user.setPosition(pos);
  195.             }
  196.             throw new TrampolineBiteException("PingPong! I am a Trampoline, you advance "+ trampoline.getShake_tile()+" tiles");
  197.         }
  198.         else{
  199.             System.out.println("I am a white Tile!");
  200.         }
  201.     }
  202.  
  203.     void play(User user, Tile snake, Tile vulture, Tile cricket, Tile trampoline, Tile white)
  204.             throws DieValueException, SnakeBiteException, VultureBiteException, CricketBiteException,
  205.             TrampolineBiteException, GameWinnerException {
  206.         if(user.getRolls()==1){
  207.             System.out.println("Game Started ======================>");
  208.         }
  209.  
  210.  
  211.         Die die = new Die();
  212.         int roll_value=0;
  213.  
  214.         while(!user.isWin_status()){
  215.             if(user.getPosition()==1){
  216.                 while(roll_value!=6){
  217.                     roll_value = die.roll();
  218.                     roll_message(user, roll_value);
  219.                     if(roll_value!=6){
  220.                         user.setRolls(user.getRolls()+1);
  221.                         throw new DieValueException("OOPs you need 6 to start");
  222.                     }
  223.                     else{
  224.                         System.out.println("You are out of the cage! You get a free roll");
  225.                     }
  226.                     user.setRolls(user.getRolls()+1);
  227.                 }
  228.                 roll_value = die.roll();
  229.                 roll_message(user, roll_value);
  230.                 if(user.getPosition()+roll_value<=track.size()-1){
  231.                     exceptionnn(user, snake, vulture, cricket, trampoline, white, roll_value);
  232.                 }
  233.                 else{
  234.                     System.out.println("Landed on Tile: "+user.getPosition());
  235.                 }
  236.  
  237.             }
  238.             else{
  239.                 roll_value = die.roll();
  240.                 roll_message(user, roll_value);
  241.                 user.setRolls(user.getRolls()+1);
  242.                 if(user.getPosition()+roll_value<=track.size()-1){
  243.                     exceptionnn(user, snake, vulture, cricket, trampoline, white, roll_value);
  244.                     if(user.getPosition()>=track.size()-1){
  245.                         user.setWin_status(true);
  246.                     }
  247.                 }
  248.                 else{
  249.                     System.out.println("Landed on Tile: "+user.getPosition());
  250.                 }
  251.             }
  252.         }
  253.         throw new GameWinnerException(user.getName()+" wins the race in "+user.getRolls()+" rolls.");
  254.     }
  255.  
  256.     private void exceptionnn(User user, Tile snake, Tile vulture, Tile cricket, Tile trampoline, Tile white, int roll_value) {
  257.         user.setPosition(user.getPosition()+roll_value);
  258.         try{
  259.             roll_message_position(user, snake, vulture, cricket, trampoline, white);
  260.         }
  261.         catch(SnakeBiteException e){
  262.             System.out.println(" Hiss...! I am a Snake, you go back "+ snake.getShake_tile() +" tiles!");
  263.         }
  264.         catch (VultureBiteException e){
  265.             System.out.println("Yapping...! I am a Vulture, you go back "+ vulture.getShake_tile() +" tiles!");
  266.         }
  267.         catch(CricketBiteException e){
  268.             System.out.println("Chirp...! I am a Cricket, you go back "+ cricket.getShake_tile() +" tiles!");
  269.         }
  270.         catch (TrampolineBiteException e){
  271.             System.out.println("PingPong! I am a Trampoline, you advance + "+trampoline.getShake_tile()+" tiles");
  272.         }
  273.         System.out.println(user.getName()+" moved to Tile: "+user.getPosition());
  274. //        user.setRolls(user.getRolls()+1);
  275.     }
  276. }
  277.  
  278. class User{
  279.     private final String name;
  280.     private int rolls=1;
  281.     private int position;
  282.     private boolean win_status;
  283.  
  284.     User(String name){
  285.         this.name= name;
  286.         this.position = 1;
  287.         this.win_status = false;
  288.  
  289.     }
  290.  
  291.     public int getPosition() {
  292.         return position;
  293.     }
  294.  
  295.     public void setPosition(int position) {
  296.         this.position = position;
  297.     }
  298.  
  299.     public boolean isWin_status() {
  300.         return win_status;
  301.     }
  302.  
  303.     public void setWin_status(boolean win_status) {
  304.         this.win_status = win_status;
  305.     }
  306.  
  307.     public String getName() {
  308.         return name;
  309.     }
  310.  
  311.     public int getRolls() {
  312.         return rolls;
  313.     }
  314.  
  315.     public void setRolls(int rolls) {
  316.         this.rolls = rolls;
  317.     }
  318. }
  319.  
  320. class Die{
  321.     int roll(){
  322.         Random random = new Random();
  323.         int value = random.nextInt(6);
  324.         return value+1;
  325.     }
  326. }
  327.  
  328. class Map{
  329.  
  330.     void generate(int length, Tile snake, Tile vulture, Tile cricket, Tile trampoline, Tile white){
  331.         Random random = new Random();
  332.         int range = length/5;
  333.         int x = random.nextInt(range)+1;
  334.         snake.setTile(x);
  335.         length=length-x;
  336.  
  337.         x = random.nextInt(range)+1;
  338.         vulture.setTile(x);
  339.         length= length-x;
  340.  
  341.         x = random.nextInt(range)+1;
  342.         cricket.setTile(x);
  343.         length= length-x;
  344.  
  345.         x = random.nextInt(range)+1;
  346.         trampoline.setTile(x);
  347.         length= length-x;
  348.         white.setTile(length);
  349.     }
  350.  
  351.     void set(ArrayList<String> track, ArrayList<Integer> temp, String value, Tile type){
  352.         Random random = new Random();
  353.         int tile = type.getTile();
  354.         while(tile>0){
  355.             int rand = random.nextInt(temp.size());
  356.             track.set(temp.get(rand), value);
  357.             temp.remove(rand);
  358.             tile--;
  359.         }
  360.     }
  361.  
  362.     void generate_race_track(ArrayList<String> track, int length, Tile snake, Tile vulture, Tile cricket, Tile trampoline, Tile white){
  363.         ArrayList<Integer> temp = new ArrayList<>();
  364.         for(int i=0;i<length; i++){
  365.             temp.add(i);
  366.             track.add("W");
  367.         }
  368.         set(track, temp, "S", snake);
  369.         set(track, temp, "V", vulture);
  370.         set(track, temp, "C", cricket);
  371.         set(track, temp, "T", trampoline);
  372.         set(track, temp, "W", white);
  373.     }
  374. }
  375.  
  376. abstract class Tile {
  377.     private int shake_tile;
  378.     abstract int getTile();
  379.     abstract void setTile(int x);
  380.     private int bites=0;
  381.  
  382.     public int getBites() {
  383.         return bites;
  384.     }
  385.  
  386.     public void setBites(int bites) {
  387.         this.bites = bites;
  388.     }
  389.  
  390.     void setShake_tile(boolean type){
  391.         Random random = new Random();
  392.         int value = random.nextInt(10);
  393.         shake_tile = value+1;
  394.         if(!type){
  395.             shake_tile = (-1*shake_tile);
  396.         }
  397.     }
  398.  
  399.     public int getShake_tile() {
  400.         return shake_tile;
  401.     }
  402.  
  403.     void setShake_tile(int l){
  404.         shake_tile = l;
  405.     }
  406.  
  407. }
  408.  
  409. class White extends Tile{
  410.     private int tile_White;
  411.  
  412.     @Override
  413.     public int getTile() {
  414.         return tile_White;
  415.     }
  416.  
  417.     @Override
  418.     public void setTile(int tile_White) {
  419.         this.tile_White = tile_White;
  420.     }
  421.  
  422. }
  423.  
  424. class Snake extends Tile{
  425.     private int tile_Snake;
  426.  
  427.     @Override
  428.     public int getTile() {
  429.         return tile_Snake;
  430.     }
  431.  
  432.     @Override
  433.     public void setTile(int tile_Snake) {
  434.         this.tile_Snake = tile_Snake;
  435.     }
  436. }
  437.  
  438. class Cricket extends Tile{
  439.     private int tile_Cricket;
  440.  
  441.     @Override
  442.     public int getTile() {
  443.         return tile_Cricket;
  444.     }
  445.  
  446.     @Override
  447.     public void setTile(int tile_Cricket) {
  448.         this.tile_Cricket = tile_Cricket;
  449.     }
  450. }
  451.  
  452. class Vulture extends Tile{
  453.     private int tile_Vulture;
  454.  
  455.     @Override
  456.     public int getTile() {
  457.         return tile_Vulture;
  458.     }
  459.  
  460.     @Override
  461.     public void setTile(int tile_Vulture) {
  462.         this.tile_Vulture = tile_Vulture;
  463.     }
  464. }
  465.  
  466. class Trampoline extends Tile{
  467.     private int tile_Trampoline;
  468.  
  469.     @Override
  470.     public int getTile() {
  471.         return tile_Trampoline;
  472.     }
  473.  
  474.     @Override
  475.     public void setTile(int tile_Trampoline) {
  476.         this.tile_Trampoline = tile_Trampoline;
  477.     }
  478. }
  479.  
  480. public class Main {
  481.     public static void main(String[] args) throws DieValueException, SnakeBiteException, VultureBiteException,
  482.             CricketBiteException, TrampolineBiteException, GameWinnerException, TrackLengthException {
  483.         input temp=new input();
  484.         Game game = new Game();
  485.         game.start();
  486.     }
  487. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top