Advertisement
makispaiktis

Game

Dec 10th, 2018 (edited)
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.12 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Map;
  3. import java.util.TreeMap;
  4.  
  5. public class Game {
  6.    
  7.     int round;
  8.    
  9.     // Constructors
  10.     public Game() {
  11.         round = 0;
  12.     }
  13.    
  14.     public Game(int round) {
  15.         this.round = round;
  16.     }
  17.    
  18.     // Getters, setters
  19.     public void setRound(int round) {
  20.         this.round = round;
  21.     }
  22.    
  23.     public int getRound() {
  24.         return round;
  25.     }
  26.    
  27.     Map<Integer,Integer> setTurns(ArrayList<Player> players) {
  28.         Map<Integer, Integer> playerTurns = new TreeMap<Integer, Integer>();
  29.         int rolls[] = new int[players.size()];
  30.         boolean flag;
  31.         do {
  32.             flag =  false;
  33.             for(int i = 0; i < rolls.length; i++) {
  34.                 rolls[i] = 1 + (int)(Math.random() * 6);
  35.                 for(int j = i - 1; j > -1; j--) {
  36.                     if(rolls[i] == rolls[j]) {
  37.                         flag = true;    //if one dice of a player matches another player's dice do while is repeated;
  38.                     }
  39.                 }
  40.                 //System.out.println("Key: " + rolls[i] + " Value: " + (i+1));
  41.                 playerTurns.put(rolls[i], i + 1);    //key = roll of the player with id i + 1 , value = id of player (i + 1)
  42.                 if(flag) {
  43.                     playerTurns.clear();
  44.                     break;
  45.                 }
  46.             }
  47.         } while(flag);
  48.         return playerTurns;
  49.     }
  50.  
  51.     public static void main(String[] args) {
  52.         //Initializing the necessary values
  53.         ArrayList<Integer[]> templatePath = new ArrayList<Integer[]>();
  54.         int numOfPlayers = 2;
  55.         Board gameBoard = new Board(10, 20, 3, 3, 6);
  56.         gameBoard.createBoard();
  57.         gameBoard.createElementBoard();
  58.         Player mavioux = new Player(1, "Mavioux", 0, gameBoard);
  59.         MinMaxPlayer thomas = new MinMaxPlayer(2, "Thomas", 0, gameBoard, templatePath);
  60.         Game game = new Game(0);
  61.        
  62.         //Print the board
  63.         System.out.println();
  64.         for(int i = 0; i < gameBoard.tiles.length; i++) {
  65.             for(int j = 0; j < gameBoard.tiles[i].length; j++) {
  66.                 if(gameBoard.tiles[i][j] < 10)
  67.                     System.out.print("  " + gameBoard.tiles[i][j] + " ");
  68.                 else if(gameBoard.tiles[i][j] < 100)
  69.                     System.out.print(" " + gameBoard.tiles[i][j] + " ");
  70.                 else
  71.                     System.out.print(gameBoard.tiles[i][j] + " ");
  72.             }
  73.             System.out.println();
  74.         }
  75.         System.out.println();
  76.         System.out.println();
  77.         System.out.println();
  78.        
  79.         int roll= 0;
  80.         int[][] playersMove = new int[6][5];
  81.        
  82.         //playersMove arrays gets zero in every instance (this array shows us the position of each player)
  83.         for(int i = 0; i < playersMove.length; i++) {
  84.             playersMove[i][0] = 0;
  85.         }
  86.        
  87.         //Initializing the arrayList of players and the array of sorted by dice Players
  88.         Player[] players = new Player[numOfPlayers];
  89.         ArrayList<Player> playerlist = new ArrayList<Player>(numOfPlayers);
  90.         playerlist.add(mavioux);
  91.         playerlist.add(thomas);
  92.         Map<Integer, Integer> playerTurns = new TreeMap<Integer, Integer>();
  93.         playerTurns = game.setTurns(playerlist);
  94.        
  95.         //Saving the players in the appropriate array position based on the result of setTurns function
  96.         int counter = 0;
  97.         for (Map.Entry<Integer, Integer> entry : playerTurns.entrySet())
  98.         {
  99.             Player temp = new Player();
  100.             //System.out.println(entry.getKey() + " " + entry.getValue());
  101.             for(int i = 0; i < playerlist.size(); i++) {
  102.                 if(playerlist.get(i).getPlayerId() == entry.getValue()) {
  103.                     temp = playerlist.get(i);
  104.                     //System.out.println(temp.getName());
  105.                     break;
  106.                 }
  107.             }
  108.             //System.out.println("Counter: " + counter);
  109.             players[counter++] = temp;
  110.            
  111.         }      
  112.        
  113.         //Game loop
  114.         while(playersMove[0][0] < gameBoard.getM() * gameBoard.getN() && playersMove[1][0] < gameBoard.getM() * gameBoard.getN() && playersMove[2][0] < gameBoard.getM() * gameBoard.getN() && playersMove[3][0] < gameBoard.getM() * gameBoard.getN() && playersMove[4][0] < gameBoard.getM() * gameBoard.getN() && playersMove[5][0] < gameBoard.getM() * gameBoard.getN()) {
  115.             for(int i = 0; i < numOfPlayers; i++) {
  116.                 if(players[i] instanceof MinMaxPlayer) {
  117.                     System.out.println(((MinMaxPlayer)players[i]).getName() + "'s turn:");
  118.                     System.out.println("Starting tile: " + playersMove[i][0]);
  119.                     playersMove[i][0] = ((MinMaxPlayer)players[i]).getNextMinMaxMove(playersMove[i][0], mavioux.getPlayerTileId(), mavioux);
  120.                     System.out.println("Ending tile: " + playersMove[i][0]);
  121.                     thomas.statistics();
  122.                     System.out.println();
  123.                     players[i].setPlayerTileId(playersMove[i][0]);
  124.                 }
  125.                 else {
  126.                     System.out.println(players[i].getName() + "'s turn:");
  127.                     System.out.println("Starting tile: " + playersMove[i][0]);
  128.                     roll = 1 + (int)(Math.random() * 6);
  129.                     System.out.println("You rolled: " + roll);
  130.                     playersMove[i] = players[i].move(playersMove[i][0], roll);
  131.                     System.out.println("Ending tile: " + playersMove[i][0]);
  132.                     System.out.println();
  133.                     players[i].setPlayerTileId(playersMove[i][0]);
  134.                 }
  135.                 //if one player reaches the end for loop is terminated, while no longer is true so the game ends
  136.                 if(playersMove[0][0] >= gameBoard.getM() * gameBoard.getN() || playersMove[1][0] >= gameBoard.getM() * gameBoard.getN() || playersMove[2][0] >= gameBoard.getM() * gameBoard.getN() || playersMove[3][0] >= gameBoard.getM() * gameBoard.getN() || playersMove[4][0] >= gameBoard.getM() * gameBoard.getN() || playersMove[5][0] >= gameBoard.getM() * gameBoard.getN()) {
  137.                     break;
  138.                 }
  139.             }
  140.             game.setRound(game.getRound() + 1);
  141.             if(game.round >= 100) {
  142.                 game.setRound(99);
  143.                 break;
  144.             }
  145.         }
  146.         game.setRound(game.getRound() + 1);
  147.        
  148.         //Printing the result
  149.         System.out.println();
  150.         System.out.println("Rounds: " + game.getRound());
  151.         for(int i = 0; i < players.length; i++) {
  152.             System.out.println("Score of " + players[i].getName() + " : " + players[i].getScore());
  153.            
  154.         }
  155.         System.out.print("The winner of the game is: ");
  156.        
  157.         int maxScore = players[0].getScore();
  158.         int indexOfMaxScore = 0;
  159.         for(int i = 1; i < players.length; i++) {
  160.             if(players[i].getScore() >= maxScore) {
  161.                 if(playersMove[i][0] > playersMove[indexOfMaxScore][0] && players[i].getScore() == maxScore) {
  162.                     maxScore = players[i].getScore();
  163.                     indexOfMaxScore = i;
  164.                 }
  165.                 else if(players[i].getScore() > maxScore) {
  166.                     maxScore = players[i].getScore();
  167.                     indexOfMaxScore = i;
  168.                 }
  169.             }
  170.         }
  171.         System.out.println(players[indexOfMaxScore].getName());
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement