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:
parent
0bf76f0d61
commit
6fa1450445
2 changed files with 39 additions and 11 deletions
|
@ -8,6 +8,11 @@
|
|||
|
||||
* sample/GnomeHelloWorld.cs: use DeleteEventHandler.
|
||||
|
||||
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.
|
||||
|
||||
2002-08-07 Duncan Mak <duncan@ximian.com>
|
||||
|
||||
* sample/Fifteen.cs: Added new canvas example.
|
||||
|
|
|
@ -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);
|
||||
|
@ -109,12 +113,13 @@ public class Fifteen
|
|||
case EventType.ButtonPress:
|
||||
int y = piece.Position / 4;
|
||||
int x = piece.Position % 4;
|
||||
Print_Position ("from", piece.Position, true);
|
||||
|
||||
bool toMove = false;
|
||||
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,24 +131,43 @@ 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)
|
||||
|
@ -152,7 +176,6 @@ public class Fifteen
|
|||
int blank;
|
||||
int position;
|
||||
|
||||
|
||||
// find blank spot
|
||||
for (position = 0; position < 16; position ++)
|
||||
if (board [position] == null)
|
||||
|
|
Loading…
Reference in a new issue