[Careercup] 8.6 Jigsaw Puzzle jigsaw puzzle game

Source: Internet
Author: User

8.6 Implement a jigsaw puzzle. Design The data structures and explain an algorithm to solve the puzzle. You can assume so you had a F Itswith method which, when passed, puzzle pieces, returns True if the both pieces Belon G together.

This question lets us design a puzzle game, according to the book explanation, as shown is one kind of most basic puzzle game, each piece has four sides, altogether has three kinds of sides, inner, outer, and flat, the corner piece has two flat side, the middle piece has no flat side. Then we need a edge class edge, also need a piece class piece, and a puzzle class puzzle, in the puzzle class with sort to initialize parameters, and then use the solve to complete the puzzle. Here have to spit, this problem is downloaded on the Internet in the code is wood, and the book code has a lot of small errors, corrected see the code as follows:

classEdge;classPiece { Public: Vector<Edge*>_edges; BOOLIscorner () {}// ...};enumType {inner, outer, flat};classEdge { Public: Piece*_parent;    Type _type; int_index; Edge*_attachedto; BOOLFitswith (Edge *edge) {}// ...};classPuzzle { Public: Vector<Piece*>_pieces; Vector<vector<Piece*> >_solution; Vector<Edge*>_inners, _outers, _flats; Vector<Piece*>_corners; voidsort () { for(Piece *P: _pieces) {            if(p->Iscorner ()) _corners.push_back (p);  for(Edge *e:p->_edges) {                if(E->_type = =Type::inner) _inners.push_back (e); if(E->_type = =type::outer) _outers.push_back (e); }        }    }    voidsolve () {Edge*currentedge = Getexposededge (_corners[0]);  while(Currentedge! =nullptr) {Vector<Edge*> Opposites = Currentedge->_type = = Type::inner?_outers: _inners;  for(Edge *e:opposites) {                if(currentedge->Fitswith (E))                    {attachedges (Currentedge, E);                    RemoveFromList (Currentedge);                    RemoveFromList (e); Currentedge=Nextexposededge (e);  Break; }            }        }    }    voidRemoveFromList (Edge *Edge) {        if(Edge->_type = = Type::flat)return; Vector<Edge*> array = Edge->_type = = Type::inner?_inners: _outers;  for(Vector<edge*>::iterator it = Array.begin (); It! = Array.end (); + +it) {            if(*it = =Edge)                {array.erase (IT);  Break; }}} Edge* Nextexposededge (Edge *Edge) {        intNext_idx = (Edge->_index +2) %4;//opposite EdgeEdge *next_edge = edge->_parent->_edges[next_idx]; if(isexposed (Next_edge)) {returnNext_edge; }        returnGetexposededge (edge->_parent); }    voidAttachedges (Edge *e1, Edge *E2) {E1->_attachedto =E2; E2->_attachedto =E1; }    BOOLisexposed (Edge *Edge) {        returnEdge->_type! = Type::flat && Edge->_attachedto = =nullptr; } Edge* Getexposededge (Piece *p) { for(Edge *e:p->_edges) {            if(Isexposed (E))returne; }        returnnullptr; }};

[Careercup] 8.6 Jigsaw Puzzle jigsaw puzzle game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.