Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Game{
- public static void main (String [] args){
- System.out.println("Main");
- Board board=new Board();
- Map<Integer,SpecialTile> gameBoardSpecial=board.fillBoard();
- int [] gameBoardPlain=board.fillVanillaBoard();
- Die gameDie = new Die();
- int turn = 0; // Turn tracking variable.
- int winnersID = 999; // Random number, other than the actual players.
- boolean winFlag = false; // This will keep the loop of the game going.
- int numberOfPlaers;
- Scanner getInput = new Scanner(System.in); // Scanner used for user input.
- System.out.println("Enter the number of players: ");
- int noOfPlayers=getInput.nextInt(); // Take user input for the number of players that will take part in the game.
- // If the value entered is greater than 4 enter the while loop below, and ask the user to enter a new value.
- while(noOfPlayers>4){ // While loop that restricts the number of players to 4. If more than 4, ask for valid range.
- System.out.println("\nSorry, the maximum # of players is 4. Try again: ");
- noOfPlayers=getInput.nextInt(); // Get the value from the user.
- }
- getInput.close(); // Closing the scanner object.
- ArrayList<Player>myListOfPlayers=playerSetup(noOfPlayers); // We're creating a list of players.
- while(winFlag==false){ // Game loop starts here.
- myListOfPlayers.get(turn).playerLocation = myListOfPlayers.get(turn).playerLocation + gameDie.rollDie(); // Moving the player forwards.
- if(myListOfPlayers.get(turn).playerLocation >= 100){ // Here I noticed we don't need the "isWinner bool flag"
- winnersID = turn; // The winnersID is the one that's taking the turn.
- winFlag = true; // This will break the loop.
- }
- 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.
- turn = 0;
- }else{ // Else just increment so that the next player can take turn.
- turn++;
- }
- }
- System.out.println("The WINNER is player #" + winnersID + " ! CONGRATULATIONS!!!!"); // Announcing the winner.
- }
- /* ----------------------------------------------------------------------------------------------------------------------------
- * playerSetup() -> This method will accept as parameter the number of players to take part in the game.
- * Afterwards a new ArrayList will be created that will contain all the players and then return the ArrayList.
- */
- static ArrayList<Player> playerSetup(int numberOfPlayers){
- ArrayList<Player> playerList = new ArrayList<>(); // ArrayList containing Players.
- for(int i=0;i<numberOfPlayers;i++){ // This for loop will put the players into the ArrayList.
- playerList.add(new Player(1,i,false)); // Add new Player with parameters 1) Position | 2) Player ID | 3) Is a winner?
- System.out.println("Player #" + (i+1) + ". was created!\n"); // Notifying the user the players have been successfully created.
- }
- return playerList; // Method returns the created ArrayList.
- }
- // -------------- END OF playerSetup() METHOD ---------------------------------
- }
- // ---------------------- END OF the Game CLASS ---------------------------------------
- class Die{
- int dieValue;
- int rollDie(){
- Random rngForDie = new Random();
- dieValue=rngForDie.nextInt(6)+1;
- System.out.println("The Die rolled: "+dieValue);
- return dieValue;
- }
- }
- class Player{
- int playerLocation;
- int playerID;
- Player(int playerLocation, int playerID, boolean isWinner){
- this.playerLocation=playerLocation;
- this.playerID=playerID;
- this.isWinner=isWinner;
- }
- }
- class SpecialTile{
- int locationTile;
- boolean isSnake;
- int pointerToSpecialTile;
- SpecialTile(int locationTile,boolean isSnake,int pointerToSpecialTile){
- this.locationTile=locationTile;
- this.isSnake=isSnake;
- this.pointerToSpecialTile=pointerToSpecialTile;
- }
- }
- class Board{
- Map<Integer,SpecialTile> mapOfSpecialTiles = new HashMap<>();
- int [] vanillaBoard = new int[100];
- //Need to possibly rewrite algorithm
- int [] getArrayOfSpecialLocation(){
- int low=4,high=12,i=1,counter=0;
- Random rngForNextPosition=new Random();
- int[]tempHoldingArray = new int [20];
- while(tempHoldingArray[i-1]<90){
- tempHoldingArray[i]=(rngForNextPosition.nextInt(high-low)+low)+tempHoldingArray[i-1];
- i++;
- }
- int [] finalArrayOfSpecial = new int [i-1];
- for(int k=0;k<tempHoldingArray.length;k++){
- if(tempHoldingArray[k]!=0){
- finalArrayOfSpecial[counter]=tempHoldingArray[k];
- counter++;
- }
- }
- return finalArrayOfSpecial;
- }
- static void putMap(Map<Integer,SpecialTile>mapX,int [] arrayX){
- for(int i=0;i<mapX.size();i++){
- SpecialTile testObject=mapX.get(arrayX[i]);
- System.out.println(testObject.isSnake+" "+testObject.locationTile+" "+testObject.pointerToSpecialTile);
- }
- }
- public Map<Integer,SpecialTile> fillBoard(){
- int [] specialAddresses=getArrayOfSpecialLocation();
- for(int j=0;j<specialAddresses.length;j++){
- boolean isSnake=getSnake(specialAddresses,specialAddresses[j]);
- mapOfSpecialTiles.put(specialAddresses[j],new SpecialTile(specialAddresses[j],isSnake,getOtherEnd(specialAddresses[j],isSnake,specialAddresses)));
- }
- putMap(mapOfSpecialTiles,specialAddresses);
- return mapOfSpecialTiles;
- }
- int getOtherEnd(int matchingSide, boolean isSnake, int [] locationsOfSpecial){
- Random rngToPointer = new Random();
- int otherHalf;
- do{
- otherHalf=(rngToPointer.nextInt(100))%31+10;
- }while (checkUnique(locationsOfSpecial,otherHalf)==true);
- if(isSnake==true){
- otherHalf=matchingSide-otherHalf;
- }
- else{
- otherHalf=matchingSide+otherHalf;
- }
- return otherHalf;
- }
- boolean checkUnique(int [] locationsOfSpecial,int otherHalfPointer){
- for(int i=0;i<locationsOfSpecial.length;i++){
- if(otherHalfPointer==locationsOfSpecial[i]){
- return false;
- }
- }
- return true;
- }
- boolean getSnake(int [] argArray,int valueFromSpecialArray){
- Random rngOfSnake=new Random();
- int snakeOrLadder=rngOfSnake.nextInt(2)+1;
- if(valueFromSpecialArray<2){
- return false;
- }
- else if(valueFromSpecialArray>argArray[argArray.length-2]){
- return true;
- }
- else return snakeOrLadder==1;
- }
- int [] fillVanillaBoard(){
- for(int i=0;i<vanillaBoard.length;i++){
- vanillaBoard[i]=i;
- }
- return vanillaBoard;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment