// c'tor public TetrisModelImpl() { Debug.WriteLine("c'tor TetrisModel"); this.random = new Random(); this.testTetriCounter = 0; this.board = new TetrisBoard(this.NumRows, this.NumColumns); this.board.BoardChanged += this.TetrisBoard_Changed; this.board.LinesCompleted += this.TetrisBoard_LinesCompleted; this.state = TetrisState.Idle; this.gameState = GameState.GameIdle; this.viewCellList = new ViewCellList(); // initialize score management this.level = 1; this.lines = 0; this.score = 0; // setup timer this.timerMode = TimerMode.Normal; this.maxIntervalCounter = MaxIntervalCounter; this.currentIntervalCounter = 0; }
public void MoveNonEmptyRowsDown() { ViewCellList list = new ViewCellList(); // compute number of complete rows - beginning from the bottom int completeRows = 0; for (int row = this.numRows - 1; row >= 0; row--) { if (this.IsRowComplete(row)) { completeRows++; } else { break; } } // calculate number of rows to move int startRow = this.numRows - 1; while (!this.IsRowEmpty(startRow)) { startRow--; } for (int i = this.numRows - 2; i >= startRow; i--) { this.CopySingleRow(list, i); } this.OnBoardChanged(list); this.OnLinesCompleted(completeRows); }
// public interface public void Clear() { // reset internal state of board for (int i = 0; i < this.numRows; i++) { for (int j = 0; j < this.numColumns; j++) { this.board[i, j].Color = CellColor.LightGray; this.board[i, j].State = CellState.Free; } } // update view ViewCellList list = new ViewCellList(); for (int i = 0; i < this.numRows; i++) { for (int j = 0; j < this.numColumns; j++) { ViewCell cell = new ViewCell(); cell.Color = CellColor.LightGray; cell.Point = new CellPoint() { X = j, Y = i }; list.Add(cell); } } this.OnBoardChanged(list); }
private void OnBoardChanged(ViewCellList list) { if (this.BoardChanged != null) { this.BoardChanged.Invoke(list); } }
// public interface (movement specific methods) public void SetToTop() { this.Set(); ViewCellList list = new ViewCellList(); this.UpdateModifiedCellList(list, this.color); this.board.PostChanges(list); }
public void Rotate() { ViewCellList list = new ViewCellList(); this.Delete(); this.UpdateModifiedCellList(list, CellColor.LightGray); this.RotateTetrimino(); this.Set(); this.UpdateModifiedCellList(list, this.color); this.board.PostChanges(list); }
public void MoveRight() { ViewCellList list = new ViewCellList(); this.Delete(); this.UpdateModifiedCellList(list, CellColor.LightGray); this.MoveAnchorRight(); this.Set(); this.UpdateModifiedCellList(list, this.color); this.board.PostChanges(list); }
public bool MoveDown() { if (!this.CanMoveDown()) { return(false); } ViewCellList list = new ViewCellList(); this.Delete(); this.UpdateModifiedCellList(list, CellColor.LightGray); this.MoveAnchorDown(); this.Set(); this.UpdateModifiedCellList(list, this.color); this.board.PostChanges(list); return(true); }
public override void UpdateModifiedCellList(ViewCellList list, CellColor color) { list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X, Y = this.anchorPoint.Y })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X + 1, Y = this.anchorPoint.Y })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X, Y = this.anchorPoint.Y + 1 })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X + 1, Y = this.anchorPoint.Y + 1 })); }
private void CopySingleRow(ViewCellList list, int row) { for (int j = 0; j < this.numColumns; j++) { // create cell to update view ViewCell cell = new ViewCell(); cell.Color = this.board[row, j].Color; cell.Point = new CellPoint() { X = j, Y = row + 1 }; list.Add(cell); // finally copy block one row down ... // Note: TetrisCell is a 'struct', so '=' works fine -- // In case of a reference type: Clone needed (deep copy) !!! this.board[row + 1, j] = this.board[row, j]; } }
// private helper methods private void TetrisBoard_Changed(ViewCellList list) { this.BoardStateChanges = list; }
public abstract void UpdateModifiedCellList(ViewCellList list, CellColor color);
public override void UpdateModifiedCellList(ViewCellList list, CellColor color) { if (this.rotation == RotationAngle.Degrees_0) { list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X - 1, Y = this.anchorPoint.Y + 1 })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X, Y = this.anchorPoint.Y + 1 })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X, Y = this.anchorPoint.Y })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X + 1, Y = this.anchorPoint.Y })); } else if (this.rotation == RotationAngle.Degrees_90) { list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X - 1, Y = this.anchorPoint.Y - 1 })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X - 1, Y = this.anchorPoint.Y })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X, Y = this.anchorPoint.Y })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X, Y = this.anchorPoint.Y + 1 })); } else if (this.rotation == RotationAngle.Degrees_180) { list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X - 1, Y = this.anchorPoint.Y })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X, Y = this.anchorPoint.Y })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X, Y = this.anchorPoint.Y - 1 })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X + 1, Y = this.anchorPoint.Y - 1 })); } else if (this.rotation == RotationAngle.Degrees_270) { list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X, Y = this.anchorPoint.Y - 1 })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X, Y = this.anchorPoint.Y })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X + 1, Y = this.anchorPoint.Y })); list.Add(new ViewCell(color, new CellPoint() { X = this.anchorPoint.X + 1, Y = this.anchorPoint.Y + 1 })); } }
public void PostChanges(ViewCellList list) { this.OnBoardChanged(list); }