Example #1
0
        public int run(bool output)
        {
            //only wait or draw boards if output is set to true
            string move = "";
            bool   valid;

            Generate_Board();
            drawing.Invalidate();
            reversible_moves = 0;
            //main gameloop
            int   player     = 0;
            Timer turn_timer = new Timer();

            turn_timer.Interval = 1000;      //how long to wait on CPU moves?
            turn_timer.Tick    += (s, e) =>
            {
                turn_timer.Enabled = false;
                turn_timer.Stop();
            };
            while (!is_lost(player) && reversible_moves < 30)
            {
                //start turntimer
                turn_timer.Enabled = true;
                turn_timer.Start();
                if (output)
                {
                    CPU temp = new CPU(drawing);
                    Console.WriteLine(Player[player].name + " am Zug");
                    drawing.labelText(Player[player].name + " am Zug\n Boardvalue: " + temp.calcuteBoard_Value(board, player).ToString());
                }
                valid = false;
                while (!valid)
                {
                    move  = Player[player].move(new Board(board), player);
                    valid = Check_Move(move, player);
                    if (!valid)
                    {
                        if (Player[player].is_cpu)
                        {
                            Console.WriteLine("CPU invalid move");
                            Console.WriteLine(move);
                            return(-2);
                        }
                        else
                        {
                            drawing.Invalidate();
                        }
                    }
                }

                //if output mode is enabled CPU should wait if the minimum turn time is not yet over
                while (turn_timer.Enabled && Player[player].is_cpu && output)
                {
                    Application.DoEvents();
                }
                Perform_Move(move, player);
                drawing.Invalidate();
                //next player
                player = 1 - player;
            }
            if (output)
            {
                drawing.Draw_Board(board);
            }
            if (reversible_moves < 30)  //game ended in a win/loss
            {
                if (output)
                {
                    Console.WriteLine(Player[1 - player].name + " hat gewonnen");
                    drawing.labelText(Player[1 - player].name + " hat gewonnen");
                }
                return(1 - player);
            }
            else
            {
                if (output)
                {
                    Console.WriteLine("Unentschieden!");
                    drawing.labelText("Unentschieden");
                }
                return(-1);
            }
        }