Ejemplo n.º 1
0
        public static void PrintBoard(eCell[,] i_Board, eCell i_TurnOf)
        {
            int lengthOfRowsAndColumns = (int)Math.Sqrt(i_Board.Length);
            Ex02.ConsoleUtils.Screen.Clear();
            printTurnOf(i_TurnOf);
            printColumnIndexes(lengthOfRowsAndColumns);
            printRowSaparator(lengthOfRowsAndColumns);
            for (int row = 0; row < lengthOfRowsAndColumns; row++)
            {
                Console.Write((row + 1).ToString() + " ");
                for (int column = 0; column < lengthOfRowsAndColumns; column++)
                {
                    if (i_Board[row, column] == eCell.Empty)
                    {
                        Console.Write("|   ");
                    }
                    else if (i_Board[row, column] == eCell.White)
                    {
                        Console.Write("| O ");
                    }
                    else if (i_Board[row, column] == eCell.Black)
                    {
                        Console.Write("| X ");
                    }
                }

                Console.WriteLine("|");
                printRowSaparator(lengthOfRowsAndColumns);
            }
        }
Ejemplo n.º 2
0
        private int getMinPointsAfterRivalTurn(List <Point> i_RivalLegalMoves, eCell[,] i_Board, eCell i_TurnOf)
        {
            int minPointsAfterRivalMove;

            int[] countPointsAfterRivalMove = new int[i_RivalLegalMoves.Count];
            eCell rivalColor = getOppositeColor(i_TurnOf);

            eCell[,] futureBoardInMove;
            for (int i = 0; i < countPointsAfterRivalMove.Length; i++)
            {
                futureBoardInMove = copyBoard(i_Board);
                makeUserMove(i_RivalLegalMoves[i], rivalColor, futureBoardInMove);
                countPointsAfterRivalMove[i] = getCurrentPoints(i_TurnOf, futureBoardInMove);
            }

            minPointsAfterRivalMove = countPointsAfterRivalMove[0];
            for (int i = 1; i < countPointsAfterRivalMove.Length; i++)
            {
                if (countPointsAfterRivalMove[i] < minPointsAfterRivalMove)
                {
                    minPointsAfterRivalMove = countPointsAfterRivalMove[i];
                }
            }

            return(minPointsAfterRivalMove);
        }
Ejemplo n.º 3
0
 private void endGame()
 {
     m_BlackPoints     = getPointsByColor(eCell.Red);
     m_WhitePoints     = getPointsByColor(eCell.Yellow);
     m_Winner          = setWinner();
     this.DialogResult = DialogResult.OK;
     this.Close();
 }
Ejemplo n.º 4
0
        public void Insert(int i_InsertColumn, eCell i_PlayerNumber)
        {
            PlayBoardMat[NextAvailableMoveArr[i_InsertColumn], i_InsertColumn] = i_PlayerNumber;
            NextAvailableMoveArr[i_InsertColumn]--;

            if (NextAvailableMoveArr[i_InsertColumn] == -1)
            {
                NumOfAvailableColumns--;
            }
        }
Ejemplo n.º 5
0
 private void makeUserMove(Point i_UserMove, eCell i_TurnOf, eCell[,] i_Board)
 {
     foreach (eDirection direction in Direction.AllDirections)
     {
         if (isLegalMove(direction, i_UserMove.X, i_UserMove.Y, i_TurnOf, i_Board))
         {
             makeMove(direction, i_UserMove.X, i_UserMove.Y, i_TurnOf, i_Board);
         }
     }
 }
Ejemplo n.º 6
0
 private void toggleTurn()
 {
     if (m_TurnOf == eCell.Black)
     {
         m_TurnOf = eCell.White;
     }
     else
     {
         m_TurnOf = eCell.Black;
     }
 }
Ejemplo n.º 7
0
 private void toggleTurn()
 {
     if (m_TurnOf == eCell.Red)
     {
         m_TurnOf = eCell.Yellow;
     }
     else
     {
         m_TurnOf = eCell.Red;
     }
 }
Ejemplo n.º 8
0
        private void makeMove(eDirection i_Direction, int i_Row, int i_Column, eCell i_TurnOf, eCell[,] i_Board)
        {
            eCell rivalColor = getOppositeColor(i_TurnOf);

            i_Board[i_Row, i_Column] = i_TurnOf;
            Direction.Move(i_Direction, ref i_Row, ref i_Column);
            while (i_Board[i_Row, i_Column] == rivalColor)
            {
                i_Board[i_Row, i_Column] = i_TurnOf;
                Direction.Move(i_Direction, ref i_Row, ref i_Column);
            }
        }
Ejemplo n.º 9
0
 private static void printTurnOf(eCell i_TurnOf)
 {
     PrintMassageLine(Messages.k_TurnOfMassage);
     if (i_TurnOf == eCell.Black)
     {
         PrintMassageLine(Messages.k_Black);
     }
     else
     {
         PrintMassageLine(Messages.k_White);
     }
 }
Ejemplo n.º 10
0
        private eCell[,] copyBoard(eCell[,] i_Board)
        {
            eCell[,] copyedBoard = new eCell[m_BoardSize, m_BoardSize];
            for (int i = 0; i < m_BoardSize; i++)
            {
                for (int j = 0; j < m_BoardSize; j++)
                {
                    copyedBoard[i, j] = i_Board[i, j];
                }
            }

            return(copyedBoard);
        }
Ejemplo n.º 11
0
        private int getPointsByColor(eCell i_Color)
        {
            int pointsCount = 0;

            foreach (eCell cell in m_GameBoardEngine.Board)
            {
                if (cell == i_Color)
                {
                    pointsCount++;
                }
            }

            return(pointsCount);
        }
Ejemplo n.º 12
0
 internal void SetLegalMoves(eCell i_TurnOf, eCell[,] i_board, List <Point> o_LegalMoves)
 {
     o_LegalMoves.Clear();
     for (int row = 0; row < m_BoardSize; row++)
     {
         for (int column = 0; column < m_BoardSize; column++)
         {
             if (i_board[row, column] == eCell.Empty)
             {
                 setLegalMovesByDirections(row, column, i_TurnOf, o_LegalMoves, i_board);
             }
         }
     }
 }
Ejemplo n.º 13
0
 private void setLegalMovesByDirections(int i_Row, int i_Column, eCell i_TurnOf, List <Point> o_LegalMoves, eCell[,] i_Board)
 {
     if (isLegalMove(eDirection.Down, i_Row, i_Column, i_TurnOf, i_Board) ||
         isLegalMove(eDirection.DownLeft, i_Row, i_Column, i_TurnOf, i_Board) ||
         isLegalMove(eDirection.DownRight, i_Row, i_Column, i_TurnOf, i_Board) ||
         isLegalMove(eDirection.Left, i_Row, i_Column, i_TurnOf, i_Board) ||
         isLegalMove(eDirection.Right, i_Row, i_Column, i_TurnOf, i_Board) ||
         isLegalMove(eDirection.Up, i_Row, i_Column, i_TurnOf, i_Board) ||
         isLegalMove(eDirection.UpLeft, i_Row, i_Column, i_TurnOf, i_Board) ||
         isLegalMove(eDirection.UpRight, i_Row, i_Column, i_TurnOf, i_Board))
     {
         o_LegalMoves.Add(new Point(i_Row, i_Column));
     }
 }
Ejemplo n.º 14
0
        private eCell getOppositeColor(eCell i_Color)
        {
            eCell oppositeColor;

            if (i_Color == eCell.Black)
            {
                oppositeColor = eCell.White;
            }
            else
            {
                oppositeColor = eCell.Black;
            }

            return(oppositeColor);
        }
Ejemplo n.º 15
0
        private eCell getOppositeColor(eCell i_Color)
        {
            eCell oppositeColor;

            if (i_Color == eCell.Red)
            {
                oppositeColor = eCell.Yellow;
            }
            else
            {
                oppositeColor = eCell.Red;
            }

            return(oppositeColor);
        }
Ejemplo n.º 16
0
        public bool TryInsert(int i_InsertColumn, out int o_rowInserted, eCell i_PlayerNumber)
        {
            bool flag = m_NextAvailableMoveArr[i_InsertColumn] >= 0;

            if (flag)
            {
                o_rowInserted = NextAvailableMoveArr[i_InsertColumn];
                Insert(i_InsertColumn, i_PlayerNumber);
            }
            else
            {
                o_rowInserted = -1;
            }

            return(flag);
        }
Ejemplo n.º 17
0
        private int getCurrentPoints(eCell i_TurnOf, eCell[,] i_Board)
        {
            int countPoints = 0;

            foreach (eCell cell in i_Board)
            {
                if (cell == i_TurnOf)
                {
                    countPoints++;
                }
                else if (cell != eCell.Empty)
                {
                    countPoints--;
                }
            }

            return(countPoints);
        }
Ejemplo n.º 18
0
        private bool isLegalMove(eDirection i_Direction, int i_Row, int i_Column, eCell i_TurnOf, eCell[,] i_Board)
        {
            bool  isLegal    = false;
            eCell rivalColor = getOppositeColor(i_TurnOf);

            Direction.Move(i_Direction, ref i_Row, ref i_Column);
            if (isValidCoordinates(i_Row, i_Column) && i_Board[i_Row, i_Column] == rivalColor)
            {
                while (isValidCoordinates(i_Row, i_Column) && i_Board[i_Row, i_Column] == rivalColor)
                {
                    Direction.Move(i_Direction, ref i_Row, ref i_Column);
                }

                if (isValidCoordinates(i_Row, i_Column) && i_Board[i_Row, i_Column] == i_TurnOf)
                {
                    isLegal = true;
                }
            }

            return(isLegal);
        }
Ejemplo n.º 19
0
        private int getIndexOfBestMoveFromLegalMoves(eCell i_TurnOf)
        {
            int bestMoveIndex;

            int[] countMovePoints = new int[m_LegalMoves.Count];
            for (int i = 0; i < m_LegalMoves.Count; i++)
            {
                countMovePoints[i] = getAmountOfAdittionalDiscsAfter2Moves(m_LegalMoves[i], i_TurnOf);
            }

            bestMoveIndex = 0;
            for (int i = 1; i < countMovePoints.Length; i++)
            {
                if (countMovePoints[i] > bestMoveIndex)
                {
                    bestMoveIndex = i;
                }
            }

            return(bestMoveIndex);
        }
Ejemplo n.º 20
0
        private void CellClick(object sender, EventArgs e)
        {
            if (!needPrepare)
            {
                return;
            }
            Control c = (Control)sender;

            if (!GetPanel().Contains(c))
            {
                return;
            }
            TableLayoutPanelCellPosition pos = GetPanel().GetCellPosition(c);

            info.Text = $"panel:{isPrepareFirstBoard},col:{pos.Column}, row:{pos.Row}";
            if (needPrepare)
            {
                PictureBox pictureBox = (PictureBox)sender;
                if (pictureBox.BackColor == Color.Green)
                {
                    ResetPreparedPictureBoxes(Color.White);
                    return;
                }
                List <eCell> cells       = preparedShip.Cells();
                eCell        newShipCell = new eCell(pos.Column - 1, pos.Row - 1);
                if (!cells.Contains(newShipCell))
                {
                    preparedShip.AddCell(newShipCell);
                    if (!preparedShip.IsValid())
                    {
                        ResetPreparedPictureBoxes(Color.White);
                        preparedShip.AddCell(newShipCell);
                    }
                    preparedShip.Cells(cells);
                    pictureBox.BackColor = Color.Yellow;
                }
            }
        }
Ejemplo n.º 21
0
        private int getAmountOfAdittionalDiscsAfter2Moves(Point i_PotentialMove, eCell i_TurnOf)
        {
            int amountOfAdittionalDiscs;
            int currentPoints = getCurrentPoints(i_TurnOf, m_Board);

            eCell[,] potentialFutureBoard = copyBoard(m_Board);
            makeUserMove(i_PotentialMove, i_TurnOf, potentialFutureBoard);
            eCell        rivalColor = getOppositeColor(i_TurnOf);
            List <Point> PotentialRivalLegalMoves = new List <Point>();

            SetLegalMoves(rivalColor, potentialFutureBoard, PotentialRivalLegalMoves);
            if (PotentialRivalLegalMoves.Count > 0)
            {
                int minPointsAfter2Moves = getMinPointsAfterRivalTurn(PotentialRivalLegalMoves, potentialFutureBoard, i_TurnOf);
                amountOfAdittionalDiscs = minPointsAfter2Moves - currentPoints;
            }
            else
            {
                amountOfAdittionalDiscs = currentPoints;
            }

            return(amountOfAdittionalDiscs);
        }
Ejemplo n.º 22
0
        private int getIndexOfBestMoveFromLegalMoves(eCell i_TurnOf)
        {
            GameUI.PrintMassageLine(GameUI.Messages.k_ComputersTurnMassage);
            Thread.Sleep(k_Milliseconds);
            int bestMoveIndex;

            int[] countMovePoints = new int[m_LegalMoves.Count];
            for (int i = 0; i < m_LegalMoves.Count; i++)
            {
                countMovePoints[i] = getAmountOfAdittionalDiscsAfter2Moves(m_LegalMoves[i], i_TurnOf);
            }

            bestMoveIndex = 0;
            for (int i = 1; i < countMovePoints.Length; i++)
            {
                if (countMovePoints[i] > bestMoveIndex)
                {
                    bestMoveIndex = i;
                }
            }

            return(bestMoveIndex);
        }
Ejemplo n.º 23
0
        private void playRematchGame()
        {
            string userInput = null;

            do
            {
                GameUI.PrintMassageLine(GameUI.Messages.k_IsRematchGameMassage);
                userInput = Console.ReadLine();
                if (userInput.ToLower() == GameUI.Messages.k_UserInputStringEqualsYes)
                {
                    m_CountBlack      = 0;
                    m_CountWhite      = 0;
                    m_TurnOf          = eCell.White;
                    m_GameBoardEngine = new GameBoardEngine(m_BoardSize);
                    startGame();
                }
                else if (userInput.ToLower() != GameUI.Messages.k_UserInputStringEqualsNo)
                {
                    GameUI.PrintMassageLine(GameUI.Messages.k_InvalidInputUserNameMassage);
                }
            }while (userInput.ToLower() != GameUI.Messages.k_UserInputStringEqualsNo);
            GameUI.EndGameMessage();
        }
Ejemplo n.º 24
0
        internal string GetUserName(eCell i_SearchNameByItColor)
        {
            string playerName;

            if (i_SearchNameByItColor == eCell.Black)
            {
                playerName = m_Player2;
            }
            else if (i_SearchNameByItColor == eCell.White)
            {
                playerName = m_Player1;
            }
            else
            {
                playerName = null;
            }

            if (playerName == null)
            {
                Console.WriteLine(GameUI.Messages.k_UserNameDidNotFoundErrorMassage);
            }

            return(playerName);
        }
Ejemplo n.º 25
0
 private void setLegalMovesByDirections(int i_Row, int i_Column, eCell i_TurnOf)
 {
     setLegalMovesByDirections(i_Row, i_Column, i_TurnOf, m_LegalMoves, m_Board);
 }
Ejemplo n.º 26
0
 private void makeMove(eDirection i_Direction, int i_Row, int i_Column, eCell i_TurnOf)
 {
     makeMove(i_Direction, i_Row, i_Column, i_TurnOf, m_Board);
 }
Ejemplo n.º 27
0
 internal void MakeUserMove(Point i_UserMove, eCell i_TurnOf)
 {
     makeUserMove(i_UserMove, i_TurnOf, m_Board);
 }
Ejemplo n.º 28
0
 private bool isLegalMove(eDirection i_Direction, int i_Row, int i_Column, eCell i_TurnOf)
 {
     return(isLegalMove(i_Direction, i_Row, i_Column, i_TurnOf, m_Board));
 }
Ejemplo n.º 29
0
        internal Point GetPcMove(eCell i_TurnOf)
        {
            int indexOfBestMove = getIndexOfBestMoveFromLegalMoves(i_TurnOf);

            return(m_LegalMoves[indexOfBestMove]);
        }
Ejemplo n.º 30
0
 internal void SetLegalMoves(eCell i_TurnOf)
 {
     SetLegalMoves(i_TurnOf, m_Board, m_LegalMoves);
 }