I will guess your number

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

I will guess your number

Post by srvaldez »

Code: Select all

print " Think of a Number between 1 and 127"
print "press RETURN when you have picked a number"
print "and I will present you with 7 screens of numbers"
print "on each screen of numbers, see if your number is there"
print "and answer y or n to the question"
print
dim as integer i, j, m, n, k, l, p, yn = 0
dim as string a
m = 7
n = (2 ^ m) - 1
DIM as integer c(m, (2 ^ (m - 1)))
k = 1
l = 1
input "Press Return to start ";a
WHILE k < n
  p = 1
  FOR i = k TO n STEP k * 2
    FOR j = i TO i + k - 1
      c(l, p) = j
      p = p + 1
    NEXT
  NEXT
  l = l + 1
  k = k * 2
WEND
FOR i = 1 TO m
  FOR j = 1 TO (2 ^ (m - 1)) STEP 4
    print using "####";c(i, j);
    PRINT using "####";c(i, j + 1);
    PRINT using "####";c(i, j + 2);
    PRINT using "####";c(i, j + 3)
  NEXT j
  INPUT "is your number there? ",a
  if a = "y" then yn+=c(i,1)
  cls
NEXT i
print "your number is ";yn
sleep
can anyone tell how it works? (I know, of course)
I3I2UI/I0
Posts: 90
Joined: Jun 03, 2005 10:39
Location: Germany

Post by I3I2UI/I0 »

Code: Select all

Print " Think of a Number between 1 and 127"
Print "press RETURN when you have picked a number"
Print "and I will present you with 7 screens of numbers"
Print "on each screen of numbers, see if your number is there"
Print "and answer y or n to the question"
Print
Dim As Integer i, j, m, n, k, l, p, yn = 0
Dim As String a
m = 7
n = (2 ^ m) - 1
Dim As Integer c(m, (2 ^ (m - 1)))
k = 1
l = 1
Input "Press Return to start ";a
While k < n
  p = 1
  For i = k To n Step k * 2
    For j = i To i + k - 1
      c(l, p) = j
      p = p + 1
    Next
  Next
  l = l + 1
  k = k * 2
Wend
For i = 1 To m
  For j = 1 To (2 ^ (m - 1)) Step 4
    Print Using "####";c(i, j);
    Print Using "####";c(i, j + 1);
    Print Using "####";c(i, j + 2);
    Print Using "####";c(i, j + 3)
  Next j
'------------------------------------  
  If Bit(79,(i-1)) Then '<- NUM3ER
    a="y"
  Else
    a="n"
  EndIf 
  Print "is your number there? ",a
  Sleep 1000
'------------------------------------  
  If a = "y" Then yn+=c(i,1)
  Cls
Next i
Print "your number is ";yn
Sleep
 
I know, of course :-))
j_milton
Posts: 458
Joined: Feb 11, 2010 17:35

Post by j_milton »

Here is a different version of the same thing, a bit easier maybe... I have given the player an extra choice, they can think of 0 if they want ;-)

Code: Select all

Print "A different version of the guessing puzzle:"
Print 
Print "Think of a Number between 0 and 127"
Print "press RETURN when you have picked a number"
Print "and I will present you with 7 screens of numbers"
Print "on each screen of numbers, see if your number is there"
Print "and answer y or n to the question"
Print
Dim As String s
Input "Press Return to start ";s
Dim As Integer page, printcount
Dim As UByte count
Dim As UByte mask = 127
Dim As UByte thisbit = 1 
For page = 1 To 7
    Cls
    printcount = 0
    For count = 1 To 127 
        If count And thisbit Then
            Print count,
            printcount += 1
            If printcount = 4 Then
                Print
                printcount = 0
            EndIf
        EndIf
    Next
    Print
    Input "Is your number here (y/n)";s
    If LCase(Left(Trim(s),1)) = "n" Then mask -= thisbit
    thisbit *= 2
Next
Print "You were thinking of the number: " & mask
Sleep
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

A bit easier, you say?
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Post by marcov »

If you want to make it difficult, at least make it in base-17 instead of 2 :-)
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Post by srvaldez »

Ok, here's how it works, admitedly the the way the array is calculated is brute.
1: (print first screen of numbers) Print all number between 1 and 127 whose bit 0 is set.
2: (print second screen of numbers) Print all number between 1 and 127 whose bit 1 is set.
and so on for the rest.
notice that the top-lef numbers are powers of 2, to get the number that was picked add the powers of 2 where the number is present.
because any number can be represented as the sum of powers of 2.

Code: Select all

print " Think of a Number between 1 and 127"
print "press RETURN when you have picked a number"
print "and I will present you with 7 screens of numbers"
print "on each screen of numbers, see if your number is there"
print "and answer y or n to the question"
print

dim as integer i, j, k, yn = 0
dim as string a

input "Press Return to start ";a
for k=0 to 7-1
    i=1
    do
        j=0
        do
        if bit(i,k) then
            print using "####";i;
            j+=1
        end if
        i+=1
        loop until j=4
        print
    loop until i>127
    input "is your number there? ",a
    if a = "y" then yn+=2^k
next
print "your number is ";yn
sleep
<edit> @marcov
that's it.
Last edited by srvaldez on Sep 17, 2010 17:10, edited 1 time in total.
j_milton
Posts: 458
Joined: Feb 11, 2010 17:35

Post by j_milton »

this variation has a bit more variety to it, not always the same number of possibilities offered per screen

Code: Select all

Print "A variation on the guessing puzzle:"
Print 
Print "Think of a Number between 0 and 127"
Print "press RETURN when you have picked a number"
Print "and I will present you with 7 screens of numbers"
Print "on each screen of numbers, see if your number is there"
Print "and answer y or n to the question"
Print
Dim As String s
Input "Press Return to start ";s
Dim As Integer page, printcount
Dim As UByte count
Dim As UByte mask = 127
Dim As UByte thisbit = 1
Dim As Byte notmap(1 To 127)
For page = 1 To 7
    Cls
    printcount = 0
    For count = 1 To 127 
        If count And thisbit And (notmap(count) = 0) Then
            Print count,
            printcount += 1
            If printcount = 4 Then
                Print
                printcount = 0
            EndIf
        EndIf
    Next
    Print
    Input "Is your number here (y/n)";s
    If LCase(Left(Trim(s),1)) = "n" Then 
        mask -= thisbit
        For count = 1 To 127
            If count And thisbit Then notmap(count) = 1
        Next
    EndIf
    thisbit *= 2
Next
Print "You were thinking of the number: " & mask
Sleep
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

My binary search macros work the same way. (naturally)

http://www.freebasic.net/forum/viewtopic.php?t=12765
JaDogg
Posts: 345
Joined: Apr 13, 2008 12:11
Location: Sri Lanka - Negombo
Contact:

Post by JaDogg »

Post Reply