Please read my question if you post copy and pasted code you will get a negative
ID: 3836963 • Letter: P
Question
Please read my question if you post copy and pasted code you will get a negative review.
In this graphical program, you are going to simulate arcade car racing game. You need a single player that is movable with the arrow keys, and you need another vehicle that will race the player. You should be able to race by hitting the right arrow key toward the end of the screen. If you get to the end of the screen, the car should start over on the left side of the screen. The AI car should start over but should move faster this time. Each time you make it to the end of the screen before the AI, you gain a point. If the AI makes it before you, the game is over. You should be able to play the game for 30 seconds. (i.e. you should survive for at least 30 seconds). Don’t worry about collision detection with the AI car.
At the end of the simulation, you should gather the person’s name, their favorite number, and their email address. You should check to make sure it is correctly formatted.
Specifics:
Gather the person’s name.
Gather the person’s favorite number – should be a number.
Collect the individual’s email – should have an @ and the ". " operator.
Should pre-emptively check all those values as well as have exception handlers where
appropriate.
Should have a countdown clock that starts at 30 and counts down to 0.
The AI car should race the player until the clock hits zero.
The player object should be able to move using the arrow keys.
Ignore collision detection.
For any exception handlers, you should write any exceptions to a log file.
Explanation / Answer
import java.awt.*; // Uses AWT's Layout Managers
import java.awt.event.*; // Uses AWT's Event Handlers
import javax.swing.*; // Uses Swing's Container/Components
/**
* The Sudoku game.
* to unravel the quantity puzzle, each row, every column, and every of the
* 9 3×3 sub-grids shall contain all of the digits from one to nine
*/
public category Sudoku extends JFrame the sport properties
public static final int GRID_SIZE = 9; // Size of the board
public static final int SUBGRID_SIZE = 3; // Size of the sub-grid
// Name-constants for UI management (sizes, colours and fonts)
public static final int CELL_SIZE = 60; // Cell width/height in pixels
public static final int CANVAS_WIDTH = CELL_SIZE * GRID_SIZE;
public static final int CANVAS_HEIGHT = CELL_SIZE * GRID_SIZE;
// Board width/height in pixels
public static final Color OPEN_CELL_BGCOLOR = Color.YELLOW;
public static final Color OPEN_CELL_TEXT_YES = new Color(0, 255, 0); // RGB
public static final Color OPEN_CELL_TEXT_NO = Color.RED;
public static final Color CLOSED_CELL_BGCOLOR = new Color(240, 240, 240); // RGB
public static final Color CLOSED_CELL_TEXT = Color.BLACK;
public static final Font FONT_NUMBERS = new Font("Monospaced", Font.BOLD, 20);
// the sport board composes of 9x9 JTextFields,
// every containing String "1" to "9", or empty String
personal JTextField[][] tfCells = new JTextField[GRID_SIZE][GRID_SIZE];
// Puzzle to be resolved and also the mask (which is accustomed management the
// issue level).
// Hardcoded here. further credit for automatic puzzle generation
// with varied issue levels.
personal int[][] puzzle =
,
,
,
,
,
,
,
,
};
// For testing, open solely a pair of cells.
personal boolean[][] masks =
,
,
,
,
,
,
,
,
};
/**
* creator to setup the sport and also the UI parts
*/
public Sudoku() instrumentality cp = getContentPane();
cp.setLayout(new GridLayout(GRID_SIZE, GRID_SIZE)); // 9x9 GridLayout
// allot a typical auditor because the ActionEvent auditor for all the
// JTextFields
// ... [TODO 3] (Later) ....
// Construct 9x9 JTextFields and augment the content-pane
for (int row = 0; row < GRID_SIZE; ++row) mountain pass = 0; mountain pass < GRID_SIZE; ++col) {
tfCells[row][col] = new JTextField(); // allot part of array
cp.add(tfCells[row][col]); // ContentPane adds JTextField
if (masks[row][col]) {
tfCells[row][col].setText(""); // set to empty string
tfCells[row][col].setEditable(true);
tfCells[row][col].setBackground(OPEN_CELL_BGCOLOR);
// Add ActionEvent auditor to method the input
// ... [TODO 4] (Later) ...
} else guide on a way to run the creator
.......
}
// outline the auditor Inner category
// ... [TODO 2] (Later) ...
}
3. Event Handling
Next, we have a tendency to shall program the event handling.
We shall use a typical instance of a Named Inner category (called InputListener) because the ActionEvent auditor for "all" the editable JTextFields. Hence, within the actionPerformed(), we want to spot the actual JTextField (in terms of row and col) that trigger the event. you'll use the ActionEvent.getSource() methodology to retrieve the supply object that has dismissed the event and compare the article with all the 9×9 JTextFields.
Place the inner category at [TODO 2].
// [TODO 2]
// Inner category to be used as ActionEvent auditor for ALL JTextFields
personal category InputListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// All the 9*9 JTextFileds invoke this handler. we want to work out
// that JTextField (which row and column) is that the supply for this invocation.
int rowSelected = -1;
int colSelected = -1;
// Get the supply object that dismissed the event
JTextField supply = (JTextField)e.getSource();
// Scan JTextFileds for all rows and columns, and match with the supply object
Boolean found = false;
for (int row = 0; row < GRID_SIZE && !found; ++row) {
for (int mountain pass = 0; mountain pass < GRID_SIZE && !found; ++col)
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.