Computer Science Canada

Reverse Game

Author:  ruff_riders91 [ Tue Dec 09, 2003 11:22 am ]
Post subject:  Reverse Game

code:

%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)

Author:  Dan [ Tue Dec 09, 2003 3:11 pm ]
Post subject: 

well it is intresting and looks nice for a text based game, but i have no idea about the object of the game Razz also if you use the code tags when posting code it makes it look better in your posts. or you could upload them in there .t format

Author:  PaddyLong [ Tue Dec 09, 2003 4:17 pm ]
Post subject: 

I think the point is to get them into the right order in as less turns (of switching numbers that are beside each other) as possible ..... I think it really is just a matter of using bubble sort on it.. or maybe some other sort algorithm that compares neighbours?


: