POSTS
Guess the Number, Part 1 - Hello, World!
Guess the Number is a very simple game where you try to guess the number that the opponent is thinking of. The opponent will then tell you whether your guess was bigger or smaller than the actual answer.
In this project, we will create a Guess the Number game where both you and the computer can take turns coming up with the number. Here’s what the game will look like after it’s done:
I've thought of a number between 0 to 99... can you guess it?
> 45
Bigger!
> 78
Smaller!
> 66
You got it!
Along the way, you’ll create your first Clojure project and learn about functions, input/output, and control flow using looping and conditional constructs.
Creating a Clojure project
Open your command line, change to the directory you want to create a project in, and type:
lein new guessnumber
This will create a project under the folder name “guessnumber”. Explore the project folder, and you’ll see that leiningen has already created a directory structure for you:
guessnumber
|-- README.md
|-- doc
| `-- intro.md
|-- project.clj
|-- resources
|-- src
| `-- guessnumber
| `-- core.clj
`-- test
`-- guessnumber
`-- core_test.clj
Open core.clj
, which is in the src
folder, using your editor. Within core.clj
, you’ll see the following code:
(ns guessnumber.core)
(defn foo
"I don't do a whole lot."
[x]
(println x "Hello, World!"))
Delete the foo
function (it’s just a placeholder). After you’re done, the only line left in the file should be (ns guessnumber.core)
. Now you’re ready to write some code!
Creating the main method
Modify the core.clj
file until it looks like the following:
(ns guessnumber.core)
(defn -main []
(println "Hello, World!"))
Save all changes to core.clj
.
Running the program
Go back to the command line, make sure you’re in the guessnumber
directory, then type the following command:
lein run -m guessnumber.core
You’ll see the following output:
Hello, World!
Congratulations on running your first Clojure program!
What just happened?
First, here’s how to read a Clojure statement:
(foo 1 2 "x")
All Clojure code is encased in round brackets. Whenever a pair of round brackets are encountered, Clojure will treat the first element as a function name. It will execute that function while passing in the remaining elements within the brackets as arguments. So, the code above executes a function called foo
, and passes in three parameters: 1, 2, and “x”. This is the smallest element of code in Clojure, and we call this a form.
Now, let’s take a look at the program again:
(ns guessnumber.core)
(defn -main []
(println "Hello, World!"))
On line 1, (ns guessnumber.core)
, we are declaring that all the code in this file will be stored in the guessnumber.core
namespace. Namespaces are like address “prefixes” which Clojure uses to locate your code. In the future, we will be using the ns
declaration to import code from other libraries so that they can be used within your program.
On lines 3 and 4, we are declaring a function called -main
. In Clojure, it is allowed (see the section on symbols) to name a function with a dash in front. The round brackets surrounding defn
indicate the start and end of the function (once again, this is called a form).
On line 3 next to -main
, there is a pair of empty square brackets. This indicates that the main function does not take in any parameters. An example of a function that takes in two parameters (and does nothing) would be: (defn myfunction [a b])
.
Within the -main
function, we have a single form: (println "Hello, World!")
. The println function simply prints any given parameters to the console.
Next time
Although we haven’t starting writing out our game logic yet, we’ve learned some important basics: creating and editing Clojure, and how to read simple Clojure code.
Next time, we’ll cover how to receive input from the user, and allow the player to decide on a number for our computer to guess.
-
clojure