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

Username:   Password: 
 RegisterRegister   
 making a diamond with *
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
kousha41564




PostPosted: Fri Dec 12, 2008 8:39 pm   Post subject: making a diamond with *

hi , i am making a diamond with * (stars) and i did it after like 2 days of trying . but my code seems long and stupid, (programmer since 1 month, turing that is , i know html and basic) so here is the code, i was wondering if anyone could shorten it for me , if shortening it IS possible.

code:

var nlines : int
var spaces : int
var stars : int
put "enter number of lines!"
get nlines
cls
put " "

spaces := nlines
for K : 1 .. nlines

    for decreasing H : spaces .. 1

        put " " ..

    end for

    spaces := spaces - 1

    for I : 1 .. K + K - 1
        put "*" ..
    end for

    put " "

end for


spaces := nlines
stars := nlines * 2 - 1
for K1 : 1 .. nlines

    for H1 : 1 .. spaces - (nlines - 1)

        put " " ..

    end for

    spaces := spaces + 1

    for decreasing I1 : stars .. 1
        put "*" ..

    end for
    stars := stars - 2

    put " "

end for


so the point of the program is asking how many lines the use would like, and then making a diamond based on that number of lines (twice that number of lines)
Sponsor
Sponsor
Sponsor
sponsor
ecookman




PostPosted: Fri Dec 12, 2008 8:50 pm   Post subject: Re: making a diamond with *

i don't think you can make the program shorter... turing is a horrible language to use

you need long complex programs to do simple things

as an example run this

Turing:


var playerhp, monsterhp : int
var playerattk, monsterattk : int
var playerdef: int
var onetwo : int
var monsterchoice: int

randint (playerhp,10,150)
randint (monsterhp,75,250)

loop
randint (playerattk,1,10)
randint (monsterattk,1,20)
randint (playerdef,1,15)
randint (monsterchoice,1,2)

delay(2000)

%player attack or defend
cls
locate (10,1)
put "your hp is ",playerhp
locate (11,1)
put "the monster's hp is ",monsterhp


locate (3,1)

put "press 1 to attack or press 2 to defend"
get onetwo
if onetwo = 1 then
    %player attacks
    put "you hit a ",playerattk
    monsterhp := monsterhp-playerattk
   
    %monster decides what to do - attack or defend
        if monsterchoice = 1 then
            put "the monster attked and hit a ",monsterattk
            playerhp := playerhp-monsterattk
        end if
end if

if onetwo = 2 then
    %player defends
    put "your defence is ",playerdef
    playerhp:=playerhp+playerdef
    %monster decides what to do - attack or defend
        if monsterchoice= 1 then
        put "the monster attked and hit a ",monsterattk
          playerhp := playerhp-monsterattk   
       
           
        end if
end if
if monsterhp<=0 then
cls
locate (13,30)
put "you win"
exit       
elsif playerhp<=0 then
cls
locate (13,30)
put "GAME OVER"
exit
end if
end loop


good program though

my program is not the final product. but it is the base if it didn't need to be shortened can't be shortened. Only 1 way to really to do something in turing
dc116




PostPosted: Fri Dec 12, 2008 8:55 pm   Post subject: Re: making a diamond with *

You can definitely make it shorter. Here's my version, using counters.

code:

var nlines,counter,spaces : int
put "enter number of lines!"
get nlines
cls
put ""

counter:= 0
spaces:=nlines
loop
    counter:= counter + 1
    spaces:= spaces - 1
    put repeat(" ",spaces)..
    put repeat("*",2*counter-1)
    exit when counter=nlines
end loop
loop
    counter:= counter - 1
    spaces:= spaces + 1
    put repeat(" ",spaces)..
    put repeat("*",2*counter-1)
    exit when counter=0
end loop
ecookman




PostPosted: Fri Dec 12, 2008 9:05 pm   Post subject: RE:making a diamond with *

well didn't really make it shorter

just wrote another program that is probably beyond his knowledge if he is just starting...

here at compsci we like to help people get the right answers not give it to them.


if you examine his program it uses effective but simple for loops, which is in his area knowledge and understanding of he program

next time try to point them to a tutorial that shows them how to use something or break down your code and show them how it works not *slap* done here you go. The point that i am getting at is teach someone how to do something not do it for them.
-------------------------------------------------------
you are probably thinking that what i said was wrong. if he was looking for a alternative way to write his program i was wrong, but what he asked for is exactly what i gave. if what HE wrote can be shortened
dc116




PostPosted: Fri Dec 12, 2008 9:10 pm   Post subject: RE:making a diamond with *

Thanks for the advice, I'll keep that in mind next time.
ecookman




PostPosted: Fri Dec 12, 2008 9:12 pm   Post subject: RE:making a diamond with *

no problem i got sooo many warning due to that. i think like 3...just saving you from bannage
kousha41564




PostPosted: Sat Dec 13, 2008 11:00 pm   Post subject: RE:making a diamond with *

dc16, what you wrote seems so genius to me Razz but i have no idea whats going on in your program, i would love if you could break it down for me telling me how it works. (i know counters, and such, i just dont understand the part where you used repeat Sad . )

and if i actualy wrote my program effeciently then i am proud of myself thank you! Razz lol

and ecooman thats a nice game man , if you can you should try incorperating some graphics
TheGuardian001




PostPosted: Sun Dec 14, 2008 12:29 am   Post subject: Re: making a diamond with *

the key thing you would need to use is the locate statement

code:

locate(row,column)


locate moves the text output to the specified row and column.

for example, a vertical line 5 columns from the right side would be

Turing:

locate(10,5) %10 rows down, 5 columns over
put"*"
locate(11,5) %11 rows down, 5 columns over
put"*"
locate(12,5) %12 rows down, 5 columns over
put"*"
locate(13,5) %13 rows down, 5 columns over
put"*"
locate(14,5) %14 rows down, 5 columns over
put"*"
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Dec 14, 2008 2:19 am   Post subject: Re: making a diamond with *

ecookman @ Fri Dec 12, 2008 8:50 pm wrote:
i don't think you can make the program shorter... turing is a horrible language to use


Since Turing is such a horrible language to use, I challenge you come up with a shorter solution in any other language. Your baseline? 6 lines

Turing:

var lines : int
put "enter number of lines!"
get lines
for i: 1 .. lines * 2 - 1
   put repeat(" ", ceil((1 + 2*(lines-1) / 2)) - ceil((1 + 2*(i + 2*(i - lines) * round((lines - i + 0.00001) / abs(lines - i + 0.00001) / 2 - 0.5) - 1))/2)) + repeat("*", 1 + 2*(i + 2*(i - lines) * round((lines - i + 0.00001) / abs(lines - i + 0.00001) / 2 - 0.5) - 1))
end for
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ecookman




PostPosted: Sun Dec 14, 2008 11:06 am   Post subject: RE:making a diamond with *

i don't know enough about other languages to do that sorry...
i just know i hate turing and it is a pain to code anything
DanielG




PostPosted: Sun Dec 14, 2008 11:17 am   Post subject: RE:making a diamond with *

Turing has many flaws, but amount of code needed to write an avergae program is not one of them.
kousha41564




PostPosted: Sun Dec 14, 2008 12:13 pm   Post subject: RE:making a diamond with *

tony, holy crap ! 6 lines, make me feel like a retard Sad lol
Tony




PostPosted: Sun Dec 14, 2008 4:45 pm   Post subject: RE:making a diamond with *

@ecookman -- other languages will hardly make drawing ASCII patterns any easier for you. I'm not sure how you've decided that Turing is so much worse and so much more "painful" than other languages, if you don't know enough about other languages to use them.

@kousha41564 -- don't be. I'd take your solution over my own, as it's much easier to follow, understand, and maintain.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
kousha41564




PostPosted: Sun Dec 14, 2008 6:21 pm   Post subject: RE:making a diamond with *

yay lol Razz
dc116




PostPosted: Sun Dec 14, 2008 6:28 pm   Post subject: Re: RE:making a diamond with *

kousha41564 @ Sun Dec 14, 2008 12:13 pm wrote:
tony, holy crap ! 6 lines, make me feel like a retard Sad lol


Ahh same here Exclamation Exclamation Exclamation

Here's the breakdown of my version, as requested.

code:

% Declares variables
var nlines,counter,spaces : int
put "enter number of lines!"
get nlines
cls
put ""

% counter is basically the number of row (eg. row one)
counter:= 0
% spaces basically "aligns" the diamond (or making the asterisks into the shape of the diamond)
spaces:=nlines
%Draws the upper part of the diamond.
loop
% the number of rows increases
    counter:= counter + 1
% As it increase, there are more asterisks, which results in less spaces
    spaces:= spaces - 1
% here the number of spaces is controlled using repeat.
    put repeat(" ",spaces)..
% here the number of asterisks is controlled using repeat, using the formula (sequence number * 2 - 1)
    put repeat("*",2*counter-1)
    exit when counter=nlines
end loop

%Draws the lower part.
%Basically my way of reflecting the upper part on a horizontal line.
loop
    counter:= counter - 1
    spaces:= spaces + 1
    put repeat(" ",spaces)..
    put repeat("*",2*counter-1)
    exit when counter=0
end loop


Hope that helps
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 20 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: