// If user wants to open a previously saved game private void openSave_Click(object sender, RoutedEventArgs e) { // Creating \Pentegames directory so there is no error System.IO.Directory.CreateDirectory(@"\PenteGames"); OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = @"C:\PenteGames"; openFileDialog.Multiselect = true; openFileDialog.Filter = "Pente Saves|*.pente"; 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 gs = (GameSave)formatter.Deserialize(stream); // Opening the play window with the GameSave object playWindow = new PlayWindow(gs, openFileDialog.FileName, this); // Hiding main menu withle play window is up Hide(); // Displaying the play window playWindow.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."); } } }
private void playBtn_Click(object sender, RoutedEventArgs e) { if (!((bool)numP1.IsChecked | (bool)numP2.IsChecked | (bool)numP3.IsChecked | (bool)numP4.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, color, number of captures, is player AI?) playersList.Add(new Player(txtName1.Text, 1, 0)); playersList.Add(new Player(txtName2.Text, 2, 0, true)); // If there is only one human player there must be an ai } else if (numOfPlayers == 2) { playersList.Add(new Player(txtName1.Text, 1, 0)); playersList.Add(new Player(txtName2.Text, 2, 0, (bool)p2.IsChecked)); } else if (numOfPlayers == 3) { playersList.Add(new Player(txtName1.Text, 1, 0)); playersList.Add(new Player(txtName2.Text, 2, 0, (bool)p2.IsChecked)); playersList.Add(new Player(txtName3.Text, 3, 0, (bool)p2.IsChecked)); } else if (numOfPlayers == 4) { playersList.Add(new Player(txtName1.Text, 1, 0)); playersList.Add(new Player(txtName2.Text, 2, 0, (bool)p2.IsChecked)); playersList.Add(new Player(txtName3.Text, 3, 0, (bool)p2.IsChecked)); playersList.Add(new Player(txtName4.Text, 4, 0, (bool)p2.IsChecked)); } // Creating a new play window, the game will carry on there playWindow = new PlayWindow(playersList, this); Hide(); // Hiding the main menu while the play window is up playWindow.Show(); }