Eiffel Programming/Play A Game

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Would you Like to Play a Game?[edit | edit source]

Many books introduce a new computer language by presenting a small program that prints out a greeting, like "Hello, world!". The text then wanders off into other, unconnected examples. We're not going to do that. Instead, we will start with a program that is as simple as a "Hello world" example, and will build on this until we have a fully functional implementation of the game of Blackjack. Along the way, we will learn about the way Eiffel programs work, the ins and outs of Eiffel's syntax, and some useful notions about programming and software design.

Here we go:

 '''class''' BLACKJACK 
  '''create''' make 
 
 '''feature'''
  make '''is''' 
   '''do'''
    print ("Congratulations, you've won!%N") 
   '''end''' 
 '''end'''

Compile and run this program with your favorite Eiffel compiler, and it will write out:

 Congratulations, you've won!

Now we've written the worlds' most boring computer game. We'll change it to make it interesting pretty soon. First we will break down this program to see how it works.

To begin, computer programs are made of data and instructions for working on that data. In Eiffel, the data and instructions are called features. In a well-designed Eiffel program, related features are gathered together in classes.

In this version of the BLACKJACK program, we have only one feature, the collection of instructions called make. This feature is defined in lines four through seven, and makes up the guts of the program.