Flipping Bits Game
#1
http://rosettacode.org/wiki/Flipping_bits_game
I like to program in BASIC
With code that is simple and slick
I learnt it in school
And it is still cool
So it is my number one pick
Reply
#2
Interesting game, I am wondering if one could move a 1 in the top left corner down to bottom right corner passing through center on a 3x3 for starters. 
1. is it possible
2. the fewest moves
3. systematically run the fewest moves

Might be good AI skills builder?
B += x
Reply
#3
Yes, it would be a good AI skills builder. Give it a try!
I like to program in BASIC
With code that is simple and slick
I learnt it in school
And it is still cool
So it is my number one pick
Reply
#4
Smile OK Adrian, here is the game:
Code:
_TITLE "Rosetta Code - Flipping Bits - by bplus 2017-12-16"
RANDOMIZE TIMER
DIM SHARED cellsPerSide, legalMoves$, startB$, currentB$, targetB$, moveCount
restart
DO
   CLS 'display current board and target
   COLOR 9: showBoard 2, 2, currentB$, "Current:"
   COLOR 12: showBoard 2, 2 + 2 * cellsPerSide + 6, targetB$, "Target:"
   COLOR 13: PRINT: PRINT " Number of moves taken so far is"; moveCount
   COLOR 14
   IF currentB$ = targetB$ THEN
       PRINT " Congratulations, done in"; moveCount; " moves."
       PRINT: INPUT " Enter y for yes, if you want to start over "; yes$
       IF yes$ = "y" THEN restart ELSE nomore = -1
   ELSE 'get next move
       m$ = " ": PRINT
       WHILE INSTR(legalMoves$, m$) = 0 OR m$ = "0" OR m$ = "q"
           INPUT " Enter a lettered column or a numbered row to flip (0 or q, quits) "; m$
           IF m$ = "0" OR m$ = "q" THEN END
           IF m$ = "" THEN m$ = " "
       WEND
       'handle move results
       IF m$ <> "0" AND m$ <> "q" THEN moveCount = moveCount + 1: makeMove m$
   END IF
LOOP UNTIL nomore

SUB restart
   cellsPerSide = 0: legalMoves$ = "": moveCount = 0: CLS
   COLOR 9: cp 3, "Flipping Bits Game:"
   COLOR 5
   cp 5, "You will presented with a square board marked Current and"
   cp 6, "another marked Target. The object of the game is to match"
   cp 7, "the Current board to Target in the least amount of moves."
   cp 9, "To make a move, enter a letter for a column to flip or"
   cp 10, "a digit for a row to flip. In a flip, all 1's are"
   cp 11, "changed to 0's and all 0's changed to 1's."
   cp 13, "You may enter 0 or q at any time to quit."
   cp 15, "I'd say good luck but this is about skill! "
   PRINT: irow = 16: COLOR 14
   WHILE cellsPerSide < 2 OR cellsPerSide > 9
       irow = irow + 1
       cp irow, " Please enter how many cells you want per side 2 to 9 "
       INPUT ; in$
       IF in$ = "0" OR in$ = "q" THEN END ELSE cellsPerSide = VAL(in$)
   WEND
   FOR i = 1 TO cellsPerSide: legalMoves$ = legalMoves$ + CHR$(96 + i): NEXT
   FOR i = 1 TO cellsPerSide: legalMoves$ = legalMoves$ + LTRIM$(STR$(i)): NEXT
   startB$ = startBoard$: currentB$ = startB$: targetB$ = makeTarget$: currentB$ = startB$
END SUB

FUNCTION startBoard$
   FOR i = 1 TO cellsPerSide ^ 2: r$ = r$ + LTRIM$(STR$(INT(RND * 2))): NEXT
   startBoard$ = r$
END FUNCTION

SUB showBoard (row, col, board$, title$)
   LOCATE row - 1, col: PRINT title$
   FOR i = 1 TO cellsPerSide
       LOCATE row, col + 2 * (i - 1) + 3: PRINT MID$(legalMoves$, i, 1)
   NEXT
   FOR i = 1 TO cellsPerSide
       LOCATE row + i, col - 1: PRINT STR$(i);
       FOR j = 1 TO cellsPerSide
           LOCATE row + i, col + 2 * j: PRINT " " + MID$(board$, (i - 1) * cellsPerSide + j, 1)
       NEXT
   NEXT
END SUB

SUB makeMove (move$)
   ac = ASC(move$)
   IF ac > 96 THEN 'letter
       col = ac - 96
       FOR i = 1 TO cellsPerSide
           bit$ = MID$(currentB$, (i - 1) * cellsPerSide + col, 1)
           IF bit$ = "0" THEN
               MID$(currentB$, (i - 1) * cellsPerSide + col, 1) = "1"
           ELSE
               MID$(currentB$, (i - 1) * cellsPerSide + col, 1) = "0"
           END IF
       NEXT
   ELSE 'number
       row = ac - 48
       FOR i = 1 TO cellsPerSide
           bit$ = MID$(currentB$, (row - 1) * cellsPerSide + i, 1)
           IF bit$ = "0" THEN
               MID$(currentB$, (row - 1) * cellsPerSide + i, 1) = "1"
           ELSE
               MID$(currentB$, (row - 1) * cellsPerSide + i, 1) = "0"
           END IF
       NEXT
   END IF
END SUB

FUNCTION makeTarget$
   WHILE currentB$ = startB$
       FOR i = 1 TO cellsPerSide * cellsPerSide
           m$ = MID$(legalMoves$, INT(RND * LEN(legalMoves$)) + 1, 1)
           makeMove m$
       NEXT
   WEND
   makeTarget$ = currentB$
END FUNCTION

SUB cp (row, text$) 'center print at row
   LOCATE row, (80 - LEN(text$)) / 2: PRINT text$;
END SUB


Attached Files Thumbnail(s)

B += x
Reply
#5
fantastic, bplus! Added!
I like to program in BASIC
With code that is simple and slick
I learnt it in school
And it is still cool
So it is my number one pick
Reply
#6
Better get a 3x3 example posted with it as part of the requirements.


Attached Files Thumbnail(s)

B += x
Reply
#7
OK the Virtual Screen (VS) version of Flipping Bits Game:
Code:
_TITLE "Rosetta Code - Flipping Bits - by bplus 2017-12-17"
'This version employs a VS array that acts as a Virtual screen.
'All commands printing to screen have been rewritten to print to screen
'AND echo the same text and locations to the VS array.
'Just before each screen clear, the VS array is dumped into the
'Copy Flipping Bits Game.txt file. So this file always contains
'the last game played. b + 2017-12-17

RANDOMIZE TIMER
DIM SHARED cellsPerSide, legalMoves$, startB$, currentB$, targetB$, moveCount
DIM SHARED VS(23) AS STRING * 80, prow, pcol

restart
vcls
DO
   COLOR 9: showBoard 2, 2, currentB$, "Current:"
   COLOR 12: showBoard 2, 2 + 2 * cellsPerSide + 6, targetB$, "Target:"
   COLOR 13: vplf "": vplf " Number of moves taken so far is" + STR$(moveCount)
   COLOR 14
   IF currentB$ = targetB$ THEN
       vplf " Congratulations, done in" + STR$(moveCount) + " moves."
       vplf "": vpstop " Enter y for yes, if you want to start over "
       INPUT ; yes$
       vplf yes$
       vcls
       IF yes$ = "y" THEN restart: vcls ELSE nomore = -1
   ELSE 'get next move
       m$ = " ": vplf ""
       WHILE INSTR(legalMoves$, m$) = 0 OR m$ = "0" OR m$ = "q"
           vpstop " Enter a lettered column or a numbered row to flip (0 or q, quits) "
           INPUT ; m$
           vplf m$
           'get a copy of game
           vcls
           IF m$ = "0" OR m$ = "q" THEN CLOSE: END
           IF m$ = "" THEN m$ = " "
       WEND
       'handle move results
       IF m$ <> "0" AND m$ <> "q" THEN moveCount = moveCount + 1: makeMove m$
   END IF
LOOP UNTIL nomore
CLOSE

SUB restart
   CLOSE
   OPEN "Copy Flipping Bits Game.txt" FOR OUTPUT AS #3
   cellsPerSide = 0: legalMoves$ = "": moveCount = 0
   COLOR 9: cp 3, "Flipping Bits Game,    b+ 2017-12-17"
   COLOR 5
   cp 5, "You will presented with a square board marked Current and"
   cp 6, "another marked Target. The object of the game is to match"
   cp 7, "the Current board to Target in the least amount of moves."
   cp 9, "To make a move, enter a letter for a column to flip or"
   cp 10, "a digit for a row to flip. In a flip, all 1's are"
   cp 11, "changed to 0's and all 0's changed to 1's."
   cp 13, "You may enter 0 or q at any time to quit."
   cp 15, "I'd say good luck but this is about skill! "
   vplf "": irow = 16: COLOR 14
   WHILE cellsPerSide < 2 OR cellsPerSide > 9
       irow = irow + 1
       cp irow, " Please enter how many cells you want per side 2 to 9 "
       INPUT ; in$
       vplf in$
       IF in$ = "0" OR in$ = "q" THEN END ELSE cellsPerSide = VAL(in$)
   WEND
   FOR i = 1 TO cellsPerSide: legalMoves$ = legalMoves$ + CHR$(96 + i): NEXT
   FOR i = 1 TO cellsPerSide: legalMoves$ = legalMoves$ + LTRIM$(STR$(i)): NEXT
   startB$ = startBoard$: currentB$ = startB$: targetB$ = makeTarget$: currentB$ = startB$
END SUB

FUNCTION startBoard$
   FOR i = 1 TO cellsPerSide ^ 2: r$ = r$ + LTRIM$(STR$(INT(RND * 2))): NEXT
   startBoard$ = r$
END FUNCTION

SUB showBoard (row, col, board$, title$)
   vl row - 1, col: vplf title$
   FOR i = 1 TO cellsPerSide
       vl row, col + 2 * (i - 1) + 3: vpstop MID$(legalMoves$, i, 1)
   NEXT
   vplf ""
   FOR i = 1 TO cellsPerSide
       vl row + i, col - 1: vpstop STR$(i)
       FOR j = 1 TO cellsPerSide
           vl row + i, col + 2 * j: vpstop " " + MID$(board$, (i - 1) * cellsPerSide + j, 1)
       NEXT
       vplf ""
   NEXT
END SUB

SUB makeMove (move$)
   ac = ASC(move$)
   IF ac > 96 THEN 'letter
       col = ac - 96
       FOR i = 1 TO cellsPerSide
           bit$ = MID$(currentB$, (i - 1) * cellsPerSide + col, 1)
           IF bit$ = "0" THEN
               MID$(currentB$, (i - 1) * cellsPerSide + col, 1) = "1"
           ELSE
               MID$(currentB$, (i - 1) * cellsPerSide + col, 1) = "0"
           END IF
       NEXT
   ELSE 'number
       row = ac - 48
       FOR i = 1 TO cellsPerSide
           bit$ = MID$(currentB$, (row - 1) * cellsPerSide + i, 1)
           IF bit$ = "0" THEN
               MID$(currentB$, (row - 1) * cellsPerSide + i, 1) = "1"
           ELSE
               MID$(currentB$, (row - 1) * cellsPerSide + i, 1) = "0"
           END IF
       NEXT
   END IF
END SUB

FUNCTION makeTarget$
   WHILE currentB$ = startB$
       FOR i = 1 TO cellsPerSide * cellsPerSide
           m$ = MID$(legalMoves$, INT(RND * LEN(legalMoves$)) + 1, 1)
           makeMove m$
       NEXT
   WEND
   makeTarget$ = currentB$
END FUNCTION

'v subs for Virtual Screen, everything we print on screen we want to print to file

SUB cp (row, text$) 'center print at row
   LOCATE row, (80 - LEN(text$)) / 2: PRINT text$;
   MID$(VS(row), (80 - LEN(text$)) / 2, LEN(text$)) = text$
   prow = row: pcol = (80 - LEN(text$)) / 2 + LEN(text$) + 1
END SUB

SUB vplf (text$) 'print with line feed
   PRINT text$
   MID$(VS(prow), pcol, LEN(text$)) = text$
   prow = prow + 1: pcol = 1
END SUB

SUB vpstop (text$) 'print with ;
   PRINT text$;
   MID$(VS(prow), pcol, LEN(text$)) = text$
   pcol = pcol + LEN(text$) + 1
END SUB

SUB vl (r, c) 'virtual locate
   LOCATE r, c
   prow = r: pcol = c
END SUB

SUB vcls
   DIM blank AS STRING * 80
   FOR fini = 23 TO 1 STEP -1
       IF VS(fini) <> blank THEN EXIT FOR
   NEXT
   PRINT #3, ""
   FOR i = 1 TO fini
       PRINT #3, VS(i)
   NEXT
   PRINT #3, ""
   PRINT #3, STRING$(80, "-")
   CLS
   ERASE VS
   prow = 1: pcol = 1
END SUB

And the output file:
Code:
                                                                                
                                                                                
                     Flipping Bits Game,    b+ 2017-12-17                       
                                                                                
           You will presented with a square board marked Current and            
           another marked Target. The object of the game is to match            
           the Current board to Target in the least amount of moves.            
                                                                                
            To make a move, enter a letter for a column to flip or              
               a digit for a row to flip. In a flip, all 1's are                
                  changed to 0's and all 0's changed to 1's.                    
                                                                                
                   You may enter 0 or q at any time to quit.                    
                                                                                
                 I'd say good luck but this is about skill!                   
                                                                                
             Please enter how many cells you want per side 2 to 9  3            

--------------------------------------------------------------------------------

 Current:    Target:                                                            
    a b c       a b c                                                           
 1  1 0 0    1  1 1 1                                                           
 2  0 0 0    2  0 1 1                                                           
 3  0 1 0    3  1 1 0                                                           
                                                                                
 Number of moves taken so far is 0                                              
                                                                                
 Enter a lettered column or a numbered row to flip (0 or q, quits)  b           

--------------------------------------------------------------------------------

 Current:    Target:                                                            
    a b c       a b c                                                           
 1  1 1 0    1  1 1 1                                                           
 2  0 1 0    2  0 1 1                                                           
 3  0 0 0    3  1 1 0                                                           
                                                                                
 Number of moves taken so far is 1                                              
                                                                                
 Enter a lettered column or a numbered row to flip (0 or q, quits)  c           

--------------------------------------------------------------------------------

 Current:    Target:                                                            
    a b c       a b c                                                           
 1  1 1 1    1  1 1 1                                                           
 2  0 1 1    2  0 1 1                                                           
 3  0 0 1    3  1 1 0                                                           
                                                                                
 Number of moves taken so far is 2                                              
                                                                                
 Enter a lettered column or a numbered row to flip (0 or q, quits)  3           

--------------------------------------------------------------------------------

 Current:    Target:                                                            
    a b c       a b c                                                           
 1  1 1 1    1  1 1 1                                                           
 2  0 1 1    2  0 1 1                                                           
 3  1 1 0    3  1 1 0                                                           
                                                                                
 Number of moves taken so far is 3                                              
 Congratulations, done in 3 moves.                                              
                                                                                
 Enter y for yes, if you want to start over                                     

--------------------------------------------------------------------------------
A copy of the txt file is attached.
The text lines up better on screen or in Wordpad or in a Code window.


Attached Files .txt   12-17 Copy Flipping Bits Game.txt (Size: 4.74 KB / Downloads: 4)
B += x
Reply
#8
Dang it!  [Image: angry.gif]

I was working on AI for Flipping Bits Game and found a bug in previous VS version when a bad move was INPUT.
OK I fixed that but then got a double vision of the Entered Keypress, so I changed ALL the INPUTs to getKey$ function which means all prompts that asked "Enter... " had to be changed as well. There was also some messy code cleared up involving when to save a screen dump so that the game was recorded accurately, it was working just ugly.

One bug caused a wave of change to my VS version of the Flipping Bits Game now revised to:
Code:
_TITLE "Rosetta Code - Flipping Bits - by bplus 2017-12-17"
'2017-12-17 3 PM found bug in program when bad move is input - fixed
'Oh but now have to fix double vision of input, need new function getKey$
'replace all INPUTs with getKey$ and update whole program.
'2017-12-17 ~6 AM
'This version employs a VS array that acts as a Virtual screen.
'All commands printing to screen have been rewritten to print to screen
'AND echo the same text and locations to the VS array.
'Just before each screen clear, the VS array is dumped into the
'Copy Flipping Bits Game.txt file. So this file always contains
'the last game played. b + 2017-12-17

RANDOMIZE TIMER
DIM SHARED cellsPerSide, legalMoves$, startB$, currentB$, targetB$, moveCount
DIM SHARED VS(23) AS STRING * 80, prow, pcol

restart
DO
   COLOR 9: showBoard 2, 2, currentB$, "Current:"
   COLOR 12: showBoard 2, 2 + 2 * cellsPerSide + 6, targetB$, "Target:"
   COLOR 13: vplf "": vplf " Number of moves taken so far is" + STR$(moveCount)
   COLOR 14
   IF currentB$ = targetB$ THEN
       vplf " Congratulations, done in" + STR$(moveCount) + " moves."
       vplf "": vpstop " Press y for yes, if you want to start over "
       yes$ = getKey$
       vcls
       IF yes$ = "y" THEN restart ELSE nomore = -1
   ELSE 'get next move
       m$ = " ": vplf ""
       WHILE INSTR(legalMoves$, m$) = 0
           vpstop " Press a lettered column or a numbered row to flip (0 or q, quits) > "
           m$ = getKey$
           'get a copy of game
           IF m$ = "0" OR m$ = "q" THEN vcls: CLOSE: END
           IF m$ = "" THEN m$ = " "
       WEND
       vcls
       moveCount = moveCount + 1: makeMove m$
   END IF
LOOP UNTIL nomore
CLOSE

SUB restart
   CLOSE
   OPEN "Copy Flipping Bits Game.txt" FOR OUTPUT AS #3
   cellsPerSide = 0: legalMoves$ = "": moveCount = 0
   COLOR 9: cp 3, "Flipping Bits Game,    b+ 2017-12-17"
   COLOR 5
   cp 5, "You will be presented with a square board marked Current and"
   cp 6, "another marked Target. The object of the game is to match"
   cp 7, "the Current board to Target in the least amount of moves."
   cp 9, "To make a move, press a letter for a column to flip or"
   cp 10, "a digit for a row to flip. In a flip, all 1's are"
   cp 11, "changed to 0's and all 0's changed to 1's."
   cp 13, "You may press 0 or q at any time to quit."
   cp 15, "I'd say good luck but this is about skill! "
   vplf "": irow = 16: COLOR 14
   WHILE cellsPerSide < 2 OR cellsPerSide > 9
       irow = irow + 1
       cp irow, " Please press how many cells you want per side 2 to 9 > "
       in$ = getKey$
       IF in$ = "0" OR in$ = "q" THEN END ELSE cellsPerSide = VAL(in$)
   WEND
   vcls
   FOR i = 1 TO cellsPerSide: legalMoves$ = legalMoves$ + CHR$(96 + i): NEXT
   FOR i = 1 TO cellsPerSide: legalMoves$ = legalMoves$ + LTRIM$(STR$(i)): NEXT
   startB$ = startBoard$: currentB$ = startB$: targetB$ = makeTarget$: currentB$ = startB$
END SUB

FUNCTION startBoard$
   FOR i = 1 TO cellsPerSide ^ 2: r$ = r$ + LTRIM$(STR$(INT(RND * 2))): NEXT
   startBoard$ = r$
END FUNCTION

SUB showBoard (row, col, board$, title$)
   vl row - 1, col: vplf title$
   FOR i = 1 TO cellsPerSide
       vl row, col + 2 * (i - 1) + 3: vpstop MID$(legalMoves$, i, 1)
   NEXT
   vplf ""
   FOR i = 1 TO cellsPerSide
       vl row + i, col - 1: vpstop STR$(i)
       FOR j = 1 TO cellsPerSide
           vl row + i, col + 2 * j: vpstop " " + MID$(board$, (i - 1) * cellsPerSide + j, 1)
       NEXT
       vplf ""
   NEXT
END SUB

SUB makeMove (move$)
   ac = ASC(move$)
   IF ac > 96 THEN 'letter
       col = ac - 96
       FOR i = 1 TO cellsPerSide
           bit$ = MID$(currentB$, (i - 1) * cellsPerSide + col, 1)
           IF bit$ = "0" THEN
               MID$(currentB$, (i - 1) * cellsPerSide + col, 1) = "1"
           ELSE
               MID$(currentB$, (i - 1) * cellsPerSide + col, 1) = "0"
           END IF
       NEXT
   ELSE 'number
       row = ac - 48
       FOR i = 1 TO cellsPerSide
           bit$ = MID$(currentB$, (row - 1) * cellsPerSide + i, 1)
           IF bit$ = "0" THEN
               MID$(currentB$, (row - 1) * cellsPerSide + i, 1) = "1"
           ELSE
               MID$(currentB$, (row - 1) * cellsPerSide + i, 1) = "0"
           END IF
       NEXT
   END IF
END SUB

FUNCTION makeTarget$
   WHILE currentB$ = startB$
       FOR i = 1 TO cellsPerSide * cellsPerSide
           m$ = MID$(legalMoves$, INT(RND * LEN(legalMoves$)) + 1, 1)
           makeMove m$
       NEXT
   WEND
   makeTarget$ = currentB$
END FUNCTION

'v subs for Virtual Screen, everything we print on screen we want to print to file

SUB cp (row, text$) 'center print at row
   LOCATE row, (80 - LEN(text$)) / 2: PRINT text$;
   MID$(VS(row), (80 - LEN(text$)) / 2, LEN(text$)) = text$
   prow = row: pcol = (80 - LEN(text$)) / 2 + LEN(text$) + 1
END SUB

SUB vplf (text$) 'print with line feed
   PRINT text$
   MID$(VS(prow), pcol, LEN(text$)) = text$
   prow = prow + 1: pcol = 1
END SUB

SUB vpstop (text$) 'print with ;
   PRINT text$;
   MID$(VS(prow), pcol, LEN(text$)) = text$
   pcol = pcol + LEN(text$)
END SUB

SUB vl (r, c) 'virtual locate
   LOCATE r, c
   prow = r: pcol = c
END SUB

SUB vcls
   DIM blank AS STRING * 80
   FOR fini = 23 TO 1 STEP -1
       IF VS(fini) <> blank THEN EXIT FOR
   NEXT
   PRINT #3, ""
   FOR i = 1 TO fini
       PRINT #3, VS(i)
   NEXT
   PRINT #3, ""
   PRINT #3, STRING$(80, "-")
   CLS
   ERASE VS
   prow = 1: pcol = 1
END SUB

FUNCTION getKey$
   k$ = ""
   WHILE LEN(k$) = 0
       k$ = INKEY$
   WEND
   vplf k$
   _DELAY .4
   getKey$ = k$
END FUNCTION


And the Output text file copy of a Game played when several wrong move keys were pressed:
Code:
                                                                               
                                                                               
                    Flipping Bits Game,    b+ 2017-12-17                      
                                                                               
        You will be presented with a square board marked Current and          
          another marked Target. The object of the game is to match            
          the Current board to Target in the least amount of moves.            
                                                                               
           To make a move, press a letter for a column to flip or              
              a digit for a row to flip. In a flip, all 1's are                
                 changed to 0's and all 0's changed to 1's.                    
                                                                               
                  You may press 0 or q at any time to quit.                    
                                                                               
                I'd say good luck but this is about skill!                   
                                                                               
           Please press how many cells you want per side 2 to 9 >  3          

--------------------------------------------------------------------------------

Current:    Target:                                                            
   a b c       a b c                                                          
1  1 1 0    1  0 0 1                                                          
2  0 1 1    2  1 0 0                                                          
3  0 0 0    3  1 1 1                                                          
                                                                               
Number of moves taken so far is 0                                              
                                                                               
Press a lettered column or a numbered row to flip (0 or q, quits) > h          
Press a lettered column or a numbered row to flip (0 or q, quits) > 9          
Press a lettered column or a numbered row to flip (0 or q, quits) > a          

--------------------------------------------------------------------------------

Current:    Target:                                                            
   a b c       a b c                                                          
1  0 1 0    1  0 0 1                                                          
2  1 1 1    2  1 0 0                                                          
3  1 0 0    3  1 1 1                                                          
                                                                               
Number of moves taken so far is 1                                              
                                                                               
Press a lettered column or a numbered row to flip (0 or q, quits) > h          
Press a lettered column or a numbered row to flip (0 or q, quits) > i          
Press a lettered column or a numbered row to flip (0 or q, quits) > b          

--------------------------------------------------------------------------------

Current:    Target:                                                            
   a b c       a b c                                                          
1  0 0 0    1  0 0 1                                                          
2  1 0 1    2  1 0 0                                                          
3  1 1 0    3  1 1 1                                                          
                                                                               
Number of moves taken so far is 2                                              
                                                                               
Press a lettered column or a numbered row to flip (0 or q, quits) > 4          
Press a lettered column or a numbered row to flip (0 or q, quits) > 5          
Press a lettered column or a numbered row to flip (0 or q, quits) > 6          
Press a lettered column or a numbered row to flip (0 or q, quits) > c          

--------------------------------------------------------------------------------

Current:    Target:                                                            
   a b c       a b c                                                          
1  0 0 1    1  0 0 1                                                          
2  1 0 0    2  1 0 0                                                          
3  1 1 1    3  1 1 1                                                          
                                                                               
Number of moves taken so far is 3                                              
Congratulations, done in 3 moves.                                              
                                                                               
Press y for yes, if you want to start over
                                 

--------------------------------------------------------------------------------

All better now, I hope Tongue  Big Grin

PS the column letters really do line up just fine in a proper Editor! Wink
See for yourself in attached txt file.


Attached Files .txt   12-17 3PM Copy Flipping Bits Game.txt (Size: 5.3 KB / Downloads: 3)
B += x
Reply
#9
Flipping Bits now can give hints or solve the game for you:
Code:
_TITLE "AI+VS Flipping Bits now with AI - by bplus 2017-12-17"
'2017-12-17 AI+VS Flipping Bits now with AI
'adding the option for user to press ? for a hint for a move
'adding the option for user to press ! for the puzzle to be solved,
'this option will post all moves to screen and text file to
'review the game.
'2017-12-17 3 PM found bug in program when bad move is input - fixed
'Oh but now have to fix double vision of input, need new function getKey$
'replace all INPUTs with getKey$ and update whole program.
'2017-12-17 ~6 AM VS Flipping Bits
'This version employs a VS array that acts as a Virtual screen.
'All commands printing to screen have been rewritten to print to screen
'AND echo the same text and locations to the VS array.
'Just before each screen clear, the VS array is dumped into the
'Copy Flipping Bits Game.txt file. So this file always contains
'the last game played. b + 2017-12-17

RANDOMIZE TIMER
DIM SHARED cellsPerSide, legalMoves$, startB$, currentB$, targetB$, moveCount
DIM SHARED VS(23) AS STRING * 80, prow, pcol

restart
DO
   displayStatus
   IF currentB$ = targetB$ THEN
       vplf " Congratulations, done in" + STR$(moveCount) + " moves."
       vplf "": cp prow, " Press y for yes, if you want to start over "
       yes$ = getKey$: cp prow, yes$: _DELAY .4
       vcls
       IF yes$ = "y" THEN restart ELSE nomore = -1
   ELSE 'get next move
       m$ = " ": vplf ""
       WHILE INSTR(legalMoves$, m$) = 0
           vpstop " Press a lettered column or a numbered row to flip (or 0,q,?,!) > "
           m$ = getKey$: vplf m$: _DELAY .4
           IF m$ = "!" THEN
               showSolution = -1: m$ = " ": EXIT WHILE
           ELSEIF m$ = "?" THEN
               m$ = " ": cp prow, "Hint: " + hint$
           ELSEIF m$ = "0" OR m$ = "q" THEN
               vcls: CLOSE: END
           ELSEIF m$ = "" THEN
               m$ = " "
           END IF
       WEND
       IF showSolution THEN
           showSolution = 0
           mv$ = hint$
           cp prow + 1, "For the next move, the AI has chosen: " + mv$
           cp prow + 1, "Running the solution with 4 sec screen delays..."
           _DELAY 4
           vcls
           WHILE mv$ <> "Done?"
               moveCount = moveCount + 1: makeMove mv$
               displayStatus
               mv$ = hint$
               cp prow + 1, "For the next move, the AI has chosen: " + mv$
               cp prow + 1, "Running the solution with 4 sec screen delays..."
               _DELAY 4
               vcls
           WEND
           displayStatus
           IF currentB$ = targetB$ THEN
               cp prow + 1, "Done! Current board matches Target"
           ELSE
               cp prow + 1, "Hmm... Current board does NOT match Target???"
           END IF
           cp prow + 1, "Press y for yes, if you want to start over:"
           yes$ = getKey$
           cp prow, yes$: _DELAY .4
           vcls
           IF yes$ = "y" THEN restart ELSE nomore = -1
       ELSE
           vcls: moveCount = moveCount + 1: makeMove m$
       END IF
   END IF
LOOP UNTIL nomore
CLOSE

SUB displayStatus
   COLOR 9: showBoard 2, 2, currentB$, "Current:"
   COLOR 12: showBoard 2, 2 + 2 * cellsPerSide + 6, targetB$, "Target:"
   COLOR 13: vplf "": vplf " Number of moves taken so far is" + STR$(moveCount)
   COLOR 14
END SUB

'this function will compare the currentB to targetB and suggest the next move with letter or digit
FUNCTION hint$
   FOR i = 1 TO 2 * cellsPerSide 'check cols first then rows as listed in legalMoves$
       r$ = MID$(legalMoves$, i, 1)
       IF i <= cellsPerSide THEN
           currentbit$ = MID$(currentB$, i, 1)
           targetBit$ = MID$(targetB$, i, 1)
           IF currentbit$ <> targetBit$ THEN flag = -1: EXIT FOR
       ELSE
           j = i - cellsPerSide
           currentbit$ = MID$(currentB$, (j - 1) * cellsPerSide + 1, 1)
           targetBit$ = MID$(targetB$, (j - 1) * cellsPerSide + 1, 1)
           IF currentbit$ <> targetBit$ THEN flag = -1: EXIT FOR
       END IF
   NEXT
   IF flag THEN hint$ = r$ ELSE hint$ = "Done?"
END FUNCTION

SUB restart
   CLOSE
   OPEN "Copy Flipping Bits Game.txt" FOR OUTPUT AS #3
   cellsPerSide = 0: legalMoves$ = "": moveCount = 0
   COLOR 9: cp 3, "Flipping Bits Game, now with AI!  b+ 2017-12-18"
   COLOR 5
   cp 5, "You will be presented with a square board marked Current and"
   cp 6, "another marked Target. The object of the game is to match"
   cp 7, "the Current board to Target in the least amount of moves."
   cp 9, "To make a move, enter a letter for a column to flip or"
   cp 10, "a digit for a row to flip. In a flip, all 1's are"
   cp 11, "changed to 0's and all 0's changed to 1's."
   cp 13, "You may enter 0 or q at any time to quit."
   cp 14, "You may press ? when prompted for move to get a hint."
   cp 15, "You may press ! to have the program solve the puzzle."
   irow = 16: COLOR 14
   WHILE cellsPerSide < 2 OR cellsPerSide > 9
       irow = irow + 1
       vl irow, 12: vpstop "Please press how many cells you want per side 2 to 9 > "
       in$ = getKey$
       vplf in$: _DELAY .4
       IF in$ = "0" OR in$ = "q" THEN END ELSE cellsPerSide = VAL(in$)
   WEND
   vcls
   FOR i = 1 TO cellsPerSide: legalMoves$ = legalMoves$ + CHR$(96 + i): NEXT
   FOR i = 1 TO cellsPerSide: legalMoves$ = legalMoves$ + LTRIM$(STR$(i)): NEXT
   startB$ = startBoard$: currentB$ = startB$: targetB$ = makeTarget$: currentB$ = startB$
END SUB

FUNCTION startBoard$
   FOR i = 1 TO cellsPerSide ^ 2: r$ = r$ + LTRIM$(STR$(INT(RND * 2))): NEXT
   startBoard$ = r$
END FUNCTION

SUB showBoard (row, col, board$, title$)
   vl row - 1, col: vplf title$
   FOR i = 1 TO cellsPerSide
       vl row, col + 2 * (i - 1) + 3: vpstop MID$(legalMoves$, i, 1)
   NEXT
   vplf ""
   FOR i = 1 TO cellsPerSide
       vl row + i, col - 1: vpstop STR$(i)
       FOR j = 1 TO cellsPerSide
           vl row + i, col + 2 * j: vpstop " " + MID$(board$, (i - 1) * cellsPerSide + j, 1)
       NEXT
       vplf ""
   NEXT
END SUB

SUB makeMove (move$)
   ac = ASC(move$)
   IF ac > 96 THEN 'letter
       col = ac - 96
       FOR i = 1 TO cellsPerSide
           bit$ = MID$(currentB$, (i - 1) * cellsPerSide + col, 1)
           IF bit$ = "0" THEN
               MID$(currentB$, (i - 1) * cellsPerSide + col, 1) = "1"
           ELSE
               MID$(currentB$, (i - 1) * cellsPerSide + col, 1) = "0"
           END IF
       NEXT
   ELSE 'number
       row = ac - 48
       FOR i = 1 TO cellsPerSide
           bit$ = MID$(currentB$, (row - 1) * cellsPerSide + i, 1)
           IF bit$ = "0" THEN
               MID$(currentB$, (row - 1) * cellsPerSide + i, 1) = "1"
           ELSE
               MID$(currentB$, (row - 1) * cellsPerSide + i, 1) = "0"
           END IF
       NEXT
   END IF
END SUB

FUNCTION makeTarget$
   WHILE currentB$ = startB$
       FOR i = 1 TO cellsPerSide * cellsPerSide
           m$ = MID$(legalMoves$, INT(RND * LEN(legalMoves$)) + 1, 1)
           makeMove m$
       NEXT
   WEND
   makeTarget$ = currentB$
END FUNCTION

'v subs for Virtual Screen, everything we print on screen we want to print to file

SUB cp (row, text$) 'center print at row
   LOCATE row, (80 - LEN(text$)) / 2: PRINT text$;
   MID$(VS(row), (80 - LEN(text$)) / 2, LEN(text$)) = text$
   prow = row + 1: pcol = 1
END SUB

SUB vplf (text$) 'print with line feed
   PRINT text$
   MID$(VS(prow), pcol, LEN(text$)) = text$
   prow = prow + 1: pcol = 1
END SUB

SUB vpstop (text$) 'print with ;
   PRINT text$;
   MID$(VS(prow), pcol, LEN(text$)) = text$
   pcol = pcol + LEN(text$)
END SUB

SUB vl (r, c) 'virtual locate
   LOCATE r, c
   prow = r: pcol = c
END SUB

SUB vcls
   DIM blank AS STRING * 80
   FOR fini = 23 TO 1 STEP -1
       IF VS(fini) <> blank THEN EXIT FOR
   NEXT
   PRINT #3, ""
   FOR i = 1 TO fini
       PRINT #3, VS(i)
   NEXT
   PRINT #3, ""
   PRINT #3, STRING$(80, "-")
   CLS
   ERASE VS
   prow = 1: pcol = 1
END SUB

FUNCTION getKey$ 'just want printable characters
   k$ = ""
   WHILE LEN(k$) = 0
       k$ = INKEY$
       IF LEN(k$) THEN 'press something so respond
           IF LEN(k$) = 2 THEN
               k$ = "*": BEEP
           ELSE
               IF ASC(k$) > 126 OR ASC(k$) < 32 THEN k$ = "*": BEEP
           END IF
       END IF
   WEND
   getKey$ = k$
END FUNCTION

And a sample output with bad moves, hints and the puzzle solved by "AI".
Code:
                                                                               
                                                                               
              Flipping Bits Game, now with AI!  b+ 2017-12-18                  
                                                                               
        You will be presented with a square board marked Current and          
          another marked Target. The object of the game is to match            
          the Current board to Target in the least amount of moves.            
                                                                               
           To make a move, enter a letter for a column to flip or              
              a digit for a row to flip. In a flip, all 1's are                
                 changed to 0's and all 0's changed to 1's.                    
                                                                               
                  You may enter 0 or q at any time to quit.                    
            You may press ? when prompted for move to get a hint.              
            You may press ! to have the program solve the puzzle.              
                                                                               
          Please press how many cells you want per side 2 to 9 > 9            

--------------------------------------------------------------------------------

Current:                Target:                                                
   a b c d e f g h i       a b c d e f g h i                                  
1  1 1 1 1 0 0 1 1 0    1  0 0 0 0 0 1 1 0 0                                  
2  0 1 1 1 0 1 0 1 1    2  0 1 1 1 1 1 1 1 0                                  
3  1 0 0 0 0 1 1 0 0    3  0 1 1 1 0 0 1 1 0                                  
4  0 0 1 0 1 1 1 1 0    4  1 1 0 1 1 0 1 0 0                                  
5  0 1 1 0 0 0 1 0 1    5  1 0 0 1 0 1 1 1 1                                  
6  1 1 1 0 1 0 0 1 0    6  1 1 1 0 0 0 1 1 1                                  
7  0 1 1 0 0 1 0 1 1    7  0 1 1 0 1 1 1 1 0                                  
8  1 1 1 0 0 1 0 1 1    8  0 0 0 1 0 0 0 0 1                                  
9  0 0 0 1 1 1 1 1 1    9  1 1 1 0 1 0 1 0 1                                  
                                                                               
Number of moves taken so far is 0                                              
                                                                               
Press a lettered column or a numbered row to flip (or 0,q,?,!) > l            
Press a lettered column or a numbered row to flip (or 0,q,?,!) > ?            
                                  Hint: a                                      
Press a lettered column or a numbered row to flip (or 0,q,?,!) > a            

--------------------------------------------------------------------------------

Current:                Target:                                                
   a b c d e f g h i       a b c d e f g h i                                  
1  0 1 1 1 0 0 1 1 0    1  0 0 0 0 0 1 1 0 0                                  
2  1 1 1 1 0 1 0 1 1    2  0 1 1 1 1 1 1 1 0                                  
3  0 0 0 0 0 1 1 0 0    3  0 1 1 1 0 0 1 1 0                                  
4  1 0 1 0 1 1 1 1 0    4  1 1 0 1 1 0 1 0 0                                  
5  1 1 1 0 0 0 1 0 1    5  1 0 0 1 0 1 1 1 1                                  
6  0 1 1 0 1 0 0 1 0    6  1 1 1 0 0 0 1 1 1                                  
7  1 1 1 0 0 1 0 1 1    7  0 1 1 0 1 1 1 1 0                                  
8  0 1 1 0 0 1 0 1 1    8  0 0 0 1 0 0 0 0 1                                  
9  1 0 0 1 1 1 1 1 1    9  1 1 1 0 1 0 1 0 1                                  
                                                                               
Number of moves taken so far is 1                                              
                                                                               
Press a lettered column or a numbered row to flip (or 0,q,?,!) > b            

--------------------------------------------------------------------------------

Current:                Target:                                                
   a b c d e f g h i       a b c d e f g h i                                  
1  0 0 1 1 0 0 1 1 0    1  0 0 0 0 0 1 1 0 0                                  
2  1 0 1 1 0 1 0 1 1    2  0 1 1 1 1 1 1 1 0                                  
3  0 1 0 0 0 1 1 0 0    3  0 1 1 1 0 0 1 1 0                                  
4  1 1 1 0 1 1 1 1 0    4  1 1 0 1 1 0 1 0 0                                  
5  1 0 1 0 0 0 1 0 1    5  1 0 0 1 0 1 1 1 1                                  
6  0 0 1 0 1 0 0 1 0    6  1 1 1 0 0 0 1 1 1                                  
7  1 0 1 0 0 1 0 1 1    7  0 1 1 0 1 1 1 1 0                                  
8  0 0 1 0 0 1 0 1 1    8  0 0 0 1 0 0 0 0 1                                  
9  1 1 0 1 1 1 1 1 1    9  1 1 1 0 1 0 1 0 1                                  
                                                                               
Number of moves taken so far is 2                                              
                                                                               
Press a lettered column or a numbered row to flip (or 0,q,?,!) > c            

--------------------------------------------------------------------------------

Current:                Target:                                                
   a b c d e f g h i       a b c d e f g h i                                  
1  0 0 0 1 0 0 1 1 0    1  0 0 0 0 0 1 1 0 0                                  
2  1 0 0 1 0 1 0 1 1    2  0 1 1 1 1 1 1 1 0                                  
3  0 1 1 0 0 1 1 0 0    3  0 1 1 1 0 0 1 1 0                                  
4  1 1 0 0 1 1 1 1 0    4  1 1 0 1 1 0 1 0 0                                  
5  1 0 0 0 0 0 1 0 1    5  1 0 0 1 0 1 1 1 1                                  
6  0 0 0 0 1 0 0 1 0    6  1 1 1 0 0 0 1 1 1                                  
7  1 0 0 0 0 1 0 1 1    7  0 1 1 0 1 1 1 1 0                                  
8  0 0 0 0 0 1 0 1 1    8  0 0 0 1 0 0 0 0 1                                  
9  1 1 1 1 1 1 1 1 1    9  1 1 1 0 1 0 1 0 1                                  
                                                                               
Number of moves taken so far is 3                                              
                                                                               
Press a lettered column or a numbered row to flip (or 0,q,?,!) > ?            
                                  Hint: d                                      
Press a lettered column or a numbered row to flip (or 0,q,?,!) > d            

--------------------------------------------------------------------------------

Current:                Target:                                                
   a b c d e f g h i       a b c d e f g h i                                  
1  0 0 0 0 0 0 1 1 0    1  0 0 0 0 0 1 1 0 0                                  
2  1 0 0 0 0 1 0 1 1    2  0 1 1 1 1 1 1 1 0                                  
3  0 1 1 1 0 1 1 0 0    3  0 1 1 1 0 0 1 1 0                                  
4  1 1 0 1 1 1 1 1 0    4  1 1 0 1 1 0 1 0 0                                  
5  1 0 0 1 0 0 1 0 1    5  1 0 0 1 0 1 1 1 1                                  
6  0 0 0 1 1 0 0 1 0    6  1 1 1 0 0 0 1 1 1                                  
7  1 0 0 1 0 1 0 1 1    7  0 1 1 0 1 1 1 1 0                                  
8  0 0 0 1 0 1 0 1 1    8  0 0 0 1 0 0 0 0 1                                  
9  1 1 1 0 1 1 1 1 1    9  1 1 1 0 1 0 1 0 1                                  
                                                                               
Number of moves taken so far is 4                                              
                                                                               
Press a lettered column or a numbered row to flip (or 0,q,?,!) > 2            

--------------------------------------------------------------------------------

Current:                Target:                                                
   a b c d e f g h i       a b c d e f g h i                                  
1  0 0 0 0 0 0 1 1 0    1  0 0 0 0 0 1 1 0 0                                  
2  0 1 1 1 1 0 1 0 0    2  0 1 1 1 1 1 1 1 0                                  
3  0 1 1 1 0 1 1 0 0    3  0 1 1 1 0 0 1 1 0                                  
4  1 1 0 1 1 1 1 1 0    4  1 1 0 1 1 0 1 0 0                                  
5  1 0 0 1 0 0 1 0 1    5  1 0 0 1 0 1 1 1 1                                  
6  0 0 0 1 1 0 0 1 0    6  1 1 1 0 0 0 1 1 1                                  
7  1 0 0 1 0 1 0 1 1    7  0 1 1 0 1 1 1 1 0                                  
8  0 0 0 1 0 1 0 1 1    8  0 0 0 1 0 0 0 0 1                                  
9  1 1 1 0 1 1 1 1 1    9  1 1 1 0 1 0 1 0 1                                  
                                                                               
Number of moves taken so far is 5                                              
                                                                               
Press a lettered column or a numbered row to flip (or 0,q,?,!) > !            
                                                                               
                  For the next move, the AI has chosen: f                      
                                                                               
              Running the solution with 4 sec screen delays...                

--------------------------------------------------------------------------------

Current:                Target:                                                
   a b c d e f g h i       a b c d e f g h i                                  
1  0 0 0 0 0 1 1 1 0    1  0 0 0 0 0 1 1 0 0                                  
2  0 1 1 1 1 1 1 0 0    2  0 1 1 1 1 1 1 1 0                                  
3  0 1 1 1 0 0 1 0 0    3  0 1 1 1 0 0 1 1 0                                  
4  1 1 0 1 1 0 1 1 0    4  1 1 0 1 1 0 1 0 0                                  
5  1 0 0 1 0 1 1 0 1    5  1 0 0 1 0 1 1 1 1                                  
6  0 0 0 1 1 1 0 1 0    6  1 1 1 0 0 0 1 1 1                                  
7  1 0 0 1 0 0 0 1 1    7  0 1 1 0 1 1 1 1 0                                  
8  0 0 0 1 0 0 0 1 1    8  0 0 0 1 0 0 0 0 1                                  
9  1 1 1 0 1 0 1 1 1    9  1 1 1 0 1 0 1 0 1                                  
                                                                               
Number of moves taken so far is 6                                              
                                                                               
                  For the next move, the AI has chosen: h                      
                                                                               
              Running the solution with 4 sec screen delays...                

--------------------------------------------------------------------------------

Current:                Target:                                                
   a b c d e f g h i       a b c d e f g h i                                  
1  0 0 0 0 0 1 1 0 0    1  0 0 0 0 0 1 1 0 0                                  
2  0 1 1 1 1 1 1 1 0    2  0 1 1 1 1 1 1 1 0                                  
3  0 1 1 1 0 0 1 1 0    3  0 1 1 1 0 0 1 1 0                                  
4  1 1 0 1 1 0 1 0 0    4  1 1 0 1 1 0 1 0 0                                  
5  1 0 0 1 0 1 1 1 1    5  1 0 0 1 0 1 1 1 1                                  
6  0 0 0 1 1 1 0 0 0    6  1 1 1 0 0 0 1 1 1                                  
7  1 0 0 1 0 0 0 0 1    7  0 1 1 0 1 1 1 1 0                                  
8  0 0 0 1 0 0 0 0 1    8  0 0 0 1 0 0 0 0 1                                  
9  1 1 1 0 1 0 1 0 1    9  1 1 1 0 1 0 1 0 1                                  
                                                                               
Number of moves taken so far is 7                                              
                                                                               
                  For the next move, the AI has chosen: 6                      
                                                                               
              Running the solution with 4 sec screen delays...                

--------------------------------------------------------------------------------

Current:                Target:                                                
   a b c d e f g h i       a b c d e f g h i                                  
1  0 0 0 0 0 1 1 0 0    1  0 0 0 0 0 1 1 0 0                                  
2  0 1 1 1 1 1 1 1 0    2  0 1 1 1 1 1 1 1 0                                  
3  0 1 1 1 0 0 1 1 0    3  0 1 1 1 0 0 1 1 0                                  
4  1 1 0 1 1 0 1 0 0    4  1 1 0 1 1 0 1 0 0                                  
5  1 0 0 1 0 1 1 1 1    5  1 0 0 1 0 1 1 1 1                                  
6  1 1 1 0 0 0 1 1 1    6  1 1 1 0 0 0 1 1 1                                  
7  1 0 0 1 0 0 0 0 1    7  0 1 1 0 1 1 1 1 0                                  
8  0 0 0 1 0 0 0 0 1    8  0 0 0 1 0 0 0 0 1                                  
9  1 1 1 0 1 0 1 0 1    9  1 1 1 0 1 0 1 0 1                                  
                                                                               
Number of moves taken so far is 8                                              
                                                                               
                  For the next move, the AI has chosen: 7                      
                                                                               
              Running the solution with 4 sec screen delays...                

--------------------------------------------------------------------------------

Current:                Target:                                                
   a b c d e f g h i       a b c d e f g h i                                  
1  0 0 0 0 0 1 1 0 0    1  0 0 0 0 0 1 1 0 0                                  
2  0 1 1 1 1 1 1 1 0    2  0 1 1 1 1 1 1 1 0                                  
3  0 1 1 1 0 0 1 1 0    3  0 1 1 1 0 0 1 1 0                                  
4  1 1 0 1 1 0 1 0 0    4  1 1 0 1 1 0 1 0 0                                  
5  1 0 0 1 0 1 1 1 1    5  1 0 0 1 0 1 1 1 1                                  
6  1 1 1 0 0 0 1 1 1    6  1 1 1 0 0 0 1 1 1                                  
7  0 1 1 0 1 1 1 1 0    7  0 1 1 0 1 1 1 1 0                                  
8  0 0 0 1 0 0 0 0 1    8  0 0 0 1 0 0 0 0 1                                  
9  1 1 1 0 1 0 1 0 1    9  1 1 1 0 1 0 1 0 1                                  
                                                                               
Number of moves taken so far is 9                                              
                                                                               
                For the next move, the AI has chosen: Done?                    
                                                                               
              Running the solution with 4 sec screen delays...                

--------------------------------------------------------------------------------

Current:                Target:                                                
   a b c d e f g h i       a b c d e f g h i                                  
1  0 0 0 0 0 1 1 0 0    1  0 0 0 0 0 1 1 0 0                                  
2  0 1 1 1 1 1 1 1 0    2  0 1 1 1 1 1 1 1 0                                  
3  0 1 1 1 0 0 1 1 0    3  0 1 1 1 0 0 1 1 0                                  
4  1 1 0 1 1 0 1 0 0    4  1 1 0 1 1 0 1 0 0                                  
5  1 0 0 1 0 1 1 1 1    5  1 0 0 1 0 1 1 1 1                                  
6  1 1 1 0 0 0 1 1 1    6  1 1 1 0 0 0 1 1 1                                  
7  0 1 1 0 1 1 1 1 0    7  0 1 1 0 1 1 1 1 0                                  
8  0 0 0 1 0 0 0 0 1    8  0 0 0 1 0 0 0 0 1                                  
9  1 1 1 0 1 0 1 0 1    9  1 1 1 0 1 0 1 0 1                                  
                                                                               
Number of moves taken so far is 9                                              
                                                                               
                     Done! Current board matches Target                        
                                                                               
                Press y for yes, if you want to start over:                    
                                      m                                        

--------------------------------------------------------------------------------


Attached Files .txt   12-17 9PM Copy Flipping Bits Game.txt (Size: 17.18 KB / Downloads: 3)
B += x
Reply
#10
Thanks bplus, much better now! Updated!
I like to program in BASIC
With code that is simple and slick
I learnt it in school
And it is still cool
So it is my number one pick
Reply
#11
Being the first "Basic" to post a solution is cool.

To be the only one that includes Hints and Solving, really cool! 
(Maybe cool enough that the specification to show "quick" 3x3 boards in output will be ignored.)

I wonder how many got held up because screen shots weren't allowed, so output had to be printed to file (as well as on screen).
B += x
Reply
#12
well done bplus for posting the first BASIC code for the task. and btw screen shots are allowed in Rosetta Code Smile
I like to program in BASIC
With code that is simple and slick
I learnt it in school
And it is still cool
So it is my number one pick
Reply
#13
Hi Adrian, 

I got impression screen shots weren't allowed because one submission for Flipping Bits was Red Flagged for not submitting Output Sample. They had replied that Rosetta Code was closed for... see attachment...

That plus fact I've not seen any except one JAVA that looked very old! Of course, I have hardly searched the whole site...


Attached Files Thumbnail(s)

B += x
Reply
#14
OK I think I shaved off over 40 lines by eliminating the VS duplicating outputs to screen and to an VS array as well and some obvious double parking.
Code:
_TITLE "AI+VS #2 Flipping Bits now with AI - by bplus 2017-12-18"
'2017-12-18 much easier way to copy screen to file
'2017-12-17 AI+VS Flipping Bits now with AI
'adding the option for user to press ? for a hint for a move
'adding the option for user to press ! for the puzzle to be solved,
'this option will post all moves to screen and text file to
'review the game.
'2017-12-17 3 PM found bug in program when bad move is input - fixed
'Oh but now have to fix double vision of input, need new function getKey$
'replace all INPUTs with getKey$ and update whole program.
'2017-12-17 ~6 AM VS Flipping Bits
'This version employs a VS array that acts as a Virtual screen.
'All commands printing to screen have been rewritten to print to screen
'AND echo the same text and locations to the VS array.
'Just before each screen clear, the VS array is dumped into the
'Copy Flipping Bits Game.txt file. So this file always contains
'the last game played. b + 2017-12-17

RANDOMIZE TIMER
DIM SHARED cellsPerSide, legalMoves$, startB$, currentB$, targetB$, moveCount

restart
DO
   displayStatus
   IF currentB$ = targetB$ THEN 'game done!
       PRINT " Congratulations, done in"; moveCount; " moves."
       PRINT "": PRINT " Press y for yes, if you want to start over > ";
       yes$ = getKey$: PRINT yes$: _DELAY .4: vcls
       IF yes$ = "y" THEN restart ELSE nomore = -1
   ELSE 'get next move
       m$ = " ": PRINT
       WHILE INSTR(legalMoves$, m$) = 0
           PRINT " Press a lettered column or a numbered row to flip (or 0,q,?,!) > ";
           m$ = getKey$: PRINT m$: _DELAY .4
           IF m$ = "!" THEN
               showSolution = -1: m$ = " ": EXIT WHILE
           ELSEIF m$ = "?" THEN: m$ = " ": cp CSRLIN, "Hint: " + hint$
           ELSEIF m$ = "0" OR m$ = "q" THEN: vcls: CLOSE: END
           ELSEIF m$ = "" THEN: m$ = " "
           END IF
       WEND
       IF showSolution THEN 'run the solution from hints function
           showSolution = 0: mv$ = hint$
           cp CSRLIN + 1, "For the next move, the AI has chosen: " + mv$
           cp CSRLIN + 1, "Running the solution with 4 sec screen delays..."
           _DELAY 4: vcls
           WHILE mv$ <> "Done?"
               moveCount = moveCount + 1: makeMove mv$: displayStatus: mv$ = hint$
               cp CSRLIN + 1, "For the next move, the AI has chosen: " + mv$
               cp CSRLIN + 1, "Running the solution with 4 sec screen delays..."
               _DELAY 4: vcls
           WEND
           displayStatus
           cp CSRLIN + 1, "Done! Current board matches Target"
           cp CSRLIN + 1, "Press y for yes, if you want to start over: > "
           yes$ = getKey$: PRINT yes$: _DELAY .4: vcls
           IF yes$ = "y" THEN restart ELSE nomore = -1
       ELSE
           vcls: moveCount = moveCount + 1: makeMove m$
       END IF
   END IF
LOOP UNTIL nomore
CLOSE

SUB displayStatus
   COLOR 9: showBoard 2, 2, currentB$, "Current:"
   COLOR 12: showBoard 2, 2 + 2 * cellsPerSide + 6, targetB$, "Target:"
   COLOR 13: PRINT: PRINT " Number of moves taken so far is" + STR$(moveCount)
   COLOR 14
END SUB


FUNCTION hint$ 'compare the currentB to targetB and suggest letter or digit or done
   FOR i = 1 TO 2 * cellsPerSide 'check cols first then rows as listed in legalMoves$
       r$ = MID$(legalMoves$, i, 1)
       IF i <= cellsPerSide THEN
           currentbit$ = MID$(currentB$, i, 1): targetBit$ = MID$(targetB$, i, 1)
           IF currentbit$ <> targetBit$ THEN flag = -1: EXIT FOR
       ELSE
           j = i - cellsPerSide
           currentbit$ = MID$(currentB$, (j - 1) * cellsPerSide + 1, 1)
           targetBit$ = MID$(targetB$, (j - 1) * cellsPerSide + 1, 1)
           IF currentbit$ <> targetBit$ THEN flag = -1: EXIT FOR
       END IF
   NEXT
   IF flag THEN hint$ = r$ ELSE hint$ = "Done?"
END FUNCTION

SUB restart
   CLOSE
   OPEN "Copy Flipping Bits Game.txt" FOR OUTPUT AS #3
   cellsPerSide = 0: legalMoves$ = "": moveCount = 0
   COLOR 9: cp 3, "Flipping Bits Game, now with AI!  b+ 2017-12-18"
   COLOR 5
   cp 5, "You will be presented with a square board marked Current and"
   cp 6, "another marked Target. The object of the game is to match"
   cp 7, "the Current board to Target in the least amount of moves."
   cp 9, "To make a move, enter a letter for a column to flip or"
   cp 10, "a digit for a row to flip. In a flip, all 1's are"
   cp 11, "changed to 0's and all 0's changed to 1's."
   cp 13, "You may enter 0 or q at any time to quit."
   cp 14, "You may press ? when prompted for move to get a hint."
   cp 15, "You may press ! to have the program solve the puzzle."
   COLOR 14: PRINT: PRINT
   WHILE cellsPerSide < 2 OR cellsPerSide > 9
       LOCATE CSRLIN, 13: PRINT "Please press how many cells you want per side 2 to 9 > ";
       in$ = getKey$: PRINT in$: _DELAY .4
       IF in$ = "0" OR in$ = "q" THEN END ELSE cellsPerSide = VAL(in$)
   WEND
   vcls
   FOR i = 1 TO cellsPerSide: legalMoves$ = legalMoves$ + CHR$(96 + i): NEXT
   FOR i = 1 TO cellsPerSide: legalMoves$ = legalMoves$ + LTRIM$(STR$(i)): NEXT
   startB$ = startBoard$: currentB$ = startB$: targetB$ = makeTarget$: currentB$ = startB$
END SUB

FUNCTION startBoard$
   FOR i = 1 TO cellsPerSide ^ 2: r$ = r$ + LTRIM$(STR$(INT(RND * 2))): NEXT
   startBoard$ = r$
END FUNCTION

SUB showBoard (row, col, board$, title$)
   LOCATE row - 1, col: PRINT title$
   FOR i = 1 TO cellsPerSide
       LOCATE row, col + 2 * (i - 1) + 3: PRINT MID$(legalMoves$, i, 1);
   NEXT
   PRINT
   FOR i = 1 TO cellsPerSide
       LOCATE row + i, col - 1: PRINT STR$(i);
       FOR j = 1 TO cellsPerSide
           LOCATE row + i, col + 2 * j: PRINT " " + MID$(board$, (i - 1) * cellsPerSide + j, 1);
       NEXT
       PRINT
   NEXT
END SUB

SUB makeMove (move$)
   ac = ASC(move$)
   IF ac > 96 THEN 'letter
       col = ac - 96
       FOR i = 1 TO cellsPerSide
           bit$ = MID$(currentB$, (i - 1) * cellsPerSide + col, 1)
           IF bit$ = "0" THEN
               MID$(currentB$, (i - 1) * cellsPerSide + col, 1) = "1"
           ELSE
               MID$(currentB$, (i - 1) * cellsPerSide + col, 1) = "0"
           END IF
       NEXT
   ELSE 'number
       row = ac - 48
       FOR i = 1 TO cellsPerSide
           bit$ = MID$(currentB$, (row - 1) * cellsPerSide + i, 1)
           IF bit$ = "0" THEN
               MID$(currentB$, (row - 1) * cellsPerSide + i, 1) = "1"
           ELSE
               MID$(currentB$, (row - 1) * cellsPerSide + i, 1) = "0"
           END IF
       NEXT
   END IF
END SUB

FUNCTION makeTarget$
   WHILE currentB$ = startB$
       FOR i = 1 TO cellsPerSide * cellsPerSide
           m$ = MID$(legalMoves$, INT(RND * LEN(legalMoves$)) + 1, 1): makeMove m$
       NEXT
   WEND
   makeTarget$ = currentB$
END FUNCTION

SUB cp (row, text$) 'center print at row
   LOCATE row, (80 - LEN(text$)) / 2: PRINT text$;
END SUB

SUB vcls 'print the screen to file then clear it
   DIM s$(23)
   FOR lines = 1 TO 23
       FOR t = 1 TO 80: scan$ = scan$ + CHR$(SCREEN(lines, t)): NEXT
       s$(lines) = RTRIM$(scan$): scan$ = ""
   NEXT
   FOR fini = 23 TO 1 STEP -1
       IF s$(fini) <> "" THEN EXIT FOR
   NEXT
   PRINT #3, ""
   FOR i = 1 TO fini: PRINT #3, s$(i): NEXT
   PRINT #3, "": PRINT #3, STRING$(80, "-"): CLS
END SUB

FUNCTION getKey$ 'just want printable characters
   k$ = ""
   WHILE LEN(k$) = 0
       k$ = INKEY$
       IF LEN(k$) THEN 'press something so respond
           IF LEN(k$) = 2 OR ASC(k$) > 126 OR ASC(k$) < 32 THEN k$ = "*": BEEP
       END IF
   WEND
   getKey$ = k$
END FUNCTION

And here is an output on a 3x3 that demos all, bad key presses, wrong moves, hint and AI solve:
Code:
              Flipping Bits Game, now with AI!  b+ 2017-12-18

        You will be presented with a square board marked Current and
          another marked Target. The object of the game is to match
          the Current board to Target in the least amount of moves.

           To make a move, enter a letter for a column to flip or
              a digit for a row to flip. In a flip, all 1's are
                 changed to 0's and all 0's changed to 1's.

                  You may enter 0 or q at any time to quit.
            You may press ? when prompted for move to get a hint.
            You may press ! to have the program solve the puzzle.

           Please press how many cells you want per side 2 to 9 > l
           Please press how many cells you want per side 2 to 9 > *
           Please press how many cells you want per side 2 to 9 > 3

--------------------------------------------------------------------------------

Current:    Target:
   a b c       a b c
1  1 1 0    1  0 1 1
2  1 1 0    2  0 1 1
3  0 0 1    3  0 1 1

Number of moves taken so far is 0

Press a lettered column or a numbered row to flip (or 0,q,?,!) > l
Press a lettered column or a numbered row to flip (or 0,q,?,!) > 9
Press a lettered column or a numbered row to flip (or 0,q,?,!) > *
Press a lettered column or a numbered row to flip (or 0,q,?,!) > a

--------------------------------------------------------------------------------

Current:    Target:
   a b c       a b c
1  0 1 0    1  0 1 1
2  0 1 0    2  0 1 1
3  1 0 1    3  0 1 1

Number of moves taken so far is 1

Press a lettered column or a numbered row to flip (or 0,q,?,!) > ?
                                  Hint: c
Press a lettered column or a numbered row to flip (or 0,q,?,!) > c

--------------------------------------------------------------------------------

Current:    Target:
   a b c       a b c
1  0 1 1    1  0 1 1
2  0 1 1    2  0 1 1
3  1 0 0    3  0 1 1

Number of moves taken so far is 2

Press a lettered column or a numbered row to flip (or 0,q,?,!) > l
Press a lettered column or a numbered row to flip (or 0,q,?,!) > 9
Press a lettered column or a numbered row to flip (or 0,q,?,!) > !

                  For the next move, the AI has chosen: 3
              Running the solution with 4 sec screen delays...

--------------------------------------------------------------------------------

Current:    Target:
   a b c       a b c
1  0 1 1    1  0 1 1
2  0 1 1    2  0 1 1
3  0 1 1    3  0 1 1

Number of moves taken so far is 3

                For the next move, the AI has chosen: Done?
              Running the solution with 4 sec screen delays...

--------------------------------------------------------------------------------

Current:    Target:
   a b c       a b c
1  0 1 1    1  0 1 1
2  0 1 1    2  0 1 1
3  0 1 1    3  0 1 1

Number of moves taken so far is 3

                     Done! Current board matches Target
               Press y for yes, if you want to start over: > m

--------------------------------------------------------------------------------

Clapping Hands  very much improved!


Attached Files .txt   12-18 Copy Flipping Bits Game.txt (Size: 3.14 KB / Downloads: 6)
B += x
Reply