예제 #1
0
        public void Test_CHeCkwin_Outcome_Nought_Vertical()
        {
            CheckForWinner check = new CheckForWinner();

            MarkType[] mResults = new MarkType[25];

            for (int i = 0; i < 15; i++)
            {
                mResults[i]      = MarkType.Nought;
                mResults[i + 5]  = MarkType.Nought;
                mResults[i + 10] = MarkType.Nought;

                var mark  = check.CheckWin(mResults);
                var value = MarkType.Nought;
                Assert.AreEqual(mark, value);
            }
        }
예제 #2
0
        public void Test_CheCkwin_Outcome_Nought_DiagonalRight()
        {
            MarkType[]     mResults = new MarkType[25];
            CheckForWinner check    = new CheckForWinner();

            for (int i = 2; i < 15; i++)
            {
                if (i % 5 >= 2)
                {
                    mResults[i]     = MarkType.Nought;
                    mResults[i + 4] = MarkType.Nought;
                    mResults[i + 8] = MarkType.Nought;

                    var mark  = check.CheckWin(mResults);
                    var value = MarkType.Nought;

                    Assert.AreEqual(mark, value);
                }
            }
        }
예제 #3
0
        public void Test_CheCkwin_Outcome_Nought_Horizontal()
        {
            CheckForWinner check = new CheckForWinner();

            MarkType[] mResults = new MarkType[25];

            for (int i = 0; i < 23; i++)
            {
                if (i % 5 < 3)
                {
                    mResults[i]     = MarkType.Nought;
                    mResults[i + 1] = MarkType.Nought;
                    mResults[i + 2] = MarkType.Nought;

                    var mark  = check.CheckWin(mResults);
                    var value = MarkType.Nought;
                    Assert.AreEqual(mark, value);
                }
            }
        }
예제 #4
0
        public void Test_CheCkwin_Outcome_Cross_DiagonalLeft()
        {
            MarkType[]     mResults = new MarkType[25];
            CheckForWinner check    = new CheckForWinner();

            for (int i = 12; i < 25; i++)
            {
                if (i % 5 >= 2)
                {
                    mResults[i]      = MarkType.Cross;
                    mResults[i - 6]  = MarkType.Cross;
                    mResults[i - 12] = MarkType.Cross;

                    var mark  = check.CheckWin(mResults);
                    var value = MarkType.Cross;

                    Assert.AreEqual(mark, value);
                }
            }
        }
예제 #5
0
        /// <summary>
        /// Handles a button click events
        /// </summary>
        /// <param name="sender">button that was clicked</param>
        /// <param name="e">events of clicks</param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Start a new game on the click after it is finished
            if (mGameEnded)
            {
                NewGame();
                return;
            }

            // Cast a sender to a button
            var button = (Button)sender;

            // Find the buttons position in array
            var column = Grid.GetColumn(button);
            var row    = Grid.GetRow(button);

            var index = column + (row * 5);

            // dont do anythig if cell has already a value
            if (mResults[index] != MarkType.Free)
            {
                return;
            }

            // seting a value in cell based on which turn is it
            mResults[index] = mPlayer1Turn ? MarkType.Cross : MarkType.Nought;

            // seting button text
            button.Content = mPlayer1Turn ? "X" : "O";

            // change button color
            if (!mPlayer1Turn)
            {
                button.Foreground = Brushes.Red;
            }

            // toggle players turns, bool's (instead if statement)
            mPlayer1Turn ^= true;


            // check for win with returning MarkType
            CheckForWinner CheckWin = new CheckForWinner();
            var            mark     = CheckWin.CheckWin(mResults);


            if (mark != MarkType.Free)
            {
                button.Background = Brushes.Orange;
                mGameEnded        = true;
                if (mark == MarkType.Nought)
                {
                    Container.Children.Cast <Button>().ToList().ForEach(item =>
                    {
                        item.Background = Brushes.Red;
                    });
                }
                else
                {
                    Container.Children.Cast <Button>().ToList().ForEach(item =>
                    {
                        item.Background = Brushes.Blue;
                    });
                }
            }

            // if none of mResluts is free, the game has ended
            if (!mResults.Any(result => result == MarkType.Free))
            {
                // game ended
                mGameEnded = true;

                // turn all cells orange
                Container.Children.Cast <Button>().ToList().ForEach(item =>
                {
                    item.Background = Brushes.Black;
                });
            }
        }