Untitled
unknown
java
a year ago
20 kB
1
Indexable
Never
import java.util.Random; import java.util.Scanner; public class S29670_p02 { Player[] players; Field[] fields; private String[][] board; public static void main(String[] args) { Main newRound = new Main(); newRound.start(); } public void initializeBoard() { board = new String[13][14]; for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { board[i][j] = String.valueOf(' '); } } int[][] movePositions = { {1, 8}, {2, 8}, {3, 8}, {4, 8}, {5, 8}, {5, 9}, {5, 10}, {5, 11}, {5, 12}, {6, 12}, {7, 12}, {7, 11}, {7, 10}, {7, 9}, {7, 8}, {8, 8}, {9, 8}, {10, 8}, {11, 8}, {11, 7}, {11, 6}, {10, 6}, {9, 6}, {8, 6}, {7, 6}, {7, 5}, {7, 4}, {7, 3}, {7, 2}, {6, 2}, {5, 2}, {5, 3}, {5, 4}, {5, 5}, {5, 6}, {4, 6}, {3, 6}, {2, 6}, {1, 6}, {1, 7} }; fields = new Field[movePositions.length]; for (int i = 0; i < movePositions.length; i++) { int x = movePositions[i][0]; int y = movePositions[i][1]; fields[i] = new Field(x, y, i); board[x][y] = String.valueOf('X'); } board[0][8] = String.valueOf('0'); board[7][13] = "10"; board[12][6] = "20"; board[5][0] = "30"; board[5][1] = ""; for (Player player : players) { Pawn[] pawns = player.getPawns(); for (Pawn pawn : pawns) { if (pawn.getPosition() != -1) { Field field = getFieldByNumber(pawn.getPosition()); int x = field.getX(); int y = field.getY(); char symbol = pawn.getSymbol(); board[x][y] = String.valueOf(symbol); } } if (!player.allPawnsAtHome()) { int[][] hidingPositions = getHomePositions(player.getSymbol()); for (int[] position : hidingPositions) { int row = position[0]; int col = position[1]; board[row][col] = " "; } } if (player.allPawnsAtHome()) { int[][] homePositions = getHomePositions(player.getSymbol()); for (int[] position : homePositions) { int row = position[0]; int col = position[1]; if (board[row][col].equals(" ")) { board[row][col] = String.valueOf(player.getSymbol()); } } } } for (Player player : players) { int[][] hidingPositions = getHomePositions(player.getSymbol()); int count = 0; for (Pawn pawn : player.getPawns()) { if (pawn.getPosition() == -1) { int[] hidingPosition = hidingPositions[count]; int row = hidingPosition[0]; int col = hidingPosition[1]; board[row][col] = String.valueOf(player.getSymbol()); count++; } } } for (int i = 0; i < 13; i++) { for (int j = 0; j < 14; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } private int[][] getHomePositions(char playerSymbol) { switch (playerSymbol) { case 'a': return new int[][]{{1, 11}, {1, 12}, {2, 11}, {2, 12}}; case 'b': return new int[][]{{10, 11}, {10, 12}, {11, 11}, {11, 12}}; case 'c': return new int[][]{{10, 2}, {10, 3}, {11, 2}, {11, 3}}; case 'd': return new int[][]{{1, 2}, {1, 3}, {2, 2}, {2, 3}}; default: return new int[0][0]; } } public Field getFieldByNumber(int fieldNumber) { for (Field field : fields) { if (field.getFieldNumber() == fieldNumber) { return field; } } return null; } public void start() { Scanner scanner = new Scanner(System.in); int numberOfPlayers; do { System.out.println("Wprowadź liczbę graczy: "); numberOfPlayers = scanner.nextInt(); if(numberOfPlayers < 2 || numberOfPlayers > 4) { System.out.println("Nieprawidlowa liczba graczy (min:2 / max:4"); } } while (numberOfPlayers < 2 || numberOfPlayers > 4); players = new Player[numberOfPlayers]; for (int i = 0; i < numberOfPlayers; i++) { char playerSymbol = (char) ('a' + i); int startPoint = i * 10; players[i] = new Player(playerSymbol, startPoint); } playGame(); } public void removePawn(Player player, Pawn pawn) { int pawnPosition = pawn.getPosition(); Field field = getFieldByNumber(pawnPosition); if (field.getPawn() == pawn) { field.setPawn(null); System.out.println("Pionek gracza " + player.getSymbol() + " został usunięty z planszy."); } else if (pawn.isInSafeZone() || pawn.getPosition() == player.getStartPoint()) { System.out.println("Pionek gracza " + player.getSymbol() + " został usunięty z planszy."); pawn.setPosition(-2); pawn.setAtHome(true); } else { System.out.println("Nie można usunąć pionka gracza " + player.getSymbol() + " z planszy."); } } public void playGame() { Random random = new Random(); Scanner scanner = new Scanner(System.in); System.out.println("Trwa losowanie kto zaczyna ..."); int firstPlayerIndex = random.nextInt(players.length); System.out.println("Rozpoczyna gracz " + players[firstPlayerIndex].getSymbol()); int currentPlayerIndex = firstPlayerIndex; boolean isGameFinished = false; while (!isGameFinished) { Player currentPlayer = players[currentPlayerIndex]; System.out.println("Tura gracza " + currentPlayer.getSymbol() + "\n"); initializeBoard(); int diceRoll = diceRoll(currentPlayer); int moves = 1; int newPosition; boolean isMoveDone = false; do { while (currentPlayer.allPawnsAtHome() && diceRoll != 6 && moves < 3) { diceRoll = diceRoll(currentPlayer); moves++; } if (diceRoll != 6 && moves == 3) { System.out.println("Niestety nie udalo sie wylosowac 6 i wyjsc z domku.\n\n"); isMoveDone = true; break; } int howManyPawnsOnBoard = 0; int pawnField; boolean isRightField = false; for (Pawn pawn : currentPlayer.getPawns()) { if (pawn.getPosition() != -1) { howManyPawnsOnBoard++; } if (diceRoll == 6 && howManyPawnsOnBoard >= 1 && pawn.getPosition() == currentPlayer.getStartPoint()) { newPosition = (pawn.getPosition() + diceRoll) % 40; System.out.println("Niestety pomimo wylosowania 6, nie możesz wyjść kolejnym pionkiem na planszę.\n Pionek z pozycji wyjściowej zostanie przesunięty o 6 pozycji do przodu.\n\n"); pawn.setPosition(newPosition); isMoveDone = true; break; } if (diceRoll == 6 && (currentPlayer.allPawnsAtHome() || howManyPawnsOnBoard == 0)) { currentPlayer.getOutFromHome(); isMoveDone = true; break; } else if (diceRoll == 6 && howManyPawnsOnBoard >= 1 && pawn.getPosition() != currentPlayer.getStartPoint()) { System.out.println("Wybierz czy chcesz wyjść z domku - wcisnij 1\nczy przesunąć pionek - wcisnij 2"); int playerDecision = scanner.nextInt(); switch (playerDecision) { case 1: { Field startField = getFieldByNumber(currentPlayer.getStartPoint()); Pawn startPawn = startField.getPawn(); if (startPawn != null && startPawn.getSymbol() == currentPlayer.getSymbol()) { System.out.println("Nie możesz wyjść kolejnym pionkiem z domku, ponieważ na polu startowym znajduje się już pionek."); } else { currentPlayer.getOutFromHome(); isMoveDone = true; } break; } case 2: { do { System.out.println("Wybierz pole pionka, którym chcesz wykonać ruch: "); pawnField = scanner.nextInt(); Pawn chosenPawn = null; for (Pawn pawnSearch : currentPlayer.getPawns()) { if (pawnSearch.getPosition() == pawnField) { chosenPawn = pawnSearch; break; } } if (chosenPawn == null) { System.out.println("Niepoprawne pole, spróbuj jeszcze raz!"); isRightField = false; } else { newPosition = (chosenPawn.getPosition() + diceRoll) % 40; for (Pawn otherPawn : currentPlayer.getPawns()) { if (chosenPawn != otherPawn && otherPawn.getPosition() == newPosition) { newPosition = (newPosition + 1) % 40; } } chosenPawn.incrementSteps(diceRoll); if (chosenPawn.getStepsTaken() >= 40) { System.out.printf("Pionek gracza %s skończył grę.\n", currentPlayer.getSymbol()); chosenPawn.setPosition(-1); currentPlayer.removePawn(chosenPawn); } else if (chosenPawn.getStepsTaken() < 40) chosenPawn.setPosition(newPosition); System.out.println("Aktualna liczba pokonanych krokow przez pionek to: " +chosenPawn.getStepsTaken()); Field currentField = getFieldByNumber(chosenPawn.getPosition()); Field previousField = getFieldByNumber(chosenPawn.getPosition() - diceRoll); if (currentField.getPawn() != null) { Pawn otherPawn = currentField.getPawn(); otherPawn.setAtHome(true); otherPawn.setPosition(-1); System.out.printf("Pionek gracza %s zostal odeslany do schowka\n", otherPawn.getSymbol()); } currentField.setPawn(chosenPawn); isRightField = true; isMoveDone = true; break; } } while (!isRightField); } } } else if (diceRoll != 6 && howManyPawnsOnBoard >= 1) { do { System.out.println("Wybierz pole pionka, którym chcesz wykonać ruch: "); pawnField = scanner.nextInt(); Pawn chosenPawn = null; for (Pawn pawnSearch : currentPlayer.getPawns()) { if (pawnSearch.getPosition() == pawnField) { chosenPawn = pawnSearch; break; } } if (chosenPawn == null) { System.out.println("Niepoprawne pole, spróbuj jeszcze raz!"); isRightField = false; } else { newPosition = (chosenPawn.getPosition() + diceRoll) % 40; for (Pawn otherPawn : currentPlayer.getPawns()) { if (chosenPawn != otherPawn && otherPawn.getPosition() == newPosition) { newPosition = (newPosition + 1) % 40; } } chosenPawn.incrementSteps(diceRoll); if (chosenPawn.getStepsTaken() >= 40) { System.out.printf("Pionek gracza %s skończył grę.\n", currentPlayer.getSymbol()); chosenPawn.setPosition(-1); currentPlayer.removePawn(chosenPawn); } else if (chosenPawn.getStepsTaken() < 40) chosenPawn.setPosition(newPosition); System.out.println("Aktualna liczba pokonanych krokow przez pionek to: " +chosenPawn.getStepsTaken()); Field currentField = getFieldByNumber(chosenPawn.getPosition()); Field previousField = getFieldByNumber(chosenPawn.getPosition() - diceRoll); if (currentField.getPawn() != null) { Pawn otherPawn = currentField.getPawn(); otherPawn.setAtHome(true); otherPawn.setPosition(-1); System.out.printf("Pionek gracza %s zostal odeslany do schowka\n", otherPawn.getSymbol()); } if(currentPlayer.hasPawnFinished()) { currentField.setPawn(null); } currentField.setPawn(chosenPawn); isRightField = true; isMoveDone = true; break; } } while (!isRightField); } isMoveDone = true; break; } if (currentPlayer.allPawnsAtHome()) { isGameFinished = true; System.out.println("Gracz " + currentPlayer.getSymbol() + " wygrywa."); } if (isMoveDone) { currentPlayerIndex = (currentPlayerIndex + 1) % players.length; } } while(!isMoveDone); } } public int diceRoll(Player player) { int roll; Random random = new Random(); roll = random.nextInt(6)+1; System.out.println("Gracz " + player.getSymbol() + " rzucił " + roll); return roll; } } class Player { private final char symbol; private final int startPoint; private Pawn[] pawns; public Player(char playerSymbol, int playerStartPoint) { this.symbol = playerSymbol; this.startPoint = playerStartPoint; this.pawns = new Pawn[4];createPawns(); } private void createPawns() { for (int i = 0; i < pawns.length; i++) { pawns[i] = new Pawn(symbol); } } public char getSymbol() { return symbol; } public int getStartPoint() { return startPoint; } public Pawn[] getPawns() { return pawns; } public boolean allPawnsAtHome() { for (Pawn pawn : pawns) { if (!pawn.isAtHome()) return false; } return true; } public void getOutFromHome() { for (Pawn pawn : pawns) { if (pawn.isAtHome()) { pawn.setAtHome(false); pawn.setPosition(startPoint); System.out.printf("Jeden z pionkow gracza %s zostal ustawiony na pozycji startowej\n", symbol); break; } } } public boolean hasPawnFinished() { for (Pawn pawn : pawns) { if (pawn.getStepsTaken() >= 40) { return true; } } return false; } public void removePawn(Pawn pawnToRemove) { for (int i = 0; i < pawns.length; i++) { if (pawns[i] == pawnToRemove) { pawns[i] = null; break; } } } } class Pawn { private final char symbol; private int position; private boolean isAtHome; private int stepsTaken; private boolean isFinished; private boolean isInSafeZone; public Pawn(char symbol) { this.symbol = symbol; this.position = -1; this.isAtHome = true; this.isInSafeZone = false; this.stepsTaken = 0; } public char getSymbol() { return symbol; } public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } public boolean isAtHome() { return isAtHome; } public void setAtHome(boolean isAtHome) { this.isAtHome = isAtHome; } public void incrementSteps(int steps) { stepsTaken += steps; if(stepsTaken >= 40) { // jeśli pionek ma wejść do "domku" isInSafeZone = true; } } public int getStepsTaken() { return stepsTaken; } public boolean isInSafeZone() { return isInSafeZone; } } class Field { private final int x; private final int y; private final int fieldNumber; private Pawn pawn; public Field(int x, int y, int fieldNumber) { this.x = x; this.y = y; this.fieldNumber = fieldNumber; this.pawn = null; } public int getX() { return x; } public int getY() { return y; } public int getFieldNumber() { return fieldNumber; } public Pawn getPawn() { return pawn; } public void setPawn(Pawn pawn) { this.pawn = pawn; } }