public static Tuple<int, int> Think(World world, Shape shape) { gridWidth = world.Columns; gridHeight = world.Rows; lastScore = 0; bestScore = 0; xMoves = 0; rotations = 0; //Check each possible space and rotation for (int xCheck = -(gridWidth / 2); xCheck <= gridWidth / 2; xCheck++) { for (int rotCheck = 0; rotCheck <= 3; rotCheck++) { //Create a new GhostShape GhostShape ghostShape = new GhostShape(world, shape); //Calculate the score lastScore = ghostShape.CalculateScore(xCheck, rotCheck); //Remember the highest score, and that movement and rotation if (lastScore > bestScore) { bestScore = lastScore; xMoves = xCheck; rotations = rotCheck; } lastScore = 0; } } //Return the moves belonging to the best score var moves = Tuple.Create(xMoves, rotations); return moves; }
public Block(BlockType inType, Color color, World world, Color clearColor) { this.color = color; this.clearColor = clearColor; this.world = world; position = new int[4][]; CreateNew(inType, color); }
public GhostShape(World world, Shape shape) { this.world = world; this.shape = shape; this.grid = shape.Grid; gridCenter = new Vector2(grid.GetLength(0) - 1, grid.GetLength(1) - 1) / 2; location = new Point(world.Columns / 2 - 1, (int)gridCenter.Y); //If can't spawn. kill world if (!CanMove(new Point(0, 0), world, shape.Grid)) world.Kill(); }
public Game() { InitializeComponent(); // Applying the settings to the WinForm window ClientSize = new Size(colWidth * columns, rowHeight * rows); BackColor = backColor; timer.Interval = updateTime; timer.Start(); fastTimer.Start(); Color randomColor = Color.FromArgb(randomizer.Next(105) + 150, randomizer.Next(105) + 150, randomizer.Next(105) + 50); newWorld = new World(Controls, backColor, rows, columns, colWidth, rowHeight); block = new Block((BlockType)(randomizer.Next((int)BlockType.count)), CreateRandomBlockColor(), newWorld, backColor); }
public Shadow(World world, Shape shape) : base(world, ControlMode.None) { this.shape = shape; }
public Shape(World world, ControlMode controlMode) { //Select random shape switch (GameManager.Random.Next(0, 7)) { case 0: grid = IShape; Color = Color.Red; break; case 1: grid = LShape; Color = Color.Blue; break; case 2: grid = JShape; Color = Color.Yellow; break; case 3: grid = ZShape; Color = Color.Green; break; case 4: grid = SShape; Color = Color.Purple; break; case 5: grid = TShape; Color = Color.Orange; break; case 6: grid = OShape; Color = Color.White; break; } gridCenter = new Vector2(grid.GetLength(0) - 1, grid.GetLength(1) - 1) / 2; location = new Point(world.Columns / 2 - 1, (int)gridCenter.Y); this.controlMode = controlMode; this.world = world; //If can't spawn. kill world if (!CanMove(new Point(0, 0), grid)) { world.Kill(); } if (controlMode == ControlMode.Player) shadow = new Shadow(world, this); }
static void StartMP() { gameWorld = new List<World>(); //Set gamemode currentGameMode = GameMode.Multiplayer; //Load worlds GameWorld.Add(new World(new Rectangle(50, 70, 240, 400), 0, ControlMode.Player, false)); AIWorld = new World(new Rectangle(510, 70, 240, 400), 0, ControlMode.AI); GameWorld.Add(AIWorld); //Change gamestate currentGameState = GameState.Playing; }
//Rotate left void RotateLeft(World world) { Block[,] newGrid = new Block[grid.GetLength(0), grid.GetLength(1)]; for (int y = 0; y < grid.GetLength(1); y++) { for (int x = 0; x < grid.GetLength(0); x++) { newGrid[(int)(gridCenter.Y - y + gridCenter.X), (int)(x - gridCenter.X + gridCenter.Y)] = grid[x, y]; } } //Check if rotation is possible if (CanMove(Point.Zero, world, newGrid)) { grid = newGrid; } }
//Remove the shape from the world void RemoveFromWorld(World world) { for (int y = 0; y < grid.GetLength(0); y++) { for (int x = 0; x < grid.GetLength(1); x++) { if (grid[x, y] != null) { world.RemoveBlock(grid[x, y], GetWorldLocation(x, y)); } } } }
//Check if the shape can move bool CanMove(Point direction, World world, Block[,] grid) { for (int y = 0; y < grid.GetLength(1); y++) { for (int x = 0; x < grid.GetLength(0); x++) { if (grid[x, y] != null) { //This check if this block from the grid can move down on the world grid. Point worldLocation = GetWorldLocation(x, y); //Check for outside of screen if (worldLocation.Y + direction.Y >= world.Rows) { //Went out the bottom return false; } if (worldLocation.X + direction.X >= world.Columns) { //Went out the right return false; } if (worldLocation.X + direction.X < 0) { //Went out the left return false; } //Check collision with world blocks if (world.Grid[worldLocation.X + direction.X, worldLocation.Y + direction.Y] != null) { //There is a block here! return false; } } } } return true; }