Last active
April 19, 2016 22:54
-
-
Save lilrooness/a4117e3ca22bad0cc78043465ea9c864 to your computer and use it in GitHub Desktop.
Prototype for commander main gameplay loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def ALERT(message): | |
| print("ALERT: " + message) | |
| class Game: | |
| def __init__(self): | |
| self.state = { | |
| "buildQueue": [], | |
| "steel": 100, | |
| "credits": -100000, | |
| "turnsLeft": 100, | |
| "ships": [], | |
| "dryDocks": 0 | |
| } | |
| def runCommand(self, command): | |
| parts = command.split(" "); | |
| if len(parts) == 2: | |
| { | |
| "build" : {"dock" : lambda : self.changeState("buildQueue", self.state["buildQueue"] + [DryDock()], DryDock.CostMap)}, | |
| "end" : {"turn" : lambda : self.endTurn()} | |
| }[parts[0]][parts[1]]() | |
| def endTurn(self): | |
| if len(self.state["buildQueue"]) > 0: | |
| self.changeState("buildQueue", self.state["buildQueue"][1:], {}) | |
| self.changeState("dryDocks", self.state["dryDocks"] + 1, {}) | |
| self.changeState("turnsLeft", self.state["turnsLeft"] - 1, {}) | |
| def changeState(self, field, n, costMap): | |
| if self.isBuildable(costMap): | |
| self.state[field] = n | |
| for key in costMap: | |
| self.changeState(key, self.state[key] - costMap[key], {}) | |
| else: | |
| ALERT("Cannot afford to do this") | |
| def isBuildable(self, costMap): | |
| for key in costMap: | |
| if self.state[key] < costMap[key]: | |
| return False | |
| return True | |
| def printState(self): | |
| print("Turns Left: " + str(self.state["turnsLeft"])) | |
| print("Dry Docks: " + str(self.state["dryDocks"])) | |
| print("Steel: " + str(self.state["steel"])) | |
| print("Credits: " + str(self.state["credits"])) | |
| print("Ships: " + str(len(self.state["ships"]))) | |
| print("Build Queue: " + str(self.state["buildQueue"])) | |
| class DryDock: | |
| Name = "" | |
| CostMap = {"steel": 100} | |
| def __init__(self): | |
| self.buildQueue = [] | |
| def __str__(self): | |
| return DryDock.Name | |
| class Ship: | |
| def __init__(self, fuel, capacity): | |
| self.fuel = fuel | |
| self.capacity = capacity | |
| class Corvette (Ship): | |
| CostMap = {"steel": 10} | |
| def __init__(self): | |
| super(Corvette, self).__init__(50, 10); | |
| if __name__ == "__main__": | |
| game = Game() | |
| running = True | |
| while running: | |
| game.printState() | |
| input = raw_input("commander> ") | |
| game.runCommand(input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment