[MuG]
November 12th, 2009, 06:50 AM
Hey All,
I've been playing around with making a game for a couple of days now. The game itself is pretty simple and I've got the game playing through pretty well bar the tile placement rules.
Essentially its a jigsaw that doesn't have a set layout. You have 24 different tile types and each tile has certain features on it which lead off from any side of the tile. To place a tile you must place next to any existing tile. Corner to corner placement isn't allowed. The features must continue from any existing tile to the newly laid tile. Its like a jigsaw piece must "fit" with the already laid tiles. You can rotate the tiles too.
I don't think it should be that complicated but I was wondering whether anyone here might have some good ideas of how to implement this efficiently??
Cheers,
MuG
therobot
November 12th, 2009, 10:47 PM
I'll take a stab at this since no one else has yet :)
I don't know how far along you are with this, but here is a rough idea of how I would model my data.
// create a few different types of connections.
// I'd prefer to just create this with a loop since they're all so similar
var connectionA:Object = new Object();
connectionA.type = 0;// id so we can easily recognize possible connection
connectionA.piece_male = "connection_0_male";// linkage to male connection
connectionA.piece_female = "connection_0_female";// linkage to female connection
var connectionB:Object = new Object();
connectionB.type = 1;// id so we can easily recognize possible connection
connectionB.piece_male = "connection_1_male";// linkage to male connection
connectionB.piece_female = "connection_1_female";// linkage to female connection
var connectionC:Object = new Object();
connectionC.type = 2;// id so we can easily recognize possible connection
connectionC.piece_male = "connection_2_male";// linkage to male connection
connectionC.piece_female = "connection_2_female";// linkage to female connection
// here's a couple sample puzzle pieces
var puzzlePiece1:Object = new Object();
puzzlePiece1.connection_up = {connectionType:connectionA, isMale:true};
puzzlePiece1.connection_down = {connectionType:connectionB, isMale:true};
puzzlePiece1.connection_left = {connectionType:connectionC, isMale:true};
puzzlePiece1.connection_right = {connectionType:connectionA, isMale:true};
var puzzlePiece2:Object = new Object();
puzzlePiece2.connection_up = {connectionType:connectionA, isMale:false};
puzzlePiece2.connection_down = {connectionType:connectionB, isMale:true};
puzzlePiece2.connection_left = {connectionType:connectionB, isMale:false};
puzzlePiece2.connection_right = {connectionType:connectionB, isMale:true};
The important thing to consider is that a puzzle piece consists of 4 connections to neighboring pieces - a connection to the up, down, left and right. A connection in itself is an object that keeps a reference of the type of connection it is, and a reference to its male and female components. When I say male and female components, think like stereo plugs or whatever..one thing (male) snaps into another thing (female), like traditional puzzle pieces.
The idea here is that if two puzzle pieces of connections of the same type and one is male and the other female, they may connect, otherwise they cannot.
Sorry if I did a mediocre job explaining that.
Once you get your head around the data structure, assembling a puzzle would be an easy randomization job. You would want to create the fully assembled puzzle first - create a 2d array, pick a random starting position, and run a breadth first search across your 2d array, creating a new puzzle piece for each slot in your matrix.
Essentially, you'd create the first puzzle piece at a random location, assign it random connection types for all four connections it has, then move on to neighboring puzzle pieces. From this point forward, each neighbor will have at least one neighboring connection that you should already know, so you match up the ones you do know, and assign a random connection type for unchecked neighbors.
If I've lost you at this point, I feel like I'd almost need to draw a picture for you but let's see how much of that you got.
[MuG]
November 13th, 2009, 04:18 AM
Hey Robot thanks for replying :)
I can see what you're doing there and it all makes sense and I've got about as far as that when I posted the message. After I posted the message I created a 2d array to store all the possible possibles a tile could go making it easier to find out which tiles to check.
The only problem I can see with that is that it would be impossible to create the puzzle as there would (and there must) never be a fixed result.
The concept is based on a game called Carcassonne : http://en.wikipedia.org/wiki/Carcassonne_%28board_game%29
So the first piece is fixed and then when the next random piece is drawn is when I'd have to do a check to where the drawn piece can be placed.
Using this sort of data structure (instead of male / female it would be something like road / road or field / field) I can, like you say, run through the placed pieces and check where the pieces can be matched. The pieces can be rotated as well to add a bit of complexity (or simplicity as you don't have to check side to side but instead just if ANY of the sides match).
The only bit that makes this more complicated is that you can "claim" features on a piece. It's probably easier to look at the rules and see what it says - its a bit tricky to explain :S
So basically the data structure for all the different types and tiles might end up being quite complicated!
therobot
November 13th, 2009, 01:17 PM
;2516275']The only problem I can see with that is that it would be impossible to create the puzzle as there would (and there must) never be a fixed result.
By fixed, do you mean you're example requires there only being one possible solution?
With what i have written in my example, it COULD be possible to have multiple solutions in that the pieces might be able to snap together because of the way the random connections ended up getting layed out. You could easily fix this though, by storing the 'final' grid positions in each puzzle piece. With that, even if you had two puzzle pieces that could snap together because of having similar connections, you'd do a check against the puzzle's solution to see if the pieces are actually next to each other.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.