Пример #1
0
 /// <summary>
 /// Constructs a new Node
 /// </summary>
 /// <param name="b">The board that the Node will use to evaluate itself and generate its children</param>
 /// <param name="parent">The parent of this node</param>
 /// <param name="move">The move from the parent's board that generated this node's board</param>
 public Node(Board b, Node parent, TicTacToeMove move)
 {
     this.board = b;
     this.parent = parent;
     this.move = move;
     if (parent != null)
         myPiece = Board.GetOponentPiece(parent.MyPiece);
     children = new List<Node>();
 }
Пример #2
0
 public Node()
 {
     board = new char[3, 3];
     for (int i = 0; i < 3; i++)
     {
         for(int j = 0; j < 3; j++)
         {
             board[i, j] = '-';
         }
     }
     parent = null;
 }
Пример #3
0
      /// <summary>
      /// Begin the game and pass control to the first player
      /// </summary>
      public void Start()
      {
         if (_isInitialized)
         {
            throw new StartGameException("Cannot start game because game has already started.");
         }

         Node<Player> prevPlayerNode = null;
         Node<Player> lastPlayerNode = null;

         // Generic way of instantiating active player node (supports more than 2 players)
         for (int i = _players.Count - 1; i >= 0; i--)
         {
            Node<Player> curPlayerNode = new Node<Player>(_players[i], prevPlayerNode);

            if (lastPlayerNode == null)
            {
               lastPlayerNode = curPlayerNode;
            }
            if (i == 0)
            {
               _activePlayerNode = curPlayerNode;
            }

            prevPlayerNode = curPlayerNode;
         }

         lastPlayerNode.Next = _activePlayerNode;

         _isInitialized = true;
         OnGameUpdate();

         ActivePlayer.PromptForMove(this);
      }
Пример #4
0
 protected void AdvanceTurn()
 {
    _activePlayerNode = _activePlayerNode.Next;
 }
Пример #5
0
        // Paint the panel (Draw the board).
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            base.OnPaint(e);
            Pen blackpen = new Pen(Color.Black, 3);
            Graphics g = e.Graphics;
            int offset = 40;
            g.DrawLine(blackpen, new Point(top_left.X, top_left.Y - offset), new Point(bottom_left.X, bottom_left.Y + offset));
            g.DrawLine(blackpen, new Point(top_right.X, top_right.Y - offset), new Point(bottom_right.X, bottom_right.Y + offset));
            g.DrawLine(blackpen, new Point(top_left.X - offset, top_left.Y), new Point(top_right.X + offset, top_right.Y));
            g.DrawLine(blackpen, new Point(bottom_left.X - offset, bottom_left.Y), new Point(bottom_right.X + offset, bottom_right.Y));

            drawBoard(node.board, g);

            // If the user just won, ask if they want to play again.
            string text = "";
            if (node.justWon() || node.justWon(false) || node.gameOver())
            {
                if (node.justWon() || node.justWon(false))
                {
                    string winner = "o";
                    if (node.justWon() && node.justMovedChar() == 'x')
                        winner = "x";
                    text = winner + " Wins!";
                }
                else
                    text = "It was a draw";

                MessageBoxButtons button = MessageBoxButtons.YesNo;
                DialogResult result = MessageBox.Show(text + "\n Play again?", "Game has ended", button);
                if (result == DialogResult.Yes)
                    node = new Node();
                else
                    Application.Exit();
                panel1.Invalidate();
            }

            g.Dispose();
        }
Пример #6
0
        // This function handles the mouse click, calls the function to get the computer's move, then updates the board.
        private void panel1_MouseClick(Object sender, MouseEventArgs e)
        {
            var selection = getBox(new Point(e.X, e.Y));
            if (validateMove(selection, node.board))
                node = node.playerMove(selection.Item1, selection.Item2);
            numNodesLabel.Text = "" + Node.nodesVisited;
            this.numNodesLabel.Invalidate();

            this.panel1.Invalidate();
        }
Пример #7
0
 /// <summary>
 /// Constructs a MIN node
 /// </summary>
 /// <param name="b">The board that this node represents</param>
 /// <param name="parent">This node's parent</param>
 /// <param name="m">The move that was made from the parent to lead to this node's board</param>
 public MinNode(Board b, Node parent, TicTacToeMove m)
     :base(b, parent, m)
 {
     
 }
Пример #8
0
        /// <summary>
        /// Selects the best move node for the node
        /// </summary>
        public void SelectBestMove()
        {
            // if no children there is no best move for the node
            if (children.Count == 0)
            {
                bestMoveNode = null;
                return;
            }


            // sort the children so that the first element contains the 'best' node
            List<Node> sortedChildren = SortChildren(this.children);

            this.bestMoveNode = sortedChildren[0];
            Value = bestMoveNode.Value;
        }