Flush lines

This commit is contained in:
Hajime Hoshi 2013-12-27 00:37:36 +09:00
parent ce97070f66
commit 51fa65bf2f
2 changed files with 29 additions and 3 deletions

View File

@ -62,9 +62,35 @@ func (f *Field) RotatePieceRight(piece *Piece, x, y int, angle Angle) Angle {
return angle.RotateRight() return angle.RotateRight()
} }
func (f *Field) AbsorbPiece(piece *Piece, x, y int, angle Angle) { func (f *Field) AbsorbPiece(piece *Piece, x, y int, angle Angle) {
piece.absorbInto(f, x, y, angle) piece.AbsorbInto(f, x, y, angle)
f.Flush()
}
func (f *Field) flushLine(j int) bool {
for i := 0; i < fieldBlockNumX; i++ {
if f.blocks[i][j] == BlockTypeNone {
return false
}
}
for j2 := j; 1 <= j2; j2-- {
for i := 0; i < fieldBlockNumX; i++ {
f.blocks[i][j2] = f.blocks[i][j2-1]
}
}
for i := 0; i < fieldBlockNumX; i++ {
f.blocks[i][0] = BlockTypeNone
}
return true
}
func (f *Field) Flush() {
flushedLineNum := 0
for j := fieldBlockNumY - 1; 0 <= j; j-- {
if f.flushLine(j + flushedLineNum) {
flushedLineNum++
}
}
} }
func (f *Field) Draw(context graphics.Context, geo matrix.Geometry) { func (f *Field) Draw(context graphics.Context, geo matrix.Geometry) {

View File

@ -193,7 +193,7 @@ func (p *Piece) collides(field *Field, x, y int, angle Angle) bool {
return false return false
} }
func (p *Piece) absorbInto(field *Field, x, y int, angle Angle) { func (p *Piece) AbsorbInto(field *Field, x, y int, angle Angle) {
size := len(p.blocks) size := len(p.blocks)
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
for j := 0; j < size; j++ { for j := 0; j < size; j++ {