FreeBASIC syntax challenge games

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

I think this is a bug though:

Code: Select all

type print
    as string b="FreeBASIC"
    declare  sub bug
    static as string d
end type

    sub print.bug 
    redim d(0)
    d(0)=b
    print d(0)
    end sub


(@print)->bug
    
sleep
 
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: FreeBASIC syntax challenge games

Post by grindstone »

Obviously the "Print" - statement becomes redefined as a variable (but only inside the UDT).

Code: Select all

Type UDT
  Declare Sub s (Byval Print As Integer)
  Dim As Integer Print
End Type

Sub UDT.s (Byval Print As Integer)
	'Print Print '<-- this doesn't work
	write Print
End Sub

Dim As UDT test

test.s(3)
Print 5
Sleep
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

dodicat wrote:I think this is a bug though
No:
'(@Print)' is the address of a temporary instance of 'Print'.
(shortcut of '(@Print())')
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

The bug (if it is one) is in the sub bug, not the temporary instance.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

OK
'redim d(0)' should induce a compiler error message.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

At it's most basic, a variable created in the main module can be tweaked into a dynamic array in any scope block.

Code: Select all


dim as string d=__function__

print d
    scope
    redim d(1 to 10)
    d(5)=__function__
    print d(5)
    end scope

sleep
 
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

Perhaps a link with the following bug report:
#645 Access to global duplicated symbol is captured by local symbol

Code: Select all

dim as integer d

scope
  redim d(10)   '' should induce a compiler error message
  'd = 1        '' error 72: Array access, index expected, before '=' in 'd = 1'
  print d(10)
end scope
[edit]
Bug report #645 updated with that behavior.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

Your last challenge has gone unanswered for a week, so I propose:

Code: Select all

Type Print
  Dim Print As String = "You won!"
  Declare Sub Conflict (Byref Print As String)
End Type

Sub Print.Conflict (Byref Print As String)
  With This
  
  '--- Zone beginning of code insertion ---
  ..print(.print)
  '--- Zone ending of code insertion ------
  
  End With
End Sub

Dim conflict As Print
conflict.Conflict("You lost!")

Sleep 
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

OK
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

17011472061230192962203758803520196527181634231072185214065217301763591936026977
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Post by Tourist Trap »

dodicat wrote:17011472061230192962203758803520196527181634231072185214065217301763591936026977
I think here that a dangerous mind would answer ... +1...
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

No, Tourist Trap, no such danger here.
The row of numbers is a longer message than +1.
Three lines of code should do.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC syntax challenge games

Post by Tourist Trap »

dodicat wrote:No, Tourist Trap, no such danger here.
The row of numbers is a longer message than +1.
Three lines of code should do.
What's the rule of this challenge dodi? It's possible that I missed the page where you've posted it.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: FreeBASIC syntax challenge games

Post by dodicat »

Using little used FreeBASIC syntax (I haven't seen it used much on the forum), you can create a string of numbers from text.
Also the process can be reversed.
Here is a function to get the numbers.

Code: Select all




function scramble(byval s as string) as string
  s=s+string(iif(4-(len(s) mod 4)=4,0,4-(len(s) mod 4))," ")
  dim as string g,acc
  for n as long=1 to len(s) step 4
      g=str(cvl(mid(s,n,4)))
      g=string(iif(10-(len(g) mod 10)=10,0,10-(len(g) mod 10))," ")+g
      acc+=g
  next n
  return acc
end function



dim as string alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

dim as string s= scramble(alphabet)
print alphabet
print s
print
alphabet=lcase(alphabet)

s= scramble(alphabet)
print alphabet
print s

sleep 
There are no spaces in the challenge string, so it is easier to unscramble
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC syntax challenge games

Post by fxm »

Enigma #17, force 2
(a new enigma of medium difficulty, although only one instruction must to be found)

By only adding one instruction (inside the dedicated insertion line) which of course must define 'a', make so that program correctly displays the elements of 'array()' :

Code: Select all

Dim As Integer array(1 To 5, 1 To 9)
For I As Integer = 1 To 5
  For J As Integer = 1 To 9
    array(I, J) = I*10 + J
  Next J
Next I

Type Array0
   As Integer a0(1 To 9)
   Declare Operator [] (Byval index0 As Integer) Byref As Integer
End Type
Operator Array0.[] (Byval index0 As Integer) Byref As Integer
  Return This.a0(index0)
End Operator

Type Array1
   As Array0 a1(1 To 5)
   Declare Operator [] (Byval index1 As Integer) Byref As Array0
End Type
Operator Array1.[] (Byval index1 As Integer) Byref As Array0
  Return This.a1(index1)
End Operator

'--- Zone beginning of code insertion ---

'--- Zone ending of code insertion ------

For I As Integer = 1 To 5
  For J As Integer = 1 To 9
    Print a[I][J];
  Next J
  Print
Next I
Print

Sleep

Code: Select all

 11 12 13 14 15 16 17 18 19
 21 22 23 24 25 26 27 28 29
 31 32 33 34 35 36 37 38 39
 41 42 43 44 45 46 47 48 49
 51 52 53 54 55 56 57 58 59
(obviously the right solution must use the above predefined Types)

[edit]
Enigma #16 #17
Last edited by fxm on May 14, 2017 8:42, edited 1 time in total.
Post Reply