public AIController(TetrisGame game, AIType type = AIType.High) { _game = game; _id = -1; int speed = 0; int error = 0; int count = 0; // speed: 0~15 (速度,0为完全不按加速) // error: 0~100 (每次犯错的概率) // count: >=0 (每几次才可能犯错一次,0为不犯错,1为每次都可能犯错) if (type == AIType.High) { speed = 150; } else if (type == AIType.Middle) { speed = 12; count = 2; error = 10; } else if (type == AIType.Low) { speed = 12; count = 1; error = 30; } _speed = speed; _error = error; _count = count; _countError = 0; }
/// <summary> /// 生成新游戏 /// </summary> /// <param name="user">用户名</param> /// <param name="controller">控制器</param> /// <param name="withItem">是否启用道具</param> /// <returns></returns> public TetrisGame NewGame(string user = "", IController controller = null, bool withItem = true) { var id = games.Count; ITetrisFactory factory; if (withItem) { factory = new CacheFactory(Square.Styles(styles), ran); (factory as TetrisItemFactory).GenSpecialBlock = true; } else { factory = new TetrisFactory(Square.Styles(styles), ran); } // use AdvancedGUI.Styles.WindowSizeGenerator to set the game size! shuo han int gameWidth = AdvancedGUI.Styles.WindowSizeGenerator.gameWidth; int gameHeight = AdvancedGUI.Styles.WindowSizeGenerator.gameHeight; var game = new TetrisGame(id, Square.Styles(styles), _engine, factory, gameWidth, gameHeight, Settings.Default.DefaultSpeed); game.SetController(controller); ItemSystem.Bind(game); ScoreSystem.Bind(game); AchievementSystem.Bind(game, user); games[id] = game; return(game); }
public void OnDrawing(TetrisGame game, TetrisGame.DrawEventArgs e) // 绘制屏幕 { _image = game.Image; Console.Clear(); Console.WriteLine(game.ScoreSystem.Score); Console.WriteLine(game.GameSpeed); Console.WriteLine("============"); for (int i = 0; i < 15; i++) { Console.Write("|"); for (int j = 0; j < 10; j++) { Console.Out.Write(_image[i, j] == null ? " " : GetChar(_image[i, j].NewSquare ? -2 : _image[i, j].Color)); } Console.Out.WriteLine("|"); } Console.WriteLine("============"); Debug.Assert(game.Factory is CacheFactory); // 显示下一个方块 var factory = game.Factory as CacheFactory; Debug.Assert(factory.NextBlock() != null); var block = factory.NextBlock(); for (var i = block.Height - 1; i >= 0; i--) { Console.Write("|"); for (var j = 0; j < block.Width; j++) { Console.Write(GetChar(block.SquareAt(i, j) == null? -1: block.SquareAt(i, j).Color)); } Console.WriteLine("|"); } }
// used to draw the game public void OnDrawing(Tetris.GameBase.TetrisGame game, Tetris.GameBase.TetrisGame.DrawEventArgs e) { Square[,] image = game.Image; if (imageCache == null) { imageCache = new int[image.GetUpperBound(0) + 1, image.GetUpperBound(1) + 1]; Array.Clear(imageCache, 0, (image.GetUpperBound(0) + 1) * (image.GetUpperBound(1) + 1)); } int i, j; for (i = 0; i < game.Height; i++) { for (j = 0; j < game.Width; j++) { // only draw the changed part if ((image[i, j] == null && imageCache[i, j] != 0) || (image[i, j] != null && image[i, j].Color + image[i, j].SubId != imageCache[i, j])) { imageCache[i, j] = image[i, j] == null ? 0 : image[i, j].Color + image[i, j].SubId; try { squaresMatrix[i, j].Dispatcher.Invoke( new Action( delegate { /* * squaresMatrix[i, j].Fill = * new SolidColorBrush( * colorMap[ * image[i, j] == null * ? 0 * : (image[i, j].Color < colorMap.Length * ? image[i, j].Color * : colorMap.Length - 1)]); */ squaresMatrix[i, j].Fill = SquareGenerator.brushClone( brushesMap[ image[i, j] == null ? 0 : (image[i, j].Color + image[i, j].SubId < brushesMap.Length ? image[i, j].Color + +image[i, j].SubId : brushesMap.Length - 1)]); } )); } catch { } } } } }
public SingleModePage() { InitializeComponent(); int[] nextBlockSize = new int[] { 4, 3 }; ColumnDefinition aCol = new ColumnDefinition(); aCol.Width = new GridLength(50, GridUnitType.Star); outerGrid.ColumnDefinitions.Add(aCol); aCol = new ColumnDefinition(); aCol.Width = new GridLength(50, GridUnitType.Auto); outerGrid.ColumnDefinitions.Add(aCol); aCol = new ColumnDefinition(); aCol.Width = new GridLength(50, GridUnitType.Star); outerGrid.ColumnDefinitions.Add(aCol); outerGrid.Width = Styles.WindowSizeGenerator.screenWidth; outerGrid.Height = Styles.WindowSizeGenerator.screenHeight; Border border = new Border(); border.BorderBrush = new SolidColorBrush(Colors.Gray); border.BorderThickness = new Thickness(2, 2, 2, 2); game = t.NewGame(PlayersName.getName(0)); int[] gridSize = new int[2] { game.Height, game.Width }; gameGrid = new GameGrid(gridSize); game.AddDisplay(gameGrid); border.Child = gameGrid; outerGrid.Children.Add(border); border.SetValue(Grid.RowProperty, 1); border.SetValue(Grid.ColumnProperty, 1); // set left crocodile PicGen pic = new CrocodileGen(); PicGenGrid pg1 = new PicGenGrid(pic, SquareGenerator.picSquareSize); aCanvas.Children.Add(pg1); pg1.SetValue(Canvas.ZIndexProperty, 0); Canvas.SetLeft(pg1, (Styles.WindowSizeGenerator.screenWidth - gameGrid.getGameGridSize()[1]) / 6); Canvas.SetBottom(pg1, (Styles.WindowSizeGenerator.screenHeight - gameGrid.getGameGridSize()[0]) / 2 - 0.2 * pg1.getPicSize()[0]); // set right crocodile pic = new CrocodileGen(); PicGenGrid pg2 = new PicGenGrid(pic, SquareGenerator.picSquareSize); aCanvas.Children.Add(pg2); pg2.SetValue(Canvas.ZIndexProperty, 0); Canvas.SetRight(pg2, (Styles.WindowSizeGenerator.screenWidth - gameGrid.getGameGridSize()[1]) / 4); Canvas.SetBottom(pg2, (Styles.WindowSizeGenerator.screenHeight - gameGrid.getGameGridSize()[0]) / 2 - 0.2 * pg2.getPicSize()[0]); // set little sun pic = new SunGen(); PicGenGrid pg3 = new PicGenGrid(pic, SquareGenerator.picSquareSize / 1.1); aCanvas.Children.Add(pg3); pg3.SetValue(Canvas.ZIndexProperty, 0); Canvas.SetLeft(pg3, -40); Canvas.SetTop(pg3, -40); // set score board double scoreHeight = Styles.WindowSizeGenerator.scoreBoardHeight; double scoreWidth = Styles.WindowSizeGenerator.scoreBoardWidth; double scoreLeftLoc = Styles.WindowSizeGenerator.scoreBoardLeft + (Styles.WindowSizeGenerator.screenWidth + gameGrid.getGameGridSize()[1]) / 2; double scoreBottomLoc = gameGrid.getGameGridSize()[0] + Styles.WindowSizeGenerator.gameModuleTop - scoreHeight; score = new ScoreGrid(scoreHeight, scoreWidth); Grid scoreNextBlockGrid = new Grid(); aCol = new ColumnDefinition(); aCol.Width = new GridLength(0.5 * scoreWidth, GridUnitType.Pixel); scoreNextBlockGrid.ColumnDefinitions.Add(aCol); aCol = new ColumnDefinition(); scoreNextBlockGrid.ColumnDefinitions.Add(aCol); RowDefinition aRow = new RowDefinition(); aRow.Height = new GridLength(1, GridUnitType.Auto); scoreNextBlockGrid.RowDefinitions.Add(aRow); aRow = new RowDefinition(); aRow.Height = new GridLength(0.2 * scoreHeight, GridUnitType.Pixel); scoreNextBlockGrid.RowDefinitions.Add(aRow); aRow = new RowDefinition(); aRow.Height = new GridLength(1, GridUnitType.Star); scoreNextBlockGrid.RowDefinitions.Add(aRow); scoreNextBlockGrid.Children.Add(score); score.SetValue(Grid.ColumnProperty, 1); score.SetValue(Grid.RowProperty, 0); score.SetValue(Grid.HorizontalAlignmentProperty, HorizontalAlignment.Left); score.SetValue(Grid.VerticalAlignmentProperty, VerticalAlignment.Top); outerGrid.Children.Add(scoreNextBlockGrid); scoreNextBlockGrid.SetValue(Grid.ColumnProperty, 2); scoreNextBlockGrid.SetValue(Grid.RowProperty, 1); score.DataContext = game.ScoreSystem; // set next block board double nextBlockHeight = Styles.WindowSizeGenerator.nextBoardHeight; double nextBlockWidth = Styles.WindowSizeGenerator.nextBoardWidth; double nextLeftLoc = scoreLeftLoc; double nextBottomLoc = scoreBottomLoc + nextBlockHeight + 10; NextBlock nextBlock = new NextBlock(nextBlockHeight, nextBlockWidth); Console.WriteLine(nextBlockHeight + " block h"); double[] nextSize = nextBlock.getSize(); scoreNextBlockGrid.Children.Add(nextBlock); nextBlock.SetValue(Grid.RowProperty, 2); nextBlock.SetValue(Grid.ColumnProperty, 1); nextBlock.SetValue(Grid.HorizontalAlignmentProperty, HorizontalAlignment.Left); nextBlock.SetValue(Grid.VerticalAlignmentProperty, VerticalAlignment.Top); game.AddDisplay(nextBlock); aRect = new Rectangle(); aRect.Fill = new SolidColorBrush(Colors.Transparent); aRect.Width = gameGrid.getGameGridSize()[1]; aRect.Height = gameGrid.getGameGridSize()[0]; outerGrid.Children.Add(aRect); aRect.SetValue(Grid.ColumnProperty, 1); aRect.SetValue(Grid.RowProperty, 1); game.GameEndEvent += gameEndEffect; }
// next block drawing public void OnDrawing(Tetris.GameBase.TetrisGame game, Tetris.GameBase.TetrisGame.DrawEventArgs e) { var factory = game.Factory as CacheFactory; var block = factory.NextBlock(); if (block.Id == id) { return; } id = block.Id; int i, j; try { nextBlockGrid.Dispatcher.Invoke( new Action( delegate { nextBlockGrid.SetValue(Canvas.TopProperty, (this.Height - 6 - _squareSize * block.Width) / 2); nextBlockGrid.SetValue(Canvas.LeftProperty, (this.Width - 6 - _squareSize * block.Height) / 2); } )); } catch { } for (i = 0; i < _gridHeight; i++) { for (j = 0; j < _gridWidth; j++) { try { squaresMatrix[i, j].Dispatcher.Invoke( new Action( delegate { /* * squaresMatrix[i, j].Fill = * //new SolidColorBrush(Colors.Transparent); * * new SolidColorBrush(colorMap[block.SquareAt(j, i) == null ? 0 : * (block.SquareAt(j, i).Color < colorMap.Length ? * block.SquareAt(j, i).Color : colorMap.Length - 1)]); */ squaresMatrix[i, j].Fill = SquareGenerator.brushClone( brushesMap[ block.SquareAt(j, i) == null ? 0 : (block.SquareAt(j, i).Color + block.SquareAt(j, i).SubId < brushesMap.Length ? block.SquareAt(j, i).Color + block.SquareAt(j, i).SubId : brushesMap.Length - 1)]); } )); } catch { } } } }