Instructions for The Dining Philosophers Game

Below I've written a primer on just enough Javascript. Or, you can skip to the API

Just enough Javascript

The dining philosophers widget uses pre-made objects to allow anyone to experiment with solutions to this classic puzzle. Here on this single page I've included enough javascript for anyone to try writing their own solutions, and I hope you will! Who knows, maybe you'll realize how easy programming can be, and finally try something you'd wished to.

In Javascript we have objects, and those objects can have different functions, or behaviors associated with them.

To have your philosopher perform one of their functions, you will type something like this:

philo.pickUpFork("Right")

As you can see, you start by typing "philo", type a ".", then use the name of the function you'd like to perform. In this case, we're asking our philosopher to pick up his right fork.

Functions always have a pair of parentheses after their names when they're being asked to do things, and these parentheses can include special parameters or arguments. Those are fancy words for input. For example, the previous function can be used to pick up the "Right" or "Left" fork, depending on which is typed in its parentheses. Don't forget those quotation marks!

Some functions are like asking questions of the object (or philosopher), and can be used in conditional (if) statements! For example, you could write:

if(philo.holdingFork("Left")){
  philo.putDownFork("Left")
}

In this example, the function putDownFork() is wrapped up in an "if" statement. Without any functions, an if statement looks like this:

if(){}

It looks pretty lonely like this, but you can plainly see that the special if function has both parentheses after it, which takes a true or false statement. Then, depending on whether the statement was true or false, the program will execute the code between the curly braces.

An if statement can take all types of true/false statements. Sometimes you want to compare two numbers! For example, this statement will always equal true or false.

philo.lifeRemaining() < 50

Here we've made a statement that checks if the philosopher's life is less than 50. You might recognize the less-than symbol (<) from math class, and it's no different here! In fact, there are several such "operators" we can use to make true or false statements:

< Less Than
> Greater Than Than
<= Less Than or Equal To
>= Greater Than or Equal To
=== Equal To
!== Not Equal To

Also, an exclaimation mark ("!") before a true/false statement makes it the opposite. For example:

if(!philo.isEating()){
  philo.pickUpFork("Left");
}

Translates to "If the philosopher is not eating, pick up the left fork."

Well, that should be enough to get started! Below I've got the API, which is an Application Programmer's Interface. It's like a programmer's dictionary. It tells you all the functions you need to know to use this particular program.

The API

All of these functions belong to the "philo" object, which means you use it by typing "philo." and then the function name.

pickUpFork("Left"/"Right")

Will pick up the fork to the right or left of the philosopher, if it's available.

putDownFork("Left"/"Right")

Will put down the specified fork if the philosopher is holding it.

isForkOnTable("Left"/"Right")

Equals a "true" or "false" depending on whether the specified fork is on the table, available to be picked up.

holdingFork("Left"/"Right")

Equals a "true" or "false", depending on whether the specified fork is being held, making eating possible (if both are held), and available to be put down.

isEating()

Equals "true" or "false", depending on whether the philosopher is holding two forks and is therefore able to fill up on pasta.

lifeRemaining()

Equals the amount of life this philosopher has, on a scale from 0 to 100. Once the life equals 0, the philosopher will die!