Exemplo n.º 1
0
        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (!this.gameOver)
            {
                if (clickedFirst.X == -1)
                {
                    if (engine.GetGameState() == GameState.INSERTION)
                    {
                        engine.InsertByXY((e.X - 1) / this.boxSize, (e.Y - 1) / this.boxSize);
                    }
                    else if (engine.GetGameState() == GameState.PLAYING)
                    {
                        Tile tempTile = engine.GetByXY((e.X - 1) / this.boxSize, (e.Y - 1) / this.boxSize);
                        if (tempTile == Tile.MOVEABLETILE)
                        {
                            if (this.clickedTile.X == -1)
                            {
                                clickedTile.X = (e.X - 1) / this.boxSize;
                                clickedTile.Y = (e.Y - 1) / this.boxSize;
                            }
                        }
                        else
                        {
                            if (tempTile != Tile.EMPTY && tempTile != Tile.SOLIDTILE)
                            {
                                clickedFirst.X = (e.X - 1) / this.boxSize;
                                clickedFirst.Y = (e.Y - 1) / this.boxSize;
                                possibleMoves  = engine.GetPossibleMoves(clickedFirst.X, clickedFirst.Y, clickedTile.X, clickedTile.Y);
                            }
                        }
                    }
                }
                else if (clickedSecond.X == -1)
                {
                    clickedSecond.X = (e.X - 1) / this.boxSize;
                    clickedSecond.Y = (e.Y - 1) / this.boxSize;

                    if (clickedTile.X == -1)
                    {
                        engine.DoMove((clickedFirst.Y * BOARDWIDTH) + clickedFirst.X, (clickedSecond.Y * BOARDWIDTH) + clickedSecond.X, -1);
                    }
                    else
                    {
                        engine.DoMove((clickedFirst.Y * BOARDWIDTH) + clickedFirst.X, (clickedSecond.Y * BOARDWIDTH) + clickedSecond.X, (clickedTile.Y * BOARDWIDTH) + clickedTile.X);
                    }

                    clickedTile   = new Point(-1, -1);
                    clickedFirst  = new Point(-1, -1);
                    clickedSecond = new Point(-1, -1);
                    possibleMoves = null;
                }
                else
                {
                    clickedFirst  = new Point(-1, -1);
                    clickedSecond = new Point(-1, -1);
                }
            }

            UpdateGUI();
        }
Exemplo n.º 2
0
        /// <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;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }