示例#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);
        }
示例#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;
            }
        }
示例#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);
        }
示例#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);
        }
        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);
        }
示例#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);
            }
        }
示例#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);
            }
        }
示例#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);
        }
示例#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);
            }
        }
示例#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);
            }
        }
示例#11
0
 public static void endGame(game g, gamepanel gp)
 {
     g.completeGame = true;
     MessageBox.Show("Game Over!");
 }
示例#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);
            }
        }
示例#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);
        }
示例#14
0
 public static void endGame(game g,gamepanel gp)
 {
     g.completeGame = true;
     MessageBox.Show("Game Over!");
 }