Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Highscore table help.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic
Author Message
CapnChaos




PostPosted: Thu Nov 18, 2010 8:56 pm   Post subject: Highscore table help.

Hey guys, I'm new on here, and fairly new to using turing. I'm making a simple game where a computer player selects a random number, and then the human player tries to guess. I've got the game coded, and it works fine. However, I would like to create a high score table.


Now, I know that I'll have to have it read and write from/to a text file, which I have created. Unfortunately, that's all i know about the topic. I've tried looking at other guides on here, but they don't really make sense to me, and I was hoping that somebody would show me how to do so. I have my code here, so if somebody could please tell me how to make a highscore table based on the code i have here it would be much appreciated.


Turing:


var number, guesses, choice, high : int
var difmenu : string (1)
var choicei, name : string



loop
    put "" : 18, "Number guess game"
    put "" : 18, "-----------------"
    put ""
    put ""
    put "Please select a difficulty."
    put ""
    put "" : 5, "1. novice (1-10)"
    put "" : 5, "2. easy (1-100)"
    put "" : 5, "3. hard (1-1000)"
    put "" : 5, "4. insane (1-1000000)"

    getch (difmenu)
    exit when (difmenu = "1") or (difmenu = "2") or (difmenu = "3") or (difmenu = "4")
    put "Invalid entry"
    delay (1000)
    cls
end loop
guesses := 1
if (difmenu = "1") then
    randint (number, 1, 10)
    high := 10
elsif (difmenu = "2") then
    randint (number, 1, 100)
    high := 100
elsif (difmenu = "3") then
    randint (number, 1, 1000)
    high := 1000
elsif (difmenu = "4") then
    randint (number, 1, 1000000)
    high := 1000000
end if
put ""
put "Please wait while the computer chooses a number."
delay (5000)
put ""
put "The computer has chosen a number."
put ""
put "Enter a number from 1 - ", high, "."
loop
    get choicei
    if strintok (choicei) then
        choice := strint (choicei)
            exit when (choice = number)
        if (choice > number) and (choice < high) then
            put "Too high"
            guesses += 1
        elsif (choice < number) and (choice > 0) then
            put "Too low"
            guesses += 1
        elsif (choice < 0) or (choice > high) then
            put "invalid entry"
        end if
    else
        put "That's not a number, enter a number!"
    end if

end loop
put "You did it, it took you ", guesses, " tries."
put ""
put "Enter your name"
get name :*




<version 4.1.1>
 
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: Fri Nov 19, 2010 3:38 am   Post subject: Re: Highscore table help.

to use a file in turing you must assign the file as a variable, thus letting turing know "which file" its to be using. You can refer to this as "streaming" from a file, to simplify things.
code:

var streamHighScore: int %%gives the file a usable value


second to use this file, for a high score keeper, you'll have to use the "put" and "get" aspects of the I/O operations.

to add to the file, we have to OPEN it using the value its assigned (streaming) and tell the PC to "put" the info we want, something like this.

code:

open: streamHighScore, "C:\\Location\\Score.txt", put
put: streamHighScore, PlayerHighScore
close: streamingHighScore


so its simple to understand what that means. 1) We open the file (what file? Streaming file) at the location of the text we want to use. NOTE THE PUT, we use "put" to put in info, use "read" to get info, theres write/etc in there, F9 it, its all for something. 3) we tell it to "PUT" the info into the file, what info? Players scores. this can be ANY Variable really, boolean/reals,ints,strings. 4) we close the stream, it will auto close after your done with it, but its good practice to do anyways.

To read your info is basically the exact same thing. Read from the file, and assign that info AS your HIGHSCORE, so you may use it on fresh programs. Enjoy!
 
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: