2002-08-07 Duncan Mak <duncan@ximian.com>

* sample/Fifteen.cs: Fixed movement logic. It works now. Added
	'debug' flag. Run 'mono ./Fifteen.exe debug' to see movement info.

svn path=/trunk/gtk-sharp/; revision=6491
This commit is contained in:
Duncan Mak 2002-08-07 03:50:09 +00:00
parent 0bf76f0d61
commit 6fa1450445
2 changed files with 39 additions and 11 deletions

View File

@ -10,7 +10,12 @@
2002-08-07 Duncan Mak <duncan@ximian.com>
* sample/Fifteen.cs: Added new canvas example.
* sample/Fifteen.cs: Fixed movement logic. It works now. Added
'debug' flag. Run 'mono ./Fifteen.exe debug' to see movement info.
2002-08-07 Duncan Mak <duncan@ximian.com>
* sample/Fifteen.cs: Added new canvas example.
2002-08-06 Duncan Mak <duncan@ximian.com>

View File

@ -14,9 +14,13 @@ public class Fifteen
static Window window = null;
static Canvas canvas = null;
static BoardPiece [] board;
static bool debug = false;
static void Main ()
static void Main (string [] args)
{
if (args.Length > 0 && args [0] == "debug")
debug = true;
Application.Init ();
window = new Window ("Fifteen #");
VBox vbox = new VBox (false, 4);
@ -107,14 +111,15 @@ public class Fifteen
break;
case EventType.ButtonPress:
int y = piece.Position / 4;
int y = piece.Position / 4;
int x = piece.Position % 4;
bool toMove = false;
Print_Position ("from", piece.Position, true);
bool toMove = true;
if ((y > 0) && (board [(y - 1) * 4 + x] == null)) {
dx = 0.0;
dy = 1.0;
dy = -1.0;
y --;
} else if ((y < 3) && (board [(y + 1) * 4 + x] == null)) {
dx = 0.0;
@ -126,32 +131,50 @@ public class Fifteen
x --;
} else if ((x < 3) && (board [y * 4 + x + 1] == null)) {
dx = 1.0;
dy = 1.0;
y ++;
dy = 0.0;
x ++;
} else
toMove = false;
if (toMove) {
int new_position = y * 4 + x;
Print_Position ("to", new_position, false);
board [piece.Position] = null;
board [new_position] = piece;
piece.Position = new_position;
piece.Move (dx * PIECE_SIZE, dy * PIECE_SIZE);
}
} else
Print_Position ("to", piece.Position, false);
break;
default:
break;
}
args.RetVal = false;
}
static void Print_Position (string text, int position, bool newLine)
{
if (!debug)
return;
else {
int x = position / 4;
int y = position % 4;
string output = String.Format (" {0} ({1}, {2})", text, x + 1, y + 1);
if (newLine)
Console.Write (output);
else
Console.WriteLine (output);
}
}
static void Scramble (object o, EventArgs args)
{
Random rand = new Random ();
int blank;
int position;
int position;
// find blank spot
for (position = 0; position < 16; position ++)