Esempio n. 1
0
        //***************** EVENTS *****************
        private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            int    r      = Grid.GetRow(sender as Canvas);
            int    c      = Grid.GetColumn(sender as Canvas);
            Canvas canvas = (sender as Canvas);

            if (gridMatrix[r, c].Equals((int)Cell.Empty))
            {
                Circle newCircle = new Circle(25);
                if (playerTurn == 2)    //2 -> WHITE's    1-> BLACK's(AI)
                {
                    if (IsValidMove(r, c))
                    {
                        gridMatrix[r, c] = (int)Cell.White;
                        newCircle.Draw(canvas, Brushes.White);
                        Flip(flipCells, flipCount);
                        playerTurn = 1;

                        if (ai && playerTurn == 1)
                        {
                            List <FCell> posCells = GetAIPossibleCells();
                            FCell        best     = GetBestCell(posCells);
                            int          flipC    = best.FlipCount;

                            if (IsValidMove(best.Row, best.Column))
                            {
                                gridMatrix[best.Row, best.Column] = (int)Cell.Black;
                                Canvas canv = new Canvas();
                                Grid.SetRow(canv, best.Row);
                                Grid.SetColumn(canv, best.Column);
                                myGrid.Children.Add(canv);
                                Circle newCirc = new Circle(25);
                                newCirc.Draw(canv, Brushes.Black);
                                Flip(flipCells, flipC);
                            }
                            playerTurn = 2;
                        }
                    }
                }
                else
                {
                    if (!ai)
                    {
                        if (IsValidMove(r, c))
                        {
                            gridMatrix[r, c] = (int)Cell.Black;
                            newCircle.Draw(canvas, Brushes.Black);
                            Flip(flipCells, flipCount);
                            playerTurn = 2;
                        }
                    }
                }
                RefreshUI();
            }
        }
Esempio n. 2
0
        //Game initialization
        public void InitGame(int playerTurn, string playerOneName, string playerTwoName, bool ai)
        {
            this.playerTurn    = playerTurn;
            this.playerOneName = playerOneName;
            this.playerTwoName = playerTwoName;
            this.ai            = ai;

            whitePoints = 0;
            blackPoints = 0;
            WP          = 0;
            BP          = 0;

            isStarted = true;

            WhitePointsLabel.Content = $"White({playerOneName}):";
            BlackPointsLabel.Content = $"Black({playerTwoName}):";

            CreateGrid();
            RefreshUI();

            if (playerTurn == 2)
            {
                MessageBox.Show($"WHITE ({playerOneName}) starts!");
            }
            else
            {
                MessageBox.Show($"BLACK ({playerTwoName}) starts!");
            }

            ticked = 0;
            dispatcherTimer.Start();

            if (ai && playerTurn == 1)
            {
                List <FCell> posCells = GetAIPossibleCells();
                FCell        best     = GetBestCell(posCells);
                int          flipC    = best.FlipCount;

                if (IsValidMove(best.Row, best.Column))
                {
                    Flip(flipCells, flipC);
                    Canvas canv = new Canvas();
                    Grid.SetRow(canv, best.Row);
                    Grid.SetColumn(canv, best.Column);
                    myGrid.Children.Add(canv);
                    Circle newCircle = new Circle(25);
                    newCircle.Draw(canv, Brushes.Black);
                    gridMatrix[best.Row, best.Column] = (int)Cell.Black;
                }
                playerTurn = 2;
            }
        }
Esempio n. 3
0
    public FCell DeepCopy()
    {
        var tmp374 = new FCell();

        if ((C != null) && __isset.c)
        {
            tmp374.C = this.C;
        }
        tmp374.__isset.c = this.__isset.c;
        if (__isset.ts)
        {
            tmp374.Ts = this.Ts;
        }
        tmp374.__isset.ts = this.__isset.ts;
        if ((V != null) && __isset.v)
        {
            tmp374.V = this.V.ToArray();
        }
        tmp374.__isset.v = this.__isset.v;
        return(tmp374);
    }
Esempio n. 4
0
        private FCell GetBestCell(List <FCell> cells)
        {
            int   max = 0;
            FCell f   = new FCell();

            foreach (FCell cell in cells)
            {
                if (cell.FlipCount >= max)
                {
                    max = cell.FlipCount;
                }
            }

            foreach (FCell c in cells)
            {
                if (max == c.FlipCount)
                {
                    f.Row       = c.Row;
                    f.Column    = c.Column;
                    f.FlipCount = c.FlipCount;
                }
            }
            return(f);
        }
Esempio n. 5
0
        private bool IsValidMove(int row, int column)
        {
            flipCount = 0;
            bool valid = true;

            flipCells.Clear();
            //  cellsToFlip.Clear();

            if (gridMatrix[row, column].Equals((int)Cell.Empty))
            {
                List <int> possibleDirections = new List <int>();
                possibleDirections = GetPossibleDirections(row, column, playerTurn);

                if (possibleDirections.Count == 0)
                {
                    return(false);
                }

                //0 1 2
                //3 X 4 -> X = Selected cell
                //5 6 7

                flipCount = 0;
                foreach (int item in possibleDirections)
                {
                    if (item == 0)  //UP_LEFT
                    {
                        int r = row;
                        int c = column - 1;
                        for (int i = r - 1; i >= 0; i--)
                        {
                            int cc = c--;
                            if (cc >= 0)
                            {
                                if (gridMatrix[i, cc].Equals(playerTurn))
                                {
                                    break;
                                }
                                else
                                {
                                    flipCount++;
                                    FCell fcell = new FCell(i, cc);
                                    if (!flipCells.Contains(fcell))
                                    {
                                        flipCells.Add(fcell);
                                    }
                                }
                            }
                        }
                    }
                    else if (item == 1)  //UP
                    {
                        for (int i = row - 1; i >= 0; i--)
                        {
                            if (gridMatrix[i, column].Equals(playerTurn))
                            {
                                break;
                            }
                            else
                            {
                                flipCount++;
                                FCell fcell = new FCell(i, column);
                                if (!flipCells.Contains(fcell))
                                {
                                    flipCells.Add(fcell);
                                }
                            }
                        }
                    }
                    else if (item == 2)  //UP_RIGHT
                    {
                        int r = row;
                        int c = column + 1;
                        for (int i = r - 1; i >= 0; i--)
                        {
                            int cc = c++;
                            if (cc < 8)
                            {
                                if (gridMatrix[i, cc].Equals(playerTurn))
                                {
                                    break;
                                }
                                else
                                {
                                    flipCount++;
                                    FCell fcell = new FCell(i, cc);
                                    if (!flipCells.Contains(fcell))
                                    {
                                        flipCells.Add(fcell);
                                    }
                                }
                            }
                        }
                    }
                    else if (item == 3)  //LEFT
                    {
                        for (int i = column - 1; i >= 0; i--)
                        {
                            if (gridMatrix[row, i].Equals(playerTurn))
                            {
                                break;
                            }
                            else
                            {
                                flipCount++;
                                FCell fcell = new FCell(row, i);
                                if (!flipCells.Contains(fcell))
                                {
                                    flipCells.Add(fcell);
                                }
                            }
                        }
                    }
                    else if (item == 4)  //RIGHT
                    {
                        for (int i = column + 1; i < 7; i++)
                        {
                            if (gridMatrix[row, i].Equals(playerTurn))
                            {
                                break;
                            }
                            else
                            {
                                flipCount++;
                                FCell fcell = new FCell(row, i);
                                if (!flipCells.Contains(fcell))
                                {
                                    flipCells.Add(fcell);
                                }
                            }
                        }
                    }
                    else if (item == 5)  //DOWN_LEFT
                    {
                        int r = row;
                        int c = column - 1;
                        for (int i = r + 1; i < 8; i++)
                        {
                            int cc = c--;
                            if (cc >= 0)
                            {
                                if (gridMatrix[i, cc].Equals(playerTurn))
                                {
                                    break;
                                }
                                else
                                {
                                    flipCount++;
                                    FCell fcell = new FCell(i, cc);
                                    if (!flipCells.Contains(fcell))
                                    {
                                        flipCells.Add(fcell);
                                    }
                                }
                            }
                        }
                    }
                    else if (item == 6)  //DOWN
                    {
                        for (int i = row + 1; i < 8; i++)
                        {
                            if (gridMatrix[i, column].Equals(playerTurn))
                            {
                                break;
                            }
                            else
                            {
                                flipCount++;
                                FCell fcell = new FCell(i, column);
                                if (!flipCells.Contains(fcell))
                                {
                                    flipCells.Add(fcell);
                                }
                            }
                        }
                    }
                    else if (item == 7)  //DOWN_RIGHT
                    {
                        int r = row;
                        int c = column + 1;
                        for (int i = r + 1; i < 8; i++)
                        {
                            int cc = c++;
                            if (cc < 8)
                            {
                                if (gridMatrix[i, cc].Equals(playerTurn))
                                {
                                    break;
                                }
                                else
                                {
                                    flipCount++;
                                    FCell fcell = new FCell(i, cc);
                                    if (!flipCells.Contains(fcell))
                                    {
                                        flipCells.Add(fcell);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (flipCount > 0)
            {
                valid = true;
            }
            else
            {
                valid = false;
            }

            return(valid);
        }
Esempio n. 6
0
    public async global::System.Threading.Tasks.Task ReadAsync(TProtocol iprot, CancellationToken cancellationToken)
    {
        iprot.IncrementRecursionDepth();
        try
        {
            TField field;
            await iprot.ReadStructBeginAsync(cancellationToken);

            while (true)
            {
                field = await iprot.ReadFieldBeginAsync(cancellationToken);

                if (field.Type == TType.Stop)
                {
                    break;
                }

                switch (field.ID)
                {
                case 1:
                    if (field.Type == TType.Map)
                    {
                        {
                            TMap _map389 = await iprot.ReadMapBeginAsync(cancellationToken);

                            F = new Dictionary <byte[], FCells>(_map389.Count);
                            for (int _i390 = 0; _i390 < _map389.Count; ++_i390)
                            {
                                byte[] _key391;
                                FCells _val392;
                                _key391 = await iprot.ReadBinaryAsync(cancellationToken);

                                _val392 = new FCells();
                                await _val392.ReadAsync(iprot, cancellationToken);

                                F[_key391] = _val392;
                            }
                            await iprot.ReadMapEndAsync(cancellationToken);
                        }
                    }
                    else
                    {
                        await TProtocolUtil.SkipAsync(iprot, field.Type, cancellationToken);
                    }
                    break;

                case 2:
                    if (field.Type == TType.List)
                    {
                        {
                            TList _list393 = await iprot.ReadListBeginAsync(cancellationToken);

                            Cells = new List <FCell>(_list393.Count);
                            for (int _i394 = 0; _i394 < _list393.Count; ++_i394)
                            {
                                FCell _elem395;
                                _elem395 = new FCell();
                                await _elem395.ReadAsync(iprot, cancellationToken);

                                Cells.Add(_elem395);
                            }
                            await iprot.ReadListEndAsync(cancellationToken);
                        }
                    }
                    else
                    {
                        await TProtocolUtil.SkipAsync(iprot, field.Type, cancellationToken);
                    }
                    break;

                case 3:
                    if (field.Type == TType.List)
                    {
                        {
                            TList _list396 = await iprot.ReadListBeginAsync(cancellationToken);

                            Serial_cells = new List <FCellSerial>(_list396.Count);
                            for (int _i397 = 0; _i397 < _list396.Count; ++_i397)
                            {
                                FCellSerial _elem398;
                                _elem398 = new FCellSerial();
                                await _elem398.ReadAsync(iprot, cancellationToken);

                                Serial_cells.Add(_elem398);
                            }
                            await iprot.ReadListEndAsync(cancellationToken);
                        }
                    }
                    else
                    {
                        await TProtocolUtil.SkipAsync(iprot, field.Type, cancellationToken);
                    }
                    break;

                default:
                    await TProtocolUtil.SkipAsync(iprot, field.Type, cancellationToken);

                    break;
                }

                await iprot.ReadFieldEndAsync(cancellationToken);
            }

            await iprot.ReadStructEndAsync(cancellationToken);
        }
        finally
        {
            iprot.DecrementRecursionDepth();
        }
    }