Skip to main content

more options

Example 2: A dart game!

In this example we describe the implementation of an interactive dart game played on a board shown in the figure below. The game allows two players to taken turns in throwing darts at the board, by means of click of the mouse, and keep track of their scores.

a dart board

The board has several zones drawn in different colors: a center, four spots near the border, at the North, West, South and East ends, in red; and four rings, each divided into four sub-zones, North and South, in green and East and West in blue.

The following is the table of scores for each of the zones.

Zone North-South East - West
center 50
extremes 60 60
ring 1 30 20
ring 2 20 15
ring 3 15 10
ring 4 10 5

Rings are numbered in order of increasing radius. A dart reaching a white zone gets 0 points!

Our first task was to write a function makeBoardDesign that generates the previous design and stores it in a matrix, where different colors are codified as numbers. We first wrote a version of this function that, starting with 21-by-21 matrix of 0's, sets the color (number) of every contiguous set of zones. Each of these is set with a separate statement. You can see the code of the first version here. We then wrote a second, slicker version, that makes use of loops and takes advantage of the regularity of the dart board and draws all the rings using a for loop. It also takes advantage of MATLAB's more advanced indexing abilities. You can see the second version here.

Our second task was to write a function makeScoreBoard to generate a matrix enconding the scoring information for the different zones. Since the scoring pattern is based on the color design patter, we used the code from the last version of the makeBoardDesign function as a basis for the new function and modified it at a few places. The main change was to add two vectors containing the scores for each of the rings (i.e. the columns from the table above). These are then used inside the for-loop that sets the rings' scores.

Thirdly, we implemented a simple graphics function drawBoard to actually draw the board on a figure window, given a design matrix D as input. This function clears the figure window and then goes over each of the components of D, in turn, coloring square zones of the figure window according to the value of these components. The drawing of each zones is done by means of subfunction drawZone at the end of the file. This subfunction uses the MATLAB built in graphics function fill(x,y,c) that, given two vectors x and y of the same size, draws a filled polygon with vertices at (x1,y1), (x2,y2), ...(xn,yn). The polygon is filled with color c.

The interactive part of the game is implemented in function playTurn(p,n,S) . This function lets one player play one turn. The turn ends when the player has thrown n darts or after she throws one dart outside of the dart board. Dart throwing is simulated by means of function throwDart . This function gets the intended position for the dart by means of a call to MATLAB's built in function ginput (for graphical input) that returns the coordinates of a click of the user. To make the game exciting, thes coordinates are perturbed by adding random perturbations to them, trying to simulate factors such as air turbulence, popr aiming skills, etc, that might affect the final dart position. After the final dart position are returned by throwDart, function playTurn rounds these to integers and then uses them to look the score up in score matrix S obtained by the dart.

Finally,the driver script that puts all these pieces together and runs a complete game is dartGame . This script calls functions makeBoardDesign and makeScoreBoard to construct the board design and score board matrices, respectively. Then it runs into a loop that simulates a turn in each iteration. It sets up a title informing the players about whose turn it is and how many points each player has scored so far. Then it calls playTurn , to let the current player throw three darts, and updates the score according to the score that this function returns. The loop stops when one of the players has scored at least 300 points. After the loop, i.e., when the game has ended, the title is changed to announce who has wan.