Esempio n. 1
0
        private static bool checkEndGame(game g)
        {
            for (int y = 0; y < g.height; y++)
            {
                for (int x = 0; x < g.width; x++)
                {
                    area a = g.boxes[y][x].lineup;
                    if (a.isAreaUsedInGame && a.isSet == false)
                    {
                        return(false);
                    }

                    a = g.boxes[y][x].linedown;
                    if (a.isAreaUsedInGame && a.isSet == false)
                    {
                        return(false);
                    }

                    a = g.boxes[y][x].lineleft;
                    if (a.isAreaUsedInGame && a.isSet == false)
                    {
                        return(false);
                    }

                    a = g.boxes[y][x].lineright;
                    if (a.isAreaUsedInGame && a.isSet == false)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Esempio n. 2
0
        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            game g = tabcontrol.SelectedTab.Tag as game;

            game.games.Remove(g);
            tabcontrol.TabPages.Remove(tabcontrol.SelectedTab);
        }
Esempio n. 3
0
        //creating individual game controls

        static void P_MouseClick(object sender, MouseEventArgs e)
        {
            var p = sender as PanelReplacement;

            if (p == null || p.Tag == null)
            {
                return;
            }

            var a = p.Tag as area;

            if (a == null)
            {
                return;
            }

            game g = a.parents[0].parent;

            if (g == null)            // || p.Parent == null || p.Parent.Parent == null || p.Parent.Parent.Parent == null)
            {
                return;
            }

            var P = getGamePanel(a);            // p.Parent.Parent.Parent as gamepanel;

            if (P == null)
            {
                return;
            }

            ai.clickOnArea(a, g, P);
        }
Esempio n. 4
0
        public static void createGamePanel(gamepanel gp)
        {
            PanelReplacement p = gp.gamep;
            game             g = game.games[game.games.Count - 1];

            gp.Name = game.games.Count.ToString();

            //store these so we can hide solo squares surrounded by hidden areas
            var blacksquares = new List <PanelReplacement>();

            p.Controls.Clear();
            int width  = g.width;
            int height = g.height;
            int count  = 0;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    //black square
                    blacksquares.Add(addSquare(p, false));
                    addLine(g.boxes[y][x].lineup, p, count++, true, false);

                    if (x == (width - 1))
                    {
                        blacksquares.Add(addSquare(p, true));
                    }
                }

                for (int x = 0; x < width; x++)
                {
                    addLine(g.boxes[y][x].lineleft, p, count++, false, false);
                    addSquareArea(g.boxes[y][x].thisBox, p);

                    if (x == (width - 1))
                    {
                        addLine(g.boxes[y][x].lineright, p, count++, false, true);
                    }
                }

                if (y == (height - 1))
                {
                    for (int x = 0; x < width; x++)
                    {
                        //black square
                        blacksquares.Add(addSquare(p, false));
                        addLine(g.boxes[y][x].linedown, p, count++, true, false);

                        if (x == (width - 1))
                        {
                            blacksquares.Add(addSquare(p, true));
                        }
                    }
                }
            }

            PanelReplacement.FitPanel(p, p, 40, 0);
            gp.panel1.Location = new Point(0, gp.gamep.Location.Y + gp.gamep.Height + 5);
            PanelReplacement.FitPanel(gp.gameboardresize, gp.gameboardresize);
        }
Esempio n. 5
0
        public static void createNewGame(int width, int height, int playersH, int playersAI, bool removesquares)
        {
            game g;

            if (removesquares)
            {
                //25% to remove a square in the grid
                const int randomremove = 25;
                var       array        = new bool[height][];
                var       ran          = new Random();
                for (int a = 0; a < height; a++)
                {
                    array[a] = new bool[width];
                    for (int b = 0; b < width; b++)
                    {
                        array[a][b] = true;
                        int i = ran.Next() % 100;
                        if (i <= randomremove)
                        {
                            array[a][b] = false;
                        }
                    }
                }
                g = new game(playersH, playersAI, width, height, array);
            }
            else
            {
                //create game
                g = new game(playersH, playersAI, width, height);
            }
            initNewGame(g);
        }
Esempio n. 6
0
        public static void performTurn(game g, gamepanel gp)
        {
            if (g.completeGame)
            {
                return;
            }

            Thread.Sleep(100);
            Application.DoEvents();

            //get list of possible moves
            var  possible  = new List <area>();
            bool sacrifice = false;

retry:
            foreach (area a in g.lines)
            {
                if (a.isSet)
                {
                    continue;
                }

                //push wins to the front and exit
                int setedges = checkFilledBoxBlankEdges(a);

                if (setedges == 4)
                {
                    possible.Clear();
                    possible.Insert(0, a);
                    break;
                }
                //try not to sacrifice this square unless no other option, or no sac = push
                if ((setedges == 3 && sacrifice) || setedges <= 2)
                {
                    int n = 0;
                    if (possible.Count > 0)
                    {
                        var R = new Random();
                        n = R.Next() % possible.Count;
                    }
                    possible.Insert(n, a);
                }
            }

            //if there are no possible moves, but we are still here, it means we have to sacrifice
            if (possible.Count == 0)
            {
                //if sac already true, then error
                if (sacrifice)
                {
                    MessageBox.Show("Error in ai");
                    return;
                }
                sacrifice = true;
                goto retry;
            }

            clickOnArea(possible[0], g, gp);
        }
Esempio n. 7
0
        public static void performTurn(game g, gamepanel gp)
        {
            if (g.completeGame)
                return;

                Thread.Sleep(100);
                Application.DoEvents();

            //get list of possible moves
            var possible = new List<area>();
            bool sacrifice = false;
            retry:
            foreach (area a in g.lines)
            {
                if (a.isSet)
                    continue;

                //push wins to the front and exit
                int setedges = checkFilledBoxBlankEdges(a);

                if (setedges == 4)
                {
                    possible.Clear();
                    possible.Insert(0, a);
                    break;
                }
                //try not to sacrifice this square unless no other option, or no sac = push
                if ((setedges == 3&& sacrifice) || setedges <= 2)
                {
                    int n = 0;
                    if (possible.Count > 0)
                    {
                        var R = new Random();
                        n = R.Next()%possible.Count;
                    }
                    possible.Insert(n,a);
                }
            }

            //if there are no possible moves, but we are still here, it means we have to sacrifice
            if (possible.Count == 0)
            {
                //if sac already true, then error
                if (sacrifice)
                {
                    MessageBox.Show("Error in ai");
                    return;
                }
                sacrifice = true;
                goto retry;
            }

            clickOnArea(possible[0], g, gp);
        }
Esempio n. 8
0
        public static void clickOnArea(area a, game g, gamepanel gp)
        {
            if (a.isAreaUsedInGame == false || a.isSet || a.parents[0].thisBox == a)
            {
                return;
            }

            a.isSet = true;
            a.refpanel.BackColor = g.players[0].colour;

            //check boxes
            bool goodmove = false;

            foreach (var v in a.parents)
            {
                if (v.thisBox.isAreaUsedInGame == false || v.thisBox.isSet)
                {
                    continue;
                }

                bool val = checkAndSetFilledBox(v, g);
                if (val)
                {
                    goodmove = true;
                }
            }
            //if the box gets set, that means it was a filled square- the player gets another turn
            if (goodmove == false)
            {
                g.requeuePlayers();
                gp.currentplayercolour.BackColor = g.players[0].colour;
            }
            else
            {
                gamecontroller.updateScoreTable(g, gp);
            }

            //check end game
            if (checkEndGame(g))
            {
                gamecontroller.endGame(g, gp);
            }

            if (g.players[0].isAI)
            {
                Thread.Sleep(100);
                performTurn(g, gp);
            }
        }
Esempio n. 9
0
        public static void createNewGame(int playersH, int playersAI, string gamepath)
        {
            var FS = new FileStream(gamepath, FileMode.Open);
            var SR = new StreamReader(FS);

            String file = SR.ReadToEnd();
            var    sep  = new char[] { ' ', '\n', '\r' };

            String[] filesplit = file.Split(sep);
            int      width     = int.Parse(filesplit[0]);
            int      height    = int.Parse(filesplit[1]);

            String board = "";

            for (int a = 2; a < filesplit.Count(); a++)
            {
                if (filesplit[a].Length == 0)
                {
                    continue;
                }
                board += filesplit[a];
            }
            SR.Close();
            FS.Close();

            var array = new bool[height][];
            int count = 0;

            for (int a = 0; a < height; a++)
            {
                array[a] = new bool[width];
                for (int b = 0; b < width; b++)
                {
                    if (board[count].Equals(fileboardboxblank))
                    {
                        array[a][b] = false;
                    }
                    else
                    {
                        array[a][b] = true;
                    }
                    count++;
                }
            }
            var g = new game(playersH, playersAI, width, height, array);

            initNewGame(g);
        }
Esempio n. 10
0
        private static bool checkAndSetFilledBox(box b, game g)
        {
            //dont check is set so we can change later on if required via abilities or whatever
            if (b.thisBox.isAreaUsedInGame == false)
            {
                return(false);
            }

            if (b.lineup.isSet && b.lineleft.isSet &&
                b.lineright.isSet && b.linedown.isSet)
            {
                b.thisBox.refpanel.BackColor = g.players[0].colour;
                b.thisBox.isSet = true;
                g.players[0].score++;
                return(true);
            }
            return(false);
        }
Esempio n. 11
0
        public static void clickOnArea(area a, game g, gamepanel gp)
        {
            if (a.isAreaUsedInGame == false || a.isSet || a.parents[0].thisBox == a)
                return;

            a.isSet = true;
            a.refpanel.BackColor = g.players[0].colour;

            //check boxes
            bool goodmove = false;
            foreach (var v in a.parents)
            {
                if (v.thisBox.isAreaUsedInGame == false || v.thisBox.isSet)
                    continue;

                bool val = checkAndSetFilledBox(v, g);
                if (val)
                    goodmove = true;
            }
            //if the box gets set, that means it was a filled square- the player gets another turn
            if (goodmove == false)
            {
                g.requeuePlayers();
                gp.currentplayercolour.BackColor = g.players[0].colour;
            }
            else
            {
                gamecontroller.updateScoreTable(g, gp);
            }

            //check end game
            if (checkEndGame(g))
            {
                gamecontroller.endGame(g,gp);
            }

            if (g.players[0].isAI)
            {
                Thread.Sleep(100);
                performTurn(g, gp);
            }
        }
Esempio n. 12
0
        private void newplayercolours_Click(object sender, EventArgs e)
        {
            TabPage tp = Parent as TabPage;

            if (tp == null)
            {
                return;
            }
            game g = tp.Tag as game;

            g.resetPlayerColours();

            gamepanel gp = gamecontroller.getGamePanel(g.boxes[0][0].lineleft);

            if (gp == null)
            {
                return;
            }
            gamecontroller.updateScoreTable(g, gp);
        }
Esempio n. 13
0
        public static void updateScoreTable(game g, gamepanel gp)
        {
            var LV = gp.playerscoretable;

            LV.Items.Clear();
            foreach (player p in g.players)
            {
                ListViewItem LVI = new ListViewItem(p.score.ToString());
                if (p.isAI)
                {
                    LVI.SubItems.Add("AI");
                }
                else
                {
                    LVI.SubItems.Add("Player");
                }

                LVI.BackColor = p.colour;
                LV.Items.Add(LVI);
            }
        }
Esempio n. 14
0
        public static void initNewGame(game g)
        {
            //create interface page
            var TP = new TabPage();
            var gp = new gamepanel();

            TP.Tag  = g;
            gp.Dock = DockStyle.Fill;
            gp.Name = game.games.Count.ToString();
            createGamePanel(gp);
            TP.Controls.Add(gp);
            TP.Text = "Game";

            baseform.tabcontrol.Controls.Add(TP);
            baseform.tabcontrol.SelectedIndex = baseform.tabcontrol.TabPages.Count - 1;
            gp.currentplayercolour.BackColor  = g.players[0].colour;
            updateScoreTable(g, gp);
            if (g.players[0].isAI)
            {
                ai.performTurn(g, gp);
            }
        }
Esempio n. 15
0
 public static void endGame(game g, gamepanel gp)
 {
     g.completeGame = true;
     MessageBox.Show("Game Over!");
 }
Esempio n. 16
0
 public static void createNewGame(int width, int height, int playersH,int playersAI, bool removesquares)
 {
     game g;
     if (removesquares)
     {
         //25% to remove a square in the grid
         const int randomremove = 25;
         var array = new bool[height][];
         var ran = new Random();
         for (int a = 0; a < height; a++)
         {
             array[a] = new bool[width];
             for (int b = 0; b < width; b++)
             {
                 array[a][b] = true;
                 int i = ran.Next() % 100;
                 if (i <= randomremove)
                     array[a][b] = false;
             }
         }
         g = new game(playersH,playersAI, width, height, array);
     }
     else
     {
         //create game
         g = new game(playersH,playersAI, width, height);
     }
     initNewGame(g);
 }
Esempio n. 17
0
 public static void endGame(game g,gamepanel gp)
 {
     g.completeGame = true;
     MessageBox.Show("Game Over!");
 }
Esempio n. 18
0
        public static void initNewGame(game g)
        {
            //create interface page
            var TP = new TabPage();
            var gp = new gamepanel();
            TP.Tag = g;
            gp.Dock = DockStyle.Fill;
            gp.Name = game.games.Count.ToString();
            createGamePanel(gp);
            TP.Controls.Add(gp);
            TP.Text = "Game";

            baseform.tabcontrol.Controls.Add(TP);
            baseform.tabcontrol.SelectedIndex = baseform.tabcontrol.TabPages.Count - 1;
            gp.currentplayercolour.BackColor = g.players[0].colour;
            updateScoreTable(g,gp);
            if (g.players[0].isAI)
                ai.performTurn(g,gp);
        }
Esempio n. 19
0
        public static void updateScoreTable(game g,gamepanel gp)
        {
            var LV = gp.playerscoretable;
            LV.Items.Clear();
            foreach (player p in g.players)
            {
                ListViewItem LVI = new ListViewItem(p.score.ToString());
                if (p.isAI)
                    LVI.SubItems.Add("AI");
                else
                    LVI.SubItems.Add("Player");

                LVI.BackColor = p.colour;
                LV.Items.Add(LVI);
            }
        }
Esempio n. 20
0
        public static void createNewGame(int playersH,int playersAI, string gamepath)
        {
            var FS = new FileStream(gamepath, FileMode.Open);
            var SR = new StreamReader(FS);

            String file = SR.ReadToEnd();
            var sep = new char[] { ' ', '\n', '\r' };

            String[] filesplit = file.Split(sep);
            int width = int.Parse(filesplit[0]);
            int height = int.Parse(filesplit[1]);

            String board = "";
            for (int a = 2; a < filesplit.Count(); a++)
            {
                if (filesplit[a].Length == 0)
                    continue;
                board += filesplit[a];
            }
            SR.Close();
            FS.Close();

            var array = new bool[height][];
            int count = 0;
            for (int a = 0; a < height; a++)
            {
                array[a] = new bool[width];
                for (int b = 0; b < width; b++)
                {
                    if (board[count].Equals(fileboardboxblank))
                        array[a][b] = false;
                    else
                    {
                        array[a][b] = true;
                    }
                    count++;
                }
            }
            var g = new game(playersH,playersAI, width, height, array);
            initNewGame(g);
        }
Esempio n. 21
0
        private static bool checkAndSetFilledBox(box b, game g)
        {
            //dont check is set so we can change later on if required via abilities or whatever
            if (b.thisBox.isAreaUsedInGame == false)
                return false;

            if (b.lineup.isSet && b.lineleft.isSet &&
                b.lineright.isSet && b.linedown.isSet)
            {
                b.thisBox.refpanel.BackColor = g.players[0].colour;
                b.thisBox.isSet = true;
                g.players[0].score++;
                return true;
            }
            return false;
        }
Esempio n. 22
0
        private static bool checkEndGame(game g)
        {
            for (int y = 0; y < g.height; y++) for (int x = 0; x < g.width; x++)
                {
                    area a = g.boxes[y][x].lineup;
                    if (a.isAreaUsedInGame && a.isSet == false)
                        return false;

                    a = g.boxes[y][x].linedown;
                    if (a.isAreaUsedInGame && a.isSet == false)
                        return false;

                    a = g.boxes[y][x].lineleft;
                    if (a.isAreaUsedInGame && a.isSet == false)
                        return false;

                    a = g.boxes[y][x].lineright;
                    if (a.isAreaUsedInGame && a.isSet == false)
                        return false;
                }
            return true;
        }