Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spaceship exercise completed in JavaScript #13

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!DOCTYPE HTML>
<html lang = "en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="spaceship.js"></script>
<title>Spaceships</title>
<meta charset = "UTF-8" />
</head>
<body>
<h1>Futuramalama</h1>
<h2>Welcome to Space! You must run your ship properly to survive!</h1>
<h3>
This means feeding your crew, making sure they don't die of thirst, doing some accounting,
making deliveries, and other interesting things.
</h3>
<table>
<tr>
<th>Status of Ship</th>
<th>Current Level</th>
</tr>
<tr>
<td>Thirst</td>
<td id="thirst"></td>
</tr>
<tr>
<td>Work</td>
<td id="work"></td>
</tr>
<tr>
<td>Horde</td>
<td id ="horde"></td>
</tr>
<tr>
<td>Hunger</td>
<td id="hunger"></td>
</tr>
</tr>
<td>Receipt</td>
<td id="receipts"></td>
</tr>

</table>

<h3>Score:</h3>
<span id="score">0</span>

<h2>Click any of the below buttons to take action on the ship</h2>

<button id="drink">Drink</button>
<button id="eat">Eat</button>
<button id="deliver">Deliver a package</button>
<button id="account">Do some accounting</button>
<button id="steal">Steal treasure</button>
129 changes: 129 additions & 0 deletions spaceship.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
var SpaceShip = function(start, end) {
this.thirst = start;
this.work = start;
this.horde = start;
this.hunger = start;
this.receipts = start;
this.stable = true;
this.end = end;
this.totalScore = 0;
}

SpaceShip.prototype.drink = function(){
this.thirst--;
this.work++;
};

SpaceShip.prototype.deliver = function(){
this.work--;
this.receipts ++;
};

SpaceShip.prototype.steal = function(){
this.horde++;
this.work++;
};

SpaceShip.prototype.eat = function(){
this.hunger--;
this.work++;
};

SpaceShip.prototype.account = function(){
this.receipts--;
this.horde--;
$("#receipts").text(crew.receipts);
};

SpaceShip.prototype.status = function(){
$("#thirst").text(this.thirst);
$("#work").text(this.work);
$("#horde").text(this.horde);
$("#hunger").text(this.hunger);
$("#receipts").text(this.receipts);
};

SpaceShip.prototype.score = function(){
if (this.totalScore < 0) {
alert("You lost! Try again.");
location.reload();
}

if (this.thirst > 70 && this.thirst < this.end) {
this.totalScore --;
} else if (this.thirst <= 70) {
this.totalScore ++;
} else if (this.thirst >= this.end) {
alert("Your crew got too thirsty! You lose - try again.");
location.reload();
}

if (this.work > 70) {
this.totalScore --;
} else if (this.work <= 70 && this.work < this.end) {
this.totalScore ++;
} else if (this.work >= this.end) {
alert("Your crew got too hungry! You lose - try again.");
location.reload();
}

if (this.receipts < 70) {
this.totalScore ++;
} else if (this.receipts >= 70 && this.receipts > this.end) {
this.totalScore --;
} else if (this.receipts > this.end) {
alert("Your crew got too behind on receipts! You lose - try again.");
location.reload();
}

if (this.horde < 70) {
this.totalScore ++;
} else if (this.horde >= 70 && this.horde > this.end) {
this.totalScore --;
} else if (this.horde > this.end) {
alert("You didn't find enough treasure! You lose - try again.");
location.reload();
}
$("#score").text(this.totalScore);
};

var crew = new SpaceShip(0, 3);

$(document).ready(function(){
crew.status();

$("#drink").click(function(){
crew.drink();
alert("You drank something!");
crew.score();
crew.status();
});

$("#eat").click(function(){
crew.eat();
alert("You ate something!");
crew.score();
crew.status();
});

$("#deliver").click(function(){
crew.deliver();
alert("You delivered something!");
crew.score();
crew.status();
});

$("#account").click(function(){
crew.account();
alert("You did some accounting!");
crew.score();
crew.status();
});

$("#steal").click(function(){
crew.steal();
alert("You stole some treasure!");
crew.score();
crew.status();
});
});
137 changes: 137 additions & 0 deletions spaceship.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
class Spaceship:

def __init__(self):
self.thirst = 50
self.work = 50
self.horde = 50
self.hunger = 50
self.receipts = 50
self.stable = True
self.total = 0

def drink(self):
self.thirst -= 10
self.work += 2

def deliver(self):
self.work += 8
self.receipts += 4

def steal(self):
self.horde += 10
self.work += 7

def eat(self):
self.hunger -= 10
self.work += 2

def account(self):
self.receipts -= 10
self.horde -= 10

def thirst_level(self):
if self.thirst < 50:
self.total += 7
elif self.thirst >= 60:
self.total -= 7

def horde_level(self):
if self.horde > 80:
self.total += 5
elif self.horde <= 80:
self.total -= 1

def work_level(self):
if self.work < 70:
self.total += 1
elif self.work >= 70:
self.total -= 1

def hunger_level(self):
if self.hunger > 60:
self.total -= 1
elif self.hunger <= 60:
self.total -= 1

def receipt_level(self):
if self.receipts > 70:
self.total += 3
elif self.receipts <= 70:
self.total -= 3

def total_score(self):
self.thirst_level
self.horde_level
self.work_level
self.receipt_level
self.hunger_level
return self.total

def stable(self):
if any( [self.thirst > 99, self.work > 99, self.horde < 1, self.hunger > 99, self.receipts < 1] ):
return False
else:
return True

def check(self):
self.alert()
if self.stable:
return True
else:
return False
print "You lose! Try again!"

def alert(self):
if any( [self.thirst > 90, self.work > 90, self.horde > 90, self.hunger > 90, self.receipts > 90] ):
print "Uh oh! Your ship is about to sink. Here's what you need to watch out for. Remember, if any value reaches 100, you're DOOMED!"
self.thirst
print "Current thirst level: "
print self.thirst
print "Current work level:"
print self.work
print "Current hording level: "
print self.horde
print "Current hunger level: "
print self.hunger
print "Current receipt level: "
print self.receipts

def playGame():
game = Spaceship()

print "All Aboard the Planet Express!"
print "What would you like to do?"
print "Keep track of your score by typing \"score\" at any time."
print "You can also type 'bye' at any time to leave this game :(."

stop = False

while stop != True:
print "What would you like to do aboard the ship?"
action = raw_input("You can type 'eat', 'drink', 'accounting', 'deliver', or 'steal'. ")
if action == "bye":
stop = True
print "Laterz."
else:
if action == "eat":
game.eat()
print "You ate something! Your belly is fuller, but it required work!"
elif action == "drink":
game.drink()
print "You drank something! You're less thirsty, but it required some work!"
elif action == "accounting":
game.account()
print "You did some accounting! Boring! That was a lot of work!"
elif action == "deliver":
print "You did some work for once! That required some work!"
elif action == "steal":
game.steal()
print "You stole some stuff! That's awkward."
elif action == "score":
print "Your total score is: "
print game.total_score()
else:
"I don't understand that command! Try again!"
game.check()

playGame()