|
| 1 | +--[[ |
| 2 | +Game of FlipFlop |
| 3 | +=============== |
| 4 | +
|
| 5 | +Based on the original BASIC game of FlipFlop from the 1970s. |
| 6 | +
|
| 7 | +This is a faithful recreation of the classic game, maintaining the original gameplay |
| 8 | +while modernizing the implementation for Lua. For example, the original's complex |
| 9 | +trigonometric random number generation has been replaced with Lua's math.random, |
| 10 | +as modern random number generators are more sophisticated than those available |
| 11 | +in 1970s BASIC. |
| 12 | +
|
| 13 | +How to Play: |
| 14 | +----------- |
| 15 | +The game presents a row of 10 X's. Your goal is to flip all X's to O's. |
| 16 | +Enter a number from 1-10 to flip that position. The computer will also make |
| 17 | +random moves in response. Special commands: |
| 18 | +- Enter 0 to see the current board again |
| 19 | +- Enter 11 to quit the game |
| 20 | +Try to complete the puzzle in 12 moves or fewer for the best score! |
| 21 | +
|
| 22 | +Strategy Tips: |
| 23 | +------------ |
| 24 | +- When you make a move, watch how the computer responds. It will often |
| 25 | + make a predictable counter-move. |
| 26 | +- If you enter the same position twice, the computer will use a different |
| 27 | + random pattern for its response. |
| 28 | +- Try to work systematically from one end of the board to the other, |
| 29 | + rather than making random moves. |
| 30 | +- If you get stuck, sometimes starting over (11) is better than continuing |
| 31 | + with a poor position. |
| 32 | +
|
| 33 | +Technical Notes: |
| 34 | +-------------- |
| 35 | +- Written for Lua 5.1 compatibility |
| 36 | +- Uses tables for game state management |
| 37 | +- Implements original game logic without goto statements |
| 38 | +- Preserves the original game's move counting and win conditions |
| 39 | +- Maintains the classic text-based interface style |
| 40 | +
|
| 41 | +Original BASIC Version: |
| 42 | +-------------------- |
| 43 | +The original game used complex trigonometric functions for random number |
| 44 | +generation, likely due to limitations in BASIC's random number capabilities: |
| 45 | + R=TAN(Q+N/Q-N)-SIN(Q/N)+336*SIN(8*N) |
| 46 | + N=R-INT(R) |
| 47 | + N=INT(10*N) |
| 48 | +
|
| 49 | +This has been simplified to use Lua's built-in random number generator |
| 50 | +while maintaining the same gameplay experience. |
| 51 | +
|
| 52 | +Converted from BASIC to Lua by Brian Wilkins |
| 53 | +March 2025 |
| 54 | +]]-- |
| 55 | + |
| 56 | +local function showInstructions() |
| 57 | + print(" FLIP FLOP") |
| 58 | + print(" CREATIVE COMPUTING") |
| 59 | + print(" MORRISTOWN, NEW JERSEY") |
| 60 | + print("\n\n") |
| 61 | + print("THE OBJECT OF THIS GAME IS TO FLIP ALL THE X'S TO O'S.") |
| 62 | + print("WHEN YOU INPUT A NUMBER, THAT NUMBER AND ALL ADJACENT") |
| 63 | + print("NUMBERS WILL BE FLIPPED.") |
| 64 | + print("\n") |
| 65 | + print("INPUT A NUMBER FROM 1 TO 10 TO START THE GAME. TO QUIT") |
| 66 | + print("THE GAME AT ANY TIME, TYPE A 0 (ZERO). TO START OVER WITH") |
| 67 | + print("A NEW PUZZLE IN THE MIDDLE OF A GAME, TYPE A 11 (ELEVEN).") |
| 68 | + print("\n\n") |
| 69 | +end |
| 70 | + |
| 71 | +local function printUpdatedBoard(board) |
| 72 | + print("1 2 3 4 5 6 7 8 9 10") |
| 73 | + print(table.concat(board, " ")) |
| 74 | + print("\n\n") |
| 75 | +end |
| 76 | + |
| 77 | +local function processMove(gameState, position) |
| 78 | + -- Check if current position is O |
| 79 | + if gameState.board[position] == "O" then |
| 80 | + gameState.board[position] = "X" |
| 81 | + if position == gameState["lastMove"] then |
| 82 | + -- Generate random position using simplified random logic |
| 83 | + local R = math.random() -- between 0 and 1 |
| 84 | + local N = math.floor(R * 10) + 1 |
| 85 | + if gameState.board[N] == "O" then |
| 86 | + gameState.board[N] = "X" |
| 87 | + else |
| 88 | + gameState.board[N] = "O" |
| 89 | + end |
| 90 | + end |
| 91 | + else |
| 92 | + gameState.board[position] = "O" |
| 93 | + -- Generate another random position |
| 94 | + local R = math.random() |
| 95 | + local N = math.floor(R * 10) + 1 |
| 96 | + if gameState.board[N] == "O" then |
| 97 | + gameState.board[N] = "X" |
| 98 | + else |
| 99 | + gameState.board[N] = "O" |
| 100 | + end |
| 101 | + end |
| 102 | +end |
| 103 | + |
| 104 | +local function checkWin(gameState) |
| 105 | + -- Check if all positions are O's |
| 106 | + for i = 1, 10 do |
| 107 | + if gameState.board[i] ~= "O" then |
| 108 | + return false |
| 109 | + end |
| 110 | + end |
| 111 | + return true |
| 112 | +end |
| 113 | + |
| 114 | +local function initializeGame() |
| 115 | + local gameState = { |
| 116 | + board = {}, |
| 117 | + lastMove = nil, |
| 118 | + moves = 0 |
| 119 | + } |
| 120 | + |
| 121 | + -- Initialize board |
| 122 | + for i = 1, 10 do |
| 123 | + gameState.board[i] = "X" |
| 124 | + end |
| 125 | + return gameState |
| 126 | +end |
| 127 | + |
| 128 | +local function playGame() |
| 129 | + showInstructions() |
| 130 | + |
| 131 | + local gameState = initializeGame() |
| 132 | + local playing = true |
| 133 | + |
| 134 | + print("HERE IS THE STARTING LINE OF X'S.") |
| 135 | + print("\n\n") |
| 136 | + printUpdatedBoard(gameState.board) |
| 137 | + |
| 138 | + while playing do |
| 139 | + print("INPUT THE NUMBER") |
| 140 | + local N = tonumber(io.read()) |
| 141 | + |
| 142 | + -- Check if input is valid |
| 143 | + if not N or N ~= math.floor(N) then |
| 144 | + print("ILLEGAL ENTRY--TRY AGAIN.") |
| 145 | + elseif N == 11 then |
| 146 | + playing = false |
| 147 | + elseif N > 11 then |
| 148 | + print("ILLEGAL ENTRY--TRY AGAIN.") |
| 149 | + elseif N == 0 then |
| 150 | + printUpdatedBoard(gameState.board) |
| 151 | + else |
| 152 | + -- Process the move |
| 153 | + processMove(gameState, N) |
| 154 | + gameState["lastMove"] = N |
| 155 | + gameState.moves = gameState.moves + 1 |
| 156 | + |
| 157 | + -- Show the updated board |
| 158 | + printUpdatedBoard(gameState.board) |
| 159 | + |
| 160 | + -- Check for win |
| 161 | + if checkWin(gameState) then |
| 162 | + if gameState.moves <= 12 then |
| 163 | + print(string.format("VERY GOOD. YOU GUESSED IT IN ONLY %d GUESSES.", gameState.moves)) |
| 164 | + else |
| 165 | + print(string.format("TRY HARDER NEXT TIME. IT TOOK YOU %d GUESSES.", gameState.moves)) |
| 166 | + end |
| 167 | + |
| 168 | + print("DO YOU WANT TO TRY ANOTHER PUZZLE? (Y/N)") |
| 169 | + local answer = io.read():upper() |
| 170 | + if answer:sub(1,1) == "Y" then |
| 171 | + gameState = initializeGame() |
| 172 | + print("HERE IS THE STARTING LINE OF X'S.") |
| 173 | + print("\n\n") |
| 174 | + printUpdatedBoard(gameState.board) |
| 175 | + else |
| 176 | + playing = false |
| 177 | + end |
| 178 | + end |
| 179 | + end |
| 180 | + end |
| 181 | +end |
| 182 | + |
| 183 | +playGame() |
0 commit comments