Esempio n. 1
0
bool CastlingCommand::redo(Chessboard &board)
{
    Square *to = board.squareAt(toSquare());
    Square *from = board.squareAt(fromSquare());

    Q_ASSERT(to->isEmpty() && "Runtime error: square is not empty. AttackCommand should be used");
    Q_ASSERT(!from->isEmpty() && "Runtime error: square is empty");

    Piece* kingPiece = from->piece();
    if (kingPiece == nullptr)
    {
        qCritical() << "Castled King is absent";
        return false;
    }

    if (kingPiece->type() != PieceType::King)
    {
        qCritical() << "Castled piece is not a King";
        return false;
    }

    markAsMoved(*kingPiece);
    board.removePiece(kingPiece);
    board.putPiece(to, kingPiece);

    Piece* rookPiece = board.squareAt(Position(to->file() == File::C ? File::A : File::H, to->rank()))->piece();
    if (rookPiece == nullptr)
    {
        qCritical() << "Castling Rook is absent";
        return false;
    }

    if (rookPiece->type() != PieceType::Rook)
    {
        qCritical() << "Castling piece is not a Rook";
        return false;
    }

    // TODO: check for possible attack of intermediate squares
    markAsMoved(*rookPiece);
    board.removePiece(rookPiece);

    Square* rookNewPlace = board.squareAt(Position(to->file() == File::C ? File::D : File::F, to->rank()));
    board.putPiece(rookNewPlace, rookPiece);

    qDebug() << "Castling: " << " " << from->toStr() << ":" << rookNewPlace->toStr();

    return true;
}
Esempio n. 2
0
bool AttackCommand::redo(Chessboard &board)
{
    Square *to = board.squareAt(toSquare());
    Square *from = board.squareAt(fromSquare());

    if (to->isEmpty())
    {
        qCritical() << "Runtime error: square is empty. MoveCommand should be used";
        return false;
    }

    if (from->isEmpty())
    {
        qCritical() << "Runtime error: square is empty";
        return false;
    }

    Piece* movedePiece = from->piece();
    markAsMoved(*movedePiece);
    board.removePiece(movedePiece);

    m_killedPiece = to->piece();
    board.removePiece(m_killedPiece);
    board.putPiece(to, movedePiece);
    qDebug() << "Attack: " << Chess::toString(movedePiece->type()) << " " << from->toStr() << ":" << to->toStr();

    return true;
}
Esempio n. 3
0
bool compareNew()
{
	clearWindow(nppData._scintillaMainHandle, true);
	clearWindow(nppData._scintillaSecondHandle, true);
	
    active = true;

	int doc1Length;
	int *lineNum1;

	char **doc1 = getAllLines(nppData._scintillaMainHandle, &doc1Length, &lineNum1);

	if(doc1Length < 1)
    {
        return true;
    }

	int doc2Length;
	int *lineNum2;

	char **doc2 = getAllLines(nppData._scintillaSecondHandle, &doc2Length, &lineNum2);
	
    if(doc2Length < 1)
    {
        return true;
    }

	int	doc1Changed = 0;
	int	doc2Changed = 0;
	diff_edit *doc1Changes = NULL;
	diff_edit *doc2Changes = NULL;

	unsigned int *doc1Hashes = computeHashes(doc1, doc1Length, Settings.IncludeSpace);
	unsigned int *doc2Hashes = computeHashes(doc2, doc2Length, Settings.IncludeSpace);

	/* make diff */
	int sn;
	struct varray *ses = varray_new(sizeof(struct diff_edit), NULL);
	int result = (diff(doc1Hashes, 0, doc1Length, doc2Hashes, 0, doc2Length, (idx_fn)(getLineFromIndex), (cmp_fn)(compareLines), NULL, 0, ses, &sn, NULL));
	int changeOffset = 0;

    shift_boundries(ses, sn, doc1Hashes, doc2Hashes, doc1Length, doc2Length);
	find_moves(ses, sn, doc1Hashes, doc2Hashes, Settings.DetectMove);
	/* 
     * - insert empty lines
	 * - count changed lines
	 */
	doc1Changed = 0;
	doc2Changed = 0;

	for (int i = 0; i < sn; i++) 
    {
		struct diff_edit *e =(diff_edit*) varray_get(ses, i);
		if(e->op == DIFF_DELETE)
        {
			e->changeCount = 0;
			doc1Changed += e->len;
			struct diff_edit *e2 =(diff_edit*) varray_get(ses, i+1);
			e2->changeCount = 0;
			
            if(e2->op == DIFF_INSERT)
            {
				//see if the DELETE/INSERT COMBO includes changed lines or if its a completely new block
				if(compareWords(e, e2, doc1, doc2, Settings.IncludeSpace))
				{
					e->op = DIFF_CHANGE1;
					e2->op = DIFF_CHANGE2;
					doc2Changed += e2->len;
				}
			}
		}
        else if(e->op == DIFF_INSERT)
        {
			e->changeCount = 0;
			doc2Changed += e->len;
		}
	}

	int doc1CurrentChange = 0;
	int doc2CurrentChange = 0;
	changeOffset = 0;
	doc1Changes = new diff_edit[doc1Changed];
	doc2Changes = new diff_edit[doc2Changed];
	int doc1Offset = 0;
	int doc2Offset = 0;

	//switch from blocks of lines to one change per line. Change CHANGE to DELETE or INSERT if there are no changes on that line
	int added;

	for (int i = 0; i < sn; i++) 
    {
		struct diff_edit *e =(diff_edit*) varray_get(ses, i);
		e->set = i;

		switch(e->op)
        {
			case DIFF_CHANGE1:
			case DIFF_DELETE:
				added = setDiffLines(e, doc1Changes, &doc1CurrentChange, DIFF_DELETE, e->off + doc2Offset);
				doc2Offset -= added;
				doc1Offset += added;
				break;
			case DIFF_INSERT:
			case DIFF_CHANGE2:
				added = setDiffLines(e, doc2Changes, &doc2CurrentChange, DIFF_INSERT, e->off + doc1Offset);	
				doc1Offset -= added;
				doc2Offset += added;
				break;
		}
	}

	if (result != -1)
    {
		int textIndex;
		different = (doc1Changed > 0) || (doc2Changed > 0);
		
        for(int i = 0; i < doc1Changed; i++)
        {
			switch(doc1Changes[i].op)
            {
				case DIFF_DELETE:
					markAsRemoved(nppData._scintillaMainHandle, doc1Changes[i].off);					
					break;

				case DIFF_CHANGE1:
					markAsChanged(nppData._scintillaMainHandle, doc1Changes[i].off);
					textIndex = lineNum1[doc1Changes[i].off];

					for(int k = 0; k < doc1Changes[i].changeCount; k++)
                    {
						struct diff_change *change = (diff_change*)varray_get(doc1Changes[i].changes, k);
						markTextAsChanged(nppData._scintillaMainHandle, textIndex + change->off, change->len);
					}
					break;

				case DIFF_MOVE:					
					markAsMoved(nppData._scintillaMainHandle, doc1Changes[i].off);
					break;

			}
		}

		for(int i = 0; i < doc2Changed; i++)
        {
			switch(doc2Changes[i].op)
            {
				case DIFF_INSERT:					
					markAsAdded(nppData._scintillaSecondHandle, doc2Changes[i].off);						
					break;

				case DIFF_CHANGE2:
					markAsChanged(nppData._scintillaSecondHandle, doc2Changes[i].off);
					textIndex = lineNum2[doc2Changes[i].off];
					for(int k = 0; k < doc2Changes[i].changeCount; k++)
                    {
						struct diff_change *change=(diff_change*)varray_get(doc2Changes[i].changes, k);
						markTextAsChanged(nppData._scintillaSecondHandle, textIndex+change->off, change->len);
					}
					break;

				case DIFF_MOVE:					
					markAsMoved(nppData._scintillaSecondHandle,doc2Changes[i].off);										
					break;
			}
		}

		doc1Offset = 0;
		doc2Offset = 0;

		if(Settings.AddLine)
        {
			int length = 0;
			int off = -1;
			for(int i = 0; i < doc1Changed; i++)
            {
				switch(doc1Changes[i].op)
                {
					case DIFF_DELETE:
					case DIFF_MOVE:							
						if(doc1Changes[i].altLocation == off)
                        {
							length++;
						}
                        else
                        {
							addEmptyLines(nppData._scintillaSecondHandle, off + doc2Offset, length);
							doc2Offset += length;
							off = doc1Changes[i].altLocation;
							length = 1;						
						}
						break;
				}
			}

			addEmptyLines(nppData._scintillaSecondHandle, off + doc2Offset, length);

			if(doc2Offset > 0)
            {
				clearUndoBuffer(nppData._scintillaSecondHandle);
			}

			length = 0;
			off = 0;
			doc1Offset = 0;

			for(int i = 0; i < doc2Changed; i++)
            {
				switch(doc2Changes[i].op)
                {
					case DIFF_INSERT:
					case DIFF_MOVE:							
						if(doc2Changes[i].altLocation == off)
                        {
							length++;
						}
                        else
                        {
							addEmptyLines(nppData._scintillaMainHandle, off + doc1Offset, length);
							doc1Offset += length;
							off = doc2Changes[i].altLocation;
							length = 1;						
						}
						break;
				}
			}

			addEmptyLines(nppData._scintillaMainHandle, off + doc1Offset, length);

			if(doc1Offset > 0)
            {
				clearUndoBuffer(nppData._scintillaMainHandle);
			}
		}

//clean up resources
#if CLEANUP

		for(int i = 0; i < doc1Length; i++)
        {
			if(*doc1[i] != 0)
            {
				delete[] doc1[i];
			}
		}

		delete[] doc1;
		delete[] lineNum1;
		
        for(int i = 0; i < doc2Length; i++)
        {
			if(*doc2[i] != 0)
            {
				delete[] doc2[i];
			}
		}

		delete[] doc2;
		delete lineNum2;

		delete[] doc1Hashes;
		delete[] doc2Hashes;

		clearEdits(ses, sn);

		for(int i = 0; i < doc1Changed; i++)
        {
			clearEdit(doc1Changes + (i));
		}

		delete[] doc1Changes;

		for(int i = 0; i < doc2Changed; i++)
        {
			clearEdit(doc2Changes+(i));
		}

		delete[] doc2Changes;

#endif // CLEANUP

		if(!different)
        {
			::MessageBox(nppData._nppHandle, TEXT("Files Match"), TEXT("Results :"), MB_OK);
			return true;
		}

		::SendMessageA(nppData._scintillaMainHandle, SCI_SHOWLINES, 0, (LPARAM)1);
		::SendMessageA(nppData._scintillaSecondHandle, SCI_SHOWLINES, 0, (LPARAM)1);

		return false;
	}
    return false;
}