Ejemplo n.º 1
0
        protected override void buildChildren(IState parent)
        {
            Connect4State state = (Connect4State)parent;

            int Heigth = Connect4State.Heigth;
            int Width  = Connect4State.Width;

            //kogo dzieci szukamy
            int who = state.Kto;

            int[,] Move      = new int[Heigth, Width];
            int[,] array_tmp = new int[Heigth, Width];
            Array.Copy(state.Table, array_tmp, array_tmp.Length);

            int kto_tmp = state.Kto;

            if (state.Kto == 1)
            {
                kto_tmp = 2;
            }
            else
            {
                if (state.Kto == 2)
                {
                    kto_tmp = 1;
                }
            }

            for (int i = 0; i < Width; i++)
            {
                Array.Copy(state.Table, array_tmp, array_tmp.Length);
                for (int j = Heigth - 1; j >= 0; j--)
                {
                    if (array_tmp[j, i] == 0)
                    {
                        Move = Connect4State.Move(array_tmp, i, who);
                        Connect4State child = new Connect4State(state, Move, kto_tmp);
                        break;
                    }
                }
            }


            Console.Write("");
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            #region init



            Console.SetWindowPosition(0, 0);
            Console.Write(" Connect4");

            int who    = 2;
            int width  = 7;
            int heigth = 4;
            int deep   = 3;

            Console.Write("\nWysokosc planszy: ");
            heigth = Convert.ToInt32(Console.ReadLine());

            Console.Write("\nSzerokosc planszy: ");
            width = Convert.ToInt32(Console.ReadLine());

            Console.Write("\nJak gleboko szukac: ");
            deep = Convert.ToInt32(Console.ReadLine());
            Connect4State.Init(width, heigth, deep);

            DialogResult dialogResult_sudoku = MessageBox.Show("Komputer zaczyna? ( ͡° ͜ʖ ͡°)", "", MessageBoxButtons.YesNo);
            if (dialogResult_sudoku == DialogResult.Yes)
            {
                who++;
            }


            Console.Clear();
            Console.SetWindowPosition(0, 0);
            Console.Write(" Connect4");
            Console.CursorVisible = false;

            int[,] table = new int[Connect4State.Heigth, Connect4State.Width];



            //table 0 builder
            for (int i = 0; i < Connect4State.Heigth; i++)
            {
                for (int j = 0; j < Connect4State.Width; j++)
                {
                    table[i, j] = 0;
                }
            }
            #endregion //Init

            #region Gra


            while (true)
            {
                switch (who % 2)
                {
                case 0:
                    table = Connect4State.Move(table, Connect4State.GetChoise(table, who % 2 == 0 ? 1 : 2),
                                               who % 2 == 0 ? 1 : 2);

                    break;

                case 1:
                    Thread.Sleep(200);
                    table = Connect4State.ComputerChoiceTable(table, who % 2 == 0 ? 1 : 2);
                    //Thread.Sleep(300);
                    break;


                default:
                    break;
                }

                if (Connect4State.isWin(who % 2 == 0 ? 1 : 2, table) || Connect4State.isFull(table))
                {
                    break;
                }

                who++;
            }

            #endregion //Gra

            #region Wynik rozgrywki

            Connect4State.Print(table);
            if (!Connect4State.isFull(table))
            {
                if (who % 2 == 0)
                {
                    Console.WriteLine("\n\n\n\n\n\nGratulacje, wygrales!!!");
                }
                else
                {
                    Console.WriteLine("\n\n\n\n\n\nPrzegrana...");
                }
            }
            else
            {
                Console.WriteLine("\n\n\n\n\n\nRemis!");
            }
            Thread.Sleep(5000);
            #endregion //Wynik rozgrywki
        }