Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Map;
- import java.util.TreeMap;
- public class Game {
- int round;
- // Constructors
- public Game() {
- round = 0;
- }
- public Game(int round) {
- this.round = round;
- }
- // Getters, setters
- public void setRound(int round) {
- this.round = round;
- }
- public int getRound() {
- return round;
- }
- Map<Integer,Integer> setTurns(ArrayList<Player> players) {
- Map<Integer, Integer> playerTurns = new TreeMap<Integer, Integer>();
- int rolls[] = new int[players.size()];
- boolean flag;
- do {
- flag = false;
- for(int i = 0; i < rolls.length; i++) {
- rolls[i] = 1 + (int)(Math.random() * 6);
- for(int j = i - 1; j > -1; j--) {
- if(rolls[i] == rolls[j]) {
- flag = true; //if one dice of a player matches another player's dice do while is repeated;
- }
- }
- //System.out.println("Key: " + rolls[i] + " Value: " + (i+1));
- playerTurns.put(rolls[i], i + 1); //key = roll of the player with id i + 1 , value = id of player (i + 1)
- if(flag) {
- playerTurns.clear();
- break;
- }
- }
- } while(flag);
- return playerTurns;
- }
- public static void main(String[] args) {
- //Initializing the necessary values
- ArrayList<Integer[]> templatePath = new ArrayList<Integer[]>();
- int numOfPlayers = 2;
- Board gameBoard = new Board(10, 20, 3, 3, 6);
- gameBoard.createBoard();
- gameBoard.createElementBoard();
- Player mavioux = new Player(1, "Mavioux", 0, gameBoard);
- MinMaxPlayer thomas = new MinMaxPlayer(2, "Thomas", 0, gameBoard, templatePath);
- Game game = new Game(0);
- //Print the board
- System.out.println();
- for(int i = 0; i < gameBoard.tiles.length; i++) {
- for(int j = 0; j < gameBoard.tiles[i].length; j++) {
- if(gameBoard.tiles[i][j] < 10)
- System.out.print(" " + gameBoard.tiles[i][j] + " ");
- else if(gameBoard.tiles[i][j] < 100)
- System.out.print(" " + gameBoard.tiles[i][j] + " ");
- else
- System.out.print(gameBoard.tiles[i][j] + " ");
- }
- System.out.println();
- }
- System.out.println();
- System.out.println();
- System.out.println();
- int roll= 0;
- int[][] playersMove = new int[6][5];
- //playersMove arrays gets zero in every instance (this array shows us the position of each player)
- for(int i = 0; i < playersMove.length; i++) {
- playersMove[i][0] = 0;
- }
- //Initializing the arrayList of players and the array of sorted by dice Players
- Player[] players = new Player[numOfPlayers];
- ArrayList<Player> playerlist = new ArrayList<Player>(numOfPlayers);
- playerlist.add(mavioux);
- playerlist.add(thomas);
- Map<Integer, Integer> playerTurns = new TreeMap<Integer, Integer>();
- playerTurns = game.setTurns(playerlist);
- //Saving the players in the appropriate array position based on the result of setTurns function
- int counter = 0;
- for (Map.Entry<Integer, Integer> entry : playerTurns.entrySet())
- {
- Player temp = new Player();
- //System.out.println(entry.getKey() + " " + entry.getValue());
- for(int i = 0; i < playerlist.size(); i++) {
- if(playerlist.get(i).getPlayerId() == entry.getValue()) {
- temp = playerlist.get(i);
- //System.out.println(temp.getName());
- break;
- }
- }
- //System.out.println("Counter: " + counter);
- players[counter++] = temp;
- }
- //Game loop
- 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()) {
- for(int i = 0; i < numOfPlayers; i++) {
- if(players[i] instanceof MinMaxPlayer) {
- System.out.println(((MinMaxPlayer)players[i]).getName() + "'s turn:");
- System.out.println("Starting tile: " + playersMove[i][0]);
- playersMove[i][0] = ((MinMaxPlayer)players[i]).getNextMinMaxMove(playersMove[i][0], mavioux.getPlayerTileId(), mavioux);
- System.out.println("Ending tile: " + playersMove[i][0]);
- thomas.statistics();
- System.out.println();
- players[i].setPlayerTileId(playersMove[i][0]);
- }
- else {
- System.out.println(players[i].getName() + "'s turn:");
- System.out.println("Starting tile: " + playersMove[i][0]);
- roll = 1 + (int)(Math.random() * 6);
- System.out.println("You rolled: " + roll);
- playersMove[i] = players[i].move(playersMove[i][0], roll);
- System.out.println("Ending tile: " + playersMove[i][0]);
- System.out.println();
- players[i].setPlayerTileId(playersMove[i][0]);
- }
- //if one player reaches the end for loop is terminated, while no longer is true so the game ends
- 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()) {
- break;
- }
- }
- game.setRound(game.getRound() + 1);
- if(game.round >= 100) {
- game.setRound(99);
- break;
- }
- }
- game.setRound(game.getRound() + 1);
- //Printing the result
- System.out.println();
- System.out.println("Rounds: " + game.getRound());
- for(int i = 0; i < players.length; i++) {
- System.out.println("Score of " + players[i].getName() + " : " + players[i].getScore());
- }
- System.out.print("The winner of the game is: ");
- int maxScore = players[0].getScore();
- int indexOfMaxScore = 0;
- for(int i = 1; i < players.length; i++) {
- if(players[i].getScore() >= maxScore) {
- if(playersMove[i][0] > playersMove[indexOfMaxScore][0] && players[i].getScore() == maxScore) {
- maxScore = players[i].getScore();
- indexOfMaxScore = i;
- }
- else if(players[i].getScore() > maxScore) {
- maxScore = players[i].getScore();
- indexOfMaxScore = i;
- }
- }
- }
- System.out.println(players[indexOfMaxScore].getName());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement