SocialEbola User
http://socialebola.wordpress.com
GAME - Color Match
]]>
iMax) )
{
alert("Not in range (" +
iMin.toString() + " - " +
iMax.toString() + "): " + sTit + "!");
return false;
}
return true;
}
function fBack(iReason, x, y)
{
switch( iReason )
{
case 0 : //Cell mouse down
host.UI.prsBar.Visibility =
System.Windows.
Visibility.Visible;
break;
case 1 : //Cell mouse up - started
break;
case 2 : //Cell mouse up - ended
host.UI.prsBar.Visibility =
System.Windows.
Visibility.Collapsed;
break;
}
}
function hisRestore(sh)
{
if( !bHis ) return;
sHis = sh.substring(6);
for( xh = 0; xh < cx; xh++ )
{
for( yh = 0; yh < cy; yh++ )
{
var iCnum = parseInt(
sHis.substring( (xh * cy) + yh,
((xh * cy) + yh) + 1 ) );
setCnum(xh, yh, iCnum);
}
}
sTodo = "";
genView(host.UI.view);
}
function hisSave()
{
if( !bHis ) return;
sHis = (host.UI.his.Items.Count + 1).toString();
if( sHis.length == 1 ) sHis = " " + sHis;
if( sHis.length == 2 ) sHis = " " + sHis;
while( sHis.length < 6 ) sHis += " ";
for( xh = 0; xh < cx; xh++ )
{
for( yh = 0; yh < cy; yh++ )
{
sHis += getCnum(xh, yh);
}
}
host.UI.his.Items.Insert(0, sHis);
}
function getCval(x, y)
{
var iCnum = getCnum(x, y);
return getCvalDir(iCnum);
}
function getCvalDir(iCnum)
{
switch( iCnum )
{
case 1 :
return System.Windows.Media.
Colors.Red;
case 2 :
return System.Windows.Media.
Colors.Green;
case 3 :
return System.Windows.Media.
Colors.Blue;
case 4 :
return System.Windows.Media.
Colors.Orange;
case 5 :
return System.Windows.Media.
Colors.Purple;
case 6 :
return System.Windows.Media.
Colors.DarkGray;
default :
return System.Windows.Media.
Colors.White;
}
}
function genView(can)
{
can.Children.Clear();
for( x = 0; x < cx; x++)
{
for( y = 0; y < cy; y++ )
{
var iCnum = getCnum(x, y);
var ell = new System.Windows.Shapes.
Ellipse();
ell.Name = "v_" + sIdx(x) + "_" + sIdx(y);
ell.Width = rcW;
ell.Height = rcH;
ell.Margin = new System.Windows.
Thickness( rcG + (x * (rcG + rcW)),
rcG + (y * (rcG + rcH)), 0, 0 );
if( iCnum > 0 )
{
if( b3D )
{
var br = new System.Windows.Media.
RadialGradientBrush();
br.Center = new System.Windows.
Point( 0.4, 0.4 );
br.GradientOrigin = new System.Windows.
Point( 0.4, 0.4 );
var gs1 = new System.Windows.Media.
GradientStop();
gs1.Offset = 0;
gs1.Color = System.Windows.Media.
Colors.White;
br.GradientStops.Add(gs1);
var gs2 = new System.Windows.Media.
GradientStop();
gs2.Offset = 1;
gs2.Color = getCvalDir(iCnum);
br.GradientStops.Add(gs2);
ell.Fill = br;
}
else
{
ell.Fill = new System.Windows.Media.
SolidColorBrush( getCvalDir(iCnum) );
}
}
else
{
ell.Fill = host.UI.view.Background;
}
addClk(ell);
can.Children.Add(ell);
}
}
}
function doPlay(x, y)
{
fBack(1, x, y);
sTodo = "";
if( !doCell( x, y, 0 ) )
{
fBack(2, x, y);
return;
}
xT = x;
yT = y;
xB = x;
yB = y;
while( sTodo != "" )
{
var sItm = sTodo.substring(0, 6);
//alert(sItm);
var tX = parseInt(sItm.substring(0, 2));
//alert(tX.toString());
var tY = parseInt(sItm.substring(3, 5));
//alert(tY.toString());
doCell( tX, tY, 1 );
sTodo = sTodo.substring(6);
if( sTodo == ";" ) sTodo = "";
//alert(sTodo);
}
doClear();
genView(host.UI.view);
if( bHis ) hisSave();
fBack(2, x, y);
}
function doClear()
{
for( x = xT; x <= xB; x++ )
{
for(;;)
{
var bChng = false;
var iCnum = 0;
for( y = 0; y < (cy - 1); y++ )
{
iCnum = getCnum(x, y);
if( iCnum > 0 )
{
if( getCnum(x, y + 1) == 0 )
{
setCnum( x, y + 1, iCnum );
setCnum( x, y, 0 );
bChng = true;
}
}
}
if( !bChng ) break;
}
}
for( y = 0; y <= yB; y++ )
{
for(;;)
{
var bChng = false;
var iCnum = 0;
for( x = 0; x < (cx - 1); x++ )
{
iCnum = getCnum(x, y);
if( iCnum > 0 )
{
if( getCnum(x + 1, y) == 0 )
{
setCnum( x + 1, y, iCnum );
setCnum( x, y, 0 );
bChng = true;
}
}
}
if( !bChng ) break;
}
}
}
function doCell(x, y, iMatchDef)
{
var iNumCurr = getCnum(x, y);
var tX = 0;
var tY = 0;
var iMatch = iMatchDef;
if( iNumCurr == 0 ) return false;
if( x > 0 )
{
tX = x - 1;
tY = y;
if( iNumCurr == getCnum(tX, tY) )
{
sTodo = sTodo + sIdx(tX) + "-" + sIdx(tY) + ";";
iMatch++;
}
}
if( x < (cx - 1) )
{
tX = x + 1;
tY = y;
if( iNumCurr == getCnum(tX, tY) )
{
sTodo = sTodo + sIdx(tX) + "-" + sIdx(tY) + ";";
iMatch++;
}
}
if( y > 0 )
{
tX = x;
tY = y - 1;
if( iNumCurr == getCnum(tX, tY) )
{
sTodo = sTodo + sIdx(tX) + "-" + sIdx(tY) + ";";
iMatch++;
}
}
if( y < (cy - 1) )
{
tX = x;
tY = y + 1;
if( iNumCurr == getCnum(tX, tY) )
{
sTodo = sTodo + sIdx(tX) + "-" + sIdx(tY) + ";";
iMatch++;
}
}
if( iMatch )
{
xT = Math.min(xT, x);
yT = Math.min(yT, y);
xB = Math.max(xB, x);
yB = Math.max(yB, y);
setCnum( x, y, 0 );
return true;
}
return false;
}
function getX(sNm)
{
return parseInt(sNm.substring(2,4));
}
function getY(sNm)
{
return parseInt(sNm.substring(5));
}
function doStart()
{
preStart();
sTodo = "";
if( bHis ) host.UI.his.Items.Clear();
genDoc(host.UI.doc);
genView(host.UI.view);
if( bHis ) hisSave();
}
function getCnum(x, y)
{
var tb = host.UI.doc.FindName(
"d_" + sIdx(x) + "_" + sIdx(y));
return parseInt(tb.Text);
}
function setCnum(x, y, iN)
{
var tb = host.UI.doc.FindName(
"d_" + sIdx(x) + "_" + sIdx(y));
tb.Text = iN.toString();
}
function genDoc(sp)
{
sp.Children.Clear();
for( x = 0; x < cx; x++)
{
for( y = 0; y < cy; y++ )
{
var tb = new System.Windows.Controls.
TextBlock();
tb.Name = "d_" + sIdx(x) + "_" + sIdx(y);
tb.Text = Math.round(1 + ((clrCnt - 1) *
Math.random())).toString();
sp.Children.Add(tb);
}
}
}
function sIdx(i)
{
var res = "";
if( i < 10 ) res = "0";
res += i.toString();
return res;
}
function addClk(ctl)
{
ctl.add_MouseLeftButtonDown(function(sender)
{
sender.CaptureMouse();
fBack(0, getX(sender.Name),
getY(sender.Name));
});
ctl.add_MouseLeftButtonUp(function(sender)
{
doPlay(getX(sender.Name),
getY(sender.Name));
});
}
host.add_Loaded(function()
{
// Button Click event
host.UI.btnStart.add_Click(function()
{
doStart();
});
// ListBox SelectionChanged event
host.UI.his.add_SelectionChanged(function()
{
var item = host.UI.his.SelectedItem;
if( item == null ) return;
var sh=item.toString();
if( sh == "" ) return;
hisRestore(sh);
});
});
});
]]>