%Created By Joey
const row := 8
const col := 20
proc generateNumbers (var nos : array 1 .. * of int)
var no, count : int
var found : boolean
count := 0
loop
randint (no, 1, 100)
found := false
for i : 1 .. count
if no = nos (i) then
found := true
end if
end for
if not found then
count := count + 1
nos (count) := no
end if
exit when count = upper (nos)
end loop
end generateNumbers
proc displayHeading (nos : array 1 .. * of int)
cls
locate (row, col + (4 * upper (nos) - 19) div 2)
put "Reverse Number Game"
locate (row + 1, col + (4 * upper (nos) - 19) div 2)
put repeat ("=", 19)
locate (row + 3, col)
for i : 1 .. upper (nos)
put i : 4 ..
end for
locate (row + 4, col)
put repeat ("-", 4 * upper (nos))
locate (row + 7, col)
put "Starting position : "
locate (row + 9, col)
put "Ending position : "
end displayHeading
proc displayNumbers (nos : array 1 .. * of int)
locate (row + 5, col)
for i : 1 .. upper (nos)
put nos (i) : 4 ..
end for
end displayNumbers
function getstart (nos : array 1 .. * of int) : int
var temp : int
loop
locate (row + 7, col + 20)
put "" : 10 ..
locate (row + 7, col + 20)
get temp
exit when temp > 0 and temp < upper (nos)
end loop
result temp
end getstart
function getEnd (start : int, nos : array 1 .. * of int) : int
var temp : int
loop
locate (row + 9, col + 20)
put "" : 10 ..
locate (row + 9, col + 20)
get temp
exit when temp > start and temp <= upper (nos)
end loop
result temp
end getEnd
proc swapNumbers (start, finish : int, var nos : array 1 .. * of int)
var st, fin, temp : int
st := start
fin := finish
loop
temp := nos (st)
nos (st) := nos (fin)
nos (fin) := temp
st := st + 1
fin := fin - 1
exit when st >= fin
end loop
end swapNumbers
function inOrder (nos : array 1 .. * of int) : boolean
var order : boolean
order := true
for i : 1 .. upper (nos) - 1
if nos (i) > nos (i + 1) then
order := false
end if
end for
result order
end inOrder
proc displayTries (count : int)
locate (row + 11, col + 20)
put "Got it in ", count, " tries!"
end displayTries
%============================== MAIN ==============================
var numbers : array 1 .. 10 of int
var stpos, endpos, tries : int
%col := (maxcol - upper (numbers) * 4) div 2
tries := 0
displayHeading (numbers)
generateNumbers (numbers)
loop
displayNumbers (numbers)
exit when inOrder (numbers)
stpos := getstart (numbers)
endpos := getEnd (stpos, numbers)
swapNumbers (stpos, endpos, numbers)
tries := tries + 1
end loop
displayTries (tries)
|