Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public void resetPlayerColours()
        {
            foreach (player p in players)
            {
                Color rand = getRandomColour();
                //change existing colours on the board
                foreach (area a in lines)
                {
                    if (a.refpanel.BackColor == p.colour)
                    {
                        a.refpanel.BackColor = rand;
                    }

                    foreach (box b in a.parents)
                    {
                        if (b.thisBox.refpanel.BackColor == p.colour)
                        {
                            b.thisBox.refpanel.BackColor = rand;
                        }
                    }
                    gamepanel gp = gamecontroller.getGamePanel(a);

                    if (gp.currentplayercolour.BackColor == p.colour)
                    {
                        gp.currentplayercolour.BackColor = rand;
                    }
                }
                p.colour = rand;
            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
            }
        }
Ejemplo n.º 7
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);
            }
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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);
            }
        }
Ejemplo n.º 10
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);
            }
        }
Ejemplo n.º 11
0
 public static void endGame(game g, gamepanel gp)
 {
     g.completeGame = true;
     MessageBox.Show("Game Over!");
 }
Ejemplo n.º 12
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);
            }
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
0
 public static void endGame(game g,gamepanel gp)
 {
     g.completeGame = true;
     MessageBox.Show("Game Over!");
 }