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;
|
package ca.bcit.comp2522.project;
|
||||||
|
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
|
import javafx.geometry.Pos;
|
||||||
import javafx.scene.Scene;
|
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.stage.Stage;
|
||||||
import javafx.scene.control.Label;
|
import javafx.scene.control.Label;
|
||||||
|
import javafx.scene.layout.VBox;
|
||||||
import javafx.scene.control.Button;
|
import javafx.scene.control.Button;
|
||||||
import javafx.scene.control.TextField;
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class NumberGame extends Application
|
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
|
@Override
|
||||||
public void start(final Stage stage)
|
public void start(final Stage stage)
|
||||||
{
|
{
|
||||||
final VBox layout;
|
final Random rand = new Random();
|
||||||
final Scene scene;
|
|
||||||
|
|
||||||
layout = createLayout(); // see below
|
numbers = new int[RANDOM_NUMBERS];
|
||||||
scene = new Scene(layout, 300, 200);
|
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.setScene(scene);
|
||||||
stage.setTitle("20 Number Challenge");
|
stage.setTitle("20 Number Challenge");
|
||||||
|
stage.setResizable(false);
|
||||||
stage.show();
|
stage.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private VBox createLayout()
|
private GridPane createGrid()
|
||||||
{
|
{
|
||||||
final Label greeting;
|
final GridPane grid = new GridPane();
|
||||||
final TextField textField;
|
grid.setPadding(new Insets(GRID_PADDING));
|
||||||
final Button button;
|
grid.setHgap(GRID_PADDING);
|
||||||
greeting = new Label("Enter your name:");
|
grid.setVgap(GRID_PADDING);
|
||||||
textField = new TextField();
|
|
||||||
button = new Button("Click me!");
|
final Button[] buttons;
|
||||||
button.setOnAction(e -> greeting.setText("Hello, " + textField.getText()
|
|
||||||
+ "!"));
|
buttons = new Button[GRID_WIDTH * GRID_HEIGHT];
|
||||||
// or
|
|
||||||
//button.setOnAction(event -> assignAction(greeting, textField, button));
|
for (int i = 0; i < GRID_HEIGHT; i++)
|
||||||
return new VBox(greeting, textField, button);
|
{
|
||||||
|
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)
|
public static void main(final String[] args)
|
||||||
|
|||||||
@@ -71,29 +71,29 @@ public class Score
|
|||||||
if (!line.isBlank() && line.contains("Date and Time: "))
|
if (!line.isBlank() && line.contains("Date and Time: "))
|
||||||
{
|
{
|
||||||
final DateTimeFormatter formatter;
|
final DateTimeFormatter formatter;
|
||||||
final String dateTime;
|
final String dateTime;
|
||||||
final Score score;
|
final Score score;
|
||||||
final String games;
|
final String games;
|
||||||
final String correctFirst;
|
final String correctFirst;
|
||||||
final String correctSecond;
|
final String correctSecond;
|
||||||
final String incorrect;
|
final String incorrect;
|
||||||
|
|
||||||
formatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
|
formatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
|
||||||
|
|
||||||
dateTime = line.split(": ")[SPLIT_VALUE];
|
dateTime = line.split(": ")[SPLIT_VALUE];
|
||||||
System.out.println("debug " + dateTime);
|
games = reader.readLine().split(": ")[SPLIT_VALUE];
|
||||||
games = reader.readLine().split(": ")[SPLIT_VALUE];
|
correctFirst = reader.readLine().split(": ")[SPLIT_VALUE];
|
||||||
correctFirst = reader.readLine().split(": ")[SPLIT_VALUE];
|
|
||||||
correctSecond = 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
|
reader.readLine(); // Skip over score line
|
||||||
|
|
||||||
score = new Score(
|
score = new Score(
|
||||||
LocalDateTime.parse(dateTime, formatter),
|
LocalDateTime.parse(dateTime, formatter),
|
||||||
Integer.parseInt(games),
|
Integer.parseInt(games),
|
||||||
Integer.parseInt(correctFirst),
|
Integer.parseInt(correctFirst),
|
||||||
Integer.parseInt(correctSecond),
|
Integer.parseInt(correctSecond),
|
||||||
Integer.parseInt(incorrect)
|
Integer.parseInt(incorrect)
|
||||||
);
|
);
|
||||||
|
|
||||||
scores.add(score);
|
scores.add(score);
|
||||||
@@ -101,7 +101,8 @@ public class Score
|
|||||||
}
|
}
|
||||||
|
|
||||||
reader.close();
|
reader.close();
|
||||||
} catch (final IOException e)
|
}
|
||||||
|
catch (final IOException e)
|
||||||
{
|
{
|
||||||
System.err.println("Failed to open score log file.");
|
System.err.println("Failed to open score log file.");
|
||||||
}
|
}
|
||||||
@@ -117,8 +118,8 @@ public class Score
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isHighScore(
|
public static boolean isHighScore(
|
||||||
final Score score,
|
final Score score,
|
||||||
final List<Score> scores
|
final List<Score> scores
|
||||||
) {
|
) {
|
||||||
final Score highScore;
|
final Score highScore;
|
||||||
|
|
||||||
|
|||||||
@@ -23,10 +23,10 @@ import java.util.stream.Stream;
|
|||||||
public class World
|
public class World
|
||||||
{
|
{
|
||||||
private static final String SCORE_PATH = "./data/score.txt";
|
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 COUNTRY_PARAM = 0;
|
||||||
private static final int CAPITAL_PARAM = 1;
|
private static final int CAPITAL_PARAM = 1;
|
||||||
private static final int NUMBER_OF_FACTS = 3;
|
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 NUMBER_OF_QUESTIONS = 10;
|
||||||
private static final int QUESTION_NUM_OFFSET = 1;
|
private static final int QUESTION_NUM_OFFSET = 1;
|
||||||
private static final int QUEST_ASK_CAPITAL = 0;
|
private static final int QUEST_ASK_CAPITAL = 0;
|
||||||
|
|||||||
Reference in new issue
Block a user