add simple number game logic
This commit is contained in:
3 files changed
+109
-40
No files matched your search
@@ -1,46 +1,114 @@
|
||||
package ca.bcit.comp2522.project;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.text.Font;
|
||||
import javafx.scene.text.FontWeight;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.TextField;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class NumberGame extends Application
|
||||
{
|
||||
private static final int FONT_SIZE = 24;
|
||||
private static final int WINDOW_WIDTH = 700;
|
||||
private static final int WINDOW_HEIGHT = 600;
|
||||
private static final int GRID_PADDING = 10;
|
||||
private static final int GRID_WIDTH = 5;
|
||||
private static final int GRID_HEIGHT = 4;
|
||||
private static final int BUTTON_WIDTH = 200;
|
||||
private static final int BUTTON_HEIGHT = 160;
|
||||
private static final int RANDOM_NUMBERS = 20;
|
||||
private static final int MIN_RAND_NUM = 1;
|
||||
private static final int MAX_RAND_NUM = 1001;
|
||||
private static final Font FONT = Font.font("Arial", FontWeight.BOLD, FONT_SIZE);
|
||||
private static final int STARTING_NUMBER_INDEX = 0;
|
||||
|
||||
private int currentIndex = STARTING_NUMBER_INDEX;
|
||||
|
||||
private int[] numbers;
|
||||
private Label numberLabel;
|
||||
|
||||
@Override
|
||||
public void start(final Stage stage)
|
||||
{
|
||||
final VBox layout;
|
||||
final Scene scene;
|
||||
final Random rand = new Random();
|
||||
|
||||
layout = createLayout(); // see below
|
||||
scene = new Scene(layout, 300, 200);
|
||||
numbers = new int[RANDOM_NUMBERS];
|
||||
for (int i = 0; i < RANDOM_NUMBERS; i++) {
|
||||
numbers[i] = rand.nextInt(MAX_RAND_NUM - MIN_RAND_NUM) + MIN_RAND_NUM;
|
||||
}
|
||||
|
||||
numberLabel = new Label("Next number: " + numbers[currentIndex] + " - Select a slot.");
|
||||
numberLabel.setFont(FONT);
|
||||
numberLabel.setMaxWidth(Double.MAX_VALUE);
|
||||
numberLabel.setAlignment(Pos.CENTER);
|
||||
|
||||
final VBox root = new VBox(10);
|
||||
root.getChildren().add(numberLabel);
|
||||
|
||||
final GridPane grid = createGrid();
|
||||
root.getChildren().add(grid);
|
||||
|
||||
final Scene scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
stage.setScene(scene);
|
||||
stage.setTitle("20 Number Challenge");
|
||||
stage.setResizable(false);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
private VBox createLayout()
|
||||
private GridPane createGrid()
|
||||
{
|
||||
final Label greeting;
|
||||
final TextField textField;
|
||||
final Button button;
|
||||
greeting = new Label("Enter your name:");
|
||||
textField = new TextField();
|
||||
button = new Button("Click me!");
|
||||
button.setOnAction(e -> greeting.setText("Hello, " + textField.getText()
|
||||
+ "!"));
|
||||
// or
|
||||
//button.setOnAction(event -> assignAction(greeting, textField, button));
|
||||
return new VBox(greeting, textField, button);
|
||||
final GridPane grid = new GridPane();
|
||||
grid.setPadding(new Insets(GRID_PADDING));
|
||||
grid.setHgap(GRID_PADDING);
|
||||
grid.setVgap(GRID_PADDING);
|
||||
|
||||
final Button[] buttons;
|
||||
|
||||
buttons = new Button[GRID_WIDTH * GRID_HEIGHT];
|
||||
|
||||
for (int i = 0; i < GRID_HEIGHT; i++)
|
||||
{
|
||||
for (int j = 0; j < GRID_WIDTH; j++)
|
||||
{
|
||||
final Button button = new Button("[]");
|
||||
button.setFont(FONT);
|
||||
button.setPrefSize(BUTTON_WIDTH, BUTTON_HEIGHT);
|
||||
|
||||
final int index;
|
||||
index = i * GRID_WIDTH + j;
|
||||
buttons[index] = button;
|
||||
|
||||
button.setOnAction(e -> handlePress(button));
|
||||
|
||||
grid.add(button, i, j);
|
||||
}
|
||||
}
|
||||
|
||||
return grid;
|
||||
}
|
||||
void assignAction(final Label l, final TextField t, final Button b)
|
||||
|
||||
private void handlePress(final Button button)
|
||||
{
|
||||
l.setText("Hello, " + t.getText() + "!");
|
||||
button.setText("" + numbers[currentIndex]);
|
||||
button.setDisable(true);
|
||||
|
||||
currentIndex++;
|
||||
|
||||
if (currentIndex < RANDOM_NUMBERS) {
|
||||
numberLabel.setText("Next number: " + numbers[currentIndex] + " - Select a slot.");
|
||||
}
|
||||
else
|
||||
{
|
||||
numberLabel.setText("All numbers placed!");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(final String[] args)
|
||||
|
||||
@@ -71,29 +71,29 @@ public class Score
|
||||
if (!line.isBlank() && line.contains("Date and Time: "))
|
||||
{
|
||||
final DateTimeFormatter formatter;
|
||||
final String dateTime;
|
||||
final Score score;
|
||||
final String games;
|
||||
final String correctFirst;
|
||||
final String correctSecond;
|
||||
final String incorrect;
|
||||
final String dateTime;
|
||||
final Score score;
|
||||
final String games;
|
||||
final String correctFirst;
|
||||
final String correctSecond;
|
||||
final String incorrect;
|
||||
|
||||
formatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
|
||||
|
||||
dateTime = line.split(": ")[SPLIT_VALUE];
|
||||
System.out.println("debug " + dateTime);
|
||||
games = reader.readLine().split(": ")[SPLIT_VALUE];
|
||||
correctFirst = reader.readLine().split(": ")[SPLIT_VALUE];
|
||||
dateTime = line.split(": ")[SPLIT_VALUE];
|
||||
games = reader.readLine().split(": ")[SPLIT_VALUE];
|
||||
correctFirst = reader.readLine().split(": ")[SPLIT_VALUE];
|
||||
correctSecond = reader.readLine().split(": ")[SPLIT_VALUE];
|
||||
incorrect = reader.readLine().split(": ")[SPLIT_VALUE];
|
||||
incorrect = reader.readLine().split(": ")[SPLIT_VALUE];
|
||||
|
||||
reader.readLine(); // Skip over score line
|
||||
|
||||
score = new Score(
|
||||
LocalDateTime.parse(dateTime, formatter),
|
||||
Integer.parseInt(games),
|
||||
Integer.parseInt(correctFirst),
|
||||
Integer.parseInt(correctSecond),
|
||||
Integer.parseInt(incorrect)
|
||||
LocalDateTime.parse(dateTime, formatter),
|
||||
Integer.parseInt(games),
|
||||
Integer.parseInt(correctFirst),
|
||||
Integer.parseInt(correctSecond),
|
||||
Integer.parseInt(incorrect)
|
||||
);
|
||||
|
||||
scores.add(score);
|
||||
@@ -101,7 +101,8 @@ public class Score
|
||||
}
|
||||
|
||||
reader.close();
|
||||
} catch (final IOException e)
|
||||
}
|
||||
catch (final IOException e)
|
||||
{
|
||||
System.err.println("Failed to open score log file.");
|
||||
}
|
||||
@@ -117,8 +118,8 @@ public class Score
|
||||
}
|
||||
|
||||
public static boolean isHighScore(
|
||||
final Score score,
|
||||
final List<Score> scores
|
||||
final Score score,
|
||||
final List<Score> scores
|
||||
) {
|
||||
final Score highScore;
|
||||
|
||||
|
||||
@@ -23,10 +23,10 @@ import java.util.stream.Stream;
|
||||
public class World
|
||||
{
|
||||
private static final String SCORE_PATH = "./data/score.txt";
|
||||
private static final String BASE_PATH = "./data/facts";
|
||||
private static final int COUNTRY_PARAM = 0;
|
||||
private static final int CAPITAL_PARAM = 1;
|
||||
private static final int NUMBER_OF_FACTS = 3;
|
||||
private static final String BASE_PATH = "./data/facts";
|
||||
private static final int NUMBER_OF_QUESTIONS = 10;
|
||||
private static final int QUESTION_NUM_OFFSET = 1;
|
||||
private static final int QUEST_ASK_CAPITAL = 0;
|
||||
|
||||
Reference in new issue
Block a user