コード例 #1
0
        public VisualNode AddMove(int x, int y, TicTacToeValue currentPlayer)
        {
            string nodeKey = Constants.GetKey(x, y);

            if (nodes.ContainsKey(nodeKey))
            {
                return(null);
            }

            VisualNode newNode = new VisualNode(x, y, currentPlayer);

            nodes.Add(nodeKey, newNode);

            return(newNode);
        }
コード例 #2
0
        private void TraverseBoard(VisualNode node, NodeLocation direction, Action <VisualNode> runAction)
        {
            // flag all the nodes on the winDirection
            var            currentNode = nodes[Constants.GetKey(node.X, node.Y)];
            TicTacToeValue nodeValue   = node.Value;

            // traverse all nodes of the same value
            while (currentNode.Value == nodeValue)
            {
                runAction(currentNode);
                string nodeKey = Constants.MapDirectionToComputation[direction](currentNode.X, currentNode.Y);

                // continue on the direction, if there is anything there.
                if (nodes.ContainsKey(nodeKey))
                {
                    currentNode = nodes[nodeKey];
                }
                else
                {
                    break;
                }
            }
        }
コード例 #3
0
ファイル: GameSurface.cs プロジェクト: AlexGhiondea/TicTacToe
        private bool PlaceMove(VisualNode move)
        {
            if (_game.GameEnded)
            {
                return false;
            }

            if (move == null)
            {
                MessageBox.Show("Cannot place the move there!");
                return false;
            }

            s_moves.Push(move);

            tslMoveCount.Text = $"Move: {s_moves.Count}";

            Refresh();

            NodeLocation winDirection;
            if (_game.IsWinningMove(move, out winDirection))
            {
                _game.GameEnded = true;
                // Mark the wining nodes.
                _game.MarkWinningNodes(move, winDirection);
                Refresh();

#if !COMPUTER_AGAINST_ITSELF
                MessageBox.Show($"Winner is player {CurrentPlayer}!!!");
#else

                if (CurrentPlayer == TicTacToeValue.x)
                    winX++;
                else
                    winO++;

                if (s_moves.Count > maxMove)
                    maxMove = s_moves.Count;
                if (s_moves.Count < minMove)
                    minMove = s_moves.Count;

                label3.Text = $"X: {winX}, O: {winO}, min:{minMove}, max:{maxMove}";
#endif

                _game = new TicTacToeGame(NeededForWin);
                s_moves.Clear();
                CurrentPlayer = TicTacToeValue.x;
                tslMoveCount.Text = "";
                Refresh();

#if COMPUTER_AGAINST_ITSELF
                PlaceAIMove();
#endif
                return true;
            }
            else
            {
                ChangePlayer();

#if COMPUTER_AGAINST_ITSELF
                PlaceAIMove();
#endif
            }

            Application.DoEvents();
            return false;
        }
コード例 #4
0
 internal void MarkWinningNodes(VisualNode node, NodeLocation winDirection)
 {
     // flag all the nodes on that direction so that when they are drawn, you can tell who won.
     TraverseBoard(node, winDirection, (n) => n.PartOfWinningMove = true);
     TraverseBoard(node, winDirection.GetReverseDirection(), (n) => n.PartOfWinningMove = true);
 }