pop game core

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication8
{
    public class Core
    {
        public static Dictionary<string, Cell> cells = new Dictionary<string, Cell>();
        public static string log = string.Empty;
        static bool ConsoleOutput = false;

        static Core()
        {
            #region
            cells.Add("A1", new Cell() { Name = "A1", Left = "", Right = "B1", Up = "", Down = "A2" });
            cells.Add("B1", new Cell() { Name = "B1", Left = "A1", Right = "C1", Up = "", Down = "B2" });
            cells.Add("C1", new Cell() { Name = "C1", Left = "B1", Right = "D1", Up = "", Down = "C2" });
            cells.Add("D1", new Cell() { Name = "D1", Left = "C1", Right = "E1", Up = "", Down = "D2" });
            cells.Add("E1", new Cell() { Name = "E1", Left = "D1", Right = "F1", Up = "", Down = "E2" });
            cells.Add("F1", new Cell() { Name = "F1", Left = "E1", Right = "", Up = "", Down = "F2" });
            cells.Add("A2", new Cell() { Name = "A2", Left = "", Right = "B2", Up = "A1", Down = "A3" });
            cells.Add("B2", new Cell() { Name = "B2", Left = "A2", Right = "C2", Up = "B1", Down = "B3" });
            cells.Add("C2", new Cell() { Name = "C2", Left = "B2", Right = "D2", Up = "C1", Down = "C3" });
            cells.Add("D2", new Cell() { Name = "D2", Left = "C2", Right = "E2", Up = "D1", Down = "D3" });
            cells.Add("E2", new Cell() { Name = "E2", Left = "D2", Right = "F2", Up = "E1", Down = "E3" });
            cells.Add("F2", new Cell() { Name = "F2", Left = "E2", Right = "", Up = "F1", Down = "F3" });
            cells.Add("A3", new Cell() { Name = "A3", Left = "", Right = "B3", Up = "A2", Down = "A4" });
            cells.Add("B3", new Cell() { Name = "B3", Left = "A3", Right = "C3", Up = "B2", Down = "B4" });
            cells.Add("C3", new Cell() { Name = "C3", Left = "B3", Right = "D3", Up = "C2", Down = "C4" });
            cells.Add("D3", new Cell() { Name = "D3", Left = "C3", Right = "E3", Up = "D2", Down = "D4" });
            cells.Add("E3", new Cell() { Name = "E3", Left = "D3", Right = "F3", Up = "E2", Down = "E4" });
            cells.Add("F3", new Cell() { Name = "F3", Left = "E3", Right = "", Up = "F2", Down = "F4" });
            cells.Add("A4", new Cell() { Name = "A4", Left = "", Right = "B4", Up = "A3", Down = "A5" });
            cells.Add("B4", new Cell() { Name = "B4", Left = "A4", Right = "C4", Up = "B3", Down = "B5" });
            cells.Add("C4", new Cell() { Name = "C4", Left = "B4", Right = "D4", Up = "C3", Down = "C5" });
            cells.Add("D4", new Cell() { Name = "D4", Left = "C4", Right = "E4", Up = "D3", Down = "D5" });
            cells.Add("E4", new Cell() { Name = "E4", Left = "D4", Right = "F4", Up = "E3", Down = "E5" });
            cells.Add("F4", new Cell() { Name = "F4", Left = "E4", Right = "", Up = "F3", Down = "F5" });
            cells.Add("A5", new Cell() { Name = "A5", Left = "", Right = "B5", Up = "A4", Down = "" });
            cells.Add("B5", new Cell() { Name = "B5", Left = "A5", Right = "C5", Up = "B4", Down = "" });
            cells.Add("C5", new Cell() { Name = "C5", Left = "B5", Right = "D5", Up = "C4", Down = "" });
            cells.Add("D5", new Cell() { Name = "D5", Left = "C5", Right = "E5", Up = "D4", Down = "" });
            cells.Add("E5", new Cell() { Name = "E5", Left = "D5", Right = "F5", Up = "E4", Down = "" });
            cells.Add("F5", new Cell() { Name = "F5", Left = "E5", Right = "", Up = "F4", Down = "" });
            #endregion
        }

        public static void Init(Dictionary<string, int> m, bool consoleOutput)
        {
            ConsoleOutput = consoleOutput;
            //clear
            foreach (string key in cells.Keys)
                cells[key].Value = 0;
            log = string.Empty;

            //set
            foreach (string key in m.Keys)
            {
                if (cells.ContainsKey(key))
                    cells[key].Value = m[key];
            }
        }

        public static Dictionary<string, Cell> Start(string cellName)
        {
            Run(cells, cellName, "manual", "N/A", 1);

            Output("=======Result========");
            bool allEmpty = true;
            foreach (string key in cells.Keys)
            {
                Cell c = cells[key];
                if (c.Value != 0)
                {
                    Output(key + " is " + c.Value);
                    allEmpty = false;
                }
            }
            if (allEmpty)
                Output("All cell empty!");

            return cells;
        }

        static void Run(Dictionary<string, Cell> cells, string cellName, string from, string directTo, int runTimes)
        {
            if (cells.ContainsKey(cellName))
            {
                if (cells[cellName].Value > 0)
                {
                    //if there are water, add more
                    cells[cellName].Value++;
                    Output(cellName + " reveive from " + from + " at " + runTimes.ToString() + " times");
                    if (cells[cellName].Value > 5)
                    {
                        #region explosion
                        cells[cellName].Value = 0;
                        Output(cellName + " exploded at " + runTimes.ToString() + " times");
                        //explosion to neighbor
                        if (cells[cellName].Left.Length > 0)
                        {
                            Output(cellName + " exploded to " + cells[cellName].Left + " at " + runTimes.ToString() + " times");
                            Run(cells, cells[cellName].Left, cellName, "left", runTimes + 1);
                        }

                        if (cells[cellName].Right.Length > 0)
                        {
                            Output(cellName + " exploded to " + cells[cellName].Right + " at " + runTimes.ToString() + " times");
                            Run(cells, cells[cellName].Right, cellName, "right", runTimes + 1);
                        }

                        if (cells[cellName].Up.Length > 0)
                        {
                            Output(cellName + " exploded to " + cells[cellName].Up + " at " + runTimes.ToString() + " times");
                            Run(cells, cells[cellName].Up, cellName, "up", runTimes + 1);
                        }

                        if (cells[cellName].Down.Length > 0)
                        {
                            Output(cellName + " exploded to " + cells[cellName].Down + " at " + runTimes.ToString() + " times");
                            Run(cells, cells[cellName].Down, cellName, "down", runTimes + 1);
                        }
                        #endregion
                    }
                }
                else
                {
                    #region if there are no water, let it pass
                    switch (directTo)
                    {
                        case "left":
                            if (cells[cellName].Left.Length > 0)
                            {
                                Output(cellName + " pass from " + from + " to " + directTo + " at " + runTimes.ToString() + " times");
                                Run(cells, cells[cellName].Left, from, "left", runTimes + 1);
                            }
                            else
                            {
                                Output("From " + from + " END at " + cellName + " at " + runTimes.ToString() + " times");
                            }
                            break;
                        case "right":
                            if (cells[cellName].Right.Length > 0)
                            {
                                Output(cellName + " pass from " + from + " to " + directTo + " at " + runTimes.ToString() + " times");
                                Run(cells, cells[cellName].Right, from, "right", runTimes + 1);
                            }
                            else
                            {
                                Output("From " + from + " END at " + cellName + " at " + runTimes.ToString() + " times");
                            }
                            break;
                        case "up":
                            if (cells[cellName].Up.Length > 0)
                            {
                                Output(cellName + " pass from " + from + " to " + directTo + " at " + runTimes.ToString() + " times");
                                Run(cells, cells[cellName].Up, from, "up", runTimes + 1);
                            }
                            else
                            {
                                Output("From " + from + " END at " + cellName + " at " + runTimes.ToString() + " times");
                            }
                            break;
                        case "down":
                            if (cells[cellName].Down.Length > 0)
                            {
                                Output(cellName + " pass from " + from + " to " + directTo + " at " + runTimes.ToString() + " times");
                                Run(cells, cells[cellName].Down, from, "down", runTimes + 1);
                            }
                            else
                            {
                                Output("From " + from + " END at " + cellName + " at " + runTimes.ToString() + " times");
                            }
                            break;
                    }
                    #endregion
                }
            }
            else
            {
                Output("Error cell name: " + cellName + " at " + runTimes.ToString() + " times");
            }


        }

        static void Output(string message)
        {
            if (ConsoleOutput)
                System.Console.WriteLine(message);

            log += message + "/r/n";
        }
    }

    public class Cell
    {
        public string Name;
        public string Left;
        public string Right;
        public string Up;
        public string Down;
        //1-5
        public int Value { get; set; }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值