//moves the given snake by 1 depending on the direction stored in its structure int moveSnake( Snake *sn ) { int px; int py; if( sn==NULL ) return -1; //check if last element in the list contains if( getPartType( sn, sn->length -1 ) == SNAKE_EATEN ) { setPartType( sn, sn->length -1, SNAKE_PART ); sn->length++; } //get last recorded head coordinates px = sn->buf[sn->head].x; py = sn->buf[sn->head].y; //move the head back sn->head--; if( sn->head < 0 ) sn->head = WIN_LENGTH - 1; //update the position of the new head sn->buf[sn->head].y = py; sn->buf[sn->head].x = px; switch( sn->dir ) { case UP : sn->buf[sn->head].y--; break; case LEFT : sn->buf[sn->head].x--; break; case DOWN : sn->buf[sn->head].y++; break; case RIGHT: sn->buf[sn->head].x++; break; } return 0; }
QString SnakePart::decodePart() { QString name; // Player if (getPlayerNumber() == 0) { name = QLatin1String( "tron1-" ); } else if (getPlayerNumber() == 1) { name = QLatin1String( "tron2-" ); } // Heads (or tails) if (getPartType() == SnakePartType::Head) { if (getPartTop() && getPartLeft() && getPartRight()) { name += QLatin1String( "head-north" ); } else if (getPartBottom() && getPartLeft() && getPartRight()) { name += QLatin1String( "head-south" ); } else if (getPartTop() && getPartBottom() && getPartLeft()) { name += QLatin1String( "head-west" ); } else if (getPartTop() && getPartBottom() && getPartRight()) { name += QLatin1String( "head-east" ); } } else if (getPartType() == SnakePartType::Hole) { if (getPartTop() && getPartLeft() && getPartRight()) { name += QLatin1String( "tail-south" ); } else if (getPartBottom() && getPartLeft() && getPartRight()) { name += QLatin1String( "tail-north" ); } else if (getPartTop() && getPartBottom() && getPartLeft()) { name += QLatin1String( "tail-east" ); } else if (getPartTop() && getPartBottom() && getPartRight()) { name += QLatin1String( "tail-west" ); } } else if (getPartType() == SnakePartType::Tail) { if (getPartTop() && getPartLeft() && getPartRight()) { name += QLatin1String( "tail-south2" ); } else if (getPartBottom() && getPartLeft() && getPartRight()) { name += QLatin1String( "tail-north2" ); } else if (getPartTop() && getPartBottom() && getPartLeft()) { name += QLatin1String( "tail-east2" ); } else if (getPartTop() && getPartBottom() && getPartRight()) { name += QLatin1String( "tail-west2" ); } } else if (getPartType() == SnakePartType::Body) { // Bodys if (getPartTop() && getPartBottom()) { name += QLatin1String( "body-h" ); } else if (getPartLeft() && getPartRight()) { name += QLatin1String( "body-v" ); } else if (getPartLeft() && getPartTop()) { name += QLatin1String( "body-nw" ); } else if (getPartTop() && getPartRight()) { name += QLatin1String( "body-ne" ); } else if (getPartLeft() && getPartBottom()) { name += QLatin1String( "body-sw" ); } else if (getPartBottom() && getPartRight()) { name += QLatin1String( "body-se" ); } } return name; }