Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using c++ The program will prompt the user for the filename of the game he or sh

ID: 3736507 • Letter: U

Question

using c++

The program will prompt the user for the filename of the game he or she is currently working on and display the board on the screen. The user will then be allowed to interact with the game by selecting which square he or she wishes to change. While the program will not solve the game for the user, it will ensure that the user has not selected an invalid number. If the user types 'S' in the prompt, then the program will show the user the possible valid numbers for a given square. When the user is finished, then the program will save the board to a given filename and exit.

Interface Design

Consider a game saved as myGame.txt:

Note that '0' corresponds to an unknown value. The following is an example run of the program.

Program starts

An example of input is underlined.

With the filename specified, the program will display a menu of options:

After this, the board as read from the file will be displayed:

Here, the user will be prompted for a command (the main prompt).

Please note that you will need a newline, a carat ('>'), and a space before the prompt.

The next action of the program will depend on the user's command. If the user types an invalid command, then the following message will be displayed:

Show Instructions

If the user types '?', then the menu of options will be displayed again. These are the same instructions that are displayed when the program is first run.

Display the Board

If the user types 'D', then the board will be redrawn. This is the same as the drawing of the board when the program is first run.

Save and Quit

If the user types 'Q', then he or she will be prompted for the filename:

The program will display the following message:

Then the program will terminate.

Edit One Square

If the user types 'E', then the program will prompt him for the coordinates and the value for the square to be edited:

If the value is invalid or if the square is already filled, then the program will display one of the following message and return to the main prompt:

With a valid coordinate, then the program next prompts the user for the value:

If the user types a value that is outside the accepted range (1 value 9) or does not follow the rules of Sudoku, then a message appears and the program returns to the main prompt:

Show Possible Values

If the user types 'S', then the program will prompt him for the coordinates and display the possible values:

The same parsing logic applies here as for the Edit One Square case. Once the user has selected a valid coordinate, then the program will display the possible values:

After the message appears, the program returns to the main prompt.

Explanation / Answer

import java.awt.*; import java.awt.event.*; import java.net.URL; import javax.swing.*; /** Test setting Swing's JComponents properties and appearances */ @SuppressWarnings("serial") public class SwingJComponentSetterTest extends JFrame { // Image path relative to the project root (i.e., bin) private String imgCrossFilename = "images/cross.gif"; private String imgNoughtFilename = "images/nought.gif"; /** Constructor to setup the GUI */ public SwingJComponentSetterTest() { // Prepare ImageIcons to be used with JComponents ImageIcon iconCross = null; ImageIcon iconNought = null; URL imgURL = getClass().getClassLoader().getResource(imgCrossFilename); if (imgURL != null) { iconCross = new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + imgCrossFilename); } imgURL = getClass().getClassLoader().getResource(imgNoughtFilename); if (imgURL != null) { iconNought = new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + imgNoughtFilename); } Container cp = getContentPane(); cp.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); // Create a JLabel with text and icon and set its appearances JLabel label = new JLabel("JLabel", iconCross, SwingConstants.CENTER); label.setFont(new Font(Font.DIALOG, Font.ITALIC, 14)); label.setOpaque(true); // needed for JLabel to show the background color label.setBackground(new Color(204, 238, 241)); // light blue label.setForeground(Color.RED); // foreground text color label.setPreferredSize(new Dimension(120, 80)); label.setToolTipText("This is a JLabel"); // Tool tip cp.add(label); // Create a JButton with text and icon and set its appearances JButton button = new JButton(); // use setter to set text and icon button.setText("Button"); button.setIcon(iconNought); button.setVerticalAlignment(SwingConstants.BOTTOM); // of text and icon button.setHorizontalAlignment(SwingConstants.RIGHT); // of text and icon button.setHorizontalTextPosition(SwingConstants.LEFT); // of text relative to icon button.setVerticalTextPosition(SwingConstants.TOP); // of text relative to icon button.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15)); button.setBackground(new Color(231, 240, 248)); button.setForeground(Color.BLUE); button.setPreferredSize(new Dimension(150, 80)); button.setToolTipText("This is a JButton"); button.setMnemonic(KeyEvent.VK_B); // can activate via Alt-B (buttons only) cp.add(button); // Create a JTextField with text and icon and set its appearances JTextField textField = new JTextField("Text Field", 15); textField.setFont(new Font(Font.DIALOG_INPUT, Font.PLAIN, 12)); textField.setForeground(Color.RED); textField.setHorizontalAlignment(JTextField.RIGHT); // Text alignment textField.setToolTipText("This is a JTextField"); cp.add(textField); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("JComponent Test"); setLocationRelativeTo(null); // center window on the screen setSize(500, 150); // or pack() setVisible(true); // Print description of the JComponents via toString() System.out.println(label); System.out.println(button); System.out.println(textField); } /** The entry main() method */ public static void main(String[] args) { // Run the GUI codes on Event-Dispatching thread for thread safety SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new SwingJComponentSetterTest(); // Let the constructor do the job } }); } } javax.swing.JLabel[, 41, 10, 120x80, alignmentX=0.0, alignmentY=0.0, border=, flags=25165832, maximumSize=, minimumSize=, preferredSize=java.awt.Dimension[width=120,height=80], defaultIcon=file:.../cross.gif, disabledIcon=, horizontalAlignment=CENTER, horizontalTextPosition=TRAILING, iconTextGap=4, labelFor=, text=JLabel, verticalAlignment=CENTER, verticalTextPosition=CENTER] javax.swing.JButton[, 171, 10, 150x80, alignmentX=0.0, alignmentY=0.5, border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@c5e2cf, flags=424, maximumSize=, minimumSize=, preferredSize=java.awt.Dimension[width=150,height=80], defaultIcon=file:/.../nought.gif, disabledIcon=, disabledSelectedIcon=, margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14], paintBorder=true, paintFocus=true, pressedIcon=, rolloverEnabled=true, rolloverIcon=, rolloverSelectedIcon=, selectedIcon=, text=Button, defaultCapable=true] javax.swing.JTextField[, 331, 39, 109x21, layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler, alignmentX=0.0, alignmentY=0.0, border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1991de1, flags=296, maximumSize=, minimumSize=, preferredSize=, caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51], disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229], editable=true, margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0], selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51], selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229], columns=15, columnWidth=7, command=, horizontalAlignment=RIGHT]