private void save() {
        JFileChooser fc = new JFileChooser();
        File currentDir = new File(".");
        File defaultFile = new File(currentDir, "Untitled." + FILE_EXTENSION);
        boolean saving = true;
        
        while(saving) {
            fc.setSelectedFile(defaultFile);
            int result = fc.showSaveDialog(this);
            if(result == JFileChooser.APPROVE_OPTION) {
                File saveFile = fc.getSelectedFile();
                boolean doSave = true;
                if(saveFile.exists()) {
                    String message = "The file " + saveFile.getPath() + " already exists. Are you sure you want to replace it?";
                    int replaceResult = JOptionPane.showConfirmDialog(this, message, "File exists", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null);
                    if(replaceResult == JOptionPane.CANCEL_OPTION) {
                        doSave = false;
                    }
                }
                if(doSave) {
                    try {
                        FileWriter w = new FileWriter(saveFile);
                        w.write(CAFile.write(this.getGrid().getGame()));
                        w.close();
                        this.setTitle(saveFile.getName());
                        saving = false;
                    } catch(FileNotFoundException e) {
                        JOptionPane.showMessageDialog(this, "Error saving file: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    } catch(IOException e) {
                        JOptionPane.showMessageDialog(this, "Input/output error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    defaultFile = saveFile;
                }
            } else {
                saving = false;
            }
        }
    }