2003 VITEC Software Design and Development Engineer Examination (Afternoon, Part 2)
2003 VITEC Software Design and Development Engineer Examination (Afternoon, Part 2)
1
Q1. Read the following text regarding the creation of a game program, then answer Sub-
Questions 1 through 4.
A decision was made to create a number guessing game program in which a person
(hereinafter referred to as the “player”) competes with a computer. The rules of the game
are as follows.
(1) The competitors (player and program) each select a four-digit number consisting of
four different numerals. In this example, this four-digit number is called the correct
answer. The first digit in the correct answer (most significant digit) may be 0.
(2) Competitors take turns trying to guess each other’s correct answer, and the first one to
guess correctly wins. If both competitors guess correctly in the same number of tries,
then they tie.
(3) When a competitor receives the other competitor’s guess, it is compared with the
receiving competitor’s own correct answer, and a response is sent to the other
competitor consisting of the number of hits, hit, or numerals which are correct in
both numerical value and digit position; as well as the number of misses, miss, or
numerals which are contained in the correct answer, but have incorrect digit positions.
For example, if the correct answer is 1357 and the other competitor guesses 5310, then
the response consists of one hit (hit) and two misses (miss).
Below, the variables defined in the function are local variables, and the function’s
arguments are passed by value unless otherwise specified. The array subscripts start at 0,
and array elements are designated as 0th, 1st, 2nd, etc.
Figure 1 shows the function for determining the correct answer. With this function,
numbers 0 through 9 are randomly generated one at a time, and an array tab consisting of
ten elements is used to obtain a four-digit number with no duplicate numerals. “random”
2
is a function for randomly generating natural numbers, and “%” is a modulus operator. The
array tab is used as follows.
function set_answer ()
int tab[10], n, i, a
Set all tab elements to 0
n = 0
for (i = 1 to 4)
repeat
a = random() % 10
until(tab[a] = 0)
tab[a] = 1
n = a
endfor
return n
endfunction
The algorithm for calculating hit and miss will now be considered.
First, the similarity of two four-digit numbers x and y is defined as follows, using the
hit and miss of the two numbers:
similarity(x, y) = hit * 10 + miss
The set of the similarity values and the (hit, miss) values has a one-to-one
correspondence, so the similarity is used within the program instead of the hit and
miss.
Similarity calculation is used when the player’s guess is compared with the correct
answer, and when the program is determining its own guess. This calculation is done
frequently, so a highly efficient execution method will now be considered.
In order to improve execution efficiency, before similarity is calculated during the
game, one of the numbers to be compared is translated into a form called an array
expression. In this translation process, if the subject number is d4d3d2d1, then i is stored
in the dith element of the array, and 0 is stored in the other elements. Figure 2 shows the
3
function for translating the number y into an array expression. “pos” is an array which
comprises ten elements and is called by reference.
When the array expression (pos) for the number y is used, similarity(x, y) can be
determined using the “sim” function in Figure 3.
4
function sim(x, pos)
int hit, miss, i, p
hit = 0
miss = 0
for(i = 1 to 4)
p = x % 10
x = x / 10
if( c )
if( d ) hit = hit + 1
else miss = miss + 1
endif
endif
endfor
return hit * 10 + miss
endfunction
Fig. 3 “sim” Function
Guess similarity
First 1234 12
Second 1345 02
In this case, assume that the next number taken from “nums” is 2304. A calculation is
performed to determine whether 2304 is suitable as the next guess, with the following
results: similarity(2304, 1234) = 12,similarity(2304, 1345) = 11.
These results show that 2304 is inconsistent with the second result, so it is not suitable as
the next guess.
5
Now assume that the next number taken from “nums” is 0432. A calculation is performed
to determine whether 0432 is suitable as the next guess, with the following results:
similarity(0432, 1234) = 12,similarity(0432, 1345) = 02. These
results show that 0432 is not inconsistent with the previous results, so it is used as the next
guess.
When these considerations are put together, the function for determining the kth guess is as
shown in Figure 4. In this case, the “nums” array is an array containing correct answer
candidates; guess[i-1] is the array expression for the ith guess; and log[i-1] is the
similarity value between the ith guess and the correct answer. “index” is a
subscript specifying a position before which the correct answer does not exist in the
“nums” elements. The initial value for “index” is –1.
function make_guess(k)
int j
index = index + 1
for() // Infinite loop
j = 0
while(j < k – 1 and sim(nums[index], guess[j]) = log[j])
e
endwhile
if(j < k - 1)
f
else
return nums[index]
endif
endfor
endfunction
Fig. 4 “make_guess” Function
6
When all of the above-mentioned functions are used, the number guessing game program
is as shown in Figure 5. “nums”, “guess”, “log” and “index” are global variables.
function play()
int ans, pos_a[10], i, r, g, hit, miss, guess_person
ans = set_answer()
make_pos(ans, pos_a)
index = -1
i = 1
for() // Repeat until correct
read guess_person // Read player’s guess
r = sim(guess_person, pos_a) // Calculate similarity
write r / 10, r % 10 // Display results
g = make_guess(i) // Calculate i th guess
write g // Display guess
read hit, miss // Read guess results
if(hit = 4 or r = 40) // Was guess correct?
break
endif
log[i-1] = 10 * hit + miss // Record results
make_pos(g, guess[i-1]) // Record guess
i = i + 1
endfor
if( g ) // Player wins
write “You win”
elseif( h ) // Computer wins
write “I win”
else
write “It’s a tie” // Tie
endif
endfunction
Fig. 5 Number Guessing Game Program
7
Sub-Question 1
In the program in Figure 5, what is the minimum size N of the “nums” array?
Sub-Question 2
Insert appropriate words in blanks a through h in Figs. 1 through 5.
Sub-Question 3
After the player plays the game for a while, the program stops responding. An investigation
of the issue showed that it occurred because the player’s hit or miss response value was
incorrect, so the program could not find a candidate which would be the correct answer.
Changes need to be made so that in such cases, “make_guess” returns a value of –1.
What type of judgment should be added to the “make_guess” function in Fig. 4? Answer
in 4 words or less.
Sub-Question 4
In the algorithm of the program in Figure 5, the computer always repeats the same guesses
every time, even when the player plays against the computer repeatedly. For this reason, it
becomes possible for the player to find numbers that are difficult for the computer to guess.
In order to avoid this, the program should use a different sequence of guesses each time. In
order to use a different guessing sequence each time, a certain operation should be
performed on the “nums” array value immediately before “play” is called from “main”.
Describe this operation in 4 words or less.
8
[Memo]
9
[Memo]
10