/// <summary> /// Executes a given move /// </summary> /// <param name="piece">Index of piece</param> /// <param name="tile">Index of tile to</param> /// <param name="tileFrom">Index of tile from</param> private void DoMove(int piece, int tile, int tileFrom) { if (tileFrom >= 0) { if (engine.GetGameState() == KaroEngine.GameState.PLAYING) { Point location = PieceComponents[this.selectedPiece].OnTopofTile.Location; int from = location.X + (location.Y * BOARDWIDTH); Point location2 = TileComponents[this.selectedTile].Location; int fromTile = location2.X + (location2.Y * BOARDWIDTH); Point location3 = this.moveToList[tileFrom].Location; int to = location3.X + (location3.Y * BOARDWIDTH); bool result = engine.DoMove(from, to, fromTile); if (result) { this.ClearSelectedItems(); this.ShowMove(location, location3, location2); //start undo timer startUndoTimer = true; moveUndone = false; } } } if (tile >= 0) //If the move should be on a tile { if (engine.GetGameState() == KaroEngine.GameState.INSERTION) { if (this.selectedStartingPiece >= 0) { Point location2 = TileComponents[tile].Location; if (engine.InsertByXY(location2.X, location2.Y)) { this.ShowMove(location2, location2, location2); startUndoTimer = false; // TODO :: Build in undo move in insertion state (why not just restart the game in this early stage?) moveUndone = false; } } } else if (engine.GetGameState() == KaroEngine.GameState.PLAYING) { Point location2 = TileComponents[tile].Location; int to = location2.X + (location2.Y * BOARDWIDTH); if (engine.GetByXY(location2.X, location2.Y) == KaroEngine.Tile.MOVEABLETILE) { TileComponents[tile].IsSelected = true; this.selectedTile = tile; } if (this.selectedPiece > 0) { Point location = PieceComponents[this.selectedPiece].OnTopofTile.Location; int from = location.X + (location.Y * BOARDWIDTH); this.ClearSelectedItems(); if (engine.DoMove(from, to, -1)) { this.ShowMove(location, location2, new Point()); //start undo timer startUndoTimer = true; moveUndone = false; } } } } if (piece >= 0) { foreach (var entry in TileComponents) { entry.Value.IsPossibleMove = false; } // If the game is still running. if (engine.GetGameState() == KaroEngine.GameState.INSERTION || StartingPieces.Count != 0) { Player player = engine.GetTurn(); Vector3 red = Color.Tomato.ToVector3(); Vector3 white = Color.White.ToVector3(); if (StartingPieces[piece].Color.Equals(white) && player == Player.WHITE || StartingPieces[piece].Color.Equals(red) && player == Player.RED) { if (this.selectedStartingPiece >= 0) { StartingPieces[selectedStartingPiece].IsSelected = false; } StartingPieces[piece].IsSelected = true; this.selectedStartingPiece = piece; } startUndoTimer = false; // TODO :: Build in undo move in insertion state (why not just restart the game in this early stage?) moveUndone = false; } if (engine.GetGameState() == KaroEngine.GameState.PLAYING) { Player player = engine.GetTurn(); Vector3 red = Color.Tomato.ToVector3(); Vector3 white = Color.White.ToVector3(); if (PieceComponents[piece].Color.Equals(white) && player == Player.WHITE || PieceComponents[piece].Color.Equals(red) && player == Player.RED) { this.selectedPiece = piece; PieceComponents[piece].IsSelected = true; Point locTest = PieceComponents[this.selectedPiece].OnTopofTile.Location; if (this.selectedTile > 0) { Point location = PieceComponents[this.selectedPiece].OnTopofTile.Location; Point location2 = TileComponents[this.selectedTile].Location; int[][] possibleMoves = engine.GetPossibleMoves(location.X, location.Y, location2.X, location2.Y); moveToList.Clear(); foreach (int[] item in possibleMoves) { moveToList.Add(new Tile(this, tileModel, true, new Point(item[0], item[1]))); } } else { int[][] possibleMovePiece = engine.GetPossibleMoves(locTest.X, locTest.Y, -1, -1); foreach (int[] item in possibleMovePiece) { foreach (var entry in TileComponents) { if (entry.Value.Location.X == item[0] && entry.Value.Location.Y == item[1]) { entry.Value.IsPossibleMove = true; } } } } } } } }
private void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Font drawFont = new Font("Arial", 10); for (int y = 0; y < BOARDWIDTH; y++) { for (int x = 0; x < BOARDWIDTH; x++) { // Draw the board bool drawn = false; if (possibleMoves != null) { foreach (int[] move in possibleMoves) { if (move[0] == x && move[1] == y) { g.FillRectangle(Brushes.HotPink, x * boxSize, y * boxSize, boxSize, boxSize); drawn = true; } } } if (engine.GetByXY(x, y) != Tile.EMPTY) { if (!drawn) { g.FillRectangle(brushBlack, x * boxSize, y * boxSize, boxSize, boxSize); } } else { if (tileNumbersToolStripMenuItem.Checked) { g.DrawString(((y * BOARDWIDTH + x) + ""), drawFont, brushBlack, x * boxSize, y * boxSize); } } // Draw the 'selected' tiles if (x == clickedFirst.X && y == clickedFirst.Y) { g.FillRectangle(brushBlue, x * boxSize, y * boxSize, boxSize, boxSize); } if (x == clickedSecond.X && y == clickedSecond.Y) { g.FillRectangle(brushBlue, x * boxSize, y * boxSize, boxSize, boxSize); } if (x == clickedTile.X && y == clickedTile.Y) { g.FillRectangle(Brushes.BlueViolet, x * boxSize, y * boxSize, boxSize, boxSize); } // Check what kind of tiles, pawns etc are on the board. switch (engine.GetByXY(x, y)) { case Tile.BORDER: break; case Tile.EMPTY: break; case Tile.MOVEABLETILE: break; case Tile.SOLIDTILE: break; case Tile.REDUNMARKED: g.FillEllipse(brushRed, x * boxSize + 1, y * boxSize + 1, boxSize - 2, boxSize - 2); break; case Tile.REDMARKED: g.FillEllipse(brushRed, x * boxSize + 1, y * boxSize + 1, boxSize - 2, boxSize - 2); g.DrawEllipse(penGray, x * boxSize + 5, y * boxSize + 5, boxSize - 10, boxSize - 10); break; case Tile.WHITEUNMARKED: g.FillEllipse(brushWhite, x * boxSize + 1, y * boxSize + 1, boxSize - 2, boxSize - 2); break; case Tile.WHITEMARKED: g.FillEllipse(brushWhite, x * boxSize + 1, y * boxSize + 1, boxSize - 2, boxSize - 2); g.DrawEllipse(penGray, x * boxSize + 5, y * boxSize + 5, boxSize - 10, boxSize - 10); break; } // Draw a grid g.DrawRectangle(Pens.Gray, x * boxSize, y * boxSize, boxSize, boxSize); // If debug (draw movable tiles) is on, draw the movable tiles if (movableTilesToolStripMenuItem.Checked && engine.GetByXY(x, y) == Tile.MOVEABLETILE) { g.DrawRectangle(penGreen, x * boxSize, y * boxSize, boxSize, boxSize); } } } }
/// <summary> /// Starts a new game /// </summary> public void NewGame() { engine = new KaroEngineWrapper(); this.selectedPiece = 0; this.selectedTile = 0; this.selectedStartingPiece = -1; computerIsThinking = false; undoTimer = 0; moveUndone = false; startUndoTimer = false; didLastMove = false; // clear tiles foreach (var tile in TileComponents) { Components.Remove(tile.Value); } TileComponents.Clear(); // clear pieces foreach (var piece in PieceComponents) { Components.Remove(piece.Value); } PieceComponents.Clear(); // add the tiles for (int x = 0; x < BOARDWIDTH; x++) { for (int y = 0; y < BOARDWIDTH; y++) { KaroEngine.Tile tile = engine.GetByXY(x, y); if (tile != KaroEngine.Tile.BORDER && tile != KaroEngine.Tile.EMPTY) { Tile t = new Tile(this, tileModel, false, new Point(x, y)); this.TileComponents.Add(y * BOARDWIDTH + x, t); Components.Add(t); } } } StartingPieces.Clear(); //White pawns for (int i = 0; i < 3; i += 1) { Tile t = new Tile(this, tileModel, false, new Point(i + 7, 3)); t.TileMatrix *= Matrix.CreateTranslation(0f, -1f, 0f); Piece p = new Piece(this, pieceModel, true, t, Color.White.ToVector3()); this.StartingPieces.Add(p); this.Components.Add(p); t = new Tile(this, tileModel, false, new Point(i + 7, 4)); t.TileMatrix *= Matrix.CreateTranslation(0f, -1f, 0f); p = new Piece(this, pieceModel, true, t, Color.White.ToVector3()); this.StartingPieces.Add(p); this.Components.Add(p); } //Red pawns for (int i = 0; i < 3; i += 1) { Tile t = new Tile(this, tileModel, false, new Point(i + 7, 9)); t.TileMatrix *= Matrix.CreateTranslation(0f, -1f, 0f); Piece p = new Piece(this, pieceModel, true, t, Color.Tomato.ToVector3()); this.StartingPieces.Add(p); this.Components.Add(p); t = new Tile(this, tileModel, false, new Point(i + 7, 10)); t.TileMatrix *= Matrix.CreateTranslation(0f, -1f, 0f); p = new Piece(this, pieceModel, true, t, Color.Tomato.ToVector3()); this.StartingPieces.Add(p); this.Components.Add(p); } }