我的猜數字程式AI遊戲程式碼

  • 13649
  • 0
  • C#
  • 2009-11-22

摘要:我的猜數字程式AI遊戲程式碼

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MANB
{
    public partial class Form1 : Form
    {
        public List<NumberList> list = new List<NumberList>();

        public class NumberList
        {
            public int[] Number = new int[3];

            public NumberList()
            {
                Number[0] = 0;
                Number[1] = 0;
                Number[2] = 0;
            }

            public NumberList(int num1, int num2, int num3)
            {
                Number[0] = num1;
                Number[1] = num2;
                Number[2] = num3;
            }

            public void setNumber(int number,int d)
            {
                Number[d] = number;
            }

            public int getNumber(int d)
            {
               return  Number[d];
            }

            public string NumberString()
            {
                return String.Format("{0}{1}{2}", Number[0], Number[1], Number[2]);
            }

            public bool  NumberCampre(NumberList NL ,int A ,int  B)
            {
                int intA, intB;
                intA = 0; intB = 0;

                for (int i = 0; i < 3; i++)
                    for (int j = 0; j < 3; j++)
                        if (NL.getNumber(i)== this.getNumber(j))
                            if (i == j)
                                intA++;
                            else
                                intB++;
                if (intA == A && intB == B)
                    return true;
                else
                    return false;
            }

        }

        public Form1()
        {
            InitializeComponent();
            Submit.Enabled = false;
        }

        private void Start_Click(object sender, EventArgs e)
        {
            list.Clear();
            int[] numflag = new int[10];

            for (int i = 0; i < 10; i++)
                numflag[i] = 0;

            SetNumberList(numflag,0,0,3);

            NumberList NL = list[0];
            L1.Text = NL.getNumber(0).ToString();
            L2.Text = NL.getNumber(1).ToString();
            L3.Text = NL.getNumber(2).ToString();

            listBox1.Items.Clear();

            Start.Enabled = false;
            Submit.Enabled = true;
        }

        public void SetNumberList(int[] numflag,int num, int level,int level2)
        {
          

            if (level == 0)/* start */
            {
                level++;
                for (int i = 0; i < 10; i++)
                {
                    SetNumberList(numflag, i, level,level2);
                }
            }
            else if (level == level2)
            {
                numflag[num] = level;

                PushNumberList(numflag);

                numflag[num] = 0;

            }else{
                numflag[num] = level;
                level++;
                for (int i = 0; i < 10; i++)
                {
                    if (numflag[i] == 0)
                        SetNumberList(numflag, i, level,level2);
                }
                numflag[num] = 0;
            }
        }

        public void PushNumberList(int[] numflag)
        {

            NumberList NL = new NumberList();
            for (int i = 0; i < 10; i++)
            {
                if (numflag[i] != 0)
                    NL.setNumber(i, numflag[i] - 1);
            }

            list.Add(NL);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Submit_Click(object sender, EventArgs e)
        {
            int intA,intB;
            if (T1.Text == "") T1.Text = "0";
            if (T2.Text == "") T2.Text = "0"; 
            intA = int.Parse(T1.Text);
            intB = int.Parse(T2.Text);

            List<NumberList> list2 = new List<NumberList>();

            NumberList objNL = new NumberList(int.Parse(L1.Text), int.Parse(L2.Text), int.Parse(L3.Text));
            listBox1.Items.Add(objNL.NumberString() + "  " + intA.ToString() + "A" + intB.ToString() + "B");

            if (intA == 3)
            {
                listBox1.Items.Add("Game Over");
                Start.Enabled = true;
                Submit.Enabled = false;
                return;
            }
            
            foreach (NumberList NL in list)
            {
                if (objNL.NumberCampre(NL, intA, intB))
                    list2.Add(NL);
            }

            list.Clear();

            list = list2;

            if (list.Count == 0)
            {
                listBox1.Items.Add("Game Over Your Number is not exist");
                Start.Enabled = true;
                Submit.Enabled = false;
                return ;
            }


            NumberList objNewNL = list[0];
            L1.Text = objNewNL.getNumber(0).ToString();
            L2.Text = objNewNL.getNumber(1).ToString();
            L3.Text = objNewNL.getNumber(2).ToString();
        }
    }
}