⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Arg BestFirstSearch with game demo you can play .
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NIM
{
    public partial class Form1 : Form
    {
        private TNode firstNode;
        private TNode currentNode;
        private string nexPlayer = "";
        private int DEPTH = -1;
        private const int X = 0;
        private const int Y = 50;
        private const int WIDTH = 5;
        private const int HEIGHT = 100;
        private bool flag = false;
        private int choose = -1;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            firstNode = new TNode();
            firstNode.Level = 1;
            //newGame();
            DEPTH = 10;
            lblBestPick.Text = "Bạn hãy chọn số que muốn chơi \nbằng cách click vào New Game";
            lblStickRemain.Text = "";
            lblWin.Text = "";
        }

        private void newGame()
        {            
            lblStickRemain.Text = "";
            lblWin.Text = "";
            lblBestPick.Text = "Bạn hãy chọn số que muốn chơi \nbằng cách click vào New Game";            
            nexPlayer = "1";            
            InputBox input = new InputBox();            
            try
            {
                input.setMessage("Bạn hãy nhập vào số que để chơi!!!");
                input.ShowDialog();
                if (input.value > 0 && input.value <= 20)
                    firstNode.Sticks = input.value;
                else
                    MessageBox.Show("Chỉ nhập từ 1->20 que!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex) {}
            input.Dispose();
            this.Width += 1;

            currentNode = new TNode();
            currentNode.assign(firstNode);
            flag = true;
        }                

        //minimax algorithm
        private int search(TNode b, int depth)
        {
            TNode c = null;
            int value, temp, i;
            if (depth == DEPTH && DEPTH != -1)
            {
                if ((b.Level % 2) == 1) //nguoi choi thu 1
                    return +1;//tot
                else //nguoi choi thu 2
                    return -1;//toi
            }
            else                                                                                                              
            {
                if (b.Sticks == 0)
                {
                    if ((b.Level % 2) == 1) //nguoi choi thu 1
                        return +1;//tot
                    else //nguoi choi thu 2
                        return -1;//toi
                }
                else
                {
                    //chua phai que cuoi cung
                    if ((b.Level % 2) == 1)
                    {
                        //nguoi choi thu 1
                        value = Int16.MinValue;
                    }
                    else
                    {
                        //nguoi choi thu 2
                        value = Int16.MaxValue;
                    }
                    c = new TNode();
                    c.Level = b.Level + 1;
                    for (i = 1; i <= 3; i++)
                    {
                        c.Sticks = b.Sticks - i;//boc tu 1 den 3 que
                        if (c.Sticks >= 0)
                        {
                            //neu so que van con boc duoc
                            if ((b.Level % 2) == 1)
                            {
                                //nguoi choi thu 1
                                //tim gia tri lon nhat de tao uu the cho minh
                                temp = search(c, depth + 1);
                                if (temp > value)
                                {
                                    value = temp;
                                    b.HightChildIndex = i;
                                }
                            }
                            else
                            {
                                //nguoi choi thu 2
                                //tim gia tri nho nhat de giam uu the cua doi phuong
                                temp = search(c, depth + 1);
                                if (temp < value)
                                {
                                    value = temp;
                                    b.HightChildIndex = i;
                                }
                            }
                        }
                    }
                }
                return value;
            }
        }        

        private void btnTake_Click(object sender, EventArgs e)
        {
            if (currentNode != null)
            {
                if (currentNode.Sticks >= choose)
                {
                    currentNode.Level = currentNode.Level + 1;
                    currentNode.Sticks = currentNode.Sticks - choose;
                    if (currentNode.Sticks == 0)
                    {
                        if (nexPlayer.Equals("2"))
                        {
                            MessageBox.Show("Chúc mừng bạn đã thắng!!!\nClick NewGame để chơi tiếp\nExit để thoát");
                            lblWin.Text = "Bạn đã thắng!!!";
                        }
                        else
                        {
                            MessageBox.Show("Bạn đã thua!!!\nClick NewGame để chơi tiếp\nExit để thoát");
                            lblWin.Text = "Máy đã thắng!!!";
                        }
                    }
                    else
                    {
                        if (nexPlayer.Equals("1")) nexPlayer = "2";
                        else nexPlayer = "1";
                    }
                    //may di
                    int bestIndex = -1;                    
                    search(currentNode, 0);
                    bestIndex = currentNode.HightChildIndex;
                    currentNode.Sticks = currentNode.Sticks - bestIndex;                    
                    lblBestPick.Text = "Bạn chọn " + choose + " que , máy chọn " + bestIndex + " que";
                    lblStickRemain.Text = "Số que còn lại là " + currentNode.Sticks;
                    if (currentNode.Sticks == 0)
                    {
                        if (nexPlayer.Equals("2"))
                        {
                            MessageBox.Show("Chúc mừng bạn đã thắng!!!\nClick NewGame để chơi tiếp\nExit để thoát");
                            lblWin.Text = "Bạn đã thắng!!!";
                        }
                        else
                        {
                            MessageBox.Show("Bạn đã thua!!!\nClick NewGame để chơi tiếp\nExit để thoát");
                            lblWin.Text = "Máy đã thắng!!!";
                        }
                    }
                    else
                    {
                        if (nexPlayer.Equals("1")) nexPlayer = "2";
                        else nexPlayer = "1";
                    }
                    Invalidate();
                }
                else
                {
                    MessageBox.Show("Bạn nên chọn số que <= " + currentNode.Sticks + "!!!");
                }
            }
            else
            {
                MessageBox.Show("Bạn phải chọn New Game để bắt đầu!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }       

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            newGame();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics grpx = e.Graphics;
            Brush b = new SolidBrush(Color.Green);
            if (flag == true)
            {                               
                drawSticks(grpx, b);                
            }
            grpx.DrawString("Trò chơi NIM - version 1.0", new Font("Tahoma", 15), b, 5, 180);
        }

        private void drawSticks(Graphics grpx, Brush b)
        {
            if (currentNode != null)
            {
                if (currentNode.Sticks > 0)
                {
                    for (int i = 0; i < currentNode.Sticks; i++)
                    {
                        if (i % 2 == 0)
                        {
                            b = new SolidBrush(Color.Blue);
                        }
                        else
                        {
                            b = new SolidBrush(Color.Red);
                        }
                        grpx.FillRectangle(b, X + ((i) * 12) + 5, Y, WIDTH, HEIGHT);
                        grpx.DrawString("" + (i + 1), Font, b, X + ((i) * 12) + 2, Y + HEIGHT + 5);
                    }
                }
            }
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Invalidate();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {            
            choose = 1;           
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {            
            choose = 2;         
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {            
            choose = 3;         
        }

        private void chooseDepthSearchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InputBox input = new InputBox();
            try
            {
                input.setMessage("Nhập vào độ sâu tìm kiếm!!!");
                input.ShowDialog();
                DEPTH = input.value;
            }
            catch (Exception ex) { }
            input.Dispose();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Trường Đại học Công Nghiệp Tp.HCM\n\tBài tập AI\nTrò chơi NIM Version 1.0\nNgười viết: Chung Quang Thắng\nMã số sinh viên: 0700412\nGiáo viên hướng dẫn: Thầy Trần Đắc Phiến", "About", MessageBoxButtons.OK);
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -