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.
|
* 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>
|
2002-08-07 Duncan Mak <duncan@ximian.com>
|
||||||
|
|
||||||
* sample/Fifteen.cs: Added new canvas example.
|
* sample/Fifteen.cs: Added new canvas example.
|
||||||
|
|
|
@ -14,9 +14,13 @@ public class Fifteen
|
||||||
static Window window = null;
|
static Window window = null;
|
||||||
static Canvas canvas = null;
|
static Canvas canvas = null;
|
||||||
static BoardPiece [] board;
|
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 ();
|
Application.Init ();
|
||||||
window = new Window ("Fifteen #");
|
window = new Window ("Fifteen #");
|
||||||
VBox vbox = new VBox (false, 4);
|
VBox vbox = new VBox (false, 4);
|
||||||
|
@ -109,12 +113,13 @@ public class Fifteen
|
||||||
case EventType.ButtonPress:
|
case EventType.ButtonPress:
|
||||||
int y = piece.Position / 4;
|
int y = piece.Position / 4;
|
||||||
int x = 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)) {
|
if ((y > 0) && (board [(y - 1) * 4 + x] == null)) {
|
||||||
dx = 0.0;
|
dx = 0.0;
|
||||||
dy = 1.0;
|
dy = -1.0;
|
||||||
y --;
|
y --;
|
||||||
} else if ((y < 3) && (board [(y + 1) * 4 + x] == null)) {
|
} else if ((y < 3) && (board [(y + 1) * 4 + x] == null)) {
|
||||||
dx = 0.0;
|
dx = 0.0;
|
||||||
|
@ -126,24 +131,43 @@ public class Fifteen
|
||||||
x --;
|
x --;
|
||||||
} else if ((x < 3) && (board [y * 4 + x + 1] == null)) {
|
} else if ((x < 3) && (board [y * 4 + x + 1] == null)) {
|
||||||
dx = 1.0;
|
dx = 1.0;
|
||||||
dy = 1.0;
|
dy = 0.0;
|
||||||
y ++;
|
x ++;
|
||||||
} else
|
} else
|
||||||
toMove = false;
|
toMove = false;
|
||||||
|
|
||||||
if (toMove) {
|
if (toMove) {
|
||||||
int new_position = y * 4 + x;
|
int new_position = y * 4 + x;
|
||||||
|
Print_Position ("to", new_position, false);
|
||||||
board [piece.Position] = null;
|
board [piece.Position] = null;
|
||||||
board [new_position] = piece;
|
board [new_position] = piece;
|
||||||
piece.Position = new_position;
|
piece.Position = new_position;
|
||||||
piece.Move (dx * PIECE_SIZE, dy * PIECE_SIZE);
|
piece.Move (dx * PIECE_SIZE, dy * PIECE_SIZE);
|
||||||
}
|
} else
|
||||||
|
Print_Position ("to", piece.Position, false);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
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)
|
static void Scramble (object o, EventArgs args)
|
||||||
|
@ -152,7 +176,6 @@ public class Fifteen
|
||||||
int blank;
|
int blank;
|
||||||
int position;
|
int position;
|
||||||
|
|
||||||
|
|
||||||
// find blank spot
|
// find blank spot
|
||||||
for (position = 0; position < 16; position ++)
|
for (position = 0; position < 16; position ++)
|
||||||
if (board [position] == null)
|
if (board [position] == null)
|
||||||
|
|
Loading…
Add table
Reference in a new issue