Exemplo n.º 1
0
        public static void GetStep(char[,] board, int count)
        {
            bool            answer = false;
            List <Position> steps  = new List <Position>();

            steps.Add(new Position {
                Row = 0, Column = 1
            });
            steps.Add(new Position {
                Row = 1, Column = 0
            });
            steps.Add(new Position {
                Row = -1, Column = -1
            });
            steps.Add(new Position {
                Row = 1, Column = -1
            });
            steps.Add(new Position {
                Row = -1, Column = 1
            });
            steps.Add(new Position {
                Row = 1, Column = 1
            });
            steps.Add(new Position {
                Row = 0, Column = -1
            });
            steps.Add(new Position {
                Row = -1, Column = 0
            });
            List <Position>  positionsX         = Coordinat.Get(board, 'x');
            List <Hindrance> hindrancesForThree = Counter.CountForThree(board, 'x');
            List <Hindrance> hindrancesForFour  = Counter.CountForFour(board, 'x');
            List <Hindrance> easyWin            = Counter.CountForFour(board, 'o');

            if (count == 1)
            {
                int      num  = RandomNumber.Get(0, 7);
                Position step = steps[num];
                board[positionsX.First().Row + step.Row, positionsX.First().Column + step.Column] = 'o';
            }
            else
            {
                bool answer2 = false;
                if (easyWin.Count != 0 && answer2 != true)
                {
                    foreach (var list in easyWin)
                    {
                        if (list.Case == true)
                        {
                            int rowF    = list.RowFirst - list.StepR;
                            int columnF = list.ColumnFirst - list.StepC;
                            if (rowF >= 0 && rowF <= 14 && columnF >= 0 && columnF <= 14 && board[rowF, columnF] == '_')
                            {
                                board[rowF, columnF] = 'o';
                                list.Case            = false;
                                answer2 = true;
                                break;
                            }
                            else
                            {
                                int rowL    = list.RowLast + list.StepR;
                                int columnL = list.ColumnLast + list.StepC;
                                if (rowL >= 0 && rowL <= 14 && columnL >= 0 && columnL <= 14 && board[rowL, columnL] == '_')
                                {
                                    board[rowL, columnL] = 'o';
                                    list.Case            = false;
                                    answer2 = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (hindrancesForFour.Count != 0 && answer2 != true)
                {
                    foreach (var list in hindrancesForFour)
                    {
                        if (list.Case == true)
                        {
                            int rowF    = list.RowFirst - list.StepR;
                            int columnF = list.ColumnFirst - list.StepC;
                            if (rowF >= 0 && rowF <= 14 && columnF >= 0 && columnF <= 14 && board[rowF, columnF] == '_')
                            {
                                board[rowF, columnF] = 'o';
                                list.Case            = false;
                                answer2 = true;
                                break;
                            }
                            else
                            {
                                int rowL    = list.RowLast + list.StepR;
                                int columnL = list.ColumnLast + list.StepC;
                                if (rowL >= 0 && rowL <= 14 && columnL >= 0 && columnL <= 14 && board[rowL, columnL] == '_')
                                {
                                    board[rowL, columnL] = 'o';
                                    list.Case            = false;
                                    answer2 = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (hindrancesForThree.Count != 0 && answer2 != true)
                {
                    foreach (var list in hindrancesForThree)
                    {
                        if (list.Case == true)
                        {
                            int rowF    = list.RowFirst - list.StepR;
                            int columnF = list.ColumnFirst - list.StepC;
                            if (rowF >= 0 && rowF <= 14 && columnF >= 0 && columnF <= 14 && board[rowF, columnF] == '_')
                            {
                                board[rowF, columnF] = 'o';
                                list.Case            = false;
                                answer2 = true;
                                break;
                            }
                            else
                            {
                                int rowL    = list.RowLast + list.StepR;
                                int columnL = list.ColumnLast + list.StepC;
                                if (rowL >= 0 && rowL <= 14 && columnL >= 0 && columnL <= 14 && board[rowL, columnL] == '_')
                                {
                                    board[rowL, columnL] = 'o';
                                    list.Case            = false;
                                    answer2 = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (answer2 != true)
                {
                    List <Position> positionsForX = Coordinat.Get(board, 'o');
                    int             k             = 0;
                    bool            answer3       = false;
                    while (answer3 != true)
                    {
                        k++;
                        if (k < 8)
                        {
                            int num    = RandomNumber.Get(0, 7);
                            int row    = positionsForX.Last().Row + steps[num].Row;
                            int column = positionsForX.Last().Column + steps[num].Column;
                            if (row >= 0 && row <= 14 && column >= 0 && column <= 14)
                            {
                                if (board[row, column] == '_')
                                {
                                    board[row, column] = 'o';
                                    answer             = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            List <Position> positionsFree = Coordinat.Get(board, '_');
                            if (positionsFree.Count > 0)
                            {
                                int num = RandomNumber.Get(0, positionsFree.Count - 1);
                                board[positionsFree[num].Row, positionsFree[num].Column] = 'o';
                                answer = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static List <Hindrance> CountForFour(char[,] board, char symbol)
        {
            List <Hindrance> four = new List <Hindrance>();
            int size            = Coordinat.Get(board, symbol).Count;
            var positionsForRow = Coordinat.Get(board, symbol).OrderBy(i => i.Row).ThenBy(i => i.Column);

            for (int i = 0; i < size; i++)
            {
                int k = 1;
                for (int j = 0; j < size; j++)
                {
                    if (i != j && positionsForRow.ElementAt(i).Column == positionsForRow.ElementAt(j).Column - k && positionsForRow.ElementAt(i).Row == positionsForRow.ElementAt(j).Row)
                    {
                        k++;
                        if (k == 4)
                        {
                            four.Add(new Hindrance {
                                Case = true, RowFirst = positionsForRow.ElementAt(i).Row, RowLast = positionsForRow.ElementAt(i).Row, ColumnFirst = positionsForRow.ElementAt(i).Column, ColumnLast = positionsForRow.ElementAt(i).Column + 2, StepR = 0, StepC = 1
                            });
                        }
                    }
                }
            }
            //----------------------
            var positionsForColumn = Coordinat.Get(board, symbol).OrderBy(i => i.Column).ThenBy(i => i.Row);

            for (int i = 0; i < size; i++)
            {
                int k = 1;
                for (int j = 0; j < size; j++)
                {
                    if (i != j && positionsForColumn.ElementAt(i).Column == positionsForColumn.ElementAt(j).Column&& positionsForColumn.ElementAt(i).Row == positionsForColumn.ElementAt(j).Row - k)
                    {
                        k++;
                        if (k == 4)
                        {
                            four.Add(new Hindrance {
                                Case = true, RowFirst = positionsForColumn.ElementAt(i).Row, RowLast = positionsForColumn.ElementAt(i).Row + 2, ColumnFirst = positionsForColumn.ElementAt(i).Column, ColumnLast = positionsForColumn.ElementAt(i).Column, StepC = 0, StepR = 1
                            });
                        }
                    }
                }
            }
            //-----------------------------------------------------------------------------------------------------
            List <Position> firstForDiogonal = new List <Position>();

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if ((positionsForRow.ElementAt(i).Row == positionsForRow.ElementAt(j).Row - 1 && positionsForRow.ElementAt(i).Column == positionsForRow.ElementAt(j).Column - 1))
                    {
                        firstForDiogonal.Add(new Position {
                            Row = positionsForRow.ElementAt(i).Row, Column = positionsForRow.ElementAt(i).Column
                        });
                        firstForDiogonal.Add(new Position {
                            Row = positionsForRow.ElementAt(j).Row, Column = positionsForRow.ElementAt(j).Column
                        });
                    }
                }
            }
            for (int i = 0; i < firstForDiogonal.Count; i++)
            {
                for (int j = 0; j < firstForDiogonal.Count; j++)
                {
                    if (firstForDiogonal[i].Row == firstForDiogonal[j].Row && firstForDiogonal[i].Column == firstForDiogonal[j].Column && i != j)
                    {
                        firstForDiogonal.RemoveAt(i);
                    }
                }
            }
            for (int i = 0; i < firstForDiogonal.Count - 3; i++)
            {
                int k = 1;
                for (int j = 0; j < firstForDiogonal.Count; j++)
                {
                    if (i != j && firstForDiogonal.ElementAt(i).Column == firstForDiogonal.ElementAt(j).Column - k && firstForDiogonal.ElementAt(i).Row == firstForDiogonal.ElementAt(j).Row - k)
                    {
                        k++;
                        if (k == 4)
                        {
                            four.Add(new Hindrance {
                                Case = true, ColumnFirst = firstForDiogonal[i].Column, ColumnLast = firstForDiogonal[i].Column + 2, RowFirst = firstForDiogonal[i].Row, RowLast = firstForDiogonal[i].Row + 2, StepC = 1, StepR = 1
                            });
                        }
                    }
                }
            }
            //-----------------------------------------------------------------
            List <Position> secondForDiogonal = new List <Position>();

            positionsForRow = positionsForRow.OrderByDescending(i => i.Row);
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if ((positionsForRow.ElementAt(i).Row == positionsForRow.ElementAt(j).Row + 1 && positionsForRow.ElementAt(i).Column == positionsForRow.ElementAt(j).Column - 1))
                    {
                        secondForDiogonal.Add(new Position {
                            Row = positionsForRow.ElementAt(i).Row, Column = positionsForRow.ElementAt(i).Column
                        });
                        secondForDiogonal.Add(new Position {
                            Row = positionsForRow.ElementAt(j).Row, Column = positionsForRow.ElementAt(j).Column
                        });
                    }
                }
            }
            for (int i = 0; i < secondForDiogonal.Count; i++)
            {
                for (int j = 0; j < secondForDiogonal.Count; j++)
                {
                    if (secondForDiogonal[i].Row == secondForDiogonal[j].Row && secondForDiogonal[i].Column == secondForDiogonal[j].Column && i != j)
                    {
                        secondForDiogonal.RemoveAt(i);
                    }
                }
            }
            for (int i = 0; i < secondForDiogonal.Count - 3; i++)
            {
                int k = 1;
                for (int j = 0; j < secondForDiogonal.Count; j++)
                {
                    if (i != j && secondForDiogonal[i].Column == secondForDiogonal[j].Column - k && secondForDiogonal[i].Row == secondForDiogonal[j].Row + k)
                    {
                        k++;
                        if (k == 4)
                        {
                            four.Add(new Hindrance {
                                Case = true, ColumnFirst = secondForDiogonal[i].Column, ColumnLast = secondForDiogonal[i].Column + 3, RowFirst = secondForDiogonal[i].Row, RowLast = secondForDiogonal[i].Row - 3, StepC = 1, StepR = -1
                            });
                        }
                    }
                }
            }

            return(four);
        }
Exemplo n.º 3
0
        public static bool Search(char[,] board, char symbol)
        {
            bool mainAnswer      = false;
            int  size            = Coordinat.Get(board, symbol).Count;
            var  positionsForRow = Coordinat.Get(board, symbol).OrderBy(i => i.Row).ThenBy(i => i.Column);

            for (int i = 0; i < size; i++)
            {
                int k = 1;
                for (int j = 0; j < size; j++)
                {
                    if (i != j && positionsForRow.ElementAt(i).Column == positionsForRow.ElementAt(j).Column - k && positionsForRow.ElementAt(i).Row == positionsForRow.ElementAt(j).Row)
                    {
                        k++;
                        if (k == 5)
                        {
                            mainAnswer = true;
                            //Console.WriteLine("1 " + symbol);
                        }
                    }
                }
            }
            //------------------------------------------------------------------------------------------------------------
            var positionsForColumn = Coordinat.Get(board, symbol).OrderBy(i => i.Column).ThenBy(i => i.Row);

            for (int i = 0; i < size; i++)
            {
                int k = 1;
                for (int j = 0; j < size; j++)
                {
                    if (i != j && positionsForColumn.ElementAt(i).Column == positionsForColumn.ElementAt(j).Column&& positionsForColumn.ElementAt(i).Row == positionsForColumn.ElementAt(j).Row - k)
                    {
                        k++;
                        if (k == 5)
                        {
                            mainAnswer = true;
                            //Console.WriteLine("2 " + symbol);
                        }
                    }
                }
            }
            //----------------------------------------------------------------------------------------------------------------
            List <Position> firstForDiogonal = new List <Position>();
            var             positionsOne     = Coordinat.Get(board, symbol).OrderBy(i => i.Row).ThenBy(i => i.Column);

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if ((positionsOne.ElementAt(i).Row == positionsOne.ElementAt(j).Row - 1 && positionsOne.ElementAt(i).Column == positionsOne.ElementAt(j).Column - 1))
                    {
                        firstForDiogonal.Add(new Position {
                            Row = positionsOne.ElementAt(i).Row, Column = positionsOne.ElementAt(i).Column
                        });
                        firstForDiogonal.Add(new Position {
                            Row = positionsOne.ElementAt(j).Row, Column = positionsOne.ElementAt(j).Column
                        });
                    }
                }
            }
            for (int i = 0; i < firstForDiogonal.Count; i++)
            {
                for (int j = 0; j < firstForDiogonal.Count; j++)
                {
                    if (firstForDiogonal[i].Row == firstForDiogonal[j].Row && firstForDiogonal[i].Column == firstForDiogonal[j].Column && i != j)
                    {
                        firstForDiogonal.RemoveAt(i);
                    }
                }
            }
            var firstForDiogonalTwo = firstForDiogonal.OrderBy(i => i.Row);

            for (int i = 0; i < firstForDiogonal.Count - 4; i++)
            {
                int k = 1;
                for (int j = 0; j < firstForDiogonal.Count; j++)
                {
                    if (i != j && firstForDiogonalTwo.ElementAt(i).Column == firstForDiogonalTwo.ElementAt(j).Column - k && firstForDiogonalTwo.ElementAt(i).Row == firstForDiogonalTwo.ElementAt(j).Row - k)
                    {
                        k++;
                        if (k == 5)
                        {
                            mainAnswer = true;
                            //Console.WriteLine("3 " + symbol);
                        }
                    }
                }
            }
            //----------------------------------------------------------------------------------------------------
            List <Position> secondForDiogonal = new List <Position>();
            var             positions         = Coordinat.Get(board, symbol).OrderByDescending(i => i.Row);

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if ((positions.ElementAt(i).Row == positions.ElementAt(j).Row + 1 && positions.ElementAt(i).Column == positions.ElementAt(j).Column - 1))
                    {
                        secondForDiogonal.Add(new Position {
                            Row = positions.ElementAt(i).Row, Column = positions.ElementAt(i).Column
                        });
                        secondForDiogonal.Add(new Position {
                            Row = positions.ElementAt(j).Row, Column = positions.ElementAt(j).Column
                        });
                    }
                }
            }
            for (int i = 0; i < secondForDiogonal.Count; i++)
            {
                for (int j = 0; j < secondForDiogonal.Count; j++)
                {
                    if (secondForDiogonal[i].Row == secondForDiogonal[j].Row && secondForDiogonal[i].Column == secondForDiogonal[j].Column && i != j)
                    {
                        secondForDiogonal.RemoveAt(i);
                    }
                }
            }
            for (int i = 0; i < secondForDiogonal.Count - 4; i++)
            {
                int k = 1;
                for (int j = 0; j < secondForDiogonal.Count; j++)
                {
                    if (i != j && secondForDiogonal[i].Column == secondForDiogonal[j].Column - k && secondForDiogonal[i].Row == secondForDiogonal[j].Row + k)
                    {
                        k++;
                        if (k == 5)
                        {
                            mainAnswer = true;
                            // Console.WriteLine("4 " + symbol);
                        }
                    }
                }
            }
            return(mainAnswer);
        }
Exemplo n.º 4
0
        public static List <Hindrance> CountForThree(char[,] board, char symbol)
        {
            List <Hindrance> three = new List <Hindrance>();
            int size            = Coordinat.Get(board, symbol).Count;
            var positionsForRow = Coordinat.Get(board, symbol).OrderBy(i => i.Row).ThenBy(i => i.Column);

            for (int i = 0; i < size; i++)
            {
                int k = 1;
                for (int j = 0; j < size; j++)
                {
                    if (i != j && positionsForRow.ElementAt(i).Column == positionsForRow.ElementAt(j).Column - k && positionsForRow.ElementAt(i).Row == positionsForRow.ElementAt(j).Row)
                    {
                        k++;
                        if (k == 3)
                        {
                            three.Add(new Hindrance {
                                Case = true, RowFirst = positionsForRow.ElementAt(i).Row, RowLast = positionsForRow.ElementAt(i).Row, ColumnFirst = positionsForRow.ElementAt(i).Column, ColumnLast = positionsForRow.ElementAt(i).Column + 2, StepR = 0, StepC = 1
                            });
                        }
                    }
                }
                //int count = 1;
                //for (int j = 0; j < size; j++)
                //{
                //    if (positionsForRow.ElementAt(i).Row == positionsForRow.ElementAt(j).Row && i != j)
                //    {
                //        count+=1;
                //        if (count == 3)
                //            break;
                //    }
                //}
                //if (i< size - 2 && count == 3 && positionsForRow.ElementAt(i).Column == positionsForRow.ElementAt(i + 1).Column - 1 && positionsForRow.ElementAt(i).Column == positionsForRow.ElementAt(i + 2).Column - 2)
                //{
                //    three.Add(new Hindrance { Case = true,RowFirst = positionsForRow.ElementAt(i).Row, RowLast = positionsForRow.ElementAt(i).Row, ColumnFirst = positionsForRow.ElementAt(i).Column, ColumnLast = positionsForRow.ElementAt(i).Column + 2, StepR = 0, StepC = 1 }) ;
                //}
            }
            //----------------------
            var positionsForColumn = Coordinat.Get(board, symbol).OrderBy(i => i.Column).ThenBy(i => i.Row);

            for (int i = 0; i < size; i++)
            {
                int k = 1;
                for (int j = 0; j < size; j++)
                {
                    if (i != j && positionsForColumn.ElementAt(i).Column == positionsForColumn.ElementAt(j).Column&& positionsForColumn.ElementAt(i).Row == positionsForColumn.ElementAt(j).Row - k)
                    {
                        k++;
                        if (k == 3)
                        {
                            three.Add(new Hindrance {
                                Case = true, RowFirst = positionsForColumn.ElementAt(i).Row, RowLast = positionsForColumn.ElementAt(i).Row + 2, ColumnFirst = positionsForColumn.ElementAt(i).Column, ColumnLast = positionsForColumn.ElementAt(i).Column, StepC = 0, StepR = 1
                            });
                        }
                    }
                }
                //int count2 = 1;
                //for (int j = 0; j < size; j++)
                //{
                //    if (positionsForColumn.ElementAt(i).Column == positionsForColumn.ElementAt(j).Column && i != j)
                //    {
                //        count2 += 1;
                //        if (count2 == 3)
                //            break;
                //    }
                //}
                //if (i < size - 2 && count2 == 3 && positionsForColumn.ElementAt(i).Row == positionsForColumn.ElementAt(i + 1).Row - 1 && positionsForColumn.ElementAt(i).Row == positionsForColumn.ElementAt(i + 2).Row - 2)
                //{
                //    three.Add(new Hindrance {Case=true, RowFirst = positionsForColumn.ElementAt(i).Row, RowLast = positionsForColumn.ElementAt(i).Row + 2, ColumnFirst = positionsForColumn.ElementAt(i).Column, ColumnLast = positionsForColumn.ElementAt(i).Column, StepC = 0, StepR = 1 });
                //}
            }
            //--------------------------------------------------------------
            List <Position> firstForDiogonal = new List <Position>();

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if ((positionsForRow.ElementAt(i).Row == positionsForRow.ElementAt(j).Row - 1 && positionsForRow.ElementAt(i).Column == positionsForRow.ElementAt(j).Column - 1))
                    {
                        firstForDiogonal.Add(new Position {
                            Row = positionsForRow.ElementAt(i).Row, Column = positionsForRow.ElementAt(i).Column
                        });
                        firstForDiogonal.Add(new Position {
                            Row = positionsForRow.ElementAt(j).Row, Column = positionsForRow.ElementAt(j).Column
                        });
                    }
                }
            }
            for (int i = 0; i < firstForDiogonal.Count; i++)
            {
                for (int j = 0; j < firstForDiogonal.Count; j++)
                {
                    if (firstForDiogonal[i].Row == firstForDiogonal[j].Row && firstForDiogonal[i].Column == firstForDiogonal[j].Column && i != j)
                    {
                        firstForDiogonal.RemoveAt(i);
                    }
                }
            }
            for (int i = 0; i < firstForDiogonal.Count - 2; i++)
            {
                int k = 1;
                for (int j = 0; j < firstForDiogonal.Count; j++)
                {
                    if (i != j && firstForDiogonal.ElementAt(i).Column == firstForDiogonal.ElementAt(j).Column - k && firstForDiogonal.ElementAt(i).Row == firstForDiogonal.ElementAt(j).Row - k)
                    {
                        k++;
                        if (k == 3)
                        {
                            three.Add(new Hindrance {
                                Case = true, ColumnFirst = firstForDiogonal[i].Column, ColumnLast = firstForDiogonal[i].Column + 2, RowFirst = firstForDiogonal[i].Row, RowLast = firstForDiogonal[i].Row + 2, StepC = 1, StepR = 1
                            });
                        }
                    }
                }
            }
            //--------------------------------------------------------------
            List <Position> secondForDiogonal = new List <Position>();

            positionsForRow = positionsForRow.OrderByDescending(i => i.Row);
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if ((positionsForRow.ElementAt(i).Row == positionsForRow.ElementAt(j).Row + 1 && positionsForRow.ElementAt(i).Column == positionsForRow.ElementAt(j).Column - 1))
                    {
                        secondForDiogonal.Add(new Position {
                            Row = positionsForRow.ElementAt(i).Row, Column = positionsForRow.ElementAt(i).Column
                        });
                        secondForDiogonal.Add(new Position {
                            Row = positionsForRow.ElementAt(j).Row, Column = positionsForRow.ElementAt(j).Column
                        });
                    }
                }
            }
            for (int i = 0; i < secondForDiogonal.Count; i++)
            {
                for (int j = 0; j < secondForDiogonal.Count; j++)
                {
                    if (secondForDiogonal[i].Row == secondForDiogonal[j].Row && secondForDiogonal[i].Column == secondForDiogonal[j].Column && i != j)
                    {
                        secondForDiogonal.RemoveAt(i);
                    }
                }
            }
            for (int i = 0; i < secondForDiogonal.Count - 2; i++)
            {
                int k = 1;
                for (int j = 0; j < secondForDiogonal.Count; j++)
                {
                    if (i != j && secondForDiogonal[i].Column == secondForDiogonal[j].Column - k && secondForDiogonal[i].Row == secondForDiogonal[j].Row + k)
                    {
                        k++;
                        if (k == 3)
                        {
                            three.Add(new Hindrance {
                                Case = true, ColumnFirst = secondForDiogonal[i].Column, ColumnLast = secondForDiogonal[i].Column + 3, RowFirst = secondForDiogonal[i].Row, RowLast = secondForDiogonal[i].Row - 3, StepC = 1, StepR = -1
                            });
                        }
                    }
                }
            }
            return(three);
        }