Computer Science Canada

Hangman

Author:  Amarylis [ Tue May 01, 2012 5:33 pm ]
Post subject:  Hangman

Since I have now handed this in, I'm allowed to share the code without being accused of plagiarism.

It's really just a simple game of hangman with 2 themes, and the option for you to make your own themes. Works with letters of any length, provided that it doesn't go off the screen.


Here you go~

Author:  chipanpriest [ Wed Oct 31, 2012 7:06 pm ]
Post subject:  Re: Hangman

When I ran your program, I created some words. When I looked in the containing folder, I found heythere.WDBNK. Can Turing make a type of file and read from it? I thought turing could only support .txt files. So could you type:

Turing:

var f : int
open : f,"heythere.sdfkjsfk", get, mod, seek, put
put : f , "omgdoesthiswork?"
close : f


Would it work and be able to read from those files?

Author:  TerranceN [ Wed Oct 31, 2012 11:17 pm ]
Post subject:  RE:Hangman

You can open whatever files you want in turing. Using read and write you can read/write files in binary, and using char and int you can read/write in increments of 1 and 4 bytes at a time. Just remember that files are just data, there's nothing magical about supporting a file type other than learning its specification. As an example, I just made this bmp reader:

Turing:

% Simple bitmap reader

% I skip a lot of information that's probably important.
% If you really want to implement this correctly, read over the
% bitmap specification: http://en.wikipedia.org/wiki/BMP_file_format

var stream : int

% temporary values for reading
var c : char      % this will read one byte
var intVal : int  % this will read 4 bytes

var width : int
var height : int

var offset : int


open : stream, "img.bmp", read

put "Header information:"
% the first 14 bytes are bmp file information

% make sure the file is bmp by checking first two bytes
% should be 0x42 0x4D in hex or 66 77 in decimal
read: stream, c
if ord(c) ~= 66 then
    quit
end if
put ord(c), " "..

read: stream, c
if ord(c) ~= 77 then
    quit
end if
put ord(c), " "..

for i : 1 .. 8
    read : stream, c
    put ord (c), " " ..
end for

% the last 4 of this section is an offset to
% where the pixel values are
read : stream, offset
put offset, " " ..

% DIB header size
read : stream, intVal

% width/height
read : stream, width
read : stream, height

% now skip ahead to the pixels
for i : 1 .. offset - 26 % (14 byte header
                         %  + 4 byte DIB header size
                         %  + 8 bytes for width/height)
    read : stream, c
    put ord (c), " " ..
end for

% draw the pixels
for y : 0 .. height - 1
    for x : 0 .. width - 1
        var r, g, b : int
        read : stream, c
        b := ord (c)
        read : stream, c
        g := ord (c)
        read : stream, c
        r := ord (c)
        drawdot (x, y, RGB.AddColor (r / 255, g / 255, b / 255))
    end for
   
    % each row is padded so that it is a multiple of four bytes
    % we need to remove that padding to get to the next row
    for x : 1 .. (4 - ((width * 3) mod 4))
        read : stream, c
    end for
end for
close : stream


: