TGC Codebase Backup



Flood It (4 by zenassem

5th Sep 2011 19:47
Summary

A DBpro version of a "Flood It" style game. Makes use of a 4-way recursive FloodFill algorithm with 2D Array.



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Rem Project: Dark Basic Pro Project
Rem Created: Friday, September 02, 2011

Rem ***** Main Source File *****

`Set sync rate & display mode
sync on
sync rate 60
Set display mode 800,600,32

`setup some constants
#constant BlockSizeX 32 
#constant BlockSizeY 32
#constant BoardSizeX 14
#constant BoardSizeY 14
#constant XOffset 160
#constant YOffset 32
#constant MaxTurns 30

xloc as integer
yloc as integer
Global NColor as integer
Global OColor as integer
Turns as integer
GameDone as boolean
Release as boolean   

RandColor as integer
WinColor as integer
ColorOfBlock as integer

`Create a 2D array for now as the GameBoard
dim GameBoard(BoardSizeX+1,BoardSizeY+1)

`Generate Media
`Create 6 color blocks
`Blue
ink rgb(0,0,200)
box 1,1,BlockSizeX,BlockSizeY
get image 1,1,1,BlockSizeX,BlockSizeY

`Red
ink rgb(200,0,0)
box 1,1,BlockSizeX,BlockSizeY
get image 2,1,1,BlockSizeX,BlockSizeY

`Green
ink rgb(0,200,0)
box 1,1,BlockSizeX,BlockSizeY
get image 3,1,1,BlockSizeX,BlockSizeY

`Purple
ink rgb(100,0,120)
box 1,1,BlockSizeX,BlockSizeY
get image 4,1,1,BlockSizeX,BlockSizeY

`Yellow
ink rgb(200,200,0)
box 1,1,BlockSizeX,BlockSizeY
get image 5,1,1,BlockSizeX,BlockSizeY

`pink
ink rgb(200,0,100)
box 1,1,BlockSizeX,BlockSizeY
get image 6,1,1,BlockSizeX,BlockSizeY

ink rgb(220,220,220)
Gosub InitNewGame


`* * * * * * * * * * M A I N L O O P  * * * * * * * * * *
DO

cls 0
`if leftmouse is now clicked and it had been released
if mouseclick() and  Release
     Release = 0 `leftmouse is down(clicked)
    `If the mouse is over our color choices: Determine which one
    if mousex()< BlockSizeX*3  and mousey()< BlockSizeY*2 
            xloc= mousex()/BlockSizeX+1
            yloc= mousey()/BlockSizeY+1
            OColor=GameBoard(1,1)
                if yloc>1
                    NColor=xloc+yloc+1
                else
                     NColor=xloc
                endif
        if OColor<>NColor `If we chose a new Color Inc Turns  
        inc Turns         `And perform the Flood_Fill
        floodFill4(1,1,NColor,OColor)
        endif
    endif
endif 

if not mouseclick()
    Release=1 `leftmouse is up(released)
endif

`Draw the Color Selector Blocks
index=1
for y=0 to 1
    for x=0 to 2
    paste image index,x*32,y*32
    inc index
    next x
next y

WinColor=GameBoard(1,1) `set the wincolor 
GameDone=1 `set GameDone Flag to true

for y=1 to BoardSizeY
    for x=1 to BoardSizeX
        ColorOfBlock=GameBoard(x,y)
        
        `If any color on the board is not the WinColor
        `The Game is not done
        if WinColor<>ColorOfBlock
            GameDone=0 `Set GameDone Flag to false 
        endif
        paste image ColorOfBlock,x*BlockSizeX+XOffset,y*BlockSizeY+YOffset
        //text x*9,y*9+400,str$(ColorOfBlock)
    next x
next y



text 110,26,"Color Choice = "+str$(NColor)
text 0,120,str$(Turns)+ "/" +str$(MaxTurns) + " Turns"
//text 0,150,"OldColor= "+str$(OColor)
//text 10,380,"GameBoard(x,y)"
sync

if Turns>MaxTurns
    Set Text size 30
    text 100,250, "Sorry You Lose! Press <Any Key> to continue."
    sync
    wait key
    Cls 0: Set Text size 8
    Gosub InitNewGame
endif

if GameDone=1
    set text size 30
    text 100,250,"You Win! Press <Any Key> to continue."
    sync
    wait key
    Cls 0: set text size 8
    Gosub InitNewGame
endif

LOOP
`* * * * * * * * E N D  M A I N  L O O P * * * * * * * * 

function floodFill4(nx , ny , newColor , oldColor ) 
    if (nx >= 1) and (nx=<BoardSizeX) and (ny >= 1) and (ny=<BoardSizeY) and GameBoard(nx,ny)<>newColor and GameBoard(nx,ny)=oldColor 
     
       GameBoard(nx,ny) = newColor//set color before starting recursion
        
        floodFill4(nx + 1, ny,newColor, oldColor)
        floodFill4(nx - 1, ny,newColor, oldColor)
        floodFill4(nx,ny + 1, newColor, oldColor)
        floodFill4(nx,ny - 1, newColor, oldColor)
    endif   
endfunction


InitNewGame:
GameDone = 0
OColor = 0
NColor = 0
Turns = 0
Release = 1
RandColor = 0
WinColor = 0
ColorOfBlock = 0

`Seed our Random Generator
randomize timer()

`Lets put Random Values 1-6 in our 2D array
`These values represent 1 of 6 colored blocks 
`The numbers correlate to the Image Numbers respectively
`1=BLUE : 2=RED : 3=GREEN : 4=PURPLE : 5=YELLOW : 6=PINK

for y=1 to BoardSizeY
    for x=1 to BoardSizeX
        RandColor=rnd(5)+1
        GameBoard(x,y)=RandColor
    next x
next Y

OColor=GameBoard(1,1)
return