void Initialise()
 {
     //Display Database in order of wins descending
     using (var db = new GameLeaderboard2Entities())
     {
         mainLeaderboards = db.MainLeaderboard2.ToList();
     }
     Leaderboard.ItemsSource = mainLeaderboards.OrderByDescending(i => i.Wins);
 }
        private void BeginGame_Click(object sender, RoutedEventArgs e)
        {
            //Create new rows in database when button is clicked
            if (Player1Name.Text == "" || Player2Name.Text == "")
            {
                MessageBox.Show("Make sure both players have put their names in");
            }
            else
            {
                string player1name = Player1Name.Text;
                string player2name = Player2Name.Text;

                int p1wins   = 0;
                int p1losses = 0;
                int p2wins   = 0;
                int p2losses = 0;

                using (var db = new GameLeaderboard2Entities())
                {
                    MainLeaderboard2 newplayer1 = new MainLeaderboard2();
                    newplayer1.PlayerName = Player1Name.Text;
                    newplayer1.Wins       = p1wins;
                    newplayer1.Losses     = p1losses;
                    db.MainLeaderboard2.Add(newplayer1);

                    MainLeaderboard2 newplayer2 = new MainLeaderboard2();
                    newplayer2.PlayerName = Player2Name.Text;
                    newplayer2.Wins       = p2wins;
                    newplayer2.Losses     = p2losses;
                    db.MainLeaderboard2.Add(newplayer2);
                    db.SaveChanges();
                }
            }

            //Open Main Game Window
            Game game = new Game();

            this.Visibility = Visibility.Hidden;
            game.Show();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Check if there is a winner of a 3 line straight
        /// </summary>
        private void CheckForAWinner()
        {
            //Check for Horizontal Wins

            #region Horizontal Wins
            //Row 0
            if (nResults[0] != MarkType.Free && (nResults[0] & nResults[1] & nResults[2]) == nResults[0])
            {
                //Game Ends
                GameHasEnded = true;

                //Print Out Winner
                if (nResults[0] == nResults[1] && nResults[1] == nResults[2] && nResults[2] == MarkType.Cross)
                {
                    Player1Winner = true;
                }
                else if (nResults[0] == nResults[1] && nResults[1] == nResults[2] && nResults[2] == MarkType.Nought)
                {
                    Player1Winner = false;
                }

                //Highlight winning cells in green
                Button0_0.Background = Button1_0.Background = Button2_0.Background = Brushes.Green;

                //Update the database
                using (var db = new GameLeaderboard2Entities())
                {
                    if (Player1Winner == true)
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Wins++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Losses++;
                        db.SaveChanges();
                    }
                    else
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Losses++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Wins++;
                        db.SaveChanges();
                    }
                }
            }

            //Row 1
            if (nResults[3] != MarkType.Free && (nResults[3] & nResults[4] & nResults[5]) == nResults[3])
            {
                //Game Ends
                GameHasEnded = true;


                //Print Out Winner
                if (nResults[3] == nResults[4] && nResults[4] == nResults[5] && nResults[5] == MarkType.Cross)
                {
                    Player1Winner = true;
                }
                else if (nResults[3] == nResults[4] && nResults[4] == nResults[5] && nResults[5] == MarkType.Nought)
                {
                    Player1Winner = false;
                }

                //Highlight winning cells in green
                Button0_1.Background = Button1_1.Background = Button2_0.Background = Brushes.Green;

                //Update the database
                using (var db = new GameLeaderboard2Entities())
                {
                    if (Player1Winner == true)
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Wins++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Losses++;
                        db.SaveChanges();
                    }
                    else
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Losses++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Wins++;
                        db.SaveChanges();
                    }
                }
            }

            //Row 2
            if (nResults[6] != MarkType.Free && (nResults[6] & nResults[7] & nResults[8]) == nResults[6])
            {
                //Game Ends
                GameHasEnded = true;


                //Print Out Winner
                if (nResults[6] == nResults[7] && nResults[7] == nResults[8] && nResults[8] == MarkType.Cross)
                {
                    Player1Winner = true;
                }
                else if (nResults[6] == nResults[7] && nResults[7] == nResults[8] && nResults[8] == MarkType.Nought)
                {
                    Player1Winner = false;
                }

                //Highlight winning cells in green
                Button0_2.Background = Button1_2.Background = Button2_2.Background = Brushes.Green;

                //Update the database
                using (var db = new GameLeaderboard2Entities())
                {
                    if (Player1Winner == true)
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Wins++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Losses++;
                        db.SaveChanges();
                    }
                    else
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Losses++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Wins++;
                        db.SaveChanges();
                    }
                }
            }
            #endregion

            #region Vertical Wins

            //Column 1
            if (nResults[0] != MarkType.Free && (nResults[0] & nResults[3] & nResults[6]) == nResults[0])
            {
                //Game Ends
                GameHasEnded = true;


                //Print Out Winner
                if (nResults[0] == nResults[3] && nResults[3] == nResults[6] && nResults[6] == MarkType.Cross)
                {
                    Player1Winner = true;
                }
                else if (nResults[0] == nResults[3] && nResults[3] == nResults[6] && nResults[6] == MarkType.Nought)
                {
                    Player1Winner = false;
                }

                //Highlight winning cells in green
                Button0_0.Background = Button0_1.Background = Button0_2.Background = Brushes.Green;

                //Update the database
                using (var db = new GameLeaderboard2Entities())
                {
                    if (Player1Winner == true)
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Wins++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Losses++;
                        db.SaveChanges();
                    }
                    else
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Losses++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Wins++;
                        db.SaveChanges();
                    }
                }
            }

            //Column 2
            if (nResults[1] != MarkType.Free && (nResults[1] & nResults[4] & nResults[7]) == nResults[1])
            {
                //Game Ends
                GameHasEnded = true;


                //Print Out Winner
                if (nResults[1] == nResults[4] && nResults[4] == nResults[7] && nResults[7] == MarkType.Cross)
                {
                    Player1Winner = true;
                }
                else if (nResults[1] == nResults[4] && nResults[4] == nResults[7] && nResults[7] == MarkType.Nought)
                {
                    Player1Winner = false;
                }

                //Highlight winning cells in green
                Button1_0.Background = Button1_1.Background = Button1_2.Background = Brushes.Green;


                //Update the database
                using (var db = new GameLeaderboard2Entities())
                {
                    if (Player1Winner == true)
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Wins++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Losses++;
                        db.SaveChanges();
                    }
                    else
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Losses++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Wins++;
                        db.SaveChanges();
                    }
                }
            }

            //Column 3
            if (nResults[2] != MarkType.Free && (nResults[2] & nResults[5] & nResults[8]) == nResults[2])
            {
                //Game Ends
                GameHasEnded = true;


                //Print Out Winner
                if (nResults[2] == nResults[5] && nResults[5] == nResults[8] && nResults[8] == MarkType.Cross)
                {
                    Player1Winner = true;
                }
                else if (nResults[2] == nResults[5] && nResults[5] == nResults[8] && nResults[8] == MarkType.Nought)
                {
                    Player1Winner = false;
                }

                //Highlight winning cells in green
                Button2_0.Background = Button2_1.Background = Button2_2.Background = Brushes.Green;


                //Update the database
                using (var db = new GameLeaderboard2Entities())
                {
                    if (Player1Winner == true)
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Wins++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Losses++;
                        db.SaveChanges();
                    }
                    else
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Losses++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Wins++;
                        db.SaveChanges();
                    }
                }
            }
            #endregion

            #region Diagonal Wins

            //Diagonal 1
            if (nResults[0] != MarkType.Free && (nResults[0] & nResults[4] & nResults[8]) == nResults[0])
            {
                //Game Ends
                GameHasEnded = true;


                //Print Out Winner
                if (nResults[0] == nResults[4] && nResults[4] == nResults[8] && nResults[8] == MarkType.Cross)
                {
                    Player1Winner = true;
                }
                else if (nResults[0] == nResults[4] && nResults[4] == nResults[8] && nResults[8] == MarkType.Nought)
                {
                    Player1Winner = false;
                }

                //Highlight winning cells in green
                Button0_0.Background = Button1_1.Background = Button2_2.Background = Brushes.Green;


                //Update the database
                using (var db = new GameLeaderboard2Entities())
                {
                    if (Player1Winner == true)
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Wins++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Losses++;
                        db.SaveChanges();
                    }
                    else
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Losses++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Wins++;
                        db.SaveChanges();
                    }
                }
            }

            //Diagonal 2
            if (nResults[2] != MarkType.Free && (nResults[2] & nResults[4] & nResults[6]) == nResults[2])
            {
                //Game Ends
                GameHasEnded = true;


                //Print Out Winner
                if (nResults[2] == nResults[4] && nResults[4] == nResults[6] && nResults[6] == MarkType.Cross)
                {
                    Player1Winner = true;
                }
                else if (nResults[2] == nResults[4] && nResults[4] == nResults[6] && nResults[6] == MarkType.Nought)
                {
                    Player1Winner = false;
                }

                //Highlight winning cells in green
                Button2_0.Background = Button1_1.Background = Button0_2.Background = Brushes.Green;


                //Update the database
                using (var db = new GameLeaderboard2Entities())
                {
                    if (Player1Winner == true)
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Wins++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Losses++;
                        db.SaveChanges();
                    }
                    else
                    {
                        MainLeaderboard2 updateplayer1 = new MainLeaderboard2();
                        updateplayer1 = db.MainLeaderboard2.OrderByDescending(i => i.Id).Skip(1).First();
                        updateplayer1.Losses++;

                        MainLeaderboard2 updateplayer2 = new MainLeaderboard2();
                        updateplayer2 = db.MainLeaderboard2.OrderByDescending(i => i.Id).First();
                        updateplayer2.Wins++;
                        db.SaveChanges();
                    }
                }
            }
            #endregion

            #region No Winners

            //Check for no winner and fill board
            if (!nResults.Any(item => item == MarkType.Free))
            {
                //Game Ended
                GameHasEnded = true;

                //Turn all cells orange
                Container.Children.Cast <Button>().ToList().ForEach(button =>
                {
                    button.Background = Brushes.Orange;
                });
            }
            #endregion
        }