Archived

This topic is now archived and is closed to further replies.

gamechampionx

Small Error

Recommended Posts

I''m having a small error with the GUI in Turing. I''m making a Jeopardy game. Here''s the code:
type question :
    record
        proposition : string
        first_word : string
        answer : string
    end record

var choice : int

procedure do_widget
    choice := GUI.GetEventWidgetID
end do_widget

procedure round_one

    var input : string (1)
    var category : array 1 .. 6 of string
    category (1) := "Math"
    category (2) := "Science"
    category (3) := "History"
    category (4) := "Toronto"
    category (5) := "Ends With ''y''"
    category (6) := "Food"

    var size : int := sizeof (question)

    var round_question : array 1 .. 6, 1 .. 5 of question

    var question_stream : int
    open : question_stream, "record_round_one.dat", read, seek
    assert question_stream > 0

    for category_chosen : 1 .. 6
        for question_chosen : 1 .. 5
            var rand_num : int
            randint (rand_num, 1, 4)
            seek : question_stream, size * (rand_num + 4 * (question_chosen - 1) + 20 * (category_chosen - 1) - 1)
            read : question_stream, round_question (category_chosen, question_chosen)
        end for
    end for

    close : question_stream

    var exists : array 1 .. 6, 1 .. 5 of boolean

    for button_column : 1 .. 6
        for button_value : 1 .. 5
            exists (button_column, button_value) := true
        end for
    end for

    loop

        colourback (blue)
        cls

        var array_button : array 1 .. 6, 1 .. 5 of int

        for button_column : 1 .. 6
            for button_value : 1 .. 5
                if exists (button_column, button_value) then
                    array_button (button_column, button_value) := GUI.CreateButton (round ((button_column - 1) / 6 * maxx), maxy - round (button_value / 6 * maxy + maxy div 12), round (maxx div 6),
                        "$" +
                        intstr (100 * button_value), do_widget)
                end if
            end for
        end for

        var chosen_column, chosen_row : int

        loop
            choice := 0
            chosen_column := 0
            chosen_row := 0
            exit when GUI.ProcessEvent
            var done : boolean := false
            if choice > 0 then
                for button_column : 1 .. 6
                    for button_value : 1 .. 5
                        if exists (button_column, button_value) then
                            if array_button (button_column, button_value) = choice then
                                chosen_column := button_column
                                chosen_row := button_value
                                done := true
                                exit
                            end if
                        end if
                    end for
                    exit when done
                end for
            end if
            exit when done
        end loop

        exists (chosen_column, chosen_row) := false
        GUI.Dispose (array_button (chosen_column, chosen_row))

    end loop

end round_one
 
The problem is that I''m trying to make a button disappear one you press it. It works for the first but not for subsequent buttons. The loop will not exit for later presses even though a WidgetID is read. Why would this work?

Share this post


Link to post
Share on other sites
Umm, you can only exit the loop once (I assume you don''t call round_one more than once since you have initialization code in there).

Good heavens. What twisted evil mind has you doing a GUI app in *Turing*? :/

Share this post


Link to post
Share on other sites
Yeah, I know the exit condition is a little demented. I can''t figure out why it would not work. I thought done would be assigned to true every time an "existing" button is pressed. I can''t quite put my finger on the problem... Anybody help?

Share this post


Link to post
Share on other sites
I think you should consult the turing help and read about it more since you sound like you dont know much about it.

Share this post


Link to post
Share on other sites
As far as I can tell, the loop exits as soon as "GUI.ProcessEvent", so you don''t get to do anything with the result of that. You''re going to have to read up a fair bit on how your GUI library works. It''s common for these things, for example, to take control of the "main loop", and expect you to provide "callback" functions (event-driven programming).

Share this post


Link to post
Share on other sites
exit when GUI.ProcessEvent does not exit the loop to my knowledge, but runs the appropriate button''s procedure. The thing does work for the first button press, and I don''t see why it wouldn''t work for the second, but it doesn''t. I do know the syntax fairly well, and I''m not sure that''s the real problem with this code, it''s probably something small.

Share this post


Link to post
Share on other sites
Here''s the full code:


import GUI in "%oot/support/lib/GUI"
Window.Set (defWinID, "title:JEOPARDY, nobuttonbar")
View.Set ("graphics:max;max")

procedure put_middle (output : string, row : int)
locate (row, (97 - length (output)) div 2)
put output ..
end put_middle

type character :
record
name : string
score : int
buzzer : int
end record

type question :
record
proposition : string
first_word : string
answer : string
end record

var person : array 1 .. 3 of character

var choice : int

type high_scores :
record
name : string
score : int
end record

var high_score_person : array 1 .. 5 of high_scores

procedure result_new_game
choice := 1
end result_new_game

procedure result_high_scores
choice := 2
end result_high_scores

procedure result_rules
choice := 3
end result_rules

procedure result_exit
choice := 4
end result_exit

procedure title
choice := 0
var background : int := Pic.FileNew ("title_bg.bmp")
assert background > 0
background := Pic.Scale (background, maxx, maxy)
Pic.Draw (background, 0, 0, picCopy)
Pic.Free (background)
var new_game_button : int := GUI.CreateButton (round (0.4 * maxx), round (0.5 * maxy), round (0.2 * maxx), "New Game", result_new_game)
var high_scores_button : int := GUI.CreateButton (round (0.4 * maxx), round (0.45 * maxy), round (0.2 * maxx), "High Scores", result_high_scores)
var rules_button : int := GUI.CreateButton (round (0.4 * maxx), round (0.4 * maxy), round (0.2 * maxx), "View Rules", result_rules)
var exit_button : int := GUI.CreateButton (round (0.4 * maxx), round (0.35 * maxy), round (0.2 * maxx), "Exit", result_exit)
loop
exit when GUI.ProcessEvent
exit when choice > 0
end loop
GUI.Dispose (new_game_button)
GUI.Dispose (high_scores_button)
GUI.Dispose (rules_button)
GUI.Dispose (exit_button)
end title

procedure do_widget
choice := GUI.GetEventWidgetID
end do_widget

procedure pose_question (round, category_num, question_num : int, list : array 1 .. 6, 1 .. 5 of question)
put_middle (list (category_num, question_num).proposition, 15)
end pose_question

procedure round_one

var input : string (1)
var category : array 1 .. 6 of string
category (1) := "Math"
category (2) := "Science"
category (3) := "History"
category (4) := "Toronto"
category (5) := "Ends With ''y''"
category (6) := "Food"

var size : int := sizeof (question)

var round_question : array 1 .. 6, 1 .. 5 of question

var question_stream : int
open : question_stream, "record_round_one.dat", read, seek
assert question_stream > 0

for category_chosen : 1 .. 6
for question_chosen : 1 .. 5
var rand_num : int
randint (rand_num, 1, 4)
seek : question_stream, size * (rand_num + 4 * (question_chosen - 1) + 20 * (category_chosen - 1) - 1)
read : question_stream, round_question (category_chosen, question_chosen)
end for
end for

close : question_stream

var exists : array 1 .. 6, 1 .. 5 of boolean

for button_column : 1 .. 6
for button_value : 1 .. 5
exists (button_column, button_value) := true
end for
end for

loop

colourback (blue)
cls

var array_button : array 1 .. 6, 1 .. 5 of int

for button_column : 1 .. 6
for button_value : 1 .. 5
if exists (button_column, button_value) then
array_button (button_column, button_value) := GUI.CreateButton (round ((button_column - 1) / 6 * maxx), maxy - round (button_value / 6 * maxy + maxy div 12), round (maxx div 6),
"$" +
intstr (100 * button_value), do_widget)
end if
end for
end for

var chosen_column, chosen_row : int

loop
choice := 0
loop
exit when GUI.ProcessEvent
exit when choice > 0
end loop
var done : boolean := false
for button_column : 1 .. 6
for button_value : 1 .. 5
if exists (button_column, button_value) then
if array_button (button_column, button_value) = choice then
chosen_column := button_column
chosen_row := button_value
done := true
exit
end if
end if
end for
exit when done
end for
exit when done
end loop

exists (chosen_column, chosen_row) := false

for any_column : 1 .. 6
for any_row : 1 .. 5
GUI.Dispose (array_button (any_column, any_row))
end for
end for

end loop

end round_one

procedure new_game
colourback (black)
cls
color (white)
put_middle ("How many players will there be?", 2)
var number_of_players : int
var number_of_players_string : string (1)
loop
loop
locate (3, 40)
getch (number_of_players_string)
exit when strintok (number_of_players_string)
end loop
number_of_players := strint (number_of_players_string)
exit when number_of_players >= 1 and number_of_players <= 3
end loop
put number_of_players
for set_not_human : 1 .. 3
person (set_not_human).score := 0
person (set_not_human).buzzer := 0
person (set_not_human).name := "Computer"
end for
for name_entry : 1 .. 3
if number_of_players >= name_entry then
put_middle ("Enter player #" + intstr (name_entry) + "''s name:", 3 + 6 * name_entry)
locate (4 + 6 * name_entry, 40)
get person (name_entry).name : *
if length (person (name_entry).name) > 10 then
person (name_entry).name := person (name_entry).name (1 .. 10)
end if
put_middle ("Press the key you will use for your buzzer:", 6 + 6 * name_entry)
var key : string (1)
loop
locate (7 + 6 * name_entry, 40)
getch (key)
var original : boolean := true
for test : 1 .. name_entry - 1
if ord (key) = person (test).buzzer then
original := false
end if
end for
exit when original
end loop
put "\"", key, "\"" ..
person (name_entry).buzzer := ord (key)
end if
end for
end new_game

procedure reset_high_scores
choice := 1
var score_stream : int
open : score_stream, "high_scores.txt", put
assert score_stream > 0
for list_member : 1 .. 5
high_score_person (list_member).name := "Computer"
put : score_stream, high_score_person (list_member).name
high_score_person (list_member).score := 0
put : score_stream, high_score_person (list_member).score
end for
close : score_stream
end reset_high_scores

procedure back
choice := 2
end back

procedure show_high_scores
var score_stream : int
open : score_stream, "high_scores.txt", get
assert score_stream > 0
for entry : 1 .. 5
get : score_stream, high_score_person (entry).name : *
get : score_stream, high_score_person (entry).score
end for
close : score_stream
colorback (black)
cls
var money : int := Pic.FileNew ("money.bmp")
assert money > 0
money := Pic.Scale (money, maxx div 8, maxy div 6)
for position : 1 .. 6
Pic.Draw (money, 0, (position - 1) * maxy div 6, picMerge)
end for
for position : 1 .. 6
Pic.Draw (money, maxx - maxx div 8, (position - 1) * maxy div 6, picMerge)
end for
Pic.Free (money)
colour (yellow)
put_middle ("HIGH SCORES", 2)
locate (8, 20)
put "Name" ..
locate (8, 68)
put "Score" ..
drawline (round (0.19 * maxx), round (0.73 * maxy), round (0.81 * maxx), round (0.73 * maxy), yellow)
drawline (maxx div 2, round (0.76 * maxy), maxx div 2, round (0.35 * maxy), yellow)
var reset_scores_button : int := GUI.CreateButton (round (0.4 * maxx), round (0.2 * maxy), round (0.2 * maxx), "Reset High Scores", reset_high_scores)
var back_button : int := GUI.CreateButton (round (0.4 * maxx), round (0.1 * maxy), round (0.2 * maxx), "Back", back)
loop
choice := 0
for names : 1 .. 5
locate (9 + 2 * names, 20)
put high_score_person (names).name ..
put "" : (10 - length (high_score_person (names).name)) ..
locate (9 + 2 * names, 68)
put "$", high_score_person (names).score ..
put "" : (6 - length (intstr ((high_score_person (names).score)))) ..
end for
loop
exit when GUI.ProcessEvent
exit when choice > 0
end loop
exit when choice = 2
end loop
GUI.Dispose (reset_scores_button)
GUI.Dispose (back_button)
end show_high_scores

procedure result_next
choice := 1
end result_next

procedure show_rules

var rules_stream : int
open : rules_stream, "rules.txt", get
assert rules_stream > 0

loop

colorback (white)
cls
color (black)

var rules_bg : int
rules_bg := Pic.FileNew ("rules_bg.bmp")
assert rules_bg > 0
rules_bg := Pic.Scale (rules_bg, maxx div 20, maxy div 6)
for spot : 1 .. 6
Pic.Draw (rules_bg, 0, maxy * (spot - 1) div 6, picMerge)
end for
Pic.Free (rules_bg)

var next_button : int := GUI.CreateButton (round (0.4 * maxx), round (0.05 * maxy), round (0.2 * maxx), "Next", result_next)

for screen_line : 2 .. 29
var line : string
exit when eof (rules_stream)
get : rules_stream, line : *
put_middle (line, screen_line)
end for
choice := 0
loop
exit when GUI.ProcessEvent
exit when choice > 0
end loop
exit when eof (rules_stream)

GUI.Dispose (next_button)

end loop

close : rules_stream

end show_rules

loop
title
exit when choice = 4
if choice = 1 then
new_game
round_one
elsif choice = 2 then
show_high_scores
else
show_rules
end if
end loop

colourback (black)
cls
var game_over : int := Pic.FileNew ("game_over.bmp")
game_over := Pic.Scale (game_over, maxx div 4, maxy div 8)
Pic.Draw (game_over, maxx div 2 - maxx div 8, maxy div 2 - maxy div 16, picMerge)
Pic.Free (game_over)


In Round 1, after the first button is pressed, and the gui and all that is redrawn, the second button doesn''t seem to exist and have a value. I was able to outupt its value after declaration to the screen, but when pressed, it still did not have a value. Run this itself to see my problem.

Share this post


Link to post
Share on other sites