private void btnOpenSave_Click(object sender, RoutedEventArgs e) { // Creating \Pentegames directory so there is no error System.IO.Directory.CreateDirectory(@"\CardGameGallery\Go Fish"); OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = @"C:\CardGameGallery\Go Fish"; openFileDialog.Multiselect = true; openFileDialog.Filter = "Go Fish Saves|*.GoFish"; //openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (openFileDialog.ShowDialog() == true) { try { // Get the file path chosen by the user stream = new FileStream(@$ "{openFileDialog.FileName}", FileMode.Open, FileAccess.Read); // Deserializing the game save file to a GameSave object save = (GoFishSaveGame)formatter.Deserialize(stream); // Opening the play window with the GameSave object playGoFishWindow = new PlayGoFishWindow(save, openFileDialog.FileName, this); // Hiding main menu withle play window is up Hide(); // Displaying the play window playGoFishWindow.Show(); // CLosing the stream to free up resources stream.Close(); } catch { // If an error occurs while opening save file MessageBox.Show("Something went wrong, try again."); } } }
// Is called as the window is closing private void btnPlay_Click(object sender, RoutedEventArgs e) { if (!((bool)rb1.IsChecked | (bool)rb2.IsChecked | (bool)rb3.IsChecked | (bool)rb4.IsChecked)) { MessageBox.Show("Please select number of players"); return; } this.Hide(); // If only one human player is playing if (numOfPlayers == 1) { // Creating a new player with witht he parameters (name, isAi, number of captures, is player AI?) playersList.Add(new GoFishPlayer(txtName1.Text)); playersList.Add(new GoFishPlayer(txtName2.Text, true)); // If there is only one human player there must be an ai } else if (numOfPlayers == 2) { playersList.Add(new GoFishPlayer(txtName1.Text)); playersList.Add(new GoFishPlayer(txtName2.Text, (bool)p2.IsChecked)); } else if (numOfPlayers == 3) { playersList.Add(new GoFishPlayer(txtName1.Text)); playersList.Add(new GoFishPlayer(txtName2.Text, (bool)p2.IsChecked)); playersList.Add(new GoFishPlayer(txtName3.Text, (bool)p2.IsChecked)); } else if (numOfPlayers == 4) { playersList.Add(new GoFishPlayer(txtName1.Text)); playersList.Add(new GoFishPlayer(txtName2.Text, (bool)p2.IsChecked)); playersList.Add(new GoFishPlayer(txtName3.Text, (bool)p2.IsChecked)); playersList.Add(new GoFishPlayer(txtName4.Text, (bool)p2.IsChecked)); } // Creating a new play window, the game will carry on there playGoFishWindow = new PlayGoFishWindow(playersList, this); Hide(); // Hiding the main menu while the play window is up playGoFishWindow.Show(); }