Exemplo n.º 1
0
        // CHECK FOR WINNER

        private static bool IsWinner(Form1 form1, CircularButton slot)
        {
            int   slotColumn = GetColumnNumber(slot.Name); // column where last token has been placed
            int   slotRow    = GetRowNumber(slot.Name);    // row where last token has been placed
            Color slotColor  = slot.BackColor;             // slot's color where last token has been placed



            // check vertically, horizontically and diagonally for winner
            if (Algorithm.Vertical_Bottom_Top(form1, slotColumn, slotRow, slotColor))
            {
                return(true);
            }

            if (Algorithm.Horizontal_Left_Right(form1, slotColumn, slotRow, slotColor))
            {
                return(true);
            }

            if (Algorithm.Diagonal_BottomLeft_TopRight(form1, slotColumn, slotRow, slotColor))
            {
                return(true);
            }

            if (Algorithm.Diagonal_TopLeft_BottomRight(form1, slotColumn, slotRow, slotColor))
            {
                return(true);
            }



            // no winner
            return(false);
        }
Exemplo n.º 2
0
        // PLACE TOKEN IN THE SLOT

        private static CircularButton PlaceTokenAndGetSlot(Form1 form1, object slotSender)
        {
            // the slot was clicked
            CircularButton slotClicked = slotSender as CircularButton;


            // the column number that slot was clicked
            int colClicked = GetColumnNumber(slotClicked.Name);


            // get all slots in ascending order (*from bottom to top)
            List <CircularButton> slotsFromClickedColumn = GetAllSlots(form1, colClicked);
            List <CircularButton> sortedSlots            = OrderBySlotsAsc(slotsFromClickedColumn);


            // this loop will start searching from bottom to top
            for (int i = 0; i < sortedSlots.Count; i++)
            {
                CircularButton slot = sortedSlots[i];

                // when the color is stil as its initial color, it means a token can be placed
                if (slot.BackColor == form1.slotColor)
                {
                    //make it blue if player 1
                    if (form1.currentPlayer == Form1.CurrentPlayer.P1)
                    {
                        slot.BackColor = Color.Blue; // the color for player 1

                        //form1.currentPlayer = Form1.CurrentPlayer.P2; // player's 2 turn
                        //form1.labelPlayerTurn.Text = form1.nameP2 + " is playing. . .";
                    }
                    // make it red if player 2
                    else
                    {
                        slot.BackColor = Color.Red; // the color for player 2

                        //form1.currentPlayer = Form1.CurrentPlayer.P1; // player's 1 turn
                        //form1.labelPlayerTurn.Text = form1.nameP1 + " is playing. . .";
                    }

                    // return the slot
                    return(slot);
                }


                //if not match, go to next slot in second row....
            }


            // if nothin, column means is full - return null
            return(null);
        }
Exemplo n.º 3
0
        public static void RUN(Form1 form1, object slotSender)
        {
            bool IsGameOver = false; //indicates whether game is done



            // PLACE THE TOKEN IN THE SLOT AND GET ITS SLOT BACK
            CircularButton slot = PlaceTokenAndGetSlot(form1, slotSender);



            // CHECK FOR WINNER
            if (slot == null)
            {
                Console.WriteLine("COLUMN IS FULL");
            }
            else
            {
                if (IsWinner(form1, slot))
                {
                    if (form1.currentPlayer == Form1.CurrentPlayer.P1)
                    {
                        Console.WriteLine("P1 WON");

                        // +1 win to P1
                        int currentWins = Convert.ToInt32(form1.labelWinsP1.Text);
                        form1.labelWinsP1.Text = (currentWins + 1).ToString();

                        form1.labelPlayerTurn.Text = form1.nameP1 + " WON THE GAME !!!";
                    }
                    else
                    {
                        Console.WriteLine("P2 one win");

                        // +1 win to P2
                        int currentWins = Convert.ToInt32(form1.labelWinsP2.Text);
                        form1.labelWinsP2.Text = (currentWins + 1).ToString();

                        form1.labelPlayerTurn.Text = form1.nameP2 + " WON THE GAME !!!";
                    }


                    // game stops
                    FreezeGrid(form1);
                    IsGameOver = true;
                }
                else
                {
                    Console.WriteLine("NO WINNER FOUND");
                }
            }



            // UPDATES CURRENT PLAYER OTHERWISE GAME IS OVER
            if (!IsGameOver && !IsGridFilled(form1) && slot != null)
            {
                if (form1.currentPlayer == Form1.CurrentPlayer.P1)
                {
                    form1.currentPlayer        = Form1.CurrentPlayer.P2;
                    form1.labelPlayerTurn.Text = form1.nameP2 + " is playing. . .";
                }
                else
                {
                    form1.currentPlayer        = Form1.CurrentPlayer.P1;
                    form1.labelPlayerTurn.Text = form1.nameP1 + " is playing. . .";
                }
            }
            else if (IsGridFilled(form1))
            {
                Console.WriteLine("ALL SLOTS FULL");

                form1.labelPlayerTurn.Text = "DRAW  -  GAME IS OVER !!!";
                FreezeGrid(form1);
            }
        }
        public static Panel GetGrid(Form1 form1, int gridCols, int gridRows, int gridX, int gridY, Color gridColor, Color slotColor, bool showColRowOnSlot)
        {
            Panel grid = new Panel
            {
                Location  = new Point(gridX, gridY), // the location for the grid
                BackColor = gridColor                // the background color for grid
            };

            int gridPadding       = 10;   // a padding to the grid
            int slotWidth         = 55;   // the width for the slot
            int slotHeight        = 55;   // the height for the slot
            int slotDistanceScale = 20;   // a scale for the distance between the slots



            // CREATE THE SLOTS AND ADD THEM TO GRID

            int currentGridWidth  = 0;           // the width of the grid
            int currentGridHeight = 0;           // the height o fthe gird
            int currentSlotY      = gridPadding; // the location Y for slot

            for (int row = gridRows; row > 0; row--)
            {
                int currentSlotX = gridPadding; // the location Y for slot

                for (int col = 1; col <= gridCols; col++)
                {
                    //create slot
                    CircularButton slot = new CircularButton
                    {
                        Size      = new Size(slotWidth, slotHeight),
                        Location  = new Point(currentSlotX, currentSlotY),
                        Name      = string.Format("{0},{1}", col, row),
                        BackColor = slotColor,
                        FlatStyle = FlatStyle.Flat
                    };
                    slot.FlatAppearance.BorderSize = 0;
                    if (showColRowOnSlot)
                    {
                        slot.Text = string.Format("{0},{1}", col, row);
                    }

                    //add slot to grid
                    grid.Controls.Add(slot);

                    //update next slot location X
                    currentSlotX += slotWidth + slotDistanceScale;

                    //update grid width
                    currentGridWidth = currentSlotX;
                }

                //update next slot location Y
                currentSlotY += slotHeight + slotDistanceScale;

                //update grid height
                currentGridHeight = currentSlotY;
            }



            // set the final size for grid
            int finalGridWidth  = currentGridWidth - slotDistanceScale + gridPadding;
            int finalGridHeight = currentGridHeight - slotDistanceScale + gridPadding;

            grid.Size = new Size(finalGridWidth, finalGridHeight);



            // return the grid
            return(grid);
        }