synced with my personal tree again

This commit is contained in:
Christian Kroll 2009-12-27 15:35:41 +00:00
parent 16df0e4889
commit 48badfd134
3 changed files with 31 additions and 32 deletions

View File

@ -37,13 +37,13 @@ void tetris_bastet_clearColHeights(tetris_bastet_t *pBastet,
*/ */
int tetris_bastet_qsortCompare(const void *pa, const void *pb) int tetris_bastet_qsortCompare(const void *pa, const void *pb)
{ {
tetris_bastet_scorepair_t *pScoreA = (tetris_bastet_scorepair_t *)pa; tetris_bastet_scorepair_t *pScorePairA = (tetris_bastet_scorepair_t *)pa;
tetris_bastet_scorepair_t *pScoreB = (tetris_bastet_scorepair_t *)pb; tetris_bastet_scorepair_t *pScorePairB = (tetris_bastet_scorepair_t *)pb;
if (pScoreA->nScore == pScoreB->nScore) if (pScorePairA->nScore == pScorePairB->nScore)
{ {
return 0; return 0;
} }
else if (pScoreA->nScore < pScoreB->nScore) else if (pScorePairA->nScore < pScorePairB->nScore)
{ {
return -1; return -1;
} }
@ -109,11 +109,16 @@ int16_t tetris_bastet_evalPos(tetris_bastet_t *pBastet,
tetris_piece_t *pPiece, tetris_piece_t *pPiece,
int8_t nColumn) int8_t nColumn)
{ {
// the row where the given piece collides
int8_t nDeepestRow = tetris_playfield_predictDeepestRow(pBastet->pPlayfield,
pPiece, nColumn);
// initial score of the given piece
int16_t nScore = -32000; int16_t nScore = -32000;
// modify score based on complete lines // modify score based on complete lines
int8_t nLines = tetris_playfield_predictCompleteLines(pBastet->pPlayfield, int8_t nLines = tetris_playfield_predictCompleteLines(pBastet->pPlayfield,
pPiece, nColumn); pPiece, nDeepestRow, nColumn);
nScore += 5000 * nLines; nScore += 5000 * nLines;
// determine sane start and stop columns whose heights we want to calculate // determine sane start and stop columns whose heights we want to calculate
@ -140,7 +145,7 @@ int16_t tetris_bastet_evalPos(tetris_bastet_t *pBastet,
tetris_playfield_iterator_t iterator; tetris_playfield_iterator_t iterator;
int8_t nHeight = 1; int8_t nHeight = 1;
uint16_t *pDump = tetris_playfield_predictBottomRow(&iterator, uint16_t *pDump = tetris_playfield_predictBottomRow(&iterator,
pBastet->pPlayfield, pPiece, nColumn); pBastet->pPlayfield, pPiece, nDeepestRow, nColumn);
if (pDump == NULL) if (pDump == NULL)
{ {
// an immediately returned NULL is caused by a full dump -> low score // an immediately returned NULL is caused by a full dump -> low score
@ -220,10 +225,10 @@ tetris_piece_t* tetris_bastet_choosePiece(tetris_bastet_t *pBastet)
qsort(pBastet->nPieceScores, 7, sizeof(tetris_bastet_scorepair_t), qsort(pBastet->nPieceScores, 7, sizeof(tetris_bastet_scorepair_t),
&tetris_bastet_qsortCompare); &tetris_bastet_qsortCompare);
uint8_t rnd = rand() % 100; uint8_t nRnd = rand() % 100;
for (uint8_t i = 0; i < 7; i++) for (uint8_t i = 0; i < 7; i++)
{ {
if (rnd < nPercent[i]) if (nRnd < nPercent[i])
{ {
return tetris_piece_construct(pBastet->nPieceScores[i].shape, return tetris_piece_construct(pBastet->nPieceScores[i].shape,
TETRIS_PC_ANGLE_0); TETRIS_PC_ANGLE_0);
@ -231,7 +236,7 @@ tetris_piece_t* tetris_bastet_choosePiece(tetris_bastet_t *pBastet)
} }
//should not arrive here //should not arrive here
return tetris_piece_construct(pBastet->nPieceScores[0].shape, return tetris_piece_construct(pBastet->nPieceScores[0].shape,
TETRIS_PC_ANGLE_0); TETRIS_PC_ANGLE_0);
} }
@ -244,6 +249,6 @@ tetris_piece_t* tetris_bastet_choosePiece(tetris_bastet_t *pBastet)
*/ */
tetris_piece_t* tetris_bastet_choosePreviewPiece(tetris_bastet_t *pBastet) tetris_piece_t* tetris_bastet_choosePreviewPiece(tetris_bastet_t *pBastet)
{ {
return tetris_piece_construct(pBastet->nPieceScores[6].shape, return tetris_piece_construct(pBastet->nPieceScores[6].shape,
TETRIS_PC_ANGLE_0); TETRIS_PC_ANGLE_0);
} }

View File

@ -13,7 +13,7 @@
/* Function: tetris_playfield_hoverStatus; /* Function: tetris_playfield_hoverStatus;
* Description: determines if piece is either hovering or gliding * Description: determines if piece is either hovering or gliding
* Argument pPl: playfield perform action on * Argument pPl: the playfield we want information from
* Return value: TETRIS_PFS_HOVERING or TETRIS_PFS_GLIDING * Return value: TETRIS_PFS_HOVERING or TETRIS_PFS_GLIDING
*/ */
tetris_playfield_status_t tetris_playfield_hoverStatus(tetris_playfield_t* pPl) tetris_playfield_status_t tetris_playfield_hoverStatus(tetris_playfield_t* pPl)
@ -667,11 +667,13 @@ int8_t tetris_playfield_predictDeepestRow(tetris_playfield_t *pPl,
* a given column * a given column
* Argument pPl: the playfield on which we want to test a piece * Argument pPl: the playfield on which we want to test a piece
* Argument pPiece: the piece which should be tested * Argument pPiece: the piece which should be tested
* Argument nRow: the row where the given piece collides
* Argument nColumn: the column where the piece should be dropped * Argument nColumn: the column where the piece should be dropped
* Return value: amount of complete lines * Return value: amount of complete lines
*/ */
int8_t tetris_playfield_predictCompleteLines(tetris_playfield_t *pPl, int8_t tetris_playfield_predictCompleteLines(tetris_playfield_t *pPl,
tetris_piece_t *pPiece, tetris_piece_t *pPiece,
int8_t nRow,
int8_t nColumn) int8_t nColumn)
{ {
int8_t nCompleteRows = 0; int8_t nCompleteRows = 0;
@ -679,7 +681,6 @@ int8_t tetris_playfield_predictCompleteLines(tetris_playfield_t *pPl,
// bit mask of a full row // bit mask of a full row
uint16_t nFullRow = 0xFFFF >> (16 - pPl->nWidth); uint16_t nFullRow = 0xFFFF >> (16 - pPl->nWidth);
int8_t nRow = tetris_playfield_predictDeepestRow(pPl, pPiece, nColumn);
if (nRow > -4) if (nRow > -4)
{ {
// determine sane start and stop values for the dump's index // determine sane start and stop values for the dump's index
@ -727,22 +728,25 @@ int8_t tetris_playfield_predictCompleteLines(tetris_playfield_t *pPl,
* Argument pIt: [out] a pointer to an iterator which should be initialized * Argument pIt: [out] a pointer to an iterator which should be initialized
* Argument pPl: the playfield on which we want to test a piece * Argument pPl: the playfield on which we want to test a piece
* Argument pPiece: the piece which should be tested * Argument pPiece: the piece which should be tested
* Argument nRow: the row where the given piece collides
* Argument nColumn: the column where the piece should be dropped * Argument nColumn: the column where the piece should be dropped
* Return value: appearance of the predicted dump row at the bottom * Return value: appearance of the predicted dump row at the bottom
*/ */
uint16_t* tetris_playfield_predictBottomRow(tetris_playfield_iterator_t *pIt, uint16_t* tetris_playfield_predictBottomRow(tetris_playfield_iterator_t *pIt,
tetris_playfield_t *pPl, tetris_playfield_t *pPl,
tetris_piece_t *pPiece, tetris_piece_t *pPiece,
int8_t nRow,
int8_t nColumn) int8_t nColumn)
{ {
pIt->pPlayfield = pPl; pIt->pPlayfield = pPl;
pIt->pPiece = pPiece; pIt->pPiece = pPiece;
pIt->nColumn = nColumn; pIt->nColumn = nColumn;
pIt->nDeepestPieceRow = pIt->nDeepestPieceRow = nRow;
tetris_playfield_predictDeepestRow(pPl, pPiece, nColumn);
pIt->nFullRow = 0xFFFF >> (16 - pPl->nWidth); pIt->nFullRow = 0xFFFF >> (16 - pPl->nWidth);
pIt->nCurrentRow = pPl->nHeight - 1; pIt->nCurrentRow = pPl->nHeight - 1;
pIt->nRowBuffer = 0; pIt->nRowBuffer = 0;
pIt->nStopRow = pPl->nFirstMatterRow < nRow ? pPl->nFirstMatterRow : nRow;
pIt->nStopRow = pIt->nStopRow < 0 ? 0 : pIt->nStopRow;
return tetris_playfield_predictNextRow(pIt); return tetris_playfield_predictNextRow(pIt);
} }
@ -751,13 +755,13 @@ uint16_t* tetris_playfield_predictBottomRow(tetris_playfield_iterator_t *pIt,
* Description: predicts the appearance of the next row of the playfield * Description: predicts the appearance of the next row of the playfield
* (for a given iterator) * (for a given iterator)
* Argument pIt: a pointer to a dump iterator * Argument pIt: a pointer to a dump iterator
* Return value: appearance of the next predicted dump row * Return value: appearance of next predicted row (or NULL -> no next line)
*/ */
uint16_t* tetris_playfield_predictNextRow(tetris_playfield_iterator_t *pIt) uint16_t* tetris_playfield_predictNextRow(tetris_playfield_iterator_t *pIt)
{ {
uint16_t nPieceMap = 0; uint16_t nPieceMap = 0;
if ((pIt->nDeepestPieceRow > -4) && (pIt->nCurrentRow >= 0)) if ((pIt->nDeepestPieceRow > -4) && (pIt->nCurrentRow >= pIt->nStopRow))
{ {
// determine sane start and stop values for the piece's indices // determine sane start and stop values for the piece's indices
int8_t nStartRow = int8_t nStartRow =

View File

@ -56,6 +56,7 @@ typedef struct tetris_playfield_iterator_t
int8_t nDeepestPieceRow; // the deepest possible row for a piece int8_t nDeepestPieceRow; // the deepest possible row for a piece
uint16_t nFullRow; // value of a full row uint16_t nFullRow; // value of a full row
int8_t nCurrentRow; // the actual row in the playfield int8_t nCurrentRow; // the actual row in the playfield
int8_t nStopRow; // the last row to be examined
uint16_t nRowBuffer; // internal buffer for returned row values uint16_t nRowBuffer; // internal buffer for returned row values
} }
tetris_playfield_iterator_t; tetris_playfield_iterator_t;
@ -244,29 +245,16 @@ int8_t tetris_playfield_predictDeepestRow(tetris_playfield_t *pPl,
* a given column * a given column
* Argument pPl: the playfield on which we want to test a piece * Argument pPl: the playfield on which we want to test a piece
* Argument pPiece: the piece which should be tested * Argument pPiece: the piece which should be tested
* Argument nRow: the row where the given piece collides
* Argument nColumn: the column where the piece should be dropped * Argument nColumn: the column where the piece should be dropped
* Return value: amount of complete lines * Return value: amount of complete lines
*/ */
int8_t tetris_playfield_predictCompleteLines(tetris_playfield_t *pPl, int8_t tetris_playfield_predictCompleteLines(tetris_playfield_t *pPl,
tetris_piece_t *pPiece, tetris_piece_t *pPiece,
int8_t nRow,
int8_t nColumn); int8_t nColumn);
/* Function: tetris_playfield_predictDumpRow
* Description: predicts the appearance of a playfield row for a piece
* at a given column
* Argument pPl: the playfield on which we want to test a piece
* Argument pPiece: the piece which should be tested
* Argument nColumn: the column where the piece should be dropped
* Argument nRow: the row of interest
* Return value: appearance of the predicted dump row
*/
uint16_t tetris_playfield_predictDumpRow(tetris_playfield_t *pPl,
tetris_piece_t *pPiece,
int8_t nColumn,
int8_t nRow);
/* Function: tetris_playfield_predictBottomRow /* Function: tetris_playfield_predictBottomRow
* Description: predicts the appearance of the bottom row of the * Description: predicts the appearance of the bottom row of the
* playfield (for a piece at a given column) and * playfield (for a piece at a given column) and
@ -274,12 +262,14 @@ uint16_t tetris_playfield_predictDumpRow(tetris_playfield_t *pPl,
* Argument pIt: a pointer to an iterator which should be initialized * Argument pIt: a pointer to an iterator which should be initialized
* Argument pPl: the playfield on which we want to test a piece * Argument pPl: the playfield on which we want to test a piece
* Argument pPiece: the piece which should be tested * Argument pPiece: the piece which should be tested
* Argument nRow: the row where the given piece collides
* Argument nColumn: the column where the piece should be dropped * Argument nColumn: the column where the piece should be dropped
* Return value: appearance of the predicted dump row at the bottom * Return value: appearance of the predicted dump row at the bottom
*/ */
uint16_t* tetris_playfield_predictBottomRow(tetris_playfield_iterator_t *pIt, uint16_t* tetris_playfield_predictBottomRow(tetris_playfield_iterator_t *pIt,
tetris_playfield_t *pPl, tetris_playfield_t *pPl,
tetris_piece_t *pPiece, tetris_piece_t *pPiece,
int8_t nRow,
int8_t nColumn); int8_t nColumn);
@ -287,7 +277,7 @@ uint16_t* tetris_playfield_predictBottomRow(tetris_playfield_iterator_t *pIt,
* Description: predicts the appearance of the next row of the playfield * Description: predicts the appearance of the next row of the playfield
* (for a given iterator) * (for a given iterator)
* Argument pIt: a pointer to a dump iterator * Argument pIt: a pointer to a dump iterator
* Return value: appearance of the next predicted dump row * Return value: appearance of next predicted row (or NULL -> no next line)
*/ */
uint16_t* tetris_playfield_predictNextRow(tetris_playfield_iterator_t *pIt); uint16_t* tetris_playfield_predictNextRow(tetris_playfield_iterator_t *pIt);