/** * Monopoly * * By Houston Mooers * * This is the game of Monopoly based on the 1935 edition and follows * most of the rules. I made some exceptions such as for selling houses * or hotels, the buildings are sold for the cost at which they were * purchased. Real estate goes up and down, but its unlikely that the * seller would only receive 1/2 of their investment upon sale. Players * can buy, sell, mortgage, unmortgage and trade property. * * I implemented the 1935 edition because it is surprisingly difficult to * find Monopoly title deed information on the internet. I used the pricing * and rules described here: * http://monopoly.cdbpdx.com/TM/ * * Because of the large code size, the default size of the SPIM * text segment may be too big to hold the generated MIPS. If this happens, * use the -stext flag to increase the size (200K works for me, * but it will depend on your MIPS code generator). * Ex: spim -stext 200000 -file monopoly.mips * * * Ok. I admit, I stole this comment from David Underhill's ChessMester... * he writes good comments. :-) * I also borrowed the rndModule class (generates random integers) from * tictactoe. Other than that, all other code here is mine. * */ Space[] spaces; Property[] realestate; Player[] players; rndModule gRnd; void main() { int numplayers; int i; string name; Print("\n\nWelcome to CS143 Monopoly\nWritten by Houston Mooers\nCS143 Summer 2008\n\n"); Print("\nMonopoly - Based on 1935 Edition\n"); while(true) { Print("Enter number of players (2-8)\n"); numplayers = ReadInteger(); if(numplayers > 1 && numplayers < 9) { break; } } players = NewArray(numplayers, Player); for(i = 0; i < numplayers; i = i + 1) { players[i] = New(Player); Print("Enter name for player ", (i+1), ":\n"); name = ReadLine(); players[i].initPlayer(name, 1500); } gRnd = New(rndModule); Print("Please enter random number between 0 and 100\n"); gRnd.Init(ReadInteger()); setupBoard(); playGame(); } void playGame() { int i; Player curplayer; while(true) { for(i = 0; i < players.length(); i = i + 1) { curplayer = players[i]; if(curplayer != null) curplayer.move(); if(numAlivePlayers() < 2) return; } } } int numAlivePlayers() { int i; int j; j = 0; for(i = 0; i < players.length(); i = i + 1) { if(players[i].isAlive()) j = j + 1; } return j; } void printRemainingPlayer() { int i; for(i = 0; i < players.length(); i = i + 1) { if(players[i].isAlive()) { Print(players[i].getName()); } } } void setupRealEstateArray() { int i; realestate = NewArray(40, Property); for(i = 0; i < realestate.length(); i = i + 1) { realestate[i] = null; } } void setupBoard() { GoSpace gospace; Property property; Chance chance; CommunityChest communitychest; IncomeTax itax; RailRoad railroad; Jail jail; GoToJail gotojail; Utility utility; FreeParking freeparking; LuxuryTax luxtax; spaces = NewArray(40, Space); setupRealEstateArray(); //setup the GoSpace gospace = New(GoSpace); gospace.initSpace("Go"); spaces[0] = gospace; //setup Mediterranean Ave property = New(Property); property.initSpace("Mediterranean Avenue"); property.setRentCost(2, 10, 30, 90, 160, 250); property.setMortgageValue(30); property.setUpgradeCost(50); property.setPurchaseCost(60); property.setColor("PURPLE"); spaces[1] = property; realestate[1] = property; //setup Community Chest communitychest = New(CommunityChest); communitychest.initSpace("Community Chest"); spaces[2] = communitychest; //setup Baltic Avenue property = New(Property); property.initSpace("Baltic Avenue"); property.setRentCost(4, 20, 60, 180, 320, 450); property.setMortgageValue(30); property.setUpgradeCost(50); property.setPurchaseCost(60); property.setColor("PURPLE"); spaces[3] = property; realestate[3] = property; //setup Income Tax itax = New(IncomeTax); itax.initSpace("Income Tax"); spaces[4] = itax; //setup Reading Railroad railroad = New(RailRoad); railroad.initSpace("Reading RailRoad"); spaces[5] = railroad; //setup Oriental Avenue property = New(Property); property.initSpace("Oriental Avenue"); property.setRentCost(6, 30, 90, 270, 400, 550); property.setMortgageValue(50); property.setUpgradeCost(50); property.setPurchaseCost(100); property.setColor("LIGHT BLUE"); spaces[6] = property; realestate[6] = property; //setup Chance chance = New(Chance); chance.initSpace("Chance"); spaces[7] = chance; //setup Vermont Avenue property = New(Property); property.initSpace("Vermont Avenue"); property.setRentCost(6, 30, 90, 270, 400, 550); property.setMortgageValue(50); property.setUpgradeCost(50); property.setPurchaseCost(100); property.setColor("LIGHT BLUE"); spaces[8] = property; realestate[8] = property; //setup Connecticut Avenue property = New(Property); property.initSpace("Connecticut Avenue"); property.setRentCost(8, 40, 100, 300, 450, 600); property.setMortgageValue(60); property.setUpgradeCost(50); property.setPurchaseCost(120); property.setColor("LIGHT BLUE"); spaces[9] = property; realestate[9] = property; //setup Jail jail = New(Jail); jail.initSpace("Jail / Just Visiting"); spaces[10] = jail; //setup St. Charles Place property = New(Property); property.initSpace("St. Charles Place"); property.setRentCost(10, 50, 150, 450, 625, 750); property.setMortgageValue(70); property.setUpgradeCost(100); property.setPurchaseCost(140); property.setColor("LIGHT PURPLE"); spaces[11] = property; realestate[11] = property; //setup Electric Company utility = New(Utility); utility.initSpace("Electric Company"); spaces[12] = utility; //setup States Avenue property = New(Property); property.initSpace("States Avenue"); property.setRentCost(10, 50, 150, 450, 625, 750); property.setMortgageValue(70); property.setUpgradeCost(100); property.setPurchaseCost(140); property.setColor("LIGHT PURPLE"); spaces[13] = property; realestate[13] = property; //setup Virginia Avenue property = New(Property); property.initSpace("Virginia Avenue"); property.setRentCost(12, 60, 180, 500, 700, 900); property.setMortgageValue(80); property.setUpgradeCost(100); property.setPurchaseCost(160); property.setColor("LIGHT PURPLE"); spaces[14] = property; realestate[14] = property; //setup Pennsylvania Railroad railroad = New(RailRoad); railroad.initSpace("Pennsylvania RailRoad"); spaces[15] = railroad; //setup St. James Place property = New(Property); property.initSpace("St. James Place"); property.setRentCost(14, 70, 200, 550, 750, 950); property.setMortgageValue(90); property.setUpgradeCost(100); property.setPurchaseCost(180); property.setColor("ORANGE"); spaces[16] = property; realestate[16] = property; //setup Community Chest spaces[17] = communitychest; //setup Tennessee Ave property = New(Property); property.initSpace("Tennessee Ave"); property.setRentCost(14, 70, 200, 550, 750, 950); property.setMortgageValue(90); property.setUpgradeCost(100); property.setPurchaseCost(180); property.setColor("ORANGE"); spaces[18] = property; realestate[18] = property; //setup New York Ave property = New(Property); property.initSpace("New York Ave"); property.setRentCost(16, 80, 220, 600, 800, 1000); property.setMortgageValue(100); property.setUpgradeCost(100); property.setPurchaseCost(200); property.setColor("ORANGE"); spaces[19] = property; realestate[19] = property; //setup Free Parking freeparking = New(FreeParking); freeparking.initSpace("Free Parking"); spaces[20] = freeparking; //setup Kentucky Ave property = New(Property); property.initSpace("Kentucky Ave"); property.setRentCost(18, 90, 250, 700, 875, 1050); property.setMortgageValue(110); property.setUpgradeCost(150); property.setPurchaseCost(220); property.setColor("RED"); spaces[21] = property; realestate[21] = property; //setup Chance spaces[22] = chance; //setup Indiana Avenue property = New(Property); property.initSpace("Indiana Avenue"); property.setRentCost(18, 90, 250, 700, 875, 1050); property.setMortgageValue(110); property.setUpgradeCost(150); property.setPurchaseCost(220); property.setColor("RED"); spaces[23] = property; realestate[23] = property; //setup Illinois Avenue property = New(Property); property.initSpace("Illinois Avenue"); property.setRentCost(20, 100, 300, 750, 925, 1100); property.setMortgageValue(120); property.setUpgradeCost(150); property.setColor("RED"); property.setPurchaseCost(240); spaces[24] = property; realestate[24] = property; //setup B&O Railraod railroad = New(RailRoad); railroad.initSpace("B&O RailRoad"); spaces[25] = railroad; //setup Atlantic Avenue //I had to move this to the setupBoard2() function below -- something about memory overflow in yacc //setup Ventnor Avenue //I had to move this to the setupBoard2() function below -- something about memory overflow in yacc //setup Water Works utility = New(Utility); utility.initSpace("Water Works"); spaces[28] = utility; //setup Marvin Gardens //I had to move this to the setupBoard2() function below -- something about memory overflow in yacc //setup Goto Jail gotojail = New(GoToJail); gotojail.initSpace("Go To Jail"); spaces[30] = gotojail; //setup Pacific Avenue //I had to move this to the setupBoard2() function below -- something about memory overflow in yacc //setup North Carolina Avenue //I had to move this to the setupBoard2() function below -- something about memory overflow in yacc //setup Community Chest spaces[33] = communitychest; //setup Pennsylvania Avenue //I had to move this to the setupBoard2() function below -- something about memory overflow in yacc //setup Short Line RR railroad = New(RailRoad); railroad.initSpace("Short Line RailRoad"); spaces[35] = railroad; //setup Chance spaces[36] = chance; //setup Park Place //I had to move this to the setupBoard2() function below -- something about memory overflow in yacc //setup Luxury Tax luxtax = New(LuxuryTax); luxtax.initSpace("Luxury Tax"); spaces[38] = luxtax; //setup BoardWalk //I had to move this to the setupBoard2() function below -- something about memory overflow in yacc setupBoard2(); } void setupBoard2() { Property property; //setup Atlantic Avenue property = New(Property); property.initSpace("Atlantic Avenue"); property.setRentCost(22, 110, 330, 800, 975, 1150); property.setMortgageValue(130); property.setUpgradeCost(150); property.setPurchaseCost(260); property.setColor("YELLOW"); spaces[26] = property; realestate[26] = property; //setup Ventnor Avenue property = New(Property); property.initSpace("Ventnor Avenue"); property.setRentCost(22, 110, 330, 800, 975, 1150); property.setMortgageValue(130); property.setUpgradeCost(150); property.setPurchaseCost(260); property.setColor("YELLOW"); spaces[27] = property; realestate[27] = property; //setup Marvin Gardens property = New(Property); property.initSpace("Marvin Gardens"); property.setRentCost(22, 120, 360, 850, 1025, 1200); property.setMortgageValue(140); property.setUpgradeCost(150); property.setPurchaseCost(280); property.setColor("YELLOW"); spaces[29] = property; realestate[29] = property; //setup Pacific Avenue property = New(Property); property.initSpace("Pacific Avenue"); property.setRentCost(26, 130, 390, 900, 1100, 1275); property.setMortgageValue(150); property.setUpgradeCost(200); property.setPurchaseCost(300); property.setColor("GREEN"); spaces[31] = property; realestate[31] = property; //setup North Carolina Avenue property = New(Property); property.initSpace("North Carolina Avenue"); property.setRentCost(26, 130, 390, 900, 1100, 1275); property.setMortgageValue(150); property.setUpgradeCost(200); property.setPurchaseCost(300); property.setColor("GREEN"); spaces[32] = property; realestate[32] = property; //setup Pennsylvania Avenue property = New(Property); property.initSpace("Pennsylvania Avenue"); property.setRentCost(28, 150, 450, 1000, 1200, 1400); property.setMortgageValue(200); property.setUpgradeCost(200); property.setPurchaseCost(320); property.setColor("GREEN"); spaces[34] = property; realestate[34] = property; //setup Park Place property = New(Property); property.initSpace("Park Place"); property.setRentCost(35, 175, 500, 1100, 1300, 1500); property.setMortgageValue(175); property.setUpgradeCost(200); property.setPurchaseCost(350); property.setColor("BLUE"); spaces[37] = property; realestate[37] = property; //setup BoardWalk property = New(Property); property.initSpace("Boardwalk"); property.setRentCost(50, 200, 600, 1400, 1700, 2000); property.setMortgageValue(200); property.setUpgradeCost(200); property.setPurchaseCost(400); property.setColor("BLUE"); spaces[39] = property; realestate[39] = property; } class Space { Player owner; string name; bool mortgaged; string type; string getName() { return name; } Player getOwner() { return owner; } bool ownedBybank() { return (owner == null); } void initSpace(string name) { this.name = name; owner = null; mortgaged = false; type = "string"; } bool canMortgage(int index) { return true; } void setOwner(Player player) { owner = player; } int getBoardIndex() { int i; for(i = 0; i < spaces.length(); i = i + 1) { if(this == spaces[i]) return i; } } bool isMortgaged() { return mortgaged; } void setMortgaged(bool value) { mortgaged = value; } int getMortgageValue() { return 0; } void Landed(Player player) { } //overridden by subclass } class Property extends Space { int[] rentCost; int costofupgrade; int mortgagevalue; int upgradelevel; int purchasecost; string color; void initSpace(string name) { this.name = name; owner = null; upgradelevel = 0; mortgaged = false; type = "property"; } string getColor() { return color; } void setColor(string color) { this.color = color; } bool canMortgage(int index) { if(index == 1 || index == 3) { if(realestate[1].hasBuildings() || realestate[3].hasBuildings()) { return false; //does player have buildings on Mediterranean or Baltic? } else return true; } else if(index == 6 || index == 8 || index == 9) { if(realestate[6].hasBuildings() || realestate[8].hasBuildings() || realestate[9].hasBuildings()) { return false; //does player have buildings on Oriental, Vermont, or Connecticut? } return true; } else if(index == 6 || index == 8 || index == 9) { if(realestate[6].hasBuildings() || realestate[8].hasBuildings() || realestate[9].hasBuildings()) { return false; //does player have buildings on Oriental, Vermont, or Connecticut? } return true; } else if(index == 11 || index == 13 || index == 14) { if(realestate[11].hasBuildings() || realestate[13].hasBuildings() || realestate[14].hasBuildings()) { return false; //does player have buildings on St. Charles, States, or Virginia? } return true; } else if(index == 16 || index == 18 || index == 19) { if(realestate[16].hasBuildings() || realestate[18].hasBuildings() || realestate[19].hasBuildings()) { return false; //does player have buildings on St. James, Tennessee, or New York? } return true; } else if(index == 21 || index == 23 || index == 24) { if(realestate[21].hasBuildings() || realestate[23].hasBuildings() || realestate[24].hasBuildings()) { return false; //does player have buildings on Kentucky, Indiana, or Illinois? } return true; } else if(index == 26 || index == 27 || index == 29) { if(realestate[26].hasBuildings() || realestate[27].hasBuildings() || realestate[29].hasBuildings()) { return false; //does player have buildings on Atlantic, Ventnor, or Marvin Gardens? } return true; } else if(index == 31 || index == 32 || index == 34) { if(realestate[31].hasBuildings() || realestate[32].hasBuildings() || realestate[34].hasBuildings()) { return false; //does player have buildings on Pacific, North Carolina, or Pennsylvania? } return true; } else if(index == 37 || index == 39) { if(realestate[37].hasBuildings() || realestate[39].hasBuildings()) { return false; //does player have buildings on Park Place or Boardwalk? } return true; } } int getMortgageValue() { return mortgagevalue; } void setMortgageValue(int value) { mortgagevalue = value; } void setRentCost(int base, int house1, int house2, int house3, int house4, int hotel) { rentCost = NewArray(6, int); rentCost[0] = base; rentCost[1] = house1; rentCost[2] = house2; rentCost[3] = house3; rentCost[4] = house4; rentCost[5] = hotel; } void setUpgradeCost(int cost) { costofupgrade = cost; } int getUpgradecost() { return costofupgrade; } void Landed(Player player) { Print(player.getName(), " has landed on ", getName(), "\n"); payRent(player); } void setPurchaseCost(int cost) { purchasecost = cost; } void payRent(Player player) { int rent; if(owner == null) { Print(getName(), " is up for sale. Do you want to buy for ", purchasecost, "? (y / n)\n"); if(getResponseYesNo()) { if(player.getMoney(purchasecost, true)) { Print(getName(), " has been purchased by ", player.getName(), "\n"); player.getProperty(getBoardIndex()); } } } else if(owner != player) //player needs to pay owner rent { rent = getRent(); player.getMoney(rent, false); if(owner != null) { Print(player.getName(), " pays $", rent, " to ", owner.getName(), " for rent.\n"); owner.getPayment(rent); } else { Print(player.getName(), " pays $", rent, " to bank for rent.\n"); owner.getPayment(rent); } } } int getRent() { int numBuildings; numBuildings = getNumBuildings(); if(numBuildings < 6 && numBuildings > -1) { return rentCost[numBuildings]; } Print("Problem computing rent cost.\n"); return 0; } int getNumBuildings() { return upgradelevel; } bool hasBuildings() { return (upgradelevel != 0); } void sellBuildings(int numbuildings) { upgradelevel = upgradelevel - numbuildings; } void buyBuildings(int numbuildings) { upgradelevel = upgradelevel + numbuildings; } void clearBuildings() { upgradelevel = 0; } } class RailRoad extends Space { int rent; int purchasecost; int mortgagevalue; void initSpace(string name) { this.name = name; owner = null; mortgaged = false; type = "railroad"; purchasecost = 200; mortgagevalue = 100; } int getMortgageValue() { return mortgagevalue; } void Landed(Player player) { Print(player.getName(), " has landed on ", getName(), "\n"); if(owner == null) { Print(getName(), " is up for sale. Do you want to buy for ", purchasecost, "? (y / n)\n"); if(getResponseYesNo()) { if(player.getMoney(purchasecost, true)) { player.getProperty(getBoardIndex()); } } else { Print(player.getName(), " pays $50 to bank for rent.\n"); if(owner != null) owner.getPayment(50); } } else if(owner != player) //player needs to pay owner rent { rent = owner.getNumRailroads() * 50; player.getMoney(rent, false); Print(player.getName(), " pays ", rent, " to ", owner.getName(), " for rent.\n"); if(owner != null) owner.getPayment(rent); } } } class GoSpace extends Space { void Landed(Player player) { Print(player.getName(), " has landed on ", getName(), "\n"); Print(player.getName(), " receives additional $200 for landing directly on Go square\n"); player.getPayment(200); } } class Chance extends Space { void Landed(Player player) { Print(player.getName(), " has landed on ", getName(), "\n"); getChance(player); } void getChance(Player player) { int randomchance; randomchance = gRnd.RndInt(10); if(randomchance == 0) { Print("Visit St. Charles Place\n"); player.jumpTo(11); } else if(randomchance == 1) { Print("Advance to Go\n"); player.jumpTo(0); } else if(randomchance == 2) { Print("Pay school tax of $150\n"); player.getMoney(150, false); } else if(randomchance == 3) { Print("Your XMAS Fund matures, collect $100\n"); player.getPayment(100); } else if(randomchance == 4) { Print("Take a walk on the Boardwalk\n"); player.jumpTo(39); } else if(randomchance == 5) { Print("Your building and loan matures, collect $150\n"); player.getPayment(150); } else if(randomchance == 6) { Print("Parking fine, pay $15\n"); player.getMoney(15, false); } else if(randomchance == 7) { Print("Pay poor tax of $12\n"); player.getMoney(12, false); } else if(randomchance == 8) { Print("Bank pays you dividend of $50\n"); player.getPayment(50); } else if(randomchance == 9) { Print("Call at Illinois Ave\n"); player.jumpTo(24); } else { Print("Take a ride on the Reading\n"); player.jumpTo(5); } } } class CommunityChest extends Space { void Landed(Player player) { Print(player.getName(), " has landed on ", getName(), "\n"); getCommunity(player); } void getCommunity(Player player) { int randomchance; randomchance = gRnd.RndInt(9); if(randomchance == 0) { Print("Pay hospital $100\n"); player.getMoney(100, false); } else if(randomchance == 1) { Print("Pay doctor's fee of $50\n"); player.getMoney(50, false); } else if(randomchance == 2) { Print("Pay insurance premium of $50\n"); player.getMoney(50, false); } else if(randomchance == 3) { Print("You win second prize in beauty contest, collect $11\n"); player.getPayment(11); } else if(randomchance == 4) { Print("Go To Jail\n"); player.goToJail(); } else if(randomchance == 5) { Print("Bank error in your favor, collect $200\n"); player.getPayment(200); } else if(randomchance == 6) { Print("Sale of stock, collect $45\n"); player.getPayment(45); } else if(randomchance == 7) { Print("You inherit $100\n"); player.getPayment(100); } else if(randomchance == 8) { Print("We're off the gold standard, collect $50\n"); player.getPayment(50); } else if(randomchance == 9) { Print("Income tax refund, collect $20\n"); player.getPayment(20); } else { Print("Life insurance matures, collect $100\n"); player.getPayment(100); } } } class FreeParking extends Space { void Landed(Player player) { Print(player.getName(), " has landed on ", getName(), "\n"); Print(player.getName(), " receives $500."); player.getPayment(500); } } class IncomeTax extends Space { void Landed(Player player) { Print(player.getName(), " has landed on ", getName(), "\n"); Print("Press 'y' to pay $200 or any other key to pay 10% instead.\n"); if(getResponseYesNo()) { player.getMoney(200, false); } else player.getMoney((player.getCash() / 10), false); } } class Jail extends Space { void Landed(Player player) { Print(player.getName(), " has landed on ", getName(), "\n"); } } class GoToJail extends Space { void Landed(Player player) { Print(player.getName(), " has landed on ", getName(), "\n"); player.goToJail(); } } class Utility extends Space { int rent; int purchasecost; int mortgagevalue; void initSpace(string name) { this.name = name; owner = null; mortgaged = false; type = "utility"; purchasecost = 150; mortgagevalue = 75; } int getMortgageValue() { return mortgagevalue; } void Landed(Player player) { int mustpay; Print(player.getName(), " has landed on ", getName(), "\n"); if(owner == null) { Print(getName(), " is up for sale. Do you want to buy for ", purchasecost, "? (y / n)\n"); if(getResponseYesNo()) { if(player.getMoney(purchasecost, true)) { player.getProperty(getBoardIndex()); return; } Print(player.getName(), " pays $", purchasecost, " to bank for rent.\n"); } } else { if(getOwner().hasBothUtilities()) { mustpay = 10 * gRnd.getLastRoll(); Print("Owner has both utilities.\n"); } else { mustpay = 4 * gRnd.getLastRoll(); Print("Owner has one utility.\n"); } Print("You owe owner of utility $", mustpay, ".\n"); player.getMoney(mustpay, false); } } } class LuxuryTax extends Space { void Landed(Player player) { Print(player.getName(), " has landed on ", getName(), "\n"); Print(player.getName(), " pay $75 luxury tax\n"); player.getMoney(75, false); } } class Player { string name; int cash; Space[] properities; int space; int i; int jailroll; bool alive; void initPlayer(string name, int money) { this.name = name; this.cash = money; this.space = 0; properities = NewArray(40, Space); jailroll = 0; alive = true; for(i = 0; i < 40; i = i + 1) { properities[i] = null; } } bool isAlive() { return alive; } void setAlive(bool value) { alive = value; } void jumpTo(int index) { space = index; spaces[space].Landed(this); } bool hasBothUtilities() { if(properities[28] == null) return false; if(properities[12] == null) return false; return true; } void jailBreak() { int selection; while(true) { Print(getName(), " is currently in jail.\n"); Print("Enter 1 to pay $50 and leave, 2 to attempt to escape, 3 to stay.\n"); selection = ReadInteger(); if(selection == 1) { getMoney(50, false); jailroll = 0; makeMove(); return; } if(selection == 2) { int roll1; int roll2; int movespaces; Print("rolling die: "); roll1 = rollDice(); Print(roll1, "\n"); Print("rolling die: "); roll2 = rollDice(); Print(roll2, "\n"); if(roll1 == roll2) { Print("Rolled doubles, escape successful\n"); movespaces = roll1 + roll2; space = space + movespaces; spaces[space].Landed(this); } else { if(jailroll == 4) { Print("Your term in jail has expired, you must leave, pay $50\n"); getMoney(50, false); jailroll = 0; makeMove(); } else jailroll = jailroll + 1; } return; } else if(selection == 3) { if(jailroll == 4) { Print("Your term in jail has expired, you must leave, pay $50\n"); getMoney(50, false); jailroll = 0; makeMove(); } Print("You have chosen to wait in jail.\n"); jailroll = jailroll + 1; return; } Print("Input not understood, please try again.\n"); } } void goToJail() { jailroll = 1; jumpTo(10); } int getCash() { return cash; } bool ownsProperty(int index) { if(properities[index] != null) { return true; } return false; } string getName() { return name; } void move() { if(!alive) return; //player may no longer be playing Print("It's ", this.getName(), "'s turn.\n"); printPlayerStats(); Print("Would you like to make a change before proceeding? (y / n)"); if(getResponseYesNo()) makeChange(); if(!isAlive()) return; //player may have just quit the game if(jailroll == 0) makeMove(); else jailBreak(); if(rolledDoubles()) { Print(getName(), " rolled doubles, move again."); move(); } } void printPlayerStats() { Print("\n", getName(), " has $", cash, ".\n"); Print("\n", "bank owns the following properities (excludes Railroads and Utilities): \n"); printBankProperities(false); Print("\n", getName(), " owns the following unmortgaged properities:\n"); printUnMortgagedAssets(false); Print("\n", getName(), " owns the following mortgaged properities:\n"); printMortgagedAssets(false); Print("\n\n"); } void printBankProperities(bool numbered) { int i; for(i = 0; i < realestate.length(); i = i + 1) { if(realestate[i] != null) { if(realestate[i].ownedBybank()) { if(numbered) { Print(i, ". "); } Print(realestate[i].getName(), "\n"); } } } } void printUnMortgagedAssets(bool numbered) { int i; for(i = 0; i < properities.length(); i = i + 1) { if(properities[i] != null) { if(!properities[i].isMortgaged()) { if(numbered) { Print(i, ". "); } Print(properities[i].getName(), "\t\t"); if(realestate[i] != null) { Print("(", realestate[i].getNumBuildings(), " Buildings)\t\t"); Print("(Color = ", realestate[i].getColor(), ")\n"); } else { Print("\n"); } } } } } void printMortgagedAssets(bool numbered) { int i; for(i = 0; i < properities.length(); i = i + 1) { if(properities[i] != null) { if(properities[i].isMortgaged()) { if(numbered) { Print(i, ". "); } Print(properities[i].getName(), "\t\t"); if(realestate[i] != null) { Print("(", realestate[i].getNumBuildings(), " Buildings)\t\t"); Print("(Color = ", realestate[i].getColor(), ")\n"); } else { Print("\n"); } } } } } void makeChange() { int choice; while(true) { Print("Select a change: \n"); Print("1. Mortgage Property \n"); Print("2. Unmortgage Property \n"); Print("3. Sell Buildings \n"); Print("4. Buy Buildings \n"); Print("5. Trade Property \n"); Print("6. Done making changes \n"); Print("7. Quit Game\n"); choice = ReadInteger(); if(choice < 0 || choice > 7) { Print("Enter valid choice, try again.\n"); } else if(choice == 1) { MortgageSomething(); } else if(choice == 2) { unMortgageSomething(); } else if(choice == 3) { SellSomething(); } else if(choice == 4) { BuySomething(); } else if(choice == 5) { TradeProperities(); } else if(choice == 6) { return; } else if(choice == 7) { Print("Are you sure you want to quit (y / n)\n"); if(getResponseYesNo()) { QuitGame(); return; } } } } void makeMove() { int roll1; int roll2; int movespaces; Print("rolling die: "); roll1 = rollDice(); Print(roll1, "\n"); Print("rolling die: "); roll2 = rollDice(); Print(roll2, "\n"); movespaces = roll1 + roll2; if(space + movespaces > 39) { Print("Collect $200 for passing Go!\n"); cash = cash + 200; space = (space + movespaces) % 39; space = space - 1; //go back one space...counting from 0 } else { space = space + movespaces; } spaces[space].Landed(this); } void getProperty(int index) { properities[index] = spaces[index]; properities[index].setOwner(this); } void loseProperty(int index) { properities[index].setOwner(null); properities[index] = null; } bool getMoney(int cost, bool optional) { if(cost < cash) { cash = cash - cost; } else { while(cost > cash) { if(optional) { Print("To purchase property, you need $", (cost - cash), ". Still interested?\n"); if(!getResponseYesNo()) return false; } raiseFunds(cost - cash); if(!isAlive()) return true; if(cash > cost) { cash = cash - cost; return true; } } } return true; } void raiseFunds(int raise) { int response; Print(getName(), ", you need to raise $", raise, ".\n"); Print("How would you like to raise money?\nEnter 1 to Mortgage\nEnter 2 to Sell Buildings\nEnter 3 to Quit Game\n"); response = ReadInteger(); if(response == 1) MortgageSomething(); if(response == 2) SellSomething(); if(response == 3) QuitGame(); } void QuitGame() { int i; Print(getName(), " has chosen to quit. Bank resolves all debts and collects assets.\n"); for(i = 0; i < properities.length(); i = i + 1) { if(properities[i] != null) { properities[i].setOwner(null); if(realestate[i] != null) { realestate[i].clearBuildings(); } } } setAlive(false); if(numAlivePlayers() < 2) { Print("Game over. "); printRemainingPlayer(); Print(" wins.\n"); } } void getPayment(int money) { cash = cash + money; } void MortgageSomething() { int i; Print("The following properities can be mortgaged: \n"); for(i = 0; i < properities.length(); i = i + 1) { if(properities[i] != null) { if(!properities[i].isMortgaged()) { Print(i, ". ", properities[i].getName(), "\n"); } } } Print("Enter number corresponding to property to mortgage\n"); i = ReadInteger(); if(!(i > -1 && i < 40)) { Print("Invalid selection\n"); } else if(properities[i] == null) { Print("You must own property to be mortgaged.\n"); } else if(!properities[i].canMortgage(i)) { Print("Either ", properities[i].getName(), " or other properities in the monopoly have buildings\n"); Print("which must be sold before property can be mortgaged\n"); } else { if(properities[i].isMortgaged()) { Print("Property already mortgaged\n"); } else { properities[i].setMortgaged(true); cash = cash + properities[i].getMortgageValue(); Print(properities[i].getName(), " has been mortgaged.\n"); } } } void unMortgageSomething() { int i; int costtounmortgage; Print("The following properities can be unmortgaged: \n"); Print("Cost to unmortgage is the mortgage value + 10% interest\n"); for(i = 0; i < properities.length(); i = i + 1) { if(properities[i] != null) { if(properities[i].isMortgaged()) { Print(i, ". ", properities[i].getName(), "\n"); } } } Print("Enter number corresponding to property to unmortgage\n"); i = ReadInteger(); if(!(i > -1 && i < 40)) { Print("Invalid selection\n"); } else if(properities[i] == null) { Print("You must own property to be unmortgaged.\n"); } else { costtounmortgage = properities[i].getMortgageValue(); costtounmortgage = costtounmortgage + costtounmortgage / 10; if(!properities[i].isMortgaged()) { Print("Property already unmortgaged\n"); } else if(cash > costtounmortgage) { cash = cash - costtounmortgage; properities[i].setMortgaged(false); Print(properities[i].getName(), " has been unmortgaged.\n"); } else { Print("Insufficient funds to unmortgage, cash needed = $", costtounmortgage, "\n"); } } } void SellSomething() { int i; int buildingstosell; int moneyraised; Print("The following real estate has development which can be sold: \n"); for(i = 0; i < properities.length(); i = i + 1) { //make sure index refers to real estate rather than a railroad or utility //also make sure that players owns it if(properities[i] != null && realestate[i] != null) { if(realestate[i].getNumBuildings() > 0) { Print(i, ". ", properities[i].getName(), "\n"); } } } Print("Enter number corresponding to property development to sell\n"); i = ReadInteger(); if(!(i > -1 && i < 40)) { Print("Invalid selection.\n"); } else if(realestate[i] == null) { Print("Property not owned.\n"); } else if(realestate[i].getOwner() != this) { Print(getName(), " does not own this property.\n"); } else { if(realestate[i].getNumBuildings() > 0) { while(true) { Print("Property here is worth $", realestate[i].getUpgradecost(), " per building.\n"); Print("There are ", realestate[i].getNumBuildings(), " buildings on the property.\n"); Print("How many buildings would you like to sell?\n"); buildingstosell = ReadInteger(); if(buildingstosell > -1 && buildingstosell < realestate[i].getNumBuildings()+1) { realestate[i].sellBuildings(buildingstosell); moneyraised = buildingstosell * realestate[i].getUpgradecost(); moneyraised = moneyraised; Print("Sold ", buildingstosell, " buildings on ", realestate[i].getName(), ".\n"); Print("Raised $", moneyraised, ".\n"); cash = cash + moneyraised; return; } else Print("Invalid entry, try again.\n"); } } else { Print("Selected real estate has no buildings.\n"); } } } void BuySomething() { int i; int buildingstobuild; int costofpurchase; int moneyconsumed; Print("The following real estate has land which can be developed (must have monopoly): \n"); for(i = 0; i < properities.length(); i = i + 1) { //make sure index refers to real estate rather than a railroad or utility //also make sure that players owns it if(properities[i] != null && realestate[i] != null) { if(realestate[i].getNumBuildings() < 5 && hasMonopoly(i) && availToBuild(i)) { Print(i, ". ", properities[i].getName(), "\t\t(Buildings: ", realestate[i].getNumBuildings(), ")\n"); } } } Print("Enter number corresponding to property to develop\n"); i = ReadInteger(); if(!(i > -1 && i < 40)) { Print("Invalid selection.\n"); } else if(realestate[i] == null) { Print("Property not owned.\n"); } else if(realestate[i].getOwner() != this) { Print(getName(), " does not own this property.\n"); } else { if(realestate[i].getNumBuildings() < 5) { if(!hasMonopoly(i)) { Print("You do not have a monopoly on ", realestate[i].getName(), ". You cannot develop this property.\n"); } while(true) { Print("Cost of upgrade is worth $", realestate[i].getUpgradecost(), " per building.\n"); Print("There are ", realestate[i].getNumBuildings(), " buildings on the property.\n"); Print("How many buildings would you like to build?\n"); buildingstobuild = ReadInteger(); if(buildingstobuild < 0) { Print("You cannot build less than 0 buildings, try again\n"); } else if((realestate[i].getNumBuildings() + buildingstobuild) > 5) { Print("Property cannot hold ", (realestate[i].getNumBuildings() + buildingstobuild), " buildings. Try again.\n"); } else if(buildingstobuild * realestate[i].getUpgradecost() > cash) { Print("Cost of requested upgrade is ", buildingstobuild * realestate[i].getUpgradecost(), ".\n"); Print("You do not have sufficient funds for the requested development. Raise more money, then try again.\n"); } else if(!availToBuild(i)) { Print("This property or another property in the group is mortgaged.\n"); Print("You must unmortgage the property before building.\n"); return; } else { realestate[i].buyBuildings(buildingstobuild); moneyconsumed = buildingstobuild * realestate[i].getUpgradecost(); Print("Constructed ", buildingstobuild, " buildings on ", realestate[i].getName(), ".\n"); Print("Cost $", moneyconsumed, ".\n"); cash = cash - moneyconsumed; return; } } } else { Print("Selected real estate is already fully developed.\n"); } } } void initPropArr(bool[] arr) { int i; for(i = 0; i < arr.length(); i = i + 1) { arr[i] = false; } } void TradeProperities() { int i; int playertotradewith; int selection; bool[] playerprops; bool[] opponentprops; //init arrays playerprops = NewArray(40, bool); opponentprops = NewArray(40, bool); initPropArr(playerprops); initPropArr(opponentprops); for(i = 0; i < players.length(); i = i + 1) { Print(players[i].getName(), " has the following properities:\n\n"); players[i].printPlayerStats(); } Print("Which player would you like to trade with? (Enter corresponding integer)\n"); for(i = 0; i < players.length(); i = i + 1) { if(players[i] != this) { Print(i, ". ", players[i].getName(), "\n"); } } playertotradewith = ReadInteger(); if(!(playertotradewith > -1 && playertotradewith < players.length())) { Print("Invalid number, try again.\n"); return; } if(players[playertotradewith] == this) { Print("Invalid number, try again.\n"); return; } //otherwise, user entered a valid number Print("You have the following properities available for trade: \n"); printUnMortgagedAssets(true); printMortgagedAssets(true); Print("\n\n", players[playertotradewith].getName(), " has the following properities available for trade: \n"); players[playertotradewith].printUnMortgagedAssets(true); players[playertotradewith].printMortgagedAssets(true); Print("Enter the numbers corresponding to your properities that you are willing to trade with ", players[playertotradewith].getName(), ". (Enter one at a time, enter -1 when finished.)\n"); selection = ReadInteger(); while(selection != -1) { if(selection < 0 || selection > 39) { Print("Selection invalid, try again.\n"); } else if(properities[selection] == null) { Print("Your selection is: ", selection, "\n"); Print("You do not own this property. Enter another integer.\n"); } else { playerprops[selection] = true; Print("Accepted, enter next integer or -1 to finish.\n"); } selection = ReadInteger(); } Print("Enter the numbers corresponding to your properities that you wish to receive from ", players[playertotradewith].getName(), ". (Enter one at a time, enter -1 when finished.)\n"); selection = ReadInteger(); while(selection != -1) { if(selection < 0 || selection > 39) { Print("Selection invalid, try again.\n"); } else if(!players[playertotradewith].ownsProperty(selection)) { Print(players[playertotradewith].getName(), " does not own this property.\nEnter another choice."); } else { opponentprops[selection] = true; Print("Accepted, enter next integer or -1 to finish.\n"); } selection = ReadInteger(); } Print("So you wish to give these properities: \n"); for(i = 0; i < playerprops.length(); i = i + 1) { if(playerprops[i] == true) { Print(spaces[i].getName(), "\n"); } } Print("In exchange for these properities: \n"); for(i = 0; i < opponentprops.length(); i = i + 1) { if(opponentprops[i] == true) { Print(spaces[i].getName(), "\n"); } } Print("Is this correct?\n"); if(!getResponseYesNo()) { Print("Ok, aborting the trade...\n"); return; } Print(players[playertotradewith].getName(), ", do you accept this offer?\n"); if(getResponseYesNo()) { for(i = 0; i < opponentprops.length(); i = i + 1) { if(opponentprops[i]) { players[playertotradewith].loseProperty(i); getProperty(i); } if(playerprops[i]) { loseProperty(i); players[playertotradewith].getProperty(i); } } } } bool hasMonopoly(int index) { if(index == 1 || index == 3) { if(properities[1] != null && properities[3] != null) { return true; //does player have Mediterranean and Baltic? } } else if(index == 6 || index == 8 || index == 9) { if(properities[6] != null && properities[8] != null && properities[9] != null) { return true; //does player have Oriental, Vermont, and Connecticut? } } else if(index == 6 || index == 8 || index == 9) { if(properities[6] != null && properities[8] != null && properities[9] != null) { return true; //does player have Oriental, Vermont, and Connecticut? } } else if(index == 11 || index == 13 || index == 14) { if(properities[11] != null && properities[13] != null && properities[14] != null) { return true; //does player have St. Charles, States, and Virginia? } } else if(index == 16 || index == 18 || index == 19) { if(properities[16] != null && properities[18] != null && properities[19] != null) { return true; //does player have St. James, Tennessee, and New York? } } else if(index == 21 || index == 23 || index == 24) { if(properities[21] != null && properities[23] != null && properities[24] != null) { return true; //does player have Kentucky, Indiana, and Illinois? } } else if(index == 26 || index == 27 || index == 29) { if(properities[26] != null && properities[27] != null && properities[29] != null) { return true; //does player have Atlantic, Ventnor, and Marvin Gardens? } } else if(index == 31 || index == 32 || index == 34) { if(properities[31] != null && properities[32] != null && properities[34] != null) { return true; //does player have Pacific, North Carolina, and Pennsylvania? } } else if(index == 37 || index == 39) { if(properities[37] != null && properities[39] != null) { return true; //does player have Park Place and Boardwalk? } } return false; //otherwise, return false } bool availToBuild(int index) { if(index == 1 || index == 3) { if(properities[1] != null && properities[3] != null) { if(!properities[1].isMortgaged() && !properities[3].isMortgaged()) { return true; //are Mediterranean and Baltic? unmortgaged } } } else if(index == 6 || index == 8 || index == 9) { if(properities[6] != null && properities[8] != null && properities[9] != null) { if(!properities[6].isMortgaged() && !properities[8].isMortgaged() && !properities[9].isMortgaged()) { return true; //are Oriental, Vermont, and Connecticut unmortgaged } } } else if(index == 11 || index == 13 || index == 14) { if(properities[11] != null && properities[13] != null && properities[14] != null) { if(!properities[11].isMortgaged() && !properities[13].isMortgaged() && !properities[14].isMortgaged()) { return true; //are St. Charles, States, and Virginia unmortgaged } } } else if(index == 16 || index == 18 || index == 19) { if(properities[16] != null && properities[18] != null && properities[19] != null) { if(!properities[16].isMortgaged() && !properities[18].isMortgaged() && !properities[19].isMortgaged()) { return true; //are St. James, Tennessee, and New York unmortgaged } } } else if(index == 21 || index == 23 || index == 24) { if(properities[21] != null && properities[23] != null && properities[24] != null) { if(!properities[21].isMortgaged() && !properities[23].isMortgaged() && !properities[24].isMortgaged()) { return true; //are Kentucky, Indiana, and Illinois unmortgaged } } } else if(index == 26 || index == 27 || index == 29) { if(properities[26] != null && properities[27] != null && properities[29] != null) { if(!properities[26].isMortgaged() && !properities[27].isMortgaged() && !properities[29].isMortgaged()) { return true; //are Atlantic, Ventnor, and Marvin Gardens unmortgaged } } } else if(index == 31 || index == 32 || index == 34) { if(properities[31] != null && properities[32] != null && properities[34] != null) { if(!properities[31].isMortgaged() && !properities[32].isMortgaged() && !properities[34].isMortgaged()) { return true; //are Pacific, North Carolina, and Pennsylvania unmortgaged } } } else if(index == 37 || index == 39) { if(properities[37] != null && properities[39] != null) { if(!properities[37].isMortgaged() && !properities[39].isMortgaged()) { return true; //are Park Place and Boardwalk unmortgaged } } } return false; //otherwise, return false } int getNumRailroads() { int num; num = 0; if(properities[5] != null) num = num + 1; if(properities[15] != null) num = num + 1; if(properities[25] != null) num = num + 1; if(properities[35] != null) num = num + 1; return num; } } class rndModule { int seed; int last; int secondlast; void Init(int seedVal) { seed = seedVal; } int Random() { seed = (15625 * (seed % 10000) + 22221) % 65536; return seed; } int RndInt(int max) { int result; result = Random() % max; secondlast = last; last = result; return result; } int getLastRoll() { return last + secondlast + 2; } bool rolledDbls() { if(last == secondlast) return true; return false; } } int rollDice() { return gRnd.RndInt(6) + 1; } bool rolledDoubles() { return gRnd.rolledDbls(); } bool getResponseYesNo() { string response; response = ReadLine(); if(response == "y" || response == "Y" || response == "yes" || response == "Yes") { return true; } return false; }