COMP1511 18s1 (webcms)
COMP1511 18s1 (flask)

Objectives

  • creating functions
  • manipulating arrays
  • using a while loop for repetition

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples. You should also have read the lab assessment guidelines.

Getting Started

Create a new directory for this lab called lab05 by typing:
mkdir lab05
Change to this directory by typing:
cd lab05

Farnarkle

This lab revolves around the Scandinavian game of Farnarkle, which you will have discussed in the tutorial.

The standard exercises for this lab are to write functions which together form a program which allow a user to play Farnarkle.

The challenge exercises are to write a program which itself plays Farnarkle.

Farnarkle is thought to have been invented in Iceland during the Little Ice Age. It is played with numbered tiles, originally made from sections of Narwhal tusks. Each tile is inscribed with a single positive integer. Traditionally the integers on the tiles are from the range 1..8 (the number 8 is significant in Norse mythology).

In modern Farnarkle variants the integers on the tiles may be from the range 1..10, 1..12, or another range.

Farnarkle starts with with the Arkle Master choosing a hidden sequence of tiles. Traditionally this sequence is of 4 tiles, but there are modern variants with 5 or 8 tiles in this sequence.

The player then attempts to guess the hidden sequence in a series of turns. Each turn they lay out a sequence of tiles. The Arkle Master then gives them information about how well their sequence of tiles matches the hidden sequence of tiles.

This information is given as two integers: the number of Arkles and the number of Farnarkles.

A tile is counted as an Farnarkle if it has the same number as the tile in the same position in the hidden sequence.

A tile is counted as an Arkle if it has the same number as a tile in another position in the hidden sequence.

A tile that is counted as a Farnarkle is not counted in an Arkle.

A tile can only be counted once in an Arkle.

The scoring of this example Farnarkle game should clarify the definition of Farnarkles and Arkles.

Hidden Sequence4 2 7 7
Turn GuessFarnarklesArkles
11 2 3 411
23 5 5 800
34 3 2 111
47 7 7 720
57 6 5 402
67 7 4 204
74 2 7 740

Note: the game is complete when the player guess the hidden sequence.

Initial Code

Here is the code to start the lab.

Read this file carefully. You must use this file to start the assignment.

It contains a main function which will allow you to test the functions you write in the lab.

Do not change the main function, or the other functions in the file.

Add your code to this file in the indicated places.

Do not change other parts of the file.

Note you need to submit farnarkle.c for each exercise to obtain the marks for that exercises.

Only the function for that exercise will be automarked.

Exercise: Counting Farnarkles (pair)

This is a pair exercise to complete with your lab partner.
Complete the function count_farnarkles in farnarkle.c
// return number of farnarkles
int count_farnarkles(int tiles1[N_TILES], int tiles2[N_TILES]) {
    int n_farnarkles = 0;

    int i = 0;
    while (i < N_TILES) {
        if (tiles1[i] == tiles2[i]) {
            n_farnarkles = n_farnarkles + 1;
        }
        i = i + 1;
    }

    return n_farnarkles;
}

count_farnarkles should given two tile sequences, return the number of farnarkles.

Note: it doesn't matter which sequence is the hidden sequence and which is the guess.

farnarkle.c contains a main function which will let you test count_farnarkles

For example:

dcc farnarkle.c -o farnarkle
./farnarkle
Enter 0 to call count_farnarkles()
Enter 1 to call count_arkles()
Enter 2 to call play_farnarkle()
Enter 3 to call test_ai_farnarkle_player()
Call which function: 0
Enter tile sequence 1: 1 2 3 4
Enter tile sequence 2: 2 1 3 8
count_farnarkles returned 1 for
Sequence 1: 1 2 3 4
Sequence 2: 2 1 3 8
./farnarkle
Enter 0 to call count_farnarkles()
Enter 1 to call count_arkles()
Enter 2 to call play_farnarkle()
Enter 3 to call test_ai_farnarkle_player()
Call which function: 0
Enter tile sequence 1: 5 5 6 5
Enter tile sequence 2: 6 5 5 5
count_farnarkles returned 2 for
Sequence 1: 5 5 6 5
Sequence 2: 6 5 5 5

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest count_farnarkles

Autotest Results

94% of 873 students who have autotested farnarkle.c so far, passed all autotest tests.
  • 99% passed test check_using_defines
  • 99% passed test farnarkle_0
  • 100% passed test farnarkle_1
  • 99% passed test farnarkle_2
  • 100% passed test farnarkle_3
  • 100% passed test farnarkle_4
  • 99% passed test farnarkle_5
  • 99% passed test farnarkle_6
  • 99% passed test farnarkle_7
  • 99% passed test farnarkle_8
  • 99% passed test farnarkle_9
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk05_count_farnarkles farnarkle.c
Note, even though this is a pair exercise, you both must run give from your own account before Sunday 8 April 23:59:59 to obtain the marks for this lab exercise.

Exercise: Counting Arkles (pair)

This is a pair exercise to complete with your lab partner.
Complete the function count_arkles in farnarkle.c.
INTERNAL ERROR MISSING FILE: "activities/count_arkles/farnarkle.c"
count_arkles should given two tile sequences, return the number of arkles.

Note: it doesn't matter which sequence is the hidden sequence and which is the guess.

farnarkle.c contains a main function which will let you test count_arkles

For example:

dcc farnarkle.c -o farnarkle
./farnarkle
Enter 0 to call count_farnarkles()
Enter 1 to call count_arkles()
Enter 2 to call play_farnarkle()
Enter 3 to call test_ai_farnarkle_player()
Call which function: 1
Enter tile sequence 1: 1 2 3 4
Enter tile sequence 2: 2 1 3 2
count_arkles returned 2 for
Sequence 1: 1 2 3 4
Sequence 2: 2 1 3 2
./farnarkle
Enter 0 to call count_farnarkles()
Enter 1 to call count_arkles()
Enter 2 to call play_farnarkle()
Enter 3 to call test_ai_farnarkle_player()
Call which function: 1
Enter tile sequence 1: 5 8 5 5
Enter tile sequence 2: 8 5 8 8
count_arkles returned 2 for
Sequence 1: 5 8 5 5
Sequence 2: 8 5 8 8

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest count_arkles

Autotest Results

91% of 905 students who have autotested farnarkle.c so far, passed all autotest tests.
  • 97% passed test arkle_0
  • 99% passed test arkle_1
  • 95% passed test arkle_10
  • 95% passed test arkle_2
  • 93% passed test arkle_3
  • 97% passed test arkle_4
  • 97% passed test arkle_5
  • 97% passed test arkle_6
  • 96% passed test arkle_7
  • 95% passed test arkle_8
  • 97% passed test arkle_9
  • 96% passed test check_using_defines
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk05_count_arkles farnarkle.c
Note, even though this is a pair exercise, you both must run give from your own account before Sunday 8 April 23:59:59 to obtain the marks for this lab exercise.

Exercise: Playing Farnarkle (pair)

This is a pair exercise to complete with your lab partner.
Complete the function play_farnarkle in farnarkle.c
INTERNAL ERROR MISSING FILE: "activities/play_farnarkle/farnarkle.c"
play_farnarkle should given a hidden tile sequences, reads guesses from a human player, printing the number of farnarkles and arkles for each guess, until the human player guesses the sequence.

Use read_tiles to read the guesses.

farnarkle.c contains a main function which will let you test play_farnarkle

For example:

./farnarkle
Enter 0 to call count_farnarkles()
Enter 1 to call count_arkles()
Enter 2 to call play_farnarkle()
Enter 3 to call test_ai_farnarkle_player()
Call which function: 2
Enter guess for turn 1: 1 5 7 8
0 farnarkles, 1 arkles
Enter guess for turn 2: 2 3 4 6
3 farnarkles, 0 arkles
Enter guess for turn 3: 2 3 4 7
2 farnarkles, 0 arkles
Enter guess for turn 4: 2 3 8 6
3 farnarkles, 0 arkles
Enter guess for turn 5: 2 3 1 6
4 farnarkles, 0 arkles
You win

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest play_farnarkle

Autotest Results

95% of 823 students who have autotested farnarkle.c so far, passed the autotest test.
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk05_play_farnarkle farnarkle.c
Note, even though this is a pair exercise, you both must run give from your own account before Sunday 8 April 23:59:59 to obtain the marks for this lab exercise.

Challenge Exercise: A Farnarkle AI (individual)

This is an individual exercise to complete by yourself.
Complete the function farnarkle_ai in farnarkle.c which plays Farnarkle. farnarkle_ai only makes a single guess. It is called each time a guess needs to be made - and it is given information about all previous guesses.

Do not use global or static variables to store information between calls tofarnarkle_ai .

farnarkle.c contains a main function which will let you test farnarkle_ai

For example:

dcc farnarkle.c -o farnarkle 
./farnarkle
Enter 0 to call count_farnarkles()
Enter 1 to call count_arkles()
Enter 2 to call play_farnarkle()
Enter 3 to call test_ai_farnarkle_player()
Call which function: 3
Enter hidden tiles: 4 5 3 5
Turn 1: farnarkle AI guess is: 1 1 1 1 = 0 farnarkles, 0 arkles
Turn 2: farnarkle AI guess is: 2 2 2 2 = 0 farnarkles, 0 arkles
Turn 3: farnarkle AI guess is: 3 3 3 3 = 1 farnarkles, 0 arkles
Turn 4: farnarkle AI guess is: 4 4 4 3 = 1 farnarkles, 1 arkles
Turn 5: farnarkle AI guess is: 5 4 3 5 = 2 farnarkles, 2 arkles
Turn 6: farnarkle AI guess is: 4 5 3 5 = 4 farnarkles, 0 arkles
Farnarkle AI took 6 turns to guess the tiles.

A farnarkle player tournament will be announced on the class web page at the end of the week.

N_TILES and MAX_TILE will vary between tournament rounds. N_TILES will always be in the range 2..8 (inclusive) and MAX_TILE will always be in the range 4..16 (inclusive)

The challenge mark will be based on how well your player performs in this tournament.

Can you make your farnarkle player optimal - make it take, on average, the smallest number of turns possible to guess a code?

Can you prove that your farnarkle player is optimal?

If you can not construct a proof and/or an optimal player, construct a logical argument as to why your player is close to optimal.

The autotest for farnarkle_ai give yours function previous guesses, with farnarkle and arkle counts such that only there is only possible guess consistent with them.

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest farnarkle_ai

Autotest Results

45% of 118 students who have autotested farnarkle.c so far, passed all autotest tests.
  • 100% passed test check_global_static_vars
  • 71% passed test farnarkle_ai_0
  • 67% passed test farnarkle_ai_1
  • 77% passed test farnarkle_ai_2
  • 69% passed test farnarkle_ai_3
  • 67% passed test farnarkle_ai_4
  • 68% passed test farnarkle_ai_5
  • 68% passed test farnarkle_ai_6
  • 72% passed test farnarkle_ai_7
When you are finished working on this exercise you must submit your work by running give:
give cs1511 wk05_farnarkle_ai farnarkle.c
You must run give before Sunday 8 April 23:59:59 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.

Submission

When you are finished each exercises make sure you submit your work by running give.

You can run give multiple times. Only your last submission will be marked.

Don't submit any exercises you haven't attempted.

If you are working at home, you may find it more convenient to upload your work via give's web interface.

Remember you have until Sunday 8 April 23:59:59 to submit your work.

Automarking will be run several days after the submission deadline for the test. When complete you can view automarking here and you can view the the resulting mark via give's web interface

You can read more about lab assessment here