KillianMills

Game.java

Oct 9th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.82 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4.  
  5. class Game{
  6.        
  7.         public static void main (String [] args){
  8.                 System.out.println("Main");
  9.                 Board board=new Board();
  10.                 Map<Integer,SpecialTile> gameBoardSpecial=board.fillBoard();
  11.                 int [] gameBoardPlain=board.fillVanillaBoard();
  12.                
  13.                
  14.                 Die gameDie = new Die();
  15.                
  16.                 int turn = 0; // Turn tracking variable.
  17.                 int winnersID = 999; // Random number, other than the actual players.
  18.                 boolean winFlag = false; // This will keep the loop of the game going.
  19.                 int numberOfPlaers;
  20.                
  21.                
  22.                 Scanner getInput = new Scanner(System.in); // Scanner used for user input.
  23.                 System.out.println("Enter the number of players: ");
  24.                 int noOfPlayers=getInput.nextInt(); // Take user input for the number of players that will take part in the game.
  25.                
  26.                 // If the value entered is greater than 4 enter the while loop below, and ask the user to enter a new value.
  27.                
  28.                 while(noOfPlayers>4){ // While loop that restricts the number of players to 4. If more than 4, ask for valid range.
  29.                         System.out.println("\nSorry, the maximum # of players is 4. Try again: ");
  30.                         noOfPlayers=getInput.nextInt(); // Get the value from the user.      
  31.                 }
  32.                
  33.                 getInput.close(); // Closing the scanner object.
  34.                
  35.                 ArrayList<Player>myListOfPlayers=playerSetup(noOfPlayers); // We're creating a list of players.
  36.                
  37.                
  38.                 while(winFlag==false){ // Game loop starts here.
  39.                        
  40.                         myListOfPlayers.get(turn).playerLocation = myListOfPlayers.get(turn).playerLocation + gameDie.rollDie(); // Moving the player forwards.
  41.                        
  42.                        
  43.                        
  44.                         if(myListOfPlayers.get(turn).playerLocation >= 100){ // Here I noticed we don't need the "isWinner bool flag"
  45.                                 winnersID = turn; // The winnersID is the one that's taking the turn.
  46.                         winFlag = true; // This will break the loop.
  47.                         }
  48.                        
  49.                         if(turn == (noOfPlayers-1)){ //If the turn is of the last player, and we have no winner yet, set the turn to be of first player.
  50.                                 turn = 0;
  51.                         }else{ // Else just increment so that the next player can take turn.
  52.                                 turn++;
  53.                         }
  54.                        
  55.                 }
  56.                
  57.                 System.out.println("The WINNER is player #" + winnersID + " ! CONGRATULATIONS!!!!"); // Announcing the winner.
  58.                
  59.                
  60.         }
  61.        
  62.        
  63.         /* ----------------------------------------------------------------------------------------------------------------------------
  64.          * playerSetup() -> This method will accept as parameter the number of players to take part in the game.
  65.          *                                      Afterwards a new ArrayList will be created that will contain all the players and then return the ArrayList.
  66.          */
  67.         static ArrayList<Player> playerSetup(int numberOfPlayers){
  68.                
  69.                 ArrayList<Player> playerList = new ArrayList<>(); // ArrayList containing Players.
  70.                
  71.                 for(int i=0;i<numberOfPlayers;i++){ // This for loop will put the players into the ArrayList.
  72.                         playerList.add(new Player(1,i,false)); // Add new Player  with parameters 1) Position | 2) Player ID | 3) Is a winner?
  73.                         System.out.println("Player #" + (i+1) + ". was created!\n"); // Notifying the user the players have been successfully created.
  74.                 }
  75.                
  76.                 return playerList; // Method returns the created ArrayList.
  77.         }
  78.         // -------------- END OF playerSetup() METHOD ---------------------------------
  79. }
  80. // ---------------------- END OF the Game CLASS ---------------------------------------
  81.  
  82.  
  83. class Die{
  84.         int dieValue;
  85.        
  86.         int rollDie(){
  87.                 Random rngForDie = new Random();
  88.                 dieValue=rngForDie.nextInt(6)+1;
  89.                 System.out.println("The Die rolled: "+dieValue);
  90.                 return dieValue;
  91.         }      
  92. }
  93.  
  94. class Player{
  95.         int playerLocation;
  96.         int playerID;
  97.        
  98.         Player(int playerLocation, int playerID, boolean isWinner){
  99.                 this.playerLocation=playerLocation;
  100.                 this.playerID=playerID;
  101.                 this.isWinner=isWinner;
  102.         }
  103. }
  104.  
  105. class SpecialTile{
  106.         int locationTile;
  107.         boolean isSnake;
  108.         int pointerToSpecialTile;
  109.        
  110.         SpecialTile(int locationTile,boolean isSnake,int pointerToSpecialTile){
  111.                         this.locationTile=locationTile;
  112.                         this.isSnake=isSnake;
  113.                 this.pointerToSpecialTile=pointerToSpecialTile;
  114.         }
  115. }
  116.  
  117. class Board{
  118.         Map<Integer,SpecialTile> mapOfSpecialTiles = new HashMap<>();
  119.         int [] vanillaBoard = new int[100];
  120.        
  121.         //Need to possibly rewrite algorithm
  122.         int [] getArrayOfSpecialLocation(){
  123.                 int low=4,high=12,i=1,counter=0;
  124.                 Random rngForNextPosition=new Random();
  125.                 int[]tempHoldingArray = new int [20];
  126.                 while(tempHoldingArray[i-1]<90){
  127.                         tempHoldingArray[i]=(rngForNextPosition.nextInt(high-low)+low)+tempHoldingArray[i-1];
  128.                         i++;
  129.                 }
  130.                 int [] finalArrayOfSpecial = new int [i-1];
  131.                 for(int k=0;k<tempHoldingArray.length;k++){
  132.                         if(tempHoldingArray[k]!=0){
  133.                                 finalArrayOfSpecial[counter]=tempHoldingArray[k];
  134.                                 counter++;
  135.                         }
  136.                 }
  137.                 return finalArrayOfSpecial;
  138.         }
  139.        
  140.         static void putMap(Map<Integer,SpecialTile>mapX,int [] arrayX){
  141.                 for(int i=0;i<mapX.size();i++){
  142.                         SpecialTile testObject=mapX.get(arrayX[i]);
  143.                         System.out.println(testObject.isSnake+" "+testObject.locationTile+" "+testObject.pointerToSpecialTile);
  144.                 }
  145.         }
  146.        
  147.         public Map<Integer,SpecialTile> fillBoard(){
  148.                 int [] specialAddresses=getArrayOfSpecialLocation();
  149.                 for(int j=0;j<specialAddresses.length;j++){
  150.                                 boolean isSnake=getSnake(specialAddresses,specialAddresses[j]);
  151.                                 mapOfSpecialTiles.put(specialAddresses[j],new SpecialTile(specialAddresses[j],isSnake,getOtherEnd(specialAddresses[j],isSnake,specialAddresses)));
  152.                         }
  153.                 putMap(mapOfSpecialTiles,specialAddresses);  
  154.                 return  mapOfSpecialTiles;
  155.         }
  156.        
  157.         int getOtherEnd(int matchingSide, boolean isSnake, int [] locationsOfSpecial){
  158.                 Random rngToPointer = new Random();
  159.                 int otherHalf;
  160.                 do{
  161.                         otherHalf=(rngToPointer.nextInt(100))%31+10;
  162.                 }while (checkUnique(locationsOfSpecial,otherHalf)==true);
  163.                 if(isSnake==true){
  164.                         otherHalf=matchingSide-otherHalf;
  165.                 }
  166.                 else{
  167.                         otherHalf=matchingSide+otherHalf;
  168.                 }
  169.                 return otherHalf;
  170.         }
  171.        
  172.         boolean checkUnique(int [] locationsOfSpecial,int otherHalfPointer){
  173.                 for(int i=0;i<locationsOfSpecial.length;i++){
  174.                         if(otherHalfPointer==locationsOfSpecial[i]){
  175.                                 return false;
  176.                         }
  177.                 }
  178.                 return true;
  179.         }
  180.        
  181.         boolean getSnake(int [] argArray,int valueFromSpecialArray){
  182.                 Random rngOfSnake=new Random();
  183.                 int snakeOrLadder=rngOfSnake.nextInt(2)+1;
  184.                 if(valueFromSpecialArray<2){
  185.                         return false;
  186.                 }
  187.                 else if(valueFromSpecialArray>argArray[argArray.length-2]){
  188.                         return true;
  189.                 }
  190.                 else return snakeOrLadder==1;
  191.         }
  192.        
  193.         int [] fillVanillaBoard(){
  194.                 for(int i=0;i<vanillaBoard.length;i++){
  195.                         vanillaBoard[i]=i;
  196.                 }
  197.                 return vanillaBoard;  
  198.         }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment