Flood War Sample Program

program "Flood War"

use "..\common\toolbar.bas"

$boardWidth = 24
$boardHeight = 24

$colors[1]="red"
$colors[2]="green"
$colors[3]="blue"
$colors[4]="yellow"
$colors[5]="pink"
$colors[6]="purple"

$maxColors = 6

$p1Color = "red"
$p2Color = "green"

$isEnd = 0
$score = ""

$cellSize = 20
$buttonSize = 128
$buttonMargin = 20

function PrepareToolbar()
	buttons = 0
	for i=1 to Size($colors)
		buttons[i].color = $colors[i]
	next
	
	ToolBar_SetButtonSize($buttonSize)
	ToolBar_SetMargin($buttonMargin)
	$toolbarHeight = $buttonSize + $buttonMargin * 2
	ToolBar_SetToBottom()
	ToolBar_SetButtons(buttons)
end function

function GetScore(board)
	GetScore.p1 = 0
	GetScore.p2 = 0
	
	for x=1 to $boardWidth
		for y=1 to $boardHeight
			if board[x][y] = "p1"
				GetScore.p1 = GetScore.p1 + 1
			elseif board[x][y] = "p2"
				GetScore.p2 = GetScore.p2 + 1
			end if
		next
	next	
end function

function IsEnd(board)
	for x=1 to $boardWidth
		for y=1 to $boardHeight
			if board[x][y] <> "p1" and board[x][y] <> "p2"
				IsEnd = 0
				exit function
			end if
		next
	next	
	
	IsEnd = 1
	exit function
end function

function TakeTurnAI()
	maxRank = 0
	bestColor = 1
	
	for color = 1 to $maxColors
		if color <> $p1Color and color <> $p2Color
			board = $board
			AssignPlayerColor(board, "p2", color)
			score = GetScore (board)
			rank = score.p2/score.p1
			if rank > maxRank
				maxRank = rank
				bestColor = color
			end if
		end if
	next
	
	AssignPlayerColor($board, "p2", bestColor)
	AssignAdditioalPlayerCells($board)
	$p2Color = bestColor
end function

function ToolBar_OnButtonClick(i)
	if i = $p1Color or i = $p2Color
		exit function
	end if
	
	AssignPlayerColor($board, "p1", i)
	AssignAdditioalPlayerCells($board)
	$p1Color = i
	TakeTurnAI()
	
	if IsEnd($board)
		$score = GetScore($board)
		$isEnd = 1
	end if
	
	DrawBoard()
end function

function GenerateBoard()
	for x=1 to $boardWidth
		for y=1 to $boardHeight
			color = int(rnd() * 6 + 1)
			$board[x][y] = color
		next
	next
	
	$p1Color = $board[1][1]
	$p2Color = $board[$boardHeight][$boardHeight]
	if $p2Color = $p1Color
		$p2Color = $p2Color + 1
		if $p2Color > $maxColors
			$p2Color = 1
		end if 
	end if
	
	$board[1][1] = "p1"
	$board[$boardHeight][$boardHeight] = "p2"
	
	AssignPlayerColor($board, "p1", $p1Color)
	AssignPlayerColor($board, "p2", $p2Color)
end function

function DrawBoard()
	ClearScreen()
	if $IsEnd
		SetColor("white")
		SetFontSize(64)
		
		$highScore = LoadNumericValue("ColorWarHighScore", 0)
		if $score.p1 > $highScore
			$highScore = $score.p1
			SaveNumericValue("ColorWarHighScore", $highScore)
		end if
		
		if $score.p1 > $score.p2
			scoreText1 = "YOU WON"
		elseif $score.p1 < $score.p2
			scoreText1 = "YOU LOST"
		else
			scoreText1 = "TIE"
		end if
		
		scoreText2 = $score.p1 & " : " & $score.p2
		
		scoreText3 = "HIGH SCORE"
		scoreText4 = $highScore
		
		x1 = (GetWidth() - GetTextWidth(scoreText1)) /2
		y1 = GetHeight() / 8 - GetTextHeight(scoreText1)
		
		x2 = (GetWidth() - GetTextWidth(scoreText2)) /2
		y2 = GetHeight() / 8
		
		x3 = (GetWidth() - GetTextWidth(scoreText3)) /2
		y3 = GetHeight() / 8 * 7 - GetTextHeight(scoreText3)
		
		x4 = (GetWidth() - GetTextWidth(scoreText4)) /2
		y4 = GetHeight() / 8 * 7
		
	
		DrawText(x1, y1, scoreText1)
		DrawText(x2, y2, scoreText2)
		DrawText(x3, y3, scoreText3)
		DrawText(x4, y4, scoreText4)
		
		cellSizeX = GetWidth() / $boardWidth
		cellSizeY = (GetHeight()) / $boardHeight
		$cellSize = min(cellSizeX, cellSizeY) /2
	
		startX = (GetWidth() - $cellSize * $boardWidth ) / 2
		startY = (GetHeight() - $cellSize * $boardHeight ) / 2
	else
		cellSizeX = GetWidth() / $boardWidth
		cellSizeY = (GetHeight() - $toolbarHeight) / $boardHeight
		$cellSize = min(cellSizeX, cellSizeY)
	
		startX = (GetWidth() - $cellSize * $boardWidth ) / 2
		startY = (GetHeight() - $toolbarHeight - $cellSize * $boardHeight ) / 2
		
		'exit function
	end if
	
	
	
	for x=1 to $boardWidth
		for y=1 to $boardHeight
			color = $board[x][y]
			
			if color = "p1"
				color = $p1Color
			elseif color = "p2"
				color = $p2Color
			end if
			
			x1 = startX + (x-1) * $cellSize
			y1 = startY + (y-1) * $cellSize
			x2 = x1 + $cellSize
			y2 = y1 + $cellSize
			SetColor($colors[color])
			SetColor($colors[color])
			DrawRectangle(x1, y1, x2, y2)
		next
	next
	
	if not $IsEnd
		ToolBar_Draw()
	end if
		
end function

function AssignNeighbourPlayerColor(board ByRef, player, color, x, y)
	if not IsNull(board.processed[x][y])
		exit function
	end if
	
	board.processed[x][y] = 1
	
	if x < $boardWidth and board[x+1][y] = color
		board[x+1][y] = player
		AssignNeighbourPlayerColor(board, player, color, x+1, y)
	end if
	
	if y < $boardHeight and board[x][y+1] = color
		board[x][y+1] = player
		AssignNeighbourPlayerColor(board, player, color, x, y+1)
	end if
	
	if x > 1 and board[x-1][y] = color
		board[x-1][y] = player
		AssignNeighbourPlayerColor(board, player, color, x-1, y)
	end if
	
	if y > 1 and board[x][y-1] = color
		board[x][y-1] = player
		AssignNeighbourPlayerColor(board, player, color, x, y-1)
	end if
end function

function AssignPlayerColor(board ByRef, player, color)
	for x=1 to $boardWidth
		for y=1 to $boardHeight
			if board[x][y] = player
				AssignNeighbourPlayerColor(board, player, color, x, y)
			end if
		next
	next
	board.processed = Null
end function

function FindIsland(board ByRef, x, y)
	if not IsNull(board.processed[x][y])
		exit function
	end if
	
	if board[x][y] = "p1"
		board.island.p1 = 1
		exit function
	end if

	if board[x][y] = "p2"
		board.island.p2 = 1
		exit function
	end if
	
	board.processed[x][y] = 1	
	board.island.cells[x][y] = 1
	
	if x < $boardWidth 
		FindIsland(board, x+1, y)
	end if
	
	if y < $boardHeight
		FindIsland(board, x, y+1)
	end if
	
	if x > 1
		FindIsland(board, x-1, y)
	end if
	
	if y > 1
		FindIsland(board, x, y-1)
	end if
end function

function AssignAdditioalPlayerCells(board ByRef)
	for x=1 to $boardWidth
		for y=1 to $boardHeight
			FindIsland(board, x, y)
			if not IsNull(board.island.cells[x][y]) and (not board.island.p1 or not board.island.p2)
				for xIsland=1 to $boardWidth
					for yIsland=1 to $boardHeight
						if not IsNull(board.island.cells[xIsland][yIsland])
							if not board.island.p1
								board[xIsland][yIsland] = "p2"
							else
								board[xIsland][yIsland] = "p1"
							end if
						end if
						
					next
				next
			end if
			
			board.island = Null
		next
	next
	board.processed = Null
end function


function OnSize()
	DrawBoard()
end function

function OnMouseDown(x, y)
	if $IsEnd
		exit function
	end if
	
	ToolBar_OnMouseDown(x, y)
end function

function OnMouseDrag(x, y)
	if $IsEnd
		exit function
	end if
	
	ToolBar_OnMouseDrag(x, y)
end function

function OnMouseMove(x, y)
	if $IsEnd
		exit function
	end if
	
	ToolBar_OnMouseMove(x, y)
end function

function OnMouseUp(x, y)
	if $IsEnd
		GenerateBoard()
		$IsEnd = 0
		DrawBoard()
		exit function
	end if
	
	ToolBar_OnMouseUp(x, y)
	if $IsEnd
		DrawBoard()
	end if
end function

PrepareToolbar()
GenerateBoard()
DrawBoard()