Flipping bits game: Difference between revisions

m
Line 2,869:
RANDOMIZE TIMER
DIM SHARED cellsPerSide, legalMoves$, startB$, currentB$, targetB$, moveCount
DIM SHARED VS(23) AS STRING * 80, prow, pcol
 
restart
DO
displayStatus
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
PRINTvplf " Congratulations, done in"; + STR$(moveCount;) + " moves."
PRINTvplf "": INPUTcp prow, " EnterPress y for yes, if you want to start over "; yes$
yes$ = getKey$: cp prow, yes$: _DELAY .4
vcls
IF yes$ = "y" THEN restart ELSE nomore = -1
ELSE 'get next move
m$ = " ": PRINTvplf ""
WHILE INSTR(legalMoves$, m$) = 0 OR m$ = "0" OR m$ = "q"
INPUTvpstop " EnterPress a lettered column or a numbered row to flip (0 or 0,q, quits?,!) ";> m$"
IF m$ = "0"getKey$: ORvplf m$: = "q" THEN_DELAY END.4
IF m$ = "!" THEN m$ = " "
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
'handleIF moveshowSolution resultsTHEN
showSolution = 0
IF m$ <> "0" AND m$ <> "q" THEN moveCount = moveCount + 1: makeMove m$
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
cellsPerSide = 0: legalMoves$ = "": moveCount = 0: CLS
COLOR 9: cp 3,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."
Line 2,903 ⟶ 2,967:
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 1514, "I'dYou saymay goodpress luck? butwhen thisprompted isfor aboutmove skill!to get a hint."
cp 15, "You may press ! to have the program solve the puzzle."
PRINT: irow = 16: COLOR 14
irow = 16: COLOR 14
WHILE cellsPerSide < 2 OR cellsPerSide > 9
irow = irow + 1
cpvl irow, "12: vpstop "Please enterpress how many cells you want per side 2 to 9 > "
INPUTin$ ;= ingetKey$
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
Line 2,922 ⟶ 2,989:
 
SUB showBoard (row, col, board$, title$)
LOCATEvl row - 1, col: PRINTvplf title$
FOR i = 1 TO cellsPerSide
LOCATEvl row, col + 2 * (i - 1) + 3: PRINTvpstop MID$(legalMoves$, i, 1)
NEXT
vplf ""
FOR i = 1 TO cellsPerSide
LOCATEvl row + i, col - 1: PRINTvpstop STR$(i);
FOR j = 1 TO cellsPerSide
LOCATEvl row + i, col + 2 * j: PRINTvpstop " " + MID$(board$, (i - 1) * cellsPerSide + j, 1)
NEXT
vplf ""
NEXT
END SUB
Line 2,968 ⟶ 3,037:
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
</lang>
'''Output:'''
<lang>
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
 
--------------------------------------------------------------------------------
</lang>
 
Anonymous user