web analytics

Immortal Table Solver (Preview) !! โปรแกรมแก้โจทย์เกม Immortal table

cover

บทความนี้ ไม่มีสาระเลย สามารถข้ามได้

 

Immortal Table Solver

คราวนี้  เราขอเสนอ Immortal Table Solver โปรแกรมแก้โจทย์เกม Immortal Table
เวอชัน Preview อยู่เน้อ

 

ดาวน์โหลดโปรแกรม (.exe)

https://benzneststudios.com/blog/wp-content/uploads/2016/08/ImmortalTableSolver-1.rar

 

เปิดโปรแกรมขึ้นมา กดปุ่ม ENTER

 

จะพบหน้าจอลักษณะนี้ ด้านซ้ายคือ input ซึ่งเป็น ตารางเริ่มเกม พอกดจนตารางได้ตามต้องการก็กดปุ่ม GET SOLUTION

2

 

โปรแกรมจะคำนวณ วิธีแก้ออกมาให้ อย่างง่ายดาย นอกจากนี้ยังเลือก Option ได้ด้วย อยากชนะด้วยสีแดง หรือสีน้ำเงินก็ได้

3

 

แต่เดี๋ยวก่อน ขอไว้ด่านนึงนะ แกล้งๆๆ (แล้วใครจะมาเล่น)

4

 

Source code 

code ข้างล่างนี้ ผมทำขึ้นขำๆ ก็พอแก้ได้ แต่ไม่รู้ว่าจะทุก case รึปล่าวนะ

 

Game.cs

class Game
    {
        public static char VALUE_A = 'A';
        public static char VALUE_B = 'B';
        public static char VALUE_C = 'C';

        public static Color COLOR_A = Color.Red;
        public static Color COLOR_B = Color.Blue;
        public static Color COLOR_C = Color.Green;

        public static char STATUS_WIN_WAIT_SAVE = 'A';
        public static char STATUS_SOLVE = 'B';
        public static char STATUS_WIN = 'C';

        public static int WIN_WITH_ANY = 0;
        public static int WIN_WITH_A = 1;
        public static int WIN_WITH_B = 2;
        public static int WIN_WITH_C = 3;
    }

Plot.cs

public class Plot
    {
        int row;
        int col;

        public Plot(int row,int col)
        {
            this.row = row;
            this.col = col;
        }

        public int Row
        {
            get
            {
                return row;
            }

            set
            {
                row = value;
            }
        }

        public int Col
        {
            get
            {
                return col;
            }

            set
            {
                col = value;
            }
        }
    }

ImmortalTableSolve2Colors.cs

 public class ImmortalTableSolver2Colors
    {
        char[,] map;
        int MAP_SIZE = 4;
        int MAX_DEEP = 16;
        List<Plot> result;

        public List<Plot> solve(char[,] m,int method_win)
        {
            result = new List<Plot>();
            List<Plot> list_plot = new List<Plot>();
            this.map = m;

            char[,] map_copy = new char[MAP_SIZE, MAP_SIZE];
            copy(map, map_copy);
            run(list_plot,map_copy, -1, -1, 0, method_win);

            return result;
        }

        List<Plot> copy(List<Plot> oldList)
        {
            List<Plot> newList = new List<Plot>(oldList);
            return newList;
        }

        void copy(char[,] source, char[,] des)
        {
            int i = 0;
            int j = 0;
            for (i = 0; i < MAP_SIZE; i++)
            {
                for (j = 0; j < MAP_SIZE; j++)
                {
                    des[i, j] = source[i, j];
                }
            }
        }

        public char changeColorToAcross(char value)
        {
            if (value == Game.VALUE_A)
            {
                return Game.VALUE_B;
            }
            return Game.VALUE_A;
        }

        public Color changeColorToAcross(Color value)
        {
            if (value == Game.COLOR_A)
            {
                return Game.COLOR_B;
            }
            return Game.COLOR_A;
        }

        public char[,] press(char[,] m, int row, int col)
        {
            int i, j;
            for (i = 0; i < MAP_SIZE; i++)
            {
                m[row, i] = changeColorToAcross(m[row, i]);
            }

            for (i = 0; i < MAP_SIZE; i++)
            {
                m[i, col] = changeColorToAcross(m[i, col]);
            }

            m[row, col] = changeColorToAcross(m[row, col]);
            return m;
        }

        bool isWin(char[,] map,int method_win)
        {
            int i, j;
            char value = map[0, 0];

            for (i = 0; i < MAP_SIZE; i++)
            {
                for (j = 0; j < MAP_SIZE; j++)
                {
                    if (value != map[i, j])
                    {
                        return false;
                    }

                    if (method_win != Game.WIN_WITH_ANY)
                    {
                        if (method_win == Game.WIN_WITH_A && map[i, j] == Game.VALUE_B)
                        {
                            return false;
                        }
                        else if (method_win == Game.WIN_WITH_B && map[i, j] == Game.VALUE_A)
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        public bool equalMap(char[,] map_a, char[,] map_b)
        {
            int i, j;
            for (i = 0; i < MAP_SIZE; i++)
            {
                for (j = 0; j < MAP_SIZE; j++)
                {
                    if (map_a[i, j] != map_b[i, j])
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public char[,] reverseMap(char[,] map)
        {
            int i, j;
            for (i = 0; i < MAP_SIZE; i++)
            {
                for (j = 0; j < MAP_SIZE; j++)
                {
                    map[i, j] = changeColorToAcross(map[i, j]);
                }
            }
            return map;
        }

        void addAnswer(int row, int col)
        {
            result.Insert(0,new Plot(row, col));
        }

        bool hasInList(List<Plot> list_plot,int i, int j)
        {
            foreach(Plot plot in list_plot)
            {
                if(plot.Row == i && plot.Col == j)
                {
                    return true;
                }
            }
            return false;
        }

        char run(List<Plot> list_plot,char[,] map_input, int before_row, int before_col, int times,int method_win)
        {
            
            if (times > MAX_DEEP)
            {
                return Game.STATUS_SOLVE;
            }

            List<Plot> list_plot_copy = copy(list_plot);
            char[,] map_copy = new char[MAP_SIZE, MAP_SIZE];
            int i, j;

            for (i = 0; i < MAP_SIZE; i++)
            {
                for (j = 0; j < MAP_SIZE; j++)
                {
                    if (i == before_row && j == before_col)
                    {
                        continue;
                    }

                    copy(map_input, map_copy);
                    press(map_copy, i, j);

                    if (isWin(map_copy, method_win))
                    {
                        addAnswer(i, j);
                        return Game.STATUS_WIN_WAIT_SAVE;
                    }
                }
            }

            for (i = 0; i < MAP_SIZE; i++)
            {
                for (j = 0; j < MAP_SIZE; j++)
                {
                    if (i == before_row && j == before_col)
                    {
                        continue;
                    }

                    if (hasInList(list_plot_copy, i, j))
                    {
                        continue;
                    }

                    copy(map_input, map_copy);
                    press(map_copy, i, j);
                    list_plot_copy.Add(new Plot(i, j));

                    char status = run(list_plot_copy, map_copy, i, j, times + 1, method_win);
                    if (status == Game.STATUS_WIN_WAIT_SAVE)
                    {
                        addAnswer(i, j);
                        if (equalMap(map_input, map))
                        {
                            return Game.STATUS_WIN;
                        }
                        return Game.STATUS_WIN_WAIT_SAVE;
                    }
                    else if (status == Game.STATUS_WIN)
                    {
                        return Game.STATUS_WIN;
                    }
                }
            }

            return Game.STATUS_SOLVE;
        }
    }

 

ตัวอย่างการใช้งาน

char[,] mission = { 
                    { Game.VALUE_A,Game.VALUE_A,Game.VALUE_A,Game.VALUE_B},
                    { Game.VALUE_B,Game.VALUE_B,Game.VALUE_A,Game.VALUE_A},
                    { Game.VALUE_B,Game.VALUE_A, Game.VALUE_B,Game.VALUE_A},
                    { Game.VALUE_A,Game.VALUE_B,Game.VALUE_A,Game.VALUE_A}
            };

ImmortalTableSolver2Colors im = new ImmortalTableSolver2Colors();
List<Plot> list_answer = im.solve(mission , Game.WIN_ANY);