-- GAME LIBRARY START -- Running = true GameOver = false math.randomseed(os.time()) function IsAnInt(input) return tonumber(input, 10) ~= nil end function GetIntInput(prompt) while true do print(prompt .. " ") ans = io.read(); if IsAnInt(ans) then return tonumber(ans, 10) end print("YOU DID NOT ENTER AN INTEGER.") end end function IsEmpty(string) return string == nil or string == '' end function AskQuestion(prompt) while true do print(prompt .. "?") ans = io.read() if IsEmpty(ans) then print("PLEASE ANSWER THE QUESTION.") else return string.upper(ans) end end end function AskToPlayAgain() while true do ans = AskQuestion("DO YOU WANT TO PLAY AGAIN") if ans == "YES" or ans == "Y" then return true elseif ans == "NO" or ans == "N" then return false else print("PLEASE ANSWER \"YES\" OR \"NO\"") end end end function AskToSeeTheRules() while true do ans = AskQuestion("DO YOU WANT TO SEE THE RULES") if ans == "YES" or ans == "Y" then return true elseif ans == "NO" or ans == "N" then return false else print("PLEASE ANSWER \"YES\" OR \"NO\"") end end end function GenerateRandomNumber(minimum, maximum, show) number = math.random(minimum, maximum) if show == true then print(number) end return number end -- GAME LIBRARY END -- YourGrid = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} EnemyGrid = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} Outposts = {0,0} Turn = 0 function DisplayTheRules() print("YOU ARE ON A BATTLEFIELD WITH 4 PLATOONS AND YOU") print("HAVE 25 OUTPOSTS AVAILABLE WHERE THEY MAY BE PLACED.") print("YOU CAN ONLY PLACE ONE PLATOON AT ANY ONE OUTPOST.") print("THE COMPUTER DOES THE SAME WITH ITS FOUR PLATOONS.") print("") print("THE OBJECT OF THE GAME IS TO FIRE MISSILES AT THE") print("OUTPOSTS OF THE COMPUTER. IT WILL DO THE SAME TO YOU.") print("THE ONE WHO DESTROYS ALL FOUR OF THE ENEMY'S PLATOONS") print("FIRST IS THE WINNER.") print("") print("GOOD LUCK...AND TELL US WHERE YOU WANT THE BODIES SENT.") print("") end function CheckWinner() if Outposts[1] == 0 then return 2 elseif Outposts[2] == 0 then return 1 end return 0 end function DisplayYourGrid() print("=======================") print("YOUR GRID") print("=======================") print("X | 1 | 2 | 3 | 4 | 5 |") print("Y |---+---+---+---+---|") for x = 1,5,1 do gStart = ((x - 1) * 5) + 1 gEnd = x * 5 displayLine = x.." |" for g = gStart,gEnd,1 do if YourGrid[g] == -1 then displayLine = displayLine.." X |" elseif YourGrid[g] == 1 then displayLine = displayLine.." O |" else displayLine = displayLine.." |" end end print(displayLine) end print(" |---+---+---+---+---|") end function DisplayEnemyGrid() print("=======================") print("ENEMY GRID") print("=======================") print("X | 1 | 2 | 3 | 4 | 5 |") print("Y |---+---+---+---+---|") for x = 1,5,1 do gStart = ((x - 1) * 5) + 1 gEnd = x * 5 displayLine = x.." |" for g = gStart,gEnd,1 do if EnemyGrid[g] == -1 then displayLine = displayLine.." X |" else displayLine = displayLine.." |" end end print(displayLine) end print(" |---+---+---+---+---|") end function ClearGrids() for g = 1,25,1 do YourGrid[g] = 0 EnemyGrid[g] = 0 end end function SetOutposts() Outposts[1] = 4 outpost = 0 for o = 1,4,1 do DisplayYourGrid() outpostSet = false while outpostSet == false do print("PLACE YOUR OUTPOST #"..o) x = GetIntInput("X") if x < 1 or x > 5 then print ("X MUST BE BETWEEN 1 AND 5.") else y = GetIntInput("Y") if y < 1 or y > 5 then print ("Y MUST BE BETWEEN 1 AND 5.") else outpost = ((y - 1) * 5) + x if YourGrid[outpost] == 0 then YourGrid[outpost] = 1 outpostSet = true else print("YOU ALREADY HAVE AN OUTPOST THERE.") end end end end end end function ComputerSetOutposts() Outposts[2] = 4 outpost = 0 for o = 1,4,1 do outpostSet = false while outpostSet == false do x = GenerateRandomNumber(1,5,false) y = GenerateRandomNumber(1,5,false) outpost = ((y - 1) * 5) + x if EnemyGrid[outpost] == 0 then EnemyGrid[outpost] = 1 outpostSet = true end end end end function ComputerTurn() print("MY TURN...") loc = 0 x = 0 y = 0 gotLoc = false while gotLoc == false do x = GenerateRandomNumber(1,5,false) y = GenerateRandomNumber(1,5,false) loc = ((y - 1) * 5) + x if YourGrid[loc] > -1 then gotLoc = true end end print("..........BOOM!") if YourGrid[loc] == 0 then print("I MISSED YOU, YOU DIRTY RAT.") print("I PICKED LOCATION ("..x..","..y..").") else print("I GOT YOU. IT WON'T BE LONG NOW!") print("OUTPOST AT ("..x..","..y..") WAS HIT.") YourGrid[loc] = -1 Outposts[1] = Outposts[1] - 1 if Outposts[1] == 3 then print("YOU ONLY HAVE THREE OUTPOSTS LEFT.") elseif Outposts[1] == 2 then print("YOU ONLY HAVE TWO OUTPOSTS LEFT.") elseif Outposts[1] == 1 then print("YOU ONLY HAVE ONE OUTPOSTS LEFT.") else print("YOU ARE DEAD! YOU HAVE NO OUTPOSTS LEFT.") end end end function FireMissile() print("WHERE DO YOU WANT TO FIRE YOUR MISSILE?") loc = 0 x = 0 y = 0 gotLoc = false while gotLoc == false do x = GetIntInput("X") if x < 1 or x > 5 then print ("X MUST BE BETWEEN 1 AND 5.") else gotLoc = true end end gotLoc = false while gotLoc == false do y = GetIntInput("Y") if y < 1 or y > 5 then print ("Y MUST BE BETWEEN 1 AND 5.") else gotLoc = true end end loc = ((y - 1) * 5) + x print("..........BOOM!") if EnemyGrid[loc] == -1 then print("YOU FOOL, YOU ALREADY ATTACKED THERE!") elseif EnemyGrid[loc] == 0 then print("HA HA, YOU MISSED!") else print("YOU GOT ONE OF MY OUTPOSTS!") EnemyGrid[loc] = -1 Outposts[2] = Outposts[2] - 1 if Outposts[2] == 3 then print("ONE DOWN, THREE TO GO.") elseif Outposts[2] == 2 then print("TWO DOWN, TWO TO GO.") elseif Outposts[2] == 1 then print("THREE DOWN, ONE TO GO.") else print("YOU GOT THEM ALL.") end end end print("*** BOMBARDMENT : LUA ***") if AskToSeeTheRules() then DisplayTheRules() end Turn = 0 while Running == true do ClearGrids() ComputerSetOutposts() SetOutposts() GameOver = false print("--------- LET THE WAR BEGIN ----------") while GameOver == false do print("-----------------------") print("TURN #"..(Turn + 1)) print("-----------------------") if Turn % 2 == 0 then DisplayYourGrid() DisplayEnemyGrid() FireMissile() else ComputerTurn() end winner = CheckWinner() if winner == 0 then Turn = Turn + 1 else DisplayYourGrid() DisplayEnemyGrid() if winner == 1 then print("YOU WIN!!!") else print("TOO BAD, BETTER LUCK NEXT TIME.") end GameOver = true end end print("*** GAME OVER ***") Running = AskToPlayAgain() end print("THANKS FOR PLAYING!")