- Home /
how to check if different objects from a shape
Hey there i have been trying to make a game in witch players have to draw a line in each turn and if that line form a square then that player get a point for example if its player 1 turn he draws a line and that line with the previous ones has closed/formed the shape of a square get a point if it has closed of formed the shape of 2 squares he gets 2 points what im realy stuck at is how do i detect if a square has been formed from previous drawn lines Note: all of the lines are aleady set on the screen and are inactive . they activate when ever the player press them thanks in adanvce <3 also sry if im not making any sense cose idont realy know how to explain it :(
Answer by ecv80 · Dec 02, 2018 at 10:32 PM
This is not so trivial. I can only give you a rough idea of how I would go about it, which is not necessarily the best way...
I am making a few assumptions here: The board is a rectangular grill, and for the sake of simplicity, you may complete any kind of closed shape to score, not just squares and rectangles.
First you need to somehow keep track of all the lines in your board and their connections. Every line has two ends only, and on each end, 1 (in the board corners) to up to 3 other lines may be connected. Lines may be active (drawn) or inactive.
You need to keep all this information in a structure or class, something like:
class Line {
public bool active=false;
public List<Line> end1Lines=new List<Line>();
public List<Line> end2Lines=new List<Line>();
}
I leave it up to you how to populate your map of lines. It will be a little headache but definitely doable.
Then, assuming all is set up, you have to check/parse your map every time after the player draws a line.
You need to start checking from the last line drawn, and recursively. You're trying to find if the player's last drawn line is closing a shape. So you would go something like this:
Check if in the present line (last drawn line) there are any active lines in either of ends, and go on checking on those lines repeating over and over until one of those lines matches the starting line (last drawn line). For this, you need to create a recursive function which will call itself from within itself endlessly until either you hit a dead end (no active lines in next end) or you hit the starting line. You need to pass as parameters, the starting line, the current line and previous line. Something like this:
int checkNextLines (Line currentLine, Line previousLine, Line startingLine) {
int linesCount=1;
List<Line> endLines;
//Check the lines in the opposite end only
if (currentLine.end1Lines.Contains(previousLine))
endLines=currentLine.end2Lines;
else
endLines=currentLine.end1Lines;
for (int i=0; i<endLines.Count; i++) {
if (endLines[i].active) {
if (endLines[i]==startingLine) //We arrived at the starting line and we have a closed shape!
return linesCount;
linesCount+=checkNextLines(endLines[i], currentLine, startingLine);
}
}
return linesCount;
}
Then you would first invoke it with your last drawn line, and passing null for the previous line:
int linesCount=checkNextLines(lastDrawnLine, null, lastDrawnLine);
Here I am assuming that List.Contains(null) will return false and not throw an exception, but I might be wrong, so you might need to take care of that.
NOTE that I haven't tested this code and I don't claim it's right. I'm just trying to give you a rough idea about how to go about this.
So after your call, you would check your linesCount. If it's 4 you have a square, if it's 6 you have 2 squares, if it's 8 you have 3 squares, either in an L shape or straight. If you have an odd number, then you have no closed shape (I think). Also I doubt this will work without some proper tweaking. I'm specially dubious about the return values and what will happen if you don't have a closed shape, so you might want to pay special attention to this and tweak as needed.
Anyways, just a way to go about it. Stick around for better answers. Hope it helps though.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
4 C# errors Endless Runner 0 Answers
How to have menu options perform an action using the Canvas UI system? 1 Answer