Example #1
0
        /// <summary>
        /// Scans the SadConsole's Global KeyboardState and triggers behaviour
        /// based on the button pressed.
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public override bool ProcessKeyboard(Keyboard info)
        {
            if (GameLoop.World != null)
            {
                if (HandleMove(info))
                {
                    if (!GetPlayer.Bumped && GameLoop.World.CurrentMap.ControlledEntitiy is Player)
                    {
                        GameLoop.World.ProcessTurn(TimeHelper.GetWalkTime(GetPlayer), true);
                    }
                    else if (GameLoop.World.CurrentMap.ControlledEntitiy is Player)
                    {
                        GameLoop.World.ProcessTurn(TimeHelper.GetAttackTime(GetPlayer), true);
                    }

                    return(true);
                }

                if (info.IsKeyPressed(Keys.NumPad5) || info.IsKeyPressed(Keys.OemPeriod))
                {
                    GameLoop.World.ProcessTurn(TimeHelper.Wait, true);
                }

                if (info.IsKeyPressed(Keys.A))
                {
                    bool sucess = CommandManager.DirectAttack(GameLoop.World.Player);
                    GameLoop.World.ProcessTurn(TimeHelper.GetAttackTime(GameLoop.World.Player), sucess);
                }

                if (info.IsKeyPressed(Keys.G))
                {
                    Item item   = GameLoop.World.CurrentMap.GetEntityAt <Item>(GameLoop.World.Player.Position);
                    bool sucess = CommandManager.PickUp(GameLoop.World.Player, item);
                    InventoryScreen.ShowItems(GameLoop.World.Player);
                    GameLoop.World.ProcessTurn(TimeHelper.Interact, sucess);
                }
                if (info.IsKeyPressed(Keys.D))
                {
                    bool sucess = CommandManager.DropItems(GameLoop.World.Player);
                    Item item   = GameLoop.World.CurrentMap.GetEntityAt <Item>(GameLoop.World.Player.Position);
                    InventoryScreen.RemoveItemFromConsole(item);
                    InventoryScreen.ShowItems(GameLoop.World.Player);
                    GameLoop.World.ProcessTurn(TimeHelper.Interact, sucess);
                }
                if (info.IsKeyPressed(Keys.C))
                {
                    bool sucess = CommandManager.CloseDoor(GameLoop.World.Player);
                    GameLoop.World.ProcessTurn(TimeHelper.Interact, sucess);
                    MapWindow.MapConsole.IsDirty = true;
                }
                if (info.IsKeyPressed(Keys.I))
                {
                    InventoryScreen.Show();
                }

                if (info.IsKeyPressed(Keys.H))
                {
                    bool sucess = CommandManager.SacrificeLifeEnergyToMana(GameLoop.World.Player);
                    GameLoop.World.ProcessTurn(TimeHelper.MagicalThings, sucess);
                }

                if (info.IsKeyPressed(Keys.L))
                {
                    if (!(target != null))
                    {
                        target = new Target(GetPlayer.Position);
                    }

                    if (target.EntityInTarget())
                    {
                        if (target.TargetList != null)
                        {
                            LookWindow w = new LookWindow(target.TargetList[0]);
                            w.Show();

                            return(true);
                        }
                    }

                    if (GameLoop.World.CurrentMap.ControlledEntitiy is not Player &&
                        !target.EntityInTarget())
                    {
                        GameLoop.World.ChangeControlledEntity(GetPlayer);
                        GameLoop.World.CurrentMap.Remove(target.Cursor);
                        target = null;
                        return(true);
                    }

                    GameLoop.World.CurrentMap.Add(target.Cursor);

                    GameLoop.World.ChangeControlledEntity(target.Cursor);

                    return(true);
                }

                if (info.IsKeyDown(Keys.LeftShift) && info.IsKeyPressed(Keys.Z))
                {
                    var spellBase = GetPlayer.Magic.QuerySpell("magic_missile");

                    var entity = GameLoop.World.CurrentMap.GetClosestEntity(GetPlayer.Position, spellBase.SpellRange);

                    if (entity != null)
                    {
                        bool sucess = spellBase.CastSpell(
                            entity.Position,
                            GetPlayer);

                        GameLoop.World.ProcessTurn(TimeHelper.MagicalThings, sucess);
                        return(true);
                    }
                    else
                    {
                        GameLoop.UIManager.MessageLog.Add("There is no target for the spell!");
                        return(false);
                    }
                }

#if DEBUG
                if (info.IsKeyPressed(Keys.F10))
                {
                    CommandManager.ToggleFOV();
                    MapWindow.MapConsole.IsDirty = true;
                }

                if (info.IsKeyPressed(Keys.F8))
                {
                    GetPlayer.AddComponent(new Components.TestComponent(GetPlayer));
                }

                if (info.IsKeyPressed(Keys.NumPad0))
                {
                    LookWindow w = new LookWindow(GetPlayer);
                    w.Show();
                }
#endif

                if (info.IsKeyPressed(Keys.Escape) && NoPopWindow)
                {
                    //SadConsole.Game.Instance.Exit();
                    MainMenu.Show();
                    MainMenu.IsFocused = true;
                }
            }

            return(base.ProcessKeyboard(info));
        }
        private void MNIST_BMP_button_Click(object sender, EventArgs e)
        {
            OpenFileDialog openMNISTDialog = new OpenFileDialog();

            string line;

            openMNISTDialog.InitialDirectory = "\\";
            openMNISTDialog.Filter           = "All files (*.*)|*.*|MNIST files(*.csv)|*.csv";
            openMNISTDialog.FilterIndex      = 2;
            openMNISTDialog.RestoreDirectory = true;

            if (openMNISTDialog.ShowDialog() == DialogResult.OK)
            {
                string filename    = openMNISTDialog.FileName;
                string output_addr = "";
                var    fileStream  = new FileStream(filename, FileMode.Open, FileAccess.Read);
                using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
                {
                    while (true)
                    {
                        Bitmap Image = new Bitmap(28, 28);
                        char   tmp;
                        string number = "";
                        int    x      = 0;
                        int    y      = 0;
                        int    word   = 0;
                        int    brightness;
                        output_addr = "";

                        line = streamReader.ReadLine();
                        if (line == null)
                        {
                            break;
                        }
                        //textBox1.Text = Convert.ToString(line.Length);
                        for (int symbol = 0; symbol < line.Length; symbol++)
                        {
                            tmp = Convert.ToChar(line.Substring(symbol, 1));

                            if (tmp == ',' && word == 0)
                            {
                                //textBox1.Text = Convert.ToString(tmp);
                                output_addr += line.Substring(0, symbol);
                                number       = "";
                                word++;
                            }
                            else if (tmp == ',')
                            {
                                brightness = Convert.ToInt32(number);
                                number     = "";
                                Image.SetPixel(x, y, Color.FromArgb(255, 255 - brightness, 255 - brightness, 255 - brightness));
                                x++;
                                if (x == 28)
                                {
                                    x = 0;
                                    y++;
                                }
                            }
                            else
                            {
                                number = number + tmp;
                                if (symbol == line.Length - 1)
                                {
                                    brightness = Convert.ToInt32(number);
                                    Image.SetPixel(x, y, Color.FromArgb(255, 255 - brightness, 255 - brightness, 255 - brightness));
                                }
                            }

                            //Image.SetPixel(x, y, Color.FromArgb(152, 15, 198, 255));
                        }
                        LookWindow.Image = Image;
                        LookWindow.Refresh();
                        try
                        {
                            output_addr = @"MNIST Image\\" + output_addr + "\\" + output_addr + "_[" + Convert.ToString(Directory.GetFiles(@"MNIST Image\\" + output_addr).Length) + "].bmp";
                        }
                        catch (System.IO.DirectoryNotFoundException)
                        {
                            Directory.CreateDirectory(@"MNIST Image\\" + output_addr);
                            output_addr = @"MNIST Image\\" + output_addr + "\\" + output_addr + "_[0].bmp";
                        }
                        LogBox.Text += Convert.ToString(output_addr) + "\r\n";
                        LogBox.Refresh();
                        Image.Save(output_addr);
                    }
                }
            }
        }
        private void AutoQueryButton_Click(object sender, EventArgs e)
        {
            double[] input  = new double[784];
            double[] answer = new double[10];
            double[] output = new double[10];

            string[] filePaths = Directory.GetFiles(@"MNIST Image\\", "*.bmp",
                                                    SearchOption.AllDirectories);
            string image_addr;
            string image_name;
            Bitmap image;
            Bitmap percent_graph = new Bitmap(percent_box.Width * 999, percent_box.Height);
            double error_percent = 0;

            LogBox.Clear();

            using (Graphics g = Graphics.FromImage(percent_graph))
            {
                g.Clear(Color.Black);
            }

            for (int file = 0; file < filePaths.Length; file++)
            {
                error_percent    = 0;
                image_addr       = filePaths[file];
                image_name       = Path.GetFileName(image_addr);
                image            = new Bitmap(image_addr);
                LookWindow.Image = image;
                LookWindow.Refresh();

                for (int x = 0; x < answer.Length; x++)
                {
                    if (network.neuron_arrays[network.neuron_arrays.Length - 1][x].name == "unnamed")
                    {
                        network.neuron_arrays[network.neuron_arrays.Length - 1][x].name = new DirectoryInfo(Directory.GetDirectories(@"MNIST Image")[x]).Name;
                    }
                    if (network.neuron_arrays[network.neuron_arrays.Length - 1][x].name == Path.GetFileName(Path.GetDirectoryName(image_addr)))
                    {
                        answer[x] = 1;
                    }
                    else
                    {
                        answer[x] = 0;
                    }
                }


                for (int y = 0; y < 28; y++)
                {
                    for (int x = 0; x < 28; x++)
                    {
                        input[x + y * x] = 255 * (1 - Convert.ToDouble(image.GetPixel(x, y).GetBrightness()));
                    }
                }
                output = network.Query(input);


                //LogBox.Clear();

                double sum     = 0;
                bool   mistake = false;
                for (int x = 0; x < output.Length; x++)
                {
                    if (answer[x] == 1)
                    {
                        error_percent += (1 - output[x]) / answer.Length;
                    }
                    else
                    {
                        error_percent += output[x] / answer.Length;
                        if (output[x] >= 0.5)
                        {
                            mistake = true;
                        }
                    }
                    sum += output[x];
                }
                if (mistake)
                {
                    LogBox.Text += "Error in " + Path.GetFileName(filePaths[file]) + "\r\n";
                }

                LogBox.Refresh();

                if (error_percent > 1)
                {
                    error_percent = 1;
                }
                try
                {
                    ErrorLabel.Text = Convert.ToString(error_percent * 100).Substring(0, 5) + "%";
                }
                catch (ArgumentOutOfRangeException)
                {
                    ErrorLabel.Text = "100%";
                }
                ErrorLabel.Refresh();

                using (Graphics g = Graphics.FromImage(percent_graph))
                {
                    int a = Convert.ToInt32((percent_graph.Width / filePaths.Length) * file);
                    int b = Convert.ToInt32(percent_graph.Height - percent_graph.Height * error_percent);
                    int c = Convert.ToInt32(percent_graph.Width / filePaths.Length);
                    int d = Convert.ToInt32(percent_graph.Height * error_percent);

                    g.DrawRectangle(new Pen(Color.Green, 1), new Rectangle(a, b, c, d));
                    g.FillRectangle(new SolidBrush(Color.Green), new Rectangle(a, b, c, d));
                }
                percent_box.Image = percent_graph;
                percent_box.Refresh();
            }
        }
        private void TrainButton_Click(object sender, EventArgs e)
        {
            int    epochs = 100;
            double index  = 0.3;

            string[] filePaths = Directory.GetFiles(@"MNIST Image\\", "*.bmp",
                                                    SearchOption.AllDirectories);
            string image_addr;
            string image_name;
            Bitmap image;
            Bitmap percent_graph = new Bitmap(percent_box.Width * 999, percent_box.Height);
            double error_percent = 0;
            Random rand          = new Random();

            try
            {
                epochs = Convert.ToInt32(EpochBox.Text);
            }
            catch (FormatException)
            {
                epochs        = 100;
                EpochBox.Text = "100";
            }
            try
            {
                index = Convert.ToInt32(IndexBox.Text);
            }
            catch (FormatException)
            {
                index         = 0.3;
                IndexBox.Text = "0,3";
            }

            if (EpochBox.Text == "")
            {
                EpochBox.Text = "100";
            }
            if (IndexBox.Text == "")
            {
                IndexBox.Text = "0,3";
            }

            using (Graphics g = Graphics.FromImage(percent_graph))
            {
                g.Clear(Color.Black);
            }

            for (int epoch = 0; epoch < epochs; epoch++)
            {
                error_percent = 0;
                for (int file = 0; file < filePaths.Length; file++)
                {
                    image_addr       = filePaths[rand.Next(0, filePaths.Length)];
                    image_name       = Path.GetFileName(image_addr);
                    image            = new Bitmap(image_addr);
                    LookWindow.Image = image;
                    LookWindow.Refresh();

                    for (int x = 0; x < answer.Length; x++)
                    {
                        if (network.neuron_arrays[network.neuron_arrays.Length - 1][x].name == "unnamed")
                        {
                            network.neuron_arrays[network.neuron_arrays.Length - 1][x].name = new DirectoryInfo(Directory.GetDirectories(@"MNIST Image")[x]).Name;
                        }
                        if (network.neuron_arrays[network.neuron_arrays.Length - 1][x].name == Path.GetFileName(Path.GetDirectoryName(image_addr)))
                        {
                            answer[x] = 1;
                        }
                        else
                        {
                            answer[x] = 0;
                        }
                    }


                    for (int y = 0; y < 28; y++)
                    {
                        for (int x = 0; x < 28; x++)
                        {
                            input[x + y * x] = 1 - Convert.ToDouble(image.GetPixel(x, y).GetBrightness());
                        }
                    }
                    output = network.Query(input);
                    LogBox.Clear();

                    double sum = 0;

                    for (int x = 0; x < output.Length; x++)
                    {
                        if (answer[x] == 1)
                        {
                            error_percent += (1 - output[x]) / filePaths.Length;
                        }
                        else
                        {
                            error_percent += output[x] / filePaths.Length;
                        }
                        sum += output[x];
                    }

                    for (int x = 0; x < output.Length; x++)
                    {
                        LogBox.Text += network.neuron_arrays[network.neuron_arrays.Length - 1][x].name + " => " + Convert.ToString((output[x] / sum) * 100).Substring(0, 4) + "%\r\n";
                    }
                    LogBox.Refresh();

                    network.Train(input, answer, index);
                    //Thread.Sleep(500);}
                }
                if (error_percent > 1)
                {
                    error_percent = 1;
                }
                try
                {
                    ErrorLabel.Text = Convert.ToString(error_percent * 100).Substring(0, 5) + "%";
                }
                catch (ArgumentOutOfRangeException)
                {
                    ErrorLabel.Text = "100%";
                }
                ErrorLabel.Refresh();

                using (Graphics g = Graphics.FromImage(percent_graph))
                {
                    int a = Convert.ToInt32((percent_graph.Width / epochs) * epoch);
                    int b = Convert.ToInt32(percent_graph.Height - percent_graph.Height * error_percent);
                    int c = Convert.ToInt32(percent_graph.Width / epochs);
                    int d = Convert.ToInt32(percent_graph.Height * error_percent);

                    g.DrawRectangle(new Pen(Color.Green, 1), new Rectangle(a, b, c, d));
                    g.FillRectangle(new SolidBrush(Color.Green), new Rectangle(a, b, c, d));
                }
                percent_box.Image = percent_graph;
                percent_box.Refresh();
            }

            /*ouble[] input = {1.0, 0.5, -1.5};
             * double[] answer = {0.20813168, 0.22549015, 0.99742175};
             * double index = 0.3;
             * double[] output = new double[3];
             * output = network.Query(input);
             * network.Train(input, answer, index);
             * output = network.Query(input);*/
        }