Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
--# Main -- Flood function setup() if backup then backup("Flood 101") end parameter.integer("Size",10,30,15) --size of board parameter.action("Start",Start) --starts a new game parameter.integer("Moves_so_far",0,100,0) parameter.action("Undo",UndoMove) --set up palette of six colors colrs={color(255,0,0,255), color(0,255,0,255), color(0,0,255,255), color(255,255,0,255),color(255,0,255,255),color(0,255,255,255)} ss=readProjectData("Size") --read in user's last size selection if ss~=nil then Size=ss end --set size, if a saved value exists Start() end function Start() --create random 2D table of colors board={}--create 1D table for i=1,Size do --loop through columns --this is how you create a 2D table board[i]={} --as we start each new column, create an array of rows for j=1,Size do --choose random color for each square board[i][j]=tostring(math.random(1,6)) end end state="RUNNING" --to tell draw the game has started Moves_so_far=0 output.clear() lastMove=board[1][1] back=Backup() saveProjectData("Size",Size) --save user size selection currSize=Size print("Press a color at the bottom to change the bottom left hand cell to that color, along with all adjoining cells. Try to flood the whole table with one color in as few moves as possible.") end function draw() background(200) --calculate the size of square we can fit on screen local w=(HEIGHT-100)/currSize pushStyle() --draw table of colors local finished=true --see use below for x=1,currSize do for y=1,currSize do fill(colrs[tonumber(board[x][y])]) --set colour based on value in table 1-6 rect(50+x*w-w,75+y*w-w,w,w) --draw square --if any of the squares are a different color, we haven't finished if board[x][y]~=board[1][1] then finished=false end end end --draw buttons for user to press, store button locations for touch detection button={} for i=1,6 do fill(colrs[i]) --position button at bottom of screen local x,y,w,h=100*i,10,75,50 rect(x,y,w,h) --save details of button position so we can trap touches button[i]={x=x,y=y,w=w,h=h} end popStyle() --if we've just finished, print a message if state~="FINISHED" then --we weren't finished last move if finished then --...but we are now state="FINISHED" print("Finished in",Moves_so_far,"moves") end end end function touched(touch) --state has three values, BEGAN, MOVING and ENDED --we only want ENDED, ie when the finger lifts up if touch.state~=ENDED then return end --check touch postiion against each button --that's why we stored all that info about button position for i=1,6 do if touch.x>=button[i].x and touch.x<=button[i].x+button[i].w and touch.y>=button[i].y and touch.y<=button[i].y+button[i].h then --only react if the user has chosen a different color if tonumber(board[1][1])~=i then back:store(board) fillBoard(tostring(i)) Moves_so_far = Moves_so_far + 1 end break end end end function UndoMove() board=back:restore() or board Moves_so_far = math.max(0,Moves_so_far - 1) end --this is a recursive function that calls itself function fillBoard(c,r,x,y) --the first time it's called, from touched, it just passes the new color --so we'll make a note of the current color of bottom left --square, and our starting x,y, which is 1,1 if r==nil then r=board[1][1] x=1 y=1 end --check if the square we are in, needs to be changed --if it is the first one, 1,1, the answer is always yes if board[x][y]==r then board[x][y]=c --now check the neighbours around, excl diagonals if x>1 then fillBoard(c,r,x-1,y) end if x<Size then fillBoard(c,r,x+1,y) end if y>1 then fillBoard(c,r,x,y-1) end if y<Size then fillBoard(c,r,x,y+1) end end end --# Backup Backup = class() function Backup:init() self.backups={} end --backs up a 1D or 2D table to a delimited string --one parameter is the table to be backed up, stores the string in an array backup --this provides unlimited backups function Backup:store(t) if t==nil then return end --do nothing if given nothing if self.backups==nil then self.backups={} end --if first time, create array local s={} --we use a 1D array for the backup because it's faster than strings, we convert later if type(t[1])~="table" then --1D table, we can use built in concat command self.backups[#self.backups+1]="1,"..table.concat(t,",") else --2D table, loop through for i=1,#t do for j=1,#t[i] do s[#s+1]=t[i][j] end end self.backups[#self.backups+1]=#t[1]..","..table.concat(s,",") end end --restore last backup from a string to a table (1 or 2D) --parameter is the number of cols - if missing,table is assumed to be 1D --if cols is provided, it is used to calculate rows (since # total items backed up = cols x rows) --returns the table, deletes restored backup function Backup:restore() if #self.backups==0 then return end --no backups local t={} --the table we will return local b=self:split(self.backups[#self.backups]) --split backup string into a 1D table local cols=tonumber(b[1]) if cols==1 then t=b --if a 1D table is needed, we're done table.remove(t,1) else local ii,jj=cols,(#b-1)/cols --otherwise we need a 2D table, calc number of rows (jj) local n=1 for i=1,ii do --loop through cols t[i]={} --create array for this row for j=1,jj do --loop through row n = n + 1 --counter to help us find the place in b t[i][j]=b[n] end end end table.remove(self.backups,#self.backups) --remove latest backup return t end function Backup:clear() self.backups=nil end --splits a delimited string into a table, delimiter defaults to comma function Backup:split(str, delim) if delim==nil then delim="," end local result = {} local pat = "(.-)" .. delim .. "()" local lastPos = 1 for part, pos in string.gfind(str, pat) do table.insert(result, part) lastPos = pos end -- Handle the last field table.insert(result, string.sub(str, lastPos)) return result end
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
⭐ Exploit Documentation ⭐
CSS | 19 min ago | 1.04 KB
This month smells like money
CSS | 19 min ago | 1.04 KB
✅ API Flaw Money Method
CSS | 19 min ago | 1.04 KB
Cooking Recipe
1 hour ago | 6.11 KB
Cooking Recipe
1 hour ago | 6.10 KB
HF Pixelmon Custom Form Dex
7 hours ago | 1.53 KB
bcachefs delay
11 hours ago | 1.94 KB
Fortnite Sprite drop rates
18 hours ago | 1.81 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!