diff --git a/Monopoly-Whole/Helpers/MonopolyRunner.java b/Monopoly-Whole/Helpers/MonopolyRunner.java new file mode 100644 index 0000000..6cccf2c --- /dev/null +++ b/Monopoly-Whole/Helpers/MonopolyRunner.java @@ -0,0 +1,10 @@ +package Helpers; + +import Monopoly.Monopoly; + +public class MonopolyRunner { + public static void main(String[] arg) + { + new Monopoly(); + } +} diff --git a/Monopoly-Whole/Helpers/SaveLoader.java b/Monopoly-Whole/Helpers/SaveLoader.java new file mode 100644 index 0000000..3a48691 --- /dev/null +++ b/Monopoly-Whole/Helpers/SaveLoader.java @@ -0,0 +1,344 @@ +package Helpers; + +import java.awt.GridLayout; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Scanner; + +import javax.swing.BorderFactory; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JTextArea; +import javax.swing.border.Border; +import javax.swing.border.EtchedBorder; + +import Monopoly.player; + +/** + * SaveLoader Loads and Saves Player data to and from disk + * @author Jonathan Dang + * + */ +public class SaveLoader { + private File _topScore; + private File _previousScore; + private final String TopScoreFileName = "TopScore.txt"; + private final String PreviousScoreFileName = "PreviousScore.txt"; + private ArrayList> _topInfo = new ArrayList>(); + private ArrayList> _prevInfo = new ArrayList>(); + + /* Previous Game! + * FORMAT:[playerName] [CreditCardNumber] [balance] \n + */ + + /* Top 6 + * FORMAT:[Place] [playerName] [CCN] [Balance] \n + */ + + /** + * Constructs a SaveLoader + * @throws IOException + */ + public SaveLoader() throws IOException + { + _topScore = new File(TopScoreFileName); + _previousScore = new File(PreviousScoreFileName); + + if (_topScore.createNewFile()) + { + System.out.println("TOP SCORE: NO FILE DETECTED!\nCREATING NEW FILE..."); + } + + if (_previousScore.createNewFile()) + { + System.out.println("PREVIOUS SCORE: NO FILE DETECTED!\nCREATING NEW FILE..."); + } + + this.load(); + } + + /** + * Displays the leaderBoard + */ + public void display() + { + JFrame leaderBoard = new JFrame(); + leaderBoard.setTitle("Leader Board"); + leaderBoard.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + leaderBoard.setSize(600, 300); + + JTextArea prev = new JTextArea(); + prev.setEditable(false); + JTextArea top = new JTextArea(); + top.setEditable(false); + + String prevMessage = ""; + for (ArrayList as : _prevInfo) + { + prevMessage += String.format("%-" + ((int)as.get(0).length()+5) + "s%" + ((int)as.get(1).length() + 5) + + "s%" + ((int)as.get(2).length() + 5) + "s%n%n" + , as.get(0),as.get(1),as.get(2)); + } + + if (prevMessage.isBlank()) + { + prev.setText("NONE YET!"); + } + else + prev.setText(prevMessage); + + String topMessage = ""; + for (ArrayList as : _topInfo) + { + topMessage += String.format("%-3s)%" + ((int)as.get(1).length()+5) + "s%" + + ((int)as.get(2).length() + 5) + "s%10s%n%n" + , as.get(0),as.get(1),as.get(2),as.get(3)); + } + if (topMessage.isBlank()) + top.setText("NO TOP SCORES YET"); + else + top.setText(topMessage); + + JPanel JP = new JPanel(); + JP.setLayout(new GridLayout(1,3)); + Border raisedetched = BorderFactory.createEtchedBorder(EtchedBorder.RAISED); + prev.setBorder(raisedetched); + top.setBorder(raisedetched); + + JP.add(prev); + JP.add(new JPanel()); + JP.add(top); + + leaderBoard.add(JP); + leaderBoard.setVisible(true); + } + + /** + * Saves a list of players to disk and compares them to the top 6 of all times + * @param a | player List + */ + public void save(ArrayList a) + { + ArrayList record = new ArrayList(); + for (player p : a) + { + String playerInfo = p.getName() + " " + p.getCCN() + " " + p.getBalance(); + record.add(playerInfo); + } + + try { + if (_previousScore.exists()) + { + _previousScore.delete(); + _previousScore.createNewFile(); + } + } catch (IOException e) { + e.printStackTrace(); + } finally {}; + + try { + PrintWriter PW = new PrintWriter(_previousScore); + for (String s : record) + { + PW.println(s); + } + + PW.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } finally {}; + + //Now I need to compare all the values from the Top 6 and re-distribute the values + //IE: Replace Inferior Scores! + //Solution: We first make a copy with Top6 on top and then all player balances. + //We compare them after sorting to find the indexes, anything past index 5 is in players. + //Remake the Top 6 and save to disk + + player[] playersTop = new player[6 + a.size()]; + int i = 0; + for (player p : a) + { + playersTop[i++] = p; + //i++; + } + + if (_topInfo.isEmpty()) + { + for (int j = 0; j < 6; j++) + { + player p = new player(-1,"NONE","NONE"); + p.manipulateMoney(-1300); + playersTop[i++] = p; + } + } + else + { + for (ArrayList info : _topInfo) + { + player p = new player(-1, info.get(1), info.get(2)); + p.manipulateMoney(-1300); + p.manipulateMoney(Integer.parseInt(info.get(info.size()-1))); + playersTop[i++] = p; + } + } + + + + sort(playersTop,0,playersTop.length-1); + + try { + if (_topScore.exists()) + { + _topScore.delete(); + _topScore.createNewFile(); + } + } catch (IOException e) { + e.printStackTrace(); + } finally {}; + + ArrayList topScores = new ArrayList(); + int t = 1; + for (i = playersTop.length - 1; i > playersTop.length - 7; i--) + { + + String playerInfo = t++ + " " + playersTop[i].getName() + " " + playersTop[i].getCCN() + " " + playersTop[i].getBalance(); + topScores.add(playerInfo); + } + try { + PrintWriter PW = new PrintWriter(_topScore); + for (String s : topScores) + { + PW.println(s); + } + PW.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } finally {}; + + + + //Most likely we have to sort it anyway... Maybe its easier to make something that sorts the entire thing. + } + + /** + * Sorting Algorithm + * @param a Array + * @param low lowest index + * @param high highest index + */ + private static void sort(player[] a, int low, int high) + { + if (low < high) + { + int pi = partition(a, low, high); + + sort(a, low, pi - 1); + sort(a, pi + 1, high); + } + } + + /** + * Checks the Low and high and swaps it upon certain conditions + * @param a Array + * @param low lowest Index + * @param high highest Index + * @return + */ + private static int partition(player[] a, int low, int high) + { + player pivot = a[high]; + int i = (low - 1); + + for(int j = low; j <= high - 1; j++) + { + if (a[j].getBalance() < pivot.getBalance()) + { + i++; + swap(a, i, j); + } + } + swap(a, i + 1, high); + return (i + 1); + } + + /** + * Swaps two data types between each other on the array + * @param a The Array + * @param i 1st Index + * @param j 2nd Index + */ + private static void swap(player[] a, int i, int j) + { + player temp = a[i]; + a[i] = a[j]; + a[j] = temp; + } + + /** + * Loads the data from Disk + */ + public void load() + { + try { + Scanner topIns = new Scanner(_topScore); + Scanner prevIns = new Scanner(_previousScore); + + while (topIns.hasNextLine()) + { + String input = topIns.nextLine(); + ArrayList row = new ArrayList(); + + while (!input.isBlank()) + { + int i = input.indexOf(" "); + + if (i < 0) + { + input.strip(); + row.add(input); + break; + } + + String info = input.substring(0, i); + input = input.substring(i + 1); + info.strip(); + row.add(info); + } + _topInfo.add(row); + } + + while (prevIns.hasNextLine()) + { + String input = prevIns.nextLine(); + ArrayList row = new ArrayList(); + + while (!input.isBlank()) + { + int i = input.indexOf(" "); + + if (i < 0) + { + input.strip(); + row.add(input); + break; + } + + String info = input.substring(0, i); + input = input.substring(i + 1); + info.strip(); + row.add(info); + } + _prevInfo.add(row); + } + + topIns.close(); + prevIns.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + } +} diff --git a/Monopoly-Whole/Helpers/SellingFrame.java b/Monopoly-Whole/Helpers/SellingFrame.java new file mode 100644 index 0000000..c0f5eb0 --- /dev/null +++ b/Monopoly-Whole/Helpers/SellingFrame.java @@ -0,0 +1,107 @@ +package Helpers; + +import java.awt.BorderLayout; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; + +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JDialog; + +/** + * Frame in which sells a property from a given player's owned property + * @author Jonathan Dang + * + */ +@SuppressWarnings("serial") +public class SellingFrame extends JDialog{ + private static final int FRAME_WIDTH = 300; + private static final int FRAME_HEIGHT = 150; + private ArrayList _names = new ArrayList(); + private JComboBox JCB; + private JButton JB; + private JTextField JT; + private String selection = ""; + //JFrame -> JScrollPane -> JPanel -> JTextField + //include JComboBox + + /** + * Constructs a SellingFrame + * @param names + */ + public SellingFrame(ArrayList names) + { + _names.add("NOTHING"); + for (String s : names) + { + _names.add(s); + } + + this.createFrame(); + + setTitle("Sell a Property"); + setSize(FRAME_WIDTH,FRAME_HEIGHT); + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + this.setModal(true); //<- This is VERY important + setVisible(true); + } + + /** + * Private Class to select and dipose of the selling frame + * @author Jonathan Dang + * + */ + private class ButtonActionListener implements ActionListener{ + + @Override + public void actionPerformed(ActionEvent e) { + selection = (String) JCB.getSelectedItem(); + dispose(); + } + } + + /** + * Returns the Selected Nam + * @return + */ + public String getSelection() + { + return selection; + } + + /** + * Initializes the Frame + */ + private void createFrame() + { + JCB = new JComboBox(); + for (String s : _names) + { + JCB.addItem(s); + + } + JCB.setEditable(false); + + + JB = new JButton("Confirm"); + JB.addActionListener(new ButtonActionListener()); + + JPanel jp = new JPanel(); + jp.setLayout(new GridLayout(2,1)); + + jp.add(JCB); + + JT = new JTextField(); + JT.setText("Please Select a property to sell!"); + JT.setEditable(false); + + jp.add(JB); + + add(JT, BorderLayout.CENTER); + add(jp, BorderLayout.SOUTH); + } +} diff --git a/Monopoly-Whole/Helpers/Test.java b/Monopoly-Whole/Helpers/Test.java new file mode 100644 index 0000000..b5dd7f1 --- /dev/null +++ b/Monopoly-Whole/Helpers/Test.java @@ -0,0 +1,10 @@ +package Helpers; + +public class Test { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} diff --git a/Monopoly-Whole/Monopoly/Board.java b/Monopoly-Whole/Monopoly/Board.java new file mode 100644 index 0000000..d323f0e --- /dev/null +++ b/Monopoly-Whole/Monopoly/Board.java @@ -0,0 +1,427 @@ +package Monopoly; + +import java.util.ArrayList; + +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +import Helpers.SellingFrame; +import Monopoly_Properties.*; + +/** + * The Board holds all player interactions and property interactions + * @author Jonathan Dang + * + */ +public class Board { + private Spaces[] _board = new Spaces[40]; + private player[] _players; + private int[] _jailTimer = new int[4]; + private int _hotelAmount; + private int _houseAmount; + private int _turn; //_turn controls which player is active!, _round keeps track of how many turns/4 was done + + //I want to limit the amount of players since games are usually longer and more complex the more players there is. + //The rules I implemented for this also forces for a simpler version of the game. + //The Limit is 4 players. Only Multi-player is allowed. + //Passing or Landing on the GO space will give you 200 + + //Selling Properties will require the player's index on the current turn, + //We will use the _turn as the current player wanting to sell or being forced to sell to the bank + //All selling will not be between players, but rather only be with the bank. + + /** + * Constructs a board with a set amount of players + * @param players | The players + */ + public Board(player[] players) + { + _players = players.clone(); + _hotelAmount = 12; + _houseAmount = 32; + _turn = 0; + for (int i = 0; i < 4; i++) + { + _jailTimer[i] = 0; + } + createBoard(); + } + + /** + * Initializes the Board + */ + private void createBoard() + { + /* + * Property(String name, int price, int rent, int HouseRent1, + int HouseRent2, int HouseRent3, int HouseRent4, int hotelRent, int buildingCost) + */ + _board[0] = new GoSpace(); + _board[1] = new Property("MEDITERANEAN AVENUE", 60, 2, 10, 30, 90, 160, 250, 50); + _board[2] = new CommunityChestSpace(); + _board[3] = new Property("BALTIC AVENUE", 60, 2, 20, 60, 180, 320, 450, 50); + _board[4] = new Tax(200); + _board[5] = new TrainProperty("READING RAILROAD"); + _board[6] = new Property("ORIENTAL AVENUE", 100, 6, 30, 90, 270, 400, 550, 50); + _board[7] = new ChanceSpace(); + _board[8] = new Property("VERMONT AVENUE", 100, 6, 30, 90, 270, 400, 550, 50); + _board[9] = new Property("CONNECTICUT AVENUE", 120, 8, 40, 100, 300, 450, 600, 50); + _board[10] = new Jail(); + _board[11] = new Property("ST. CHARLES PLACE", 140, 10, 50, 150, 450, 625, 750, 100); + _board[12] = new Utility("ELECTRIC COMPANY"); + _board[13] = new Property("STATES AVENUE", 140, 10, 50, 150, 450, 625, 750, 100); + _board[14] = new Property("VIRGINIA AVENUE", 160, 12, 60, 180, 500, 700, 900, 100); + _board[15] = new TrainProperty("PENNSYLVANIA RAILROAD"); + _board[16] = new Property("ST. JAMES PLACE", 180, 14, 70, 200, 550, 750, 950, 100); + _board[17] = new CommunityChestSpace(); + _board[18] = new Property("TENNESSEE AVENUE", 180, 14, 70, 200, 550, 750, 950, 100); + _board[19] = new Property("NEW YORK AVENUE", 200, 16, 80, 220, 600, 800, 1000, 100); + _board[20] = new FreeParkingSpace(); + _board[21] = new Property("KENTUCKY AVENUE", 220, 18, 90, 250, 700, 875, 1050, 150); + _board[22] = new ChanceSpace(); + _board[23] = new Property("INDIANA AVENUE", 220, 18, 90, 250, 700, 875, 1050, 150); + _board[24] = new Property("ILLINOIS AVENUE", 240, 20, 100, 300, 750, 925, 1100, 150); + _board[25] = new TrainProperty("B.B.O. RAILROAD"); + _board[26] = new Property("ATLANTIC AVENUE", 260, 22, 110, 330, 800, 975, 1150, 150); + _board[27] = new Property("VENTOR AVENUE", 260, 22, 110, 330, 800, 975, 1150, 150); + _board[28] = new Utility("WATER WORKS"); + _board[29] = new Property("MARVIN GARDENS", 280, 24, 120, 360, 850, 1025, 1200, 150); + _board[30] = new GoToJail(); + _board[31] = new Property("PACIFIC AVENUE", 300, 26, 130, 390, 900, 1100, 1275, 200); + _board[32] = new Property("NORTH CAROLINA AVENUE", 300, 26, 130, 390, 900, 1100, 1275, 200); + _board[33] = new CommunityChestSpace(); + _board[34] = new Property("PENNSYLVANIA AVENUE", 320, 28, 150, 450, 1000, 1200, 1400, 200); + _board[35] = new TrainProperty("SHORT LINE"); + _board[36] = new ChanceSpace(); + _board[37] = new Property("PARK PLACE", 350, 35, 175, 500, 1100, 1300, 1500, 200); + _board[38] = new Tax(100); + _board[39] = new Property("BOARDWALK", 400, 50, 200, 600, 1400, 1700, 2000, 200); + } + + /** + * Returns all player data at this specific time + * @return ArrayList The Player Data + */ + public ArrayList getPlayerData() + { + ArrayList playerData = new ArrayList(); + for (player p : _players) + { + playerData.add(p); + } + return playerData; + } + + /** + * Advances the current player's turn by moving through the board + */ + public void Go() + { + int roll = DiceRoller.rollDice(); + this.movePlayer(roll); + } + + /** + * Gets the name of the location + * @return String the location name + */ + public String getLocationName() + { + return _board[_players[_turn].getLocation()].getName(); + } + + /** + * Gets the turn counter's position + * @return int the turn + */ + public int getTurn() + { + return _turn; + } + + /** + * Returns the entire player's of whomst the turn it is + * @return player the Player + */ + public player getCurrentTurnPlayer() + { + return _players[_turn]; + } + + /** + * Moves the player a certain amount + * @param move the amount of spaces moved + */ + public void movePlayer(int move) + { + player p = _players[_turn]; + if (!p.inJail()) + { + p.setLocation(p.getLocation() + move); + + activateCard(p.getLocation(),p); + } + else + { + this.jailRoll(p); + } + + //nextTurn(); + } + + /** + * Advances the turn counter by one + */ + public void nextTurn() + { + int turn = 0; + if (_turn + 1 >= _players.length) + { + for (int i = 0; i < _players.length; i ++) + { + if (!_players[i].isBankrupt()) + { + turn = i; + break; + } + } + _turn = turn; + } + else + { + _turn++; + } + + } + + /** + * Detects whether or not the game has a winner + * @return boolean True if the game has a winner + */ + public boolean hasWinner() + { + int bankrupts = 0; + for (player p : _players) + { + if (p.isBankrupt()) + bankrupts++; + } + return bankrupts == 3; + } + + /** + * Returns the winner as a player + * @return player The winner + */ + public player getWinner() + { + for (player p : _players) + { + if (!p.isBankrupt()) + return p; + } + + return null; + } + + /** + * Activates the space + * @param spaceIndex The space itself + * @param p The Player + */ + public void activateCard(int spaceIndex, player p) + { + if (!p.isBankrupt()) + _board[spaceIndex].activateSpace(p, this); + } + + /** + * Decreases the total amount of usable houses by an amount + * @param amount the houses used + */ + public void useHouse(int amount) + { + if (_houseAmount <= 0) + return; + + _houseAmount -= amount; + } + + /** + * Gets the current useable house count + * @return int the House Count + */ + public int getHouseCount() + { + return _houseAmount; + } + + /** + * Decreases the total amount of usable hotel by 1 + */ + public void useHotel() + { + if (_hotelAmount <= 0) + return; + _hotelAmount--; + } + + /** + * Gets the current usable hotel Count + * @return int the Hotel count + */ + public int getHotelCount() + { + return _hotelAmount; + } + + /** + * Rents the rentie person by Rent amount + * @param ownerIndex The owner of the property + * @param rentieIndex The rented Person + * @param rent The amount + * @throws InterruptedException E + */ + public void rent(int ownerIndex, int rentieIndex, int rent) throws InterruptedException + { + while (_players[rentieIndex].getBalance() < rent) + { + sell(rentieIndex,true); + } + + _players[rentieIndex].manipulateMoney(-rent); + _players[ownerIndex].manipulateMoney(rent); + } + + /** + * Sells a property from the player's ownership + * @param playerIndex The player + * @param rented Determines if they were forced to sell or not + * @throws InterruptedException + */ + public void sell(int playerIndex, boolean rented) throws InterruptedException + { + //TODO: Get a list of owned properties. + //Then display the list, sell the selected for half price + //If there is a house or hotel, then sell those first. + + //Addendum 6/6/2021: Maybe add a String to the arguments to sell it after finding the index. + //This way it makes it easier on the system. + + ArrayList index = new ArrayList(); + ArrayList names = new ArrayList(); + + //Addendum 6/6/2021: SellingFrame doesn't work for some odd reason. + //Maybe something conflicting with a frame and a dialog + //Unsure but I guess I have to do it the old fasion way. + SellingFrame SF = new SellingFrame(names); + + //I need a timer so that the user has an ample amount of time to select + //a property in which to sell. + //Addendum: This has been solved by using a JDialog instead of a JFrame as it + //pauses the current thread to run the Dialog. + //Addendum 6/1/2021: The above JDialog does not work, using a loop as well as the + //static method Thread.sleep(LONG) will work fine + + String SelectedName = ""; + + while (SelectedName.isEmpty()) + { + Thread.sleep(1000); + SelectedName = SF.getSelection(); + } + + int SelectedIndex = -1; + + for (int i = 0; i < index.size(); i++) + { + if (SelectedName.equals(names.get(i).toUpperCase())) + { + SelectedIndex = index.get(i); + } + } + + if (SelectedName.equals("NOTHING") && rented) + { + int option = JOptionPane.showConfirmDialog(new JFrame(), "Are you sure you want to not sell?\n You will be Bankrupt."); + + if (option == JOptionPane.YES_OPTION) + { + _players[playerIndex].manipulateMoney(-999999); + } + + } + else if (SelectedName.equals("NOTHING")) + return; + + //Now I have to extract the cost of the property, Houses, hotels, etc... + //Maybe a solution would be to add a sell method to all property types. + //This seems to be the easiest to do. + //Addendum 6/1/2021: Thinking it further, maybe it its simpler for me to + //create another abstract method for the type + //Then check the typing to allow for selling. + //Making the Util and Trains have the same name of a method would help as well. + //It would be a better solution to just have an abstract sell method, since it would be + //standardized. + _board[SelectedIndex].sellSpace(_players[playerIndex]); + } + + /** + * Extorts a player for other's gains + * @param p The player + * @param amount The amount + */ + public void extortPlayer(player p, int amount) + { + p.manipulateMoney(-3*amount); + + for(int i = 0; i < 4; i ++) + { + if (_players[i] != p) + _players[i].manipulateMoney(amount); + } + } + + /** + * Absorbs the amount from other players + * @param p The Player + * @param amount The amount + */ + public void syphonPlayers(player p, int amount) + { + for (int i = 0; i < 4; i ++) + { + if (!_players[i].isBankrupt() && _players[i] != p) + { + _players[i].manipulateMoney(-amount); + } + } + p.manipulateMoney(amount * 3); + } + + /** + * Rolls for the Jail Ruling + * @param p The Prisoner + */ + public void jailRoll(player p) + { + if ( _jailTimer[p.getIndex()] == 3) + { + p.manipulateMoney(-50); + p.getOutOfJail(); + } + else + { + //Obtain Dice Roller Array option, check if they ==, true => set them free; else skip their turn. + int[] rolledDice = DiceRoller.getDiceRolls(); + if (rolledDice[0] == rolledDice[1]) + { + //The Player moves this selected amount and is out of jail. + p.getOutOfJail(); + p.setLocation(p.getLocation() + rolledDice[0] + rolledDice[1]); + } + else + _jailTimer[p.getIndex()]++; + } + } +} diff --git a/Monopoly-Whole/Monopoly/ChanceDeck.java b/Monopoly-Whole/Monopoly/ChanceDeck.java new file mode 100644 index 0000000..a5b760f --- /dev/null +++ b/Monopoly-Whole/Monopoly/ChanceDeck.java @@ -0,0 +1,185 @@ +package Monopoly; + +import java.util.Random; + +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +import Monopoly_Properties.Position_Relationships; + +/** + * The Chance Deck + * @author Jonathan Dang + * + */ +public class ChanceDeck { + private Random _randomNumberGenerator; + private Position_Relationships space; + + /** + * Constructs a Chance Deck + */ + public ChanceDeck() + { + _randomNumberGenerator = new Random(); + } + + /** + * Draws a random Card + * @param p The Player + * @param b The Board + */ + public void drawCard(player p, Board b) + { + int card = _randomNumberGenerator.nextInt(16); + String output = ""; + JFrame jf = new JFrame(); + + switch (card) + { + case(0): + p.moveToGo(); + output = "Advance to GO!"; + break; + case(1): + space = Position_Relationships.ILLINOISE_AVENUE; + if (p.getLocation() > space.getLocation()) + { + p.manipulateMoney(200); + } + p.setLocation(space.getLocation()); + output = "Advance to Illinois Ave"; + break; + case(2): + space = Position_Relationships.SAINT_CHARLES_PLACE; + if(p.getLocation() > space.getLocation()) + p.manipulateMoney(200); + p.setLocation(space.getLocation()); + output = "Advance to St. Charles Place"; + break; + case(3): + int Waterworks = Position_Relationships.WATER_WORKS.getLocation(); + int Electrical = Position_Relationships.ELECTRIC_COMPANY.getLocation(); + + if (p.getLocation() > Waterworks) //Electrical is closer + { + p.manipulateMoney(200); + p.setLocation(Electrical); + } + else if (p.getLocation() > Electrical && p.getLocation() < Waterworks) + { + p.setLocation(Waterworks); + } + else if (p.getLocation() > 0 && p.getLocation() < Electrical) + { + p.setLocation(Electrical); + } + + b.activateCard(Electrical, p); + + output = "Advance token to the nearest Utility. If unowned, you may buy it from the Bank. " + + "If owned, throw dice and pay owner a total 10 times the amount thrown."; + break; + case(4): + int Reading = Position_Relationships.READING_RAILROAD.getLocation(); + int Pennsylvania = Position_Relationships.PENNSYLVANIA_RAILROAD.getLocation(); + int Short = Position_Relationships.SHORT_LINE.getLocation(); + int BO = Position_Relationships.BBQ_RAILROAD.getLocation(); + + if (p.getLocation() < Reading) + p.setLocation(Reading); + else if (p.getLocation() > Reading && p.getLocation() < Pennsylvania) + p.setLocation(Pennsylvania); + else if (p.getLocation() > Pennsylvania && p.getLocation() < BO) + p.setLocation(BO); + else if (p.getLocation() > BO) + p.setLocation(Short); + + b.activateCard(p.getLocation(), p); + + output = "Advance to the nearest Railroad. If unowned, you may buy it from the Bank." + + " If owned, pay owner twice the rent to which they are otherwise entitled." + + " If Railroad is unowned, you may buy it from the Bank."; + break; + case(5): + p.manipulateMoney(50); + + output = "Bank pays you dividend of $50"; + break; + case(6): + p.obtainGOFJC(); + + output = "Get out of Jail Free"; + break; + case(7): + p.setLocation(p.getLocation() - 3); + + output = "Go Back Three 3 Spaces"; + + b.activateCard(p.getLocation(), p); + break; + case(8): + if(p.hasGOFJC()) + { + int use = JOptionPane.showConfirmDialog(jf,"Use Get out of Jail Free Card?"); + + if (use == JOptionPane.YES_OPTION) + p.goToJail(true); + else + { + p.goToJail(false); + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int) p.getIndex() + 1) + + " HAS BEEN SENT TO JAIL!","Alert",JOptionPane.WARNING_MESSAGE); + } + } + else + { + p.goToJail(false); + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int) p.getIndex() + 1) + + " HAS BEEN SENT TO JAIL!","Alert",JOptionPane.WARNING_MESSAGE); + } + + + output = "Go Directly to Jail!"; + break; + case(9): + p.makeRepairs(25,100); + + output = "Make general repairs on all your property: For each house pay $25, For each hotel pay $100"; + break; + case(10): + p.manipulateMoney(-15); + + output = "Pay poor tax of $15"; + break; + case(11): + if (p.getLocation() > Position_Relationships.READING_RAILROAD.getLocation()) + p.manipulateMoney(200); + + p.setLocation(Position_Relationships.READING_RAILROAD.getLocation()); + output = "Take a trip to Reading Railroad"; + break; + case(12): + p.setLocation(Position_Relationships.BOARDWALK.getLocation()); + + output = "Take a walk on the Boardwalk. Advance token to Boardwalk."; + break; + case(13): + b.extortPlayer(p, 50); + output = "You have been elected Chairman of the Board. Pay each player $50."; + break; + case(14): + p.manipulateMoney(150); + + output = "Your building and loan matures. Receive $150"; + break; + case(15): + p.manipulateMoney(100); + + output = "You have won a crossword competition. Collect $100."; + break; + } + + JOptionPane.showMessageDialog(jf,output,"Alert",JOptionPane.WARNING_MESSAGE); + } +} diff --git a/Monopoly-Whole/Monopoly/CommunityChestDeck.java b/Monopoly-Whole/Monopoly/CommunityChestDeck.java new file mode 100644 index 0000000..f1e7e12 --- /dev/null +++ b/Monopoly-Whole/Monopoly/CommunityChestDeck.java @@ -0,0 +1,126 @@ +package Monopoly; + +import java.util.Random; + +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +/** + * The Community Chest Deck + * @author Jonathan Dang + * + */ +public class CommunityChestDeck { + private Random _randomNumberGenerator; + + /** + * Constructs a Community Chest Deck + */ + public CommunityChestDeck() + { + _randomNumberGenerator = new Random(); + } + + /** + * Draws a Card + * @param p The Player + * @param b The Board + */ + public void drawCard(player p, Board b) + { + int card = _randomNumberGenerator.nextInt(17); + String output = ""; + JFrame jf = new JFrame(); + + switch (card) { + case(0): + p.moveToGo(); + output = "Advance to GO!"; + break; + case(1): + p.manipulateMoney(200); + output = "Bank error in your favor. Collect $200."; + break; + case(2): + p.manipulateMoney(-50); + output = "Doctor's fees. Pay $50."; + break; + case(3): + p.manipulateMoney(50); + output = "From sale of stock you get $50."; + break; + case(4): + p.obtainGOFJC(); + output = "Get Out of Jail Free!"; + break; + case(5): + if(p.hasGOFJC()) + { + int use = JOptionPane.showConfirmDialog(jf,"Use Get out of Jail Free Card?"); + + if (use == JOptionPane.YES_OPTION) + p.goToJail(true); + else + { + p.goToJail(false); + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int) p.getIndex() + 1) + + " HAS BEEN SENT TO JAIL!","Alert",JOptionPane.WARNING_MESSAGE); + } + } + else + { + p.goToJail(false); + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int) p.getIndex() + 1) + + " HAS BEEN SENT TO JAIL!","Alert",JOptionPane.WARNING_MESSAGE); + } + output = "Go Directly to Jail!"; + break; + case(6): + b.syphonPlayers(p, 50); + output = "Grand Opera Night. Collect $50 from every player for opening night seats."; + break; + case(7): + p.manipulateMoney(100); + output = "Holiday Fund matures. Receive $100."; + break; + case(8): + p.manipulateMoney(20); + output = "Income tax refund. Collect $20."; + break; + case(9): + b.syphonPlayers(p, 10); + output = "It is your birthday. Collect $10 from every player."; + break; + case(10): + p.manipulateMoney(100); + output = "Life insurance matures – Collect $100."; + break; + case(11): + p.manipulateMoney(-50); + output = "Hospital Fees. Pay $50."; + break; + case(12): + p.manipulateMoney(-50); + output = "School fees. Pay $50."; + break; + case(13): + p.manipulateMoney(25); + output = "Receive $25 consultancy fee."; + break; + case(14): + p.makeRepairs(40, 115); + output = "You are assessed for street repairs: Pay $40 per house and $115 per hotel you own."; + break; + case(15): + p.manipulateMoney(10); + output = "You have won second prize in a beauty contest. Collect $10."; + break; + case(16): + p.manipulateMoney(100); + output = "You inherit $100"; + break; + } + + JOptionPane.showMessageDialog(jf,output,"Alert",JOptionPane.WARNING_MESSAGE); + } +} diff --git a/Monopoly-Whole/Monopoly/DiceRoller.java b/Monopoly-Whole/Monopoly/DiceRoller.java new file mode 100644 index 0000000..9252252 --- /dev/null +++ b/Monopoly-Whole/Monopoly/DiceRoller.java @@ -0,0 +1,43 @@ +package Monopoly; + +import java.util.Random; + +/** + * The Static Function Library + * @author Jonathan Dang + * + */ +public class DiceRoller { + private static Random _randomNumberGen = new Random(); + + /** + * Returns 2 Dice rolls in an Array + * @return int[] The Dice Roll + */ + public static int[] getDiceRolls() + { + int[] rolls = new int[2]; + + for(int i = 0; i < 2; i ++) + { + rolls[i] = _randomNumberGen.nextInt(6) + 1; + } + + return rolls; + } + + /** + * Returns the total Roll of the Dice + * @return int The Total Dice ROll + */ + public static int rollDice() + { + int rolls[] = getDiceRolls(); + int total = 0; + for(int i = 0; i < 2; i++) + { + total += rolls[i]; + } + return total; + } +} diff --git a/Monopoly-Whole/Monopoly/Monopoly.java b/Monopoly-Whole/Monopoly/Monopoly.java new file mode 100644 index 0000000..97a935f --- /dev/null +++ b/Monopoly-Whole/Monopoly/Monopoly.java @@ -0,0 +1,249 @@ +package Monopoly; + +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextArea; + +import Helpers.SaveLoader; + +/** + * Monopoly the Game! + * I Do not take Ownership of Monopoly(tm), However due to an assignment + * I have recreated it into a simplistic form for Education Reasons + * @author Jonathan Dang + * + */ +@SuppressWarnings("serial") +public class Monopoly extends JFrame{ + private player[] _players; + private Board b; + private SaveLoader SL; + + private JButton go; + private JButton sell; + private JButton endTurn; + private JTextArea turnInfo; + + private boolean Moved; + + /** + * Constructs a Monopoly Game + */ + public Monopoly() + { + setTitle("Monopoly"); + setSize(900,500); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setUpScreen(); + setLocationRelativeTo(null); + Moved = false; + + try { + this.startUp(); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + + } + + /** + * Starts up the game from Console + * @throws IOException | Checks if the File is there or not + * @throws InterruptedException | Checks if something Interupts the processing time + */ + public void startUp() throws IOException, InterruptedException + { + SL= new SaveLoader(); + SL.display(); + Scanner ins = new Scanner(System.in); + ArrayList players = new ArrayList(); + int totalPlayer = 0; + while (totalPlayer < 2 || totalPlayer > 8) { + try { + System.out.println("How many people are playing?"); + System.out.print("Players (2 - 8): "); + totalPlayer = ins.nextInt(); + } + catch(Exception e) { + System.err.println("Error: Number too large."); + continue; + } + if(totalPlayer > 8) { + System.err.println("Error: Invalid player count."); + } + } + + for (int i = 0; i < totalPlayer; i++) + { + System.out.println("Player " + (i+1)); + System.out.println("What is your name: "); + String name = ins.next(); + System.out.println("Please enter a series of numbers, this will be your Credit Card Number: "); + String CCN = ins.next(); + for (int t = 0; t < player.FIGURES.length; t++) + { + System.out.println(t + " " + player.FIGURES[t]); + } + System.out.println("Please enter a number that corresponds to a game piece:"); + int piece = ins.nextInt(); + + player p = new player(piece,name,CCN); + players.add(p); + } + + _players = new player[totalPlayer]; + + int i = 0; + for (player p : players) + { + _players[i] = p; + i++; + } + + b = new Board(_players); + ins.close(); + setVisible(true); + this.updateInfo(); + this.startGame(); + } + + /** + * Runs the Game + */ + public void startGame() + { + while(!b.hasWinner()) + { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + this.endGame(); + } + + /** + * Updates the TextArea in which displays the game details + */ + private void updateInfo() + { + //FORMAT: + //Player: [NAME] + //[LocationName] + //[Balance] + + String info = ""; + + info += "TURN: " + b.getCurrentTurnPlayer().getName() + "\n"; + info += "LOCATION: " + b.getLocationName() + "\n"; + info += "BALANCE: " + b.getCurrentTurnPlayer().getBalance() + "\n"; + + turnInfo.setText(info); + } + + /** + * Initializes the Screen + */ + private void setUpScreen() + { + go = new JButton("GO!"); + go.addActionListener(new GoButtonListener()); + sell = new JButton("Sell"); + sell.addActionListener(new SellButtonListener()); + endTurn = new JButton("End Turn"); + endTurn.addActionListener(new NextTurnButtonListener()); + + turnInfo = new JTextArea(); + turnInfo.setAlignmentY(CENTER_ALIGNMENT); + turnInfo.setAlignmentX(CENTER_ALIGNMENT); + turnInfo.setEditable(false); + + JPanel control = new JPanel(); + control.setLayout(new GridLayout(1,3)); + + JPanel BuySell = new JPanel(); + BuySell.setLayout(new GridLayout(2,1)); + + BuySell.add(go); + BuySell.add(sell); + + control.add(BuySell); + control.add(turnInfo); + control.add(endTurn); + + add(control); + } + + /** + * Processes Go + */ + private class GoButtonListener implements ActionListener + { + + @Override + public void actionPerformed(ActionEvent e) { + if (!Moved) + { + b.Go(); + Moved = true; + } + + updateInfo(); + } + + } + + /** + * Processes into the Next Turn + */ + private class NextTurnButtonListener implements ActionListener + { + + @Override + public void actionPerformed(ActionEvent e) { + b.nextTurn(); + Moved = false; + updateInfo(); + } + + } + + /** + * Processes and Calculates the Selling Action + */ + private class SellButtonListener implements ActionListener + { + + @Override + public void actionPerformed(ActionEvent e) { + try { + b.sell(b.getTurn(), false); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } + updateInfo(); + } + + } + + /** + * Ends the game! + */ + public void endGame() + { + SL.save(b.getPlayerData()); + player p = b.getWinner(); + JFrame jf = new JFrame(); + JOptionPane.showMessageDialog(jf, p.getName() + " Has won the game! Balance: " + p.getBalance()); + } +} diff --git a/Monopoly-Whole/Monopoly/Spaces.java b/Monopoly-Whole/Monopoly/Spaces.java new file mode 100644 index 0000000..da6fd28 --- /dev/null +++ b/Monopoly-Whole/Monopoly/Spaces.java @@ -0,0 +1,34 @@ +package Monopoly; + +/** + * Abstract Space Object + * @author Jonathan Dang + * + */ +public abstract class Spaces { + private String _name; + + /** + * Constructs an abstract Space + * @param n The Name + */ + public Spaces(String n) + { + this._name = n; + } + + /** + * Returns the Name of the Space + * @return String theName + */ + public String getName() + { + return _name; + } + + public abstract void activateSpace(player p, Board b); + + public abstract int getOwnerIndex(); + + public abstract void sellSpace(player p); +} diff --git a/Monopoly-Whole/Monopoly/player.java b/Monopoly-Whole/Monopoly/player.java new file mode 100644 index 0000000..ec74cf8 --- /dev/null +++ b/Monopoly-Whole/Monopoly/player.java @@ -0,0 +1,313 @@ +package Monopoly; + +import java.util.Random; + +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +public class player { + private int _playerIndex; + private int _location; + private int _balance; + private boolean _inJail; + private boolean _getOutOfJailFreeCard; + private boolean _bankruptcy; + private int _housesBought; + private int _hotelsBought; + private int _trainsOwned; + private int _utilsOwned; + + private static int INDEXCOUNT = 0; + + private String _name; + private String _creditCardNumber; + private int _playerIndicator; + + public static final String[] FIGURES = {"WHEELBARROW", "BATTLESHIP", "MONEY", "HORSE", "CAR" + , "TRAIN", "THIMBLE", "CANNON", "SHOE", "DOG", "IRON", "HAT"}; + + /** + * Constructs a player ment for the game MONOPOLY + */ + public player(int indicator, String name, String CCN) + { + _name = name; + _creditCardNumber = CCN; + + if (indicator > 11 || indicator < 0) + { + Random r = new Random(); + int selection = r.nextInt(12); + _playerIndicator = selection; + } + + _playerIndex = INDEXCOUNT++; + _location = 0; + _balance = 1300; + _inJail = false; + _getOutOfJailFreeCard = false; + _bankruptcy = false; + } + + /** + * Returns the name of the player + * @return name + */ + public String getName() + { + return _name; + } + + /** + * Players Normally will have a credit card number that is more like a serial number. + * This returns it + * @return CCN + */ + public String getCCN() + { + return _creditCardNumber; + } + + /** + * Gets the game piece of the player + * @return a FIGURE + */ + public String getIndicator() + { + return FIGURES[_playerIndicator]; + } + + /** + * Moves the player to go + */ + public void moveToGo() + { + _location = 0; + _balance += 200; + } + + /** + * Sets the location of the player to the space + * @param space | The new location + */ + public void setLocation(int space) + { + int modified = -1; + if (space > 39) + { + modified = space % 39; + this.manipulateMoney(200); + } + + if(modified > 0) + { + _location = modified; + } + else + { + _location = space; + } + } + + /** + * Returns the current balance of the player + * @return the balance + */ + public int getBalance() + { + return _balance; + } + + /** + * Returns the location of the player + * @return the location + */ + public int getLocation() + { + return _location; + } + + /** + * Increments the amount of Utilities owned + */ + public void buyUtil() + { + _utilsOwned++; + } + + /** + * Returns the amount of Utilities Owned + * @return The amount of Utilities owned + */ + public int getUtilsOwned() + { + return _utilsOwned; + } + + /** + * Adds a certain amount of houses to the player's record + * @param houses | The amount of houses bought + */ + public void boughtHouse(int houses) + { + _housesBought += houses; + } + + /** + * Buys a hotel + */ + public void boughtHotel() + { + _housesBought -= 4; + _hotelsBought++; + } + + /** + * Returns the amount of houses bought by the player + * @return the amount of houses bought + */ + public int getHousesBought() + { + return _housesBought; + } + + /** + * Returns the amount of hotels bought + * @return the amount of hotels bought + */ + public int getHotelsBought() + { + return _hotelsBought; + } + + /** + * Manipulates the balance of the player with a certain amount + * @param money | The amount of money + */ + public void manipulateMoney(int money) + { + if (_balance + money < 0) + declareBankruptcy(); + + _balance += money; + } + + /** + * Declares the player as Bankrupt and removes them from the game + */ + private void declareBankruptcy() { + JFrame jf = new JFrame(); + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int)this.getIndex() + 1) + + " has declared Bankruptcy!","Alert",JOptionPane.WARNING_MESSAGE); + this._bankruptcy = true; + } + + /** + * Checks if the player has a Get out of jail free card + * @return _getOutOfJailFreeCard + */ + public boolean hasGOFJC() + { + return _getOutOfJailFreeCard; + } + + /** + * Gives the player a getOutOfJailFreeCard + */ + public void obtainGOFJC() + { + _getOutOfJailFreeCard = true; + } + + /** + * uses the getOutOfJailFreeCard + * @return True if the player has one + */ + public boolean useGOFJC() + { + if (_getOutOfJailFreeCard) + { + _getOutOfJailFreeCard = false; + return true; + } + else + return false; + } + + /** + * Checks if the player is Bankrupt + * @return _bankruptcy + */ + public boolean isBankrupt() + { + return _bankruptcy; + } + + /** + * Sends the player to jail + * @param useCard | True if they want to use the GetOutofJailFree Card + */ + public void goToJail(boolean useCard) + { + if(useCard && useGOFJC()) + _inJail = false; + else + _inJail = true; + + setLocation(10); + } + + /** + * Buys a train + */ + public void buyTrain() + { + _trainsOwned++; + } + + /** + * Returns the amount of trains owned + * @return trains owned + */ + public int getTrainsOwned() + { + return _trainsOwned; + } + + /** + * Checks if the player is in jail + * @return true if in jail + */ + public boolean inJail() + { + return _inJail; + } + + /** + * Puts the player out of jail + */ + public void getOutOfJail() + { + _inJail = false; + } + + /** + * Returns the player's index + * @return player index + */ + public int getIndex() + { + return _playerIndex; + } + + /** + * Checks the amount of houses and hotels owned, then calculates the total costs + * @param houseCost | Cost per house to repair + * @param hotelCost | Cost per hotel to repair + */ + public void makeRepairs(int houseCost, int hotelCost) + { + int costs = ((int)this.getHotelsBought() * hotelCost) + ((int)this.getHousesBought() * houseCost); + manipulateMoney(-costs); + } +} diff --git a/Monopoly-Whole/Monopoly_Properties/ChanceSpace.java b/Monopoly-Whole/Monopoly_Properties/ChanceSpace.java new file mode 100644 index 0000000..4eab305 --- /dev/null +++ b/Monopoly-Whole/Monopoly_Properties/ChanceSpace.java @@ -0,0 +1,41 @@ +package Monopoly_Properties; + +import Monopoly.Board; +import Monopoly.ChanceDeck; +import Monopoly.Spaces; +import Monopoly.player; + +/** + * The Chance Space + * @author Jonathan Dang + * + */ +public class ChanceSpace extends Spaces{ + private final ChanceDeck _chanceD = new ChanceDeck(); + + /** + * Contructs a Chance Space + */ + public ChanceSpace() { + super("Chance!"); + } + + /** + * Draws a Card + */ + @Override + public void activateSpace(player p, Board b) { + _chanceD.drawCard(p, b); + } + + @Override + public int getOwnerIndex() + { + return -1; + } + + @Override + public void sellSpace(player p) { + return; + } +} diff --git a/Monopoly-Whole/Monopoly_Properties/CommunityChestSpace.java b/Monopoly-Whole/Monopoly_Properties/CommunityChestSpace.java new file mode 100644 index 0000000..fec55cd --- /dev/null +++ b/Monopoly-Whole/Monopoly_Properties/CommunityChestSpace.java @@ -0,0 +1,40 @@ +package Monopoly_Properties; + +import Monopoly.Board; +import Monopoly.CommunityChestDeck; +import Monopoly.Spaces; +import Monopoly.player; + +/** + * The Community Chest Space + * @author Jonathan Dang + * + */ +public class CommunityChestSpace extends Spaces{ + private final CommunityChestDeck _CCD = new CommunityChestDeck(); + + /** + * Contructs a Chest Space + */ + public CommunityChestSpace() { + super("Community Chest"); + } + + /** + * Draws a Card + */ + @Override + public void activateSpace(player p, Board b) { + _CCD.drawCard(p, b); + } + + @Override + public int getOwnerIndex() { + return -1; + } + + @Override + public void sellSpace(player p) { + return; + } +} diff --git a/Monopoly-Whole/Monopoly_Properties/FreeParkingSpace.java b/Monopoly-Whole/Monopoly_Properties/FreeParkingSpace.java new file mode 100644 index 0000000..710a4fe --- /dev/null +++ b/Monopoly-Whole/Monopoly_Properties/FreeParkingSpace.java @@ -0,0 +1,38 @@ +package Monopoly_Properties; + +import Monopoly.Board; +import Monopoly.Spaces; +import Monopoly.player; + +/** + * The Free Parking Space + * @author Jonathan Dang + * + */ +public class FreeParkingSpace extends Spaces{ + + /** + * Constructs a Free Parking Space + */ + public FreeParkingSpace() { + super("Free Parking!"); + } + + /** + * Does Nothing :D + */ + @Override + public void activateSpace(player p, Board b) { + //Do Absolutely Nothing!!! :D + } + + @Override + public int getOwnerIndex() { + return -1; + } + + @Override + public void sellSpace(player p) { + return; + } +} diff --git a/Monopoly-Whole/Monopoly_Properties/GoSpace.java b/Monopoly-Whole/Monopoly_Properties/GoSpace.java new file mode 100644 index 0000000..4b06c39 --- /dev/null +++ b/Monopoly-Whole/Monopoly_Properties/GoSpace.java @@ -0,0 +1,40 @@ +package Monopoly_Properties; + +import Monopoly.Board; +import Monopoly.Spaces; +import Monopoly.player; + +/** + * The Go Space + * @author Jonathan Dang + * + */ +public class GoSpace extends Spaces{ + + /** + * Constructs the Go Space + */ + public GoSpace() { + super("GO"); + } + + //Keep in mind that you get 200 if you land OR pass GO! + /** + * Gives the Player 200 Currency + */ + @Override + public void activateSpace(player p, Board b) { + p.manipulateMoney(200); + } + + + @Override + public int getOwnerIndex() { + return -1; + } + + @Override + public void sellSpace(player p) { + return; + } +} diff --git a/Monopoly-Whole/Monopoly_Properties/GoToJail.java b/Monopoly-Whole/Monopoly_Properties/GoToJail.java new file mode 100644 index 0000000..2e4674f --- /dev/null +++ b/Monopoly-Whole/Monopoly_Properties/GoToJail.java @@ -0,0 +1,61 @@ +package Monopoly_Properties; + +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +import Monopoly.Board; +import Monopoly.Spaces; +import Monopoly.player; + +/** + * Go TO Jail Space + * @author Jonathan Dang + * + */ +public class GoToJail extends Spaces{ + + /** + * Constructs the GoToJail + */ + public GoToJail() { + super("Go To Jail!"); + } + + /** + * Sends the Player To Jail + */ + @Override + public void activateSpace(player p, Board b) { + JFrame jf = new JFrame(); + + if(p.hasGOFJC()) + { + int use = JOptionPane.showConfirmDialog(jf,"Use Get out of Jail Free Card?"); + + if (use == JOptionPane.YES_OPTION) + p.goToJail(true); + else + { + p.goToJail(false); + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int) p.getIndex() + 1) + + " HAS BEEN SENT TO JAIL!","Alert",JOptionPane.WARNING_MESSAGE); + } + } + else + { + p.goToJail(false); + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int) p.getIndex() + 1) + + " HAS BEEN SENT TO JAIL!","Alert",JOptionPane.WARNING_MESSAGE); + } + } + + @Override + public int getOwnerIndex() { + return -1; + } + + @Override + public void sellSpace(player p) { + return; + } +} diff --git a/Monopoly-Whole/Monopoly_Properties/Jail.java b/Monopoly-Whole/Monopoly_Properties/Jail.java new file mode 100644 index 0000000..2156ac1 --- /dev/null +++ b/Monopoly-Whole/Monopoly_Properties/Jail.java @@ -0,0 +1,41 @@ +package Monopoly_Properties; + +import Monopoly.Board; +import Monopoly.Spaces; +import Monopoly.player; + +/** + * The Jail + * @author Jonathan Dang + * + */ +public class Jail extends Spaces{ + + /** + * Constructs a Jail Space + */ + public Jail() { + super("Jail"); + } + + /** + * Does Nothing, PlaceHolder as for the Jail ROLL in Board + */ + @Override + public void activateSpace(player p, Board b) { + // Well, this space doesn't really do anything but it does skip your turn if you are in jail. + + } + + @Override + public int getOwnerIndex() { + + return -1; + } + + @Override + public void sellSpace(player p) { + return; + } + +} diff --git a/Monopoly-Whole/Monopoly_Properties/Position_Relationships.java b/Monopoly-Whole/Monopoly_Properties/Position_Relationships.java new file mode 100644 index 0000000..14e70ec --- /dev/null +++ b/Monopoly-Whole/Monopoly_Properties/Position_Relationships.java @@ -0,0 +1,60 @@ +package Monopoly_Properties; + +/** + * Relationships between Position and the Space + * @author Jonathan Dang + * + */ +public enum Position_Relationships { + GO(0), + MEDITERANEAN_AVENUE(1), + COMMUNITY_CHEST1(2), + BALTIC_AVENUE(3), + INCOME_TAX(4), + READING_RAILROAD(5), + ORIENTAL_AVENUE(6), + CHANCE_PURPLE(7), + VERMONT_AVENUE(8), + CONNECTICUT_AVENUE(9), + JAIL(10), + SAINT_CHARLES_PLACE(11), + ELECTRIC_COMPANY(12), + STATES_AVENUE(13), + VIRGINIA_AVENUE(14), + PENNSYLVANIA_RAILROAD(15), + SAINT_JAMES_PLACE(16), + COMMUNITY_CHEST2(17), + TENNESSEE_AVENUE(18), + NEW_YORK_AVENUE(19), + FREE_PARKING(20), + KENTUCKY_AVENUE(21), + CHANCE_BLUE(22), + INDIANA_AVENUE(23), + ILLINOISE_AVENUE(24), + BBQ_RAILROAD(25), + ATLANTIC_AVENUE(26), + VENTOR_AVENUE(27), + WATER_WORKS(28), + MARVIN_GARDENS(29), + GO_TO_JAIL(30), + PACIFIC_AVENUE(31), + NORTH_CAROLINA_AVENUE(32), + COMMUNITY_CHEST3(33), + PENNSYLVANIA_AVENUE(34), + SHORT_LINE(35), + CHANCE_ORANGE(36), + PARK_PLACE(37), + LUXURY_TAX(38), + BOARDWALK(39); + + Position_Relationships(int i) { + this._value = i; + } + + private int _value; + + public int getLocation() + { + return _value; + } +} diff --git a/Monopoly-Whole/Monopoly_Properties/Property.java b/Monopoly-Whole/Monopoly_Properties/Property.java new file mode 100644 index 0000000..6d55970 --- /dev/null +++ b/Monopoly-Whole/Monopoly_Properties/Property.java @@ -0,0 +1,327 @@ +package Monopoly_Properties; + +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +import Monopoly.Board; +import Monopoly.Spaces; +import Monopoly.player; + +/** + * The Property Space + * @author Jonathan Dang + * + */ +public class Property extends Spaces{ + private int _cost; //Base Cost + private int _rent; //Base Rent + private int _houses; //Only up to 4 houses. + private int _houseRent1; + private int _houseRent2; + private int _houseRent3; + private int _houseRent4; + private int _buildingCost; //Cost per building + private boolean _hotel; //Only 1 hotel. true if player has bought hotel + private int _hotelRent; //Rent if Hotel is purchased [Must Have 4 Houses] {sets houses to 0} + private int _ownerIndex = -1; //This is the index of the owner, 0->3 + + //I need a way to implement selling properties, + //SOLUTION 1: Put an abstract function onto the Space File and make it return owner index on only property spaces + //SOLUTION 2: Put an abstract function onto each Property File and make it sell for half price flat. + + //For Solution 1, when I make the sell method, I would run through the entire _board calling the abstract method to return the owner's Index + //as it will give me a list of the names to display. As for the selling part, each one will have their own selling but generally will be + //standardized between all types of properties. + + //I am not too sure for Solution 2, so I will proceed on Solution 1 currently. 5/31/2021 + + /** + * Constructs a Property + * @param name The Name + * @param price The Price + * @param rent The Initial Rent + * @param HouseRent1 Rent at 1 house + * @param HouseRent2 Rent at 2 Houses + * @param HouseRent3 Rent at 3 Houses + * @param HouseRent4 Rent at 4 Houses + * @param hotelRent Rent at Hotel Level + * @param buildingCost The Building Cost + */ + public Property(String name, int price, int rent, int HouseRent1, + int HouseRent2, int HouseRent3, int HouseRent4, int hotelRent, int buildingCost) { + super(name); + _cost = price; + _rent = rent; + _houseRent1 = HouseRent1; + _houseRent2 = HouseRent2; + _houseRent3 = HouseRent3; + _houseRent4 = HouseRent4; + _hotelRent = hotelRent; + _buildingCost = buildingCost; + } + + /** + * Attempts to buy a certain amount of houses + * @param p The Player + * @param amount Amount of houses + * @return True if successfully buys a house + */ + public boolean buyHouse(player p, int amount) + { + if (amount + _houses >= 4) + { + int fillHouses = amount - _houses; + if (p.getBalance() < fillHouses * _buildingCost) + { + //This person cannot buy this much, I will choose to just refuse the entire thing. + return false; + } + else + { + int costs = fillHouses * _buildingCost; + _houses = 4; + p.manipulateMoney(-costs); + return true; + } + } + else + { + + if (p.getBalance() < amount * _buildingCost) + { + //This person cannot buy this much, I will choose to just refuse the entire thing. + return false; + } + else + { + int costs = amount * _buildingCost; + _houses += amount; + p.manipulateMoney(-costs); + return true; + } + } + } + + /** + * Attempts to buy a Hotel + * @param p The Player + * @return True if successfully buys a house + */ + public boolean buyHotel(player p) + { + if (p.getBalance() >= _buildingCost && this.getHouse() == 4) + { + _hotel = true; + p.manipulateMoney(-_buildingCost); + this._houses = 0; + return true; + } + else + return false; + } + + /** + * Returns the Building Cost + * @return int The Building Cost + */ + public int getBuildingCost() + { + return _buildingCost; + } + + /** + * Returns the amount of houses on this property + * @return int the houses + */ + public int getHouse() + { + return _houses; + } + + /** + * Returns if there is a hotel + * @return boolean The Hotel + */ + public boolean getHotel() + { + return _hotel; + } + + @Override + public void activateSpace(player p, Board b) { + JFrame jf = new JFrame(); + if (_ownerIndex < 0) //No one owns this + { + int option = JOptionPane.showConfirmDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + ": Buy " + this.getName() +"?"); + if (option == JOptionPane.YES_OPTION) + { + if (p.getBalance() < this._cost) + { + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + + ": You don't have enought money!","Alert",JOptionPane.WARNING_MESSAGE); + return; + } + p.manipulateMoney(-_cost); + _ownerIndex = p.getIndex(); + + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + ": You spent $" + this._cost + " on buying " + this.getName() + "!"); + } + else + return; + } + else //a play at this index owns this + { + if (_ownerIndex != p.getIndex()) //pay rent! + { + int rent = this._rent; + if (_hotel) + rent = _hotelRent; + else + { + switch(this.getHouse()) + { + case(0): + rent = this._rent; + break; + case(1): + rent = this._houseRent1; + break; + case(2): + rent = this._houseRent2; + break; + case(3): + rent = this._houseRent3; + break; + case(4): + rent = this._houseRent4; + break; + } + } + + try { + b.rent(_ownerIndex, p.getIndex(), rent); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + if (p.isBankrupt()) + return; + + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + + ": You Lost "+ rent + "!","Alert",JOptionPane.WARNING_MESSAGE); + } + else //Prompt to buy this property's houses or hotels + { + if (this.getHouse() < 4 && b.getHouseCount() > 0) + { + String input = JOptionPane.showInputDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + + ": Input the amount of houses you want to buy [Put 0 if none]" + + "[Cost Per House: " + _buildingCost + "]"); + + if (input.isEmpty()) + return; + + int costs = Integer.parseInt(input); + + costs = costs > b.getHotelCount() ? (costs - b.getHotelCount()) : costs; + + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + + ": You spent " + ((int)(costs > 4 ? 4: costs) * this.getBuildingCost()) + " on buying houses!"); + + p.boughtHouse(costs); + b.useHouse(costs); + this.buyHouse(p,costs); + } + + if (this.getHouse() == 4 && b.getHotelCount() > 0) + { + int option = JOptionPane.showConfirmDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + + ": Do you want to buy a Hotel? [COST:" + _buildingCost + "]"); + if (option == JOptionPane.YES_OPTION) + { + if (this.buyHotel(p)) + { + JOptionPane.showMessageDialog(jf, "Player " + ((int)p.getIndex() + 1) + " has purchased a Hotel!"); + this._hotel = true; + p.boughtHotel(); + b.useHotel(); + return; + } + else + { + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + + ": You do not have enough for this!"); + return; + } + } + else + return; + } + + } + } + } + + @Override + public int getOwnerIndex() + { + return _ownerIndex; + } + + @Override + public void sellSpace(player p) { + if (p.getIndex() != this.getOwnerIndex()) + throw new IllegalArgumentException("DOES NOT OWN THIS PROPERTY!"); + + //Check if there is hotel first + //Then check if there is houses after + //Then sell the empty property + //I think it would be best to exit every time they sell. + JFrame jf = new JFrame(); + + if (this.getHotel()) + { + this._hotel = false; + p.manipulateMoney(_buildingCost); + this._houses = 4; + + JOptionPane.showMessageDialog(jf, "PLAYER " + ((int)p.getIndex() + 1) + " HAS SOLD A HOTEL ON " + + this.getName() + '.'); + + return; + } + else if (this.getHouse() > 0) + { + String input = JOptionPane.showInputDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + + ": Input the amount of houses you want to sell" + + "[Cost Per House: " + _buildingCost + "]"); + + if (input.isBlank()) + { + this.sellSpace(p); + return; + } + + int housesSold = Integer.parseInt(input); + this._houses -= housesSold; + if (this.getHouse() < 0) {this._houses = 0;} + + p.manipulateMoney(housesSold * this._buildingCost); + + + JOptionPane.showMessageDialog(jf, "PLAYER " + ((int)p.getIndex() + 1) + " HAS SOLD " + housesSold + " ON " + + this.getName() + '.'); + + return; + } + else + { + this._ownerIndex = -1; + p.manipulateMoney(_cost); + JOptionPane.showMessageDialog(jf, "PLAYER " + ((int)p.getIndex() + 1) + + " HAS SOLD " + this.getName() + '.'); + return; + } + + } +} diff --git a/Monopoly-Whole/Monopoly_Properties/Tax.java b/Monopoly-Whole/Monopoly_Properties/Tax.java new file mode 100644 index 0000000..1a59f06 --- /dev/null +++ b/Monopoly-Whole/Monopoly_Properties/Tax.java @@ -0,0 +1,55 @@ +package Monopoly_Properties; + +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +import Monopoly.Board; +import Monopoly.Spaces; +import Monopoly.player; + +/** + * The Tax Space + * @author pocke + * + */ +public class Tax extends Spaces{ + private int _taxRate; + + /** + * Constructs the Tax Space + * @param Rate The Cost of the Tax + */ + public Tax(int Rate) { + super("Tax"); + _taxRate = Rate; + } + + /** + * Taxs the Player + */ + @Override + public void activateSpace(player p, Board b) { + JFrame jf = new JFrame(); + p.manipulateMoney(-_taxRate); + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + + ": You Lost "+ _taxRate + "!","Alert",JOptionPane.WARNING_MESSAGE); + } + + /** + * Returns -1 + * @return int -1; + */ + @Override + public int getOwnerIndex() { + return -1; + } + + /** + * Nothing + */ + @Override + public void sellSpace(player p) { + return; + } + +} diff --git a/Monopoly-Whole/Monopoly_Properties/TrainProperty.java b/Monopoly-Whole/Monopoly_Properties/TrainProperty.java new file mode 100644 index 0000000..15157ce --- /dev/null +++ b/Monopoly-Whole/Monopoly_Properties/TrainProperty.java @@ -0,0 +1,104 @@ +package Monopoly_Properties; + +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +import Monopoly.Board; +import Monopoly.Spaces; +import Monopoly.player; + +/** + * The Train Property + * @author Jonathan Dang + * + */ +public class TrainProperty extends Spaces{ + private int _ownerIndex = -1; + private final int _cost = 200; + private int _rentMod = 50; + public static final int READING = 5; + public static final int PENNSYLVANIA = 15; + public static final int BBO = 25; + public static final int SHORT = 35; + + /** + * Constructs a Train Property + * @param n The name + */ + public TrainProperty(String n) { + super(n); + } + + /** + * Returns the Owner Index + * @return int the owner's Index + */ + @Override + public int getOwnerIndex() + { + return _ownerIndex; + } + + /** + * Buys, Rents, or sells the Property + */ + @Override + public void activateSpace(player p, Board b) { + // TODO Check on each Train Property, The amount owned will determine Rent + JFrame jf = new JFrame(); + if (_ownerIndex >= 0) + { + //Rent! + if (_ownerIndex != p.getIndex()) + { + int rent = _rentMod * p.getTrainsOwned(); + + try { + b.rent(_ownerIndex, p.getIndex(), rent); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + if (p.isBankrupt()) + return; + + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + + ": You Lost "+ rent + "!","Alert",JOptionPane.WARNING_MESSAGE); + } + + } + else + { + int option = JOptionPane.showConfirmDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + ": Buy " + this.getName() +"?"); + if (option == JOptionPane.YES_OPTION) + { + if (p.getBalance() < this._cost) + { + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + + ": You don't have enought money!","Alert",JOptionPane.WARNING_MESSAGE); + return; + } + p.manipulateMoney(-_cost); + _ownerIndex = p.getIndex(); + + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + ": You spent $" + this._cost + " on buying " + this.getName() + "!"); + p.buyTrain(); + } + else + return; + } + + } + + @Override + public void sellSpace(player p) { + if (p.getIndex() != this.getOwnerIndex()) + throw new IllegalArgumentException("DOES NOT OWN THIS PROPERTY!"); + + this._ownerIndex = -1; + p.manipulateMoney(_cost/2); + JOptionPane.showMessageDialog(new JFrame(), "PLAYER " + + ((int)p.getIndex() + 1) + " HAS SOLD " + this.getName() + '.'); + } + +} diff --git a/Monopoly-Whole/Monopoly_Properties/Utility.java b/Monopoly-Whole/Monopoly_Properties/Utility.java new file mode 100644 index 0000000..7cca754 --- /dev/null +++ b/Monopoly-Whole/Monopoly_Properties/Utility.java @@ -0,0 +1,110 @@ +package Monopoly_Properties; + +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +import Monopoly.Board; +import Monopoly.DiceRoller; +import Monopoly.Spaces; +import Monopoly.player; + +/** + * Utility Space + * @author Jonathan Dang + * + */ +public class Utility extends Spaces{ + private int _ownerIndex = -1; + private final int _cost = 150; + + /** + * Constructs a Utility Space + * @param n The Name + */ + public Utility(String n) { + super(n); + } + + /** + * Buys or Rents the Space + */ + @Override + public void activateSpace(player p, Board b) { + JFrame jf = new JFrame(); + if (_ownerIndex >= 0) + { + if (_ownerIndex != p.getIndex()) + { + //Rent! + int roll = DiceRoller.rollDice(); + int rent = 0; + + switch(p.getUtilsOwned()) + { + case(1): + rent = 4 * roll; + break; + case(2): + rent = 10 * roll; + break; + } + + try { + b.rent(_ownerIndex, p.getIndex(), rent); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + if (p.isBankrupt()) + return; + + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + + ": You Lost "+ rent + "!","Alert",JOptionPane.WARNING_MESSAGE); + } + } + else + { + int option = JOptionPane.showConfirmDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + ": Buy " + this.getName() +"?"); + if (option == JOptionPane.YES_OPTION) + { + if (p.getBalance() < this._cost) + { + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + + ": You don't have enought money!","Alert",JOptionPane.WARNING_MESSAGE); + return; + } + p.manipulateMoney(-_cost); + _ownerIndex = p.getIndex(); + + JOptionPane.showMessageDialog(jf,"PLAYER " + ((int)p.getIndex() + 1) + ": You spent $" + this._cost + " on buying " + this.getName() + "!"); + p.buyUtil(); + } + else + return; + } + } + + /** + * Returns the Owner Index + * @return int the Owner's Index + */ + @Override + public int getOwnerIndex() { + return _ownerIndex; + } + + /** + * Sells the property + */ + @Override + public void sellSpace(player p) { + if (p.getIndex() != this.getOwnerIndex()) + throw new IllegalArgumentException("DOES NOT OWN THIS PROPERTY!"); + + this._ownerIndex = -1; + p.manipulateMoney(_cost/2); + JOptionPane.showMessageDialog(new JFrame(), "PLAYER " + + ((int)p.getIndex() + 1) + " HAS SOLD " + this.getName() + '.'); + } + +}