예제 #1
0
파일: Reversi.cs 프로젝트: Joseph24/AI
        // ===================================================================
        // Event handlers for the square controls.
        // ===================================================================
        //
        // Handles a mouse move on a board square.
        //
        private void SquareControl_MouseMove(object sender, MouseEventArgs e)
        {
            // Check the game state to ensure that it is the user's turn.
            if (this.gameState != ReversiForm.GameState.InPlayerMove)
                return;

            SquareControl squareControl = (SquareControl) sender;

            // If the square is a valid move for the current player,
            // indicate it.
            if (this.board.IsValidMove(this.currentColor, squareControl.Row, squareControl.Col))
            {
                //
                if (!squareControl.IsActive && squareControl.PreviewContents == Board.Empty)
                {
                    // If the show valid moves option is active, mark the
                    // square.
                    if (this.options.ShowValidMoves)
                    {
                        squareControl.IsActive = true;

                        // If the preview moves option is not active, update
                        // the square display now.
                        if (!this.options.PreviewMoves)
                            squareControl.Refresh();
                    }

                    // If the preview moves option is active, mark the
                    // appropriate squares.
                    if (this.options.PreviewMoves)
                    {
                        // Create a temporary board to make the move on.
                        Board board = new Board(this.board);
                        board.MakeMove(this.currentColor, squareControl.Row, squareControl.Col);

                        // Set up the move preview.
                        for (int i = 0; i < 8; i++)
                            for (int j = 0; j < 8; j++)
                                if (board.GetSquareContents(i, j) != this.board.GetSquareContents(i, j))
                                {
                                    // Set and update the square display.
                                    this.squareControls[i, j].PreviewContents = board.GetSquareContents(i, j);
                                    this.squareControls[i, j].Refresh();
                                }
                    }
                }

                // Change the cursor.
                squareControl.Cursor = System.Windows.Forms.Cursors.Hand;
            }
        }
예제 #2
0
        private static void Main(string[] args)
        {
            Board b = new Board();

            while (true)
            {
                bool playing = true;
                b.SetForNewGame();
                bool isWhite = true;
                while (playing)
                {
                    isWhite = !isWhite;
                    Board.Color player = isWhite ? Board.Color.White : Board.Color.Black;
                    Console.Clear();
                    Console.Write(" ");
                    for (int i = 0; i < 8; i++)
                    {
                        Console.Write($" {i + 1}");
                    }
                    Console.Write(" X");
                    Console.WriteLine();
                    for (int y = 0; y < 8; y++)
                    {
                        for (int x = 0; x < 8; x++)
                        {
                            Console.Write(x == 0 ? $"{y + 1} " : " ");
                            Console.ForegroundColor =
                                b.IsValidMove(player, y, x) ? ConsoleColor.Green : ConsoleColor.Red;
                            Console.Write(GetChar(b.GetSquareContents(y, x)));
                        }
                        Console.ResetColor();
                        Console.WriteLine();
                    }
                    Console.WriteLine("Y");
                    if (!b.HasAnyValidMove(player))
                    {
                        if (!b.HasAnyValidMove(Board.Invert(player)))
                        {
                            Console.WriteLine(b.WhiteCount == b.BlackCount
                                ? "Tie"
                                : $"{(b.WhiteCount > b.BlackCount ? "White" : "Black")} won");
                            Console.ReadKey();
                            playing = false;
                        }
                        continue;
                    }
                    Console.WriteLine($"Current player: {(isWhite ? "White (+)" : "Black (-)")}");
                    Console.WriteLine($"{b.GetValidMoveCount(player)} moves possible");
                    int  nX;
                    int  nY;
                    bool first = true;
                    do
                    {
                        if (!first)
                        {
                            Console.WriteLine("Invalid move");
                        }
                        first = false;
                        Console.Write("x> ");
                        nX = int.Parse(Console.ReadKey().KeyChar.ToString()) - 1;
                        Console.WriteLine();
                        Console.Write("y> ");
                        nY = int.Parse(Console.ReadKey().KeyChar.ToString()) - 1;
                        Console.WriteLine();
                    } while (!b.IsValidMove(player, nY, nX));
                    b.MakeMove(player, nY, nX);
                }
            }
        }
예제 #3
0
파일: Reversi.cs 프로젝트: Joseph24/AI
        //
        // Makes a move for the current player.
        //
        private void MakeMove(int row, int col)
        {
            // Clean up the move history to ensure that it contains only the
            // moves made prior to this one.
            while (this.moveHistory.Count > this.moveNumber - 1)
                this.moveHistory.RemoveAt(this.moveHistory.Count - 1);

            // Add the move to the move list.
            string color = "Black";
            if (this.currentColor == Board.White)
                color = "White";
            string[] subItems =
            {
                String.Empty,
                this.moveNumber.ToString(),
                color,
                (alpha[col] + (row + 1).ToString())
            };
            ListViewItem listItem = new ListViewItem(subItems);
            this.moveListView.Items.Add(listItem);

            // If necessary, scroll the list to bring the last move into view.
            this.moveListView.EnsureVisible(this.moveListView.Items.Count - 1);

            // Add this move to the move history.
            this.moveHistory.Add(new MoveRecord(this.board, this.currentColor, listItem));

            // Enable/disable the move-related menu items and tool bar buttons as
            // appropriate.
            this.undoMoveMenuItem.Enabled =
                this.undoAllMovesMenuItem.Enabled =
                this.playToolBar.Buttons[(int) ReversiForm.ToolBarButton.UndoMove].Enabled =
                this.playToolBar.Buttons[(int) ReversiForm.ToolBarButton.UndoAllMoves].Enabled = true;
            this.redoMoveMenuItem.Enabled =
                this.redoAllMovesMenuItem.Enabled =
                this.playToolBar.Buttons[(int) ReversiForm.ToolBarButton.RedoMove].Enabled =
                this.playToolBar.Buttons[(int) ReversiForm.ToolBarButton.RedoAllMoves].Enabled = false;

            // Bump the move number.
            this.moveNumber++;

            // Update the status display.
            this.statusLabel.Text = "";
            this.statusProgressBar.Visible = false;
            this.statusPanel.Refresh();

            // Clear any board square highlighting.
            this.UnHighlightSquares();

            // Make a copy of the board (for doing move animation).
            Board oldBoard = new Board(this.board);

            // Make the move on the board.
            this.board.MakeMove(this.currentColor, row, col);

            // If the animate move option is active, set up animation for the
            // affected discs.
            if (this.options.AnimateMoves)
            {
                int i, j;
                for (i = 0; i < 8; i++)
                    for (j = 0; j < 8; j++)
                    {
                        // Mark the newly added disc.
                        if (i == row && j == col)
                            this.squareControls[i, j].IsNew = true;
                        else
                        {
                            // Initialize animation for the discs that were
                            // flipped.
                            if (this.board.GetSquareContents(i, j) != oldBoard.GetSquareContents(i, j))
                                this.squareControls[i, j].AnimationCounter = SquareControl.AnimationStart;
                        }
                    }
            }

            // Update the display to reflect the board changes.
            this.UpdateBoardDisplay();

            // Save the player color.
            this.lastMoveColor = this.currentColor;

            // If the animate moves option is active, start the animation.
            if (this.options.AnimateMoves)
            {
                this.gameState = ReversiForm.GameState.InMoveAnimation;
                this.animationTimer.Start();
            }

            // Otherwise, end the move.
            else
                this.EndMove();
        }