Пример #1
0
        public void Load(string path)
        {
            if (File.Exists(path))
            {
                FileStream   f  = new FileStream(path, FileMode.Open);
                StreamReader sr = new StreamReader(f);

                Reset();

                for (int i = 0; i < BoardSize; i++)
                {
                    string Dong = sr.ReadLine();
                    for (int j = 0; j < BoardSize; j++)
                    {
                        char c = Dong[j];
                        Cells[i, j] = (OCo.CellValues)(Convert.ToInt32(c) - 48);

                        if (Cells[i, j] != OCo.CellValues.None)
                        {
                            OCo oCo = new OCo()
                            {
                                Row = i, Col = j, OfPlayer = Cells[i, j]
                            };
                            AI.CacNuocDaDi.Add(oCo);
                            OnLoad(oCo);
                        }
                    }
                }

                ActivePlayer = (OCo.CellValues)(Convert.ToInt32(sr.ReadLine()[0]) - 48);
                sr.Close();
                f.Close();
            }
        }
Пример #2
0
        public bool Redo()
        {
            if (CheDoChoi == Models.CheDoChoi.Computer)
            {
                if (AI.CacNuocUndo.Count < 2)
                {
                    return(false);
                }

                OCo oCo = AI.CacNuocUndo[AI.CacNuocUndo.Count - 1];
                AI.CacNuocDaDi.Add(oCo);
                Cells[oCo.Row, oCo.Col] = oCo.OfPlayer;
                OnRedo(oCo);
                AI.CacNuocUndo.Remove(oCo);
                ChangePlayer();
            }

            if (AI.CacNuocUndo.Count < 1)
            {
                return(false);
            }

            OCo oCo1 = AI.CacNuocUndo[AI.CacNuocUndo.Count - 1];

            AI.CacNuocDaDi.Add(oCo1);
            Cells[oCo1.Row, oCo1.Col] = oCo1.OfPlayer;
            OnRedo(oCo1);
            AI.CacNuocUndo.Remove(oCo1);
            ChangePlayer();

            return(true);
        }
Пример #3
0
        /// <summary>
        /// Khởi động tìm ô đánh tối ưu nhất và trả về
        /// </summary>
        public OCo KhoiDongComputer()
        {
            OCo OCoSeDanh = new OCo();

            // Nước đánh đầu tiên
            if (CacNuocDaDi.Count == 0)
            {
                OCoSeDanh.Row = BoardSize / 2;
                OCoSeDanh.Col = BoardSize / 2;
            }
            else
            {
                //Reset
                for (int i = 0; i < maxMove; i++)
                {
                    WinMove[i]   = new OCo();
                    PCMove[i]    = new OCo();
                    HumanMove[i] = new OCo();
                }

                depth = 0;

                // Tìm nước đi
                TimKiemNuocDi();

                // Kiểm tra và lưu lại nước đi tối ưu vào OCoSeDanh
                if (fWin)   // nước đi => chiến thắng
                {
                    OCoSeDanh.Row      = WinMove[0].Row;
                    OCoSeDanh.Col      = WinMove[0].Col;
                    OCoSeDanh.OfPlayer = WinMove[0].OfPlayer;
                }
                else        // chưa thể => chiến thắng
                {
                    // Duyệt và lưu điểm cho các ô cờ trong BangDiem
                    EvalChessBoard(OCo.CellValues.Player2, ref BangDiem);
                    OCo temp = new OCo();

                    // Lấy ô cờ có điểm cao nhất
                    temp               = BangDiem.MaxPos();
                    OCoSeDanh.Row      = temp.Row;
                    OCoSeDanh.Col      = temp.Col;
                    OCoSeDanh.OfPlayer = temp.OfPlayer;
                }
            }

            return(OCoSeDanh);
        }
Пример #4
0
        public OCo MaxPos()
        {
            long max = 0;

            OCo oco = new OCo();

            for (int i = 0; i < soDong; i++)
            {
                for (int j = 0; j < soCot; j++)
                {
                    if (Diem[i, j] > max)
                    {
                        oco.Row = i;
                        oco.Col = j;
                        max     = Diem[i, j];
                    }
                }
            }

            return(oco);
        }
Пример #5
0
        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            OCo OCo = (OCo)e.Result;

            BanCo.PlayAt(OCo.Row, OCo.Col);
        }
Пример #6
0
        private void TimKiemNuocDi()
        {
            if (depth > maxDepth)
            {
                return;
            }

            depth++;

            fWin = false;
            bool fLose = false;

            // Reset
            OCo pcMove    = new OCo();
            OCo humanMove = new OCo();

            int countMove = 0;

            // Duyệt các ô cờ, tính và lưu điểm
            EvalChessBoard(OCo.CellValues.Player2, ref BangDiem);

            // Lấy ra điểm maxMove cao nhất
            OCo temp = new OCo();

            for (int i = 0; i < maxMove; i++)
            {
                temp      = BangDiem.MaxPos();
                PCMove[i] = temp;
                BangDiem.Diem[temp.Row, temp.Col] = 0;
            }

            // Lấy nước đi trong PCMove[] ra đánh thử
            countMove = 0;
            while (countMove < maxMove)
            {
                pcMove = PCMove[countMove++];
                BanCo.Cells[pcMove.Row, pcMove.Col] = OCo.CellValues.Player2;
                WinMove.SetValue(pcMove, depth - 1);

                //Tim cac nuoc di toi uu cua nguoi
                BangDiem.ResetDiem();
                EvalChessBoard(OCo.CellValues.Player1, ref BangDiem);
                //Lay ra maxMove nuoc di co diem cao nhat cua nguoi
                for (int i = 0; i < maxMove; i++)
                {
                    temp         = BangDiem.MaxPos();
                    HumanMove[i] = temp;
                    BangDiem.Diem[temp.Row, temp.Col] = 0;
                }

                //Danh thu cac nuoc di
                for (int i = 0; i < maxMove; i++)
                {
                    humanMove = HumanMove[i];
                    BanCo.Cells[humanMove.Row, humanMove.Col] = OCo.CellValues.Player1;

                    if (fLose)
                    {
                        BanCo.Cells[pcMove.Row, pcMove.Col]       = OCo.CellValues.None;
                        BanCo.Cells[humanMove.Row, humanMove.Col] = OCo.CellValues.None;
                        break;
                    }

                    if (fWin)
                    {
                        BanCo.Cells[pcMove.Row, pcMove.Col]       = 0;
                        BanCo.Cells[humanMove.Row, humanMove.Col] = 0;
                        return;
                    }

                    TimKiemNuocDi();
                    BanCo.Cells[humanMove.Row, humanMove.Col] = OCo.CellValues.None;
                }
                BanCo.Cells[pcMove.Row, pcMove.Col] = OCo.CellValues.None;
            }
        }