Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ae54b4cbf |
@@ -1,4 +0,0 @@
|
|||||||
3*2+1=7
|
|
||||||
5-3+2=4
|
|
||||||
9-2+1=8
|
|
||||||
6/3+4=6
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
public class CLIApp {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class GUIApp {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
SwingUtilities.invokeLater(
|
||||||
|
new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
createAndShowGUI();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void createAndShowGUI() {
|
||||||
|
INumberleModel model = new NumberleModel();
|
||||||
|
NumberleController controller = new NumberleController(model);
|
||||||
|
NumberleView view = new NumberleView(model, controller);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,28 +1,16 @@
|
|||||||
package org.example;
|
package org.example;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface INumberleModel{
|
public interface INumberleModel {
|
||||||
int Attempt = 6;
|
int MAX_ATTEMPTS = 6;
|
||||||
|
|
||||||
|
void initialize();
|
||||||
void StartGame(String oldTarget);
|
|
||||||
boolean processInput(String input);
|
boolean processInput(String input);
|
||||||
boolean isGameOver();
|
boolean isGameOver();
|
||||||
boolean isGameWon();
|
boolean isGameWon();
|
||||||
|
String getTargetNumber();
|
||||||
void setFlag3(boolean flag3);
|
|
||||||
|
|
||||||
boolean input(String input);
|
|
||||||
void setFlag(boolean flag1);
|
|
||||||
void setTargetword(String targetword);
|
|
||||||
String getTargetWord();
|
|
||||||
StringBuilder getCurrentGuess();
|
StringBuilder getCurrentGuess();
|
||||||
int getRemainingAttempts();
|
int getRemainingAttempts();
|
||||||
void setRemainingAttempts(int val);
|
void startNewGame();
|
||||||
boolean startNewGame(String oldTarget);
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
package org.example;
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
|
|
||||||
public class Junit {
|
|
||||||
private NumberleModel model;
|
|
||||||
private NumberleView view;
|
|
||||||
@Before
|
|
||||||
public void Setup(){
|
|
||||||
model=new NumberleModel();
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void TestNumber(){
|
|
||||||
model.setTargetword("2*4=4*2");
|
|
||||||
model.input("2*4=4*2");
|
|
||||||
assertEquals("2*4=4*2",model.getTargetWord());
|
|
||||||
assertTrue(model.Gamewon);
|
|
||||||
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void ProcessInput(){
|
|
||||||
model.StartGame("");
|
|
||||||
assertFalse(model.processInput(""));
|
|
||||||
assertFalse(model.processInput("3*2=2*8"));
|
|
||||||
assertFalse(model.isGameWon());
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void Remove_EmptyGuess() {
|
|
||||||
model.StartGame("");
|
|
||||||
assertEquals(0, model.getCurrentGuess().length());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void StartNewGame() {
|
|
||||||
model.StartGame("");
|
|
||||||
String oldTargetWord = model.getTargetWord();
|
|
||||||
model.startNewGame(oldTargetWord);
|
|
||||||
assertNotEquals(oldTargetWord, model.getTargetWord());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testProcessInput_InvalidEquation() {
|
|
||||||
model.setFlag3(true);
|
|
||||||
model.StartGame("");
|
|
||||||
assertFalse(model.processInput("1+2=2+3"));
|
|
||||||
assertFalse(model.isGameWon());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package org.example;
|
|
||||||
|
|
||||||
public class Main {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
INumberleModel model = new NumberleModel();
|
|
||||||
NumberleController controller=new NumberleController(model);
|
|
||||||
NumberleView view = new NumberleView(model, controller);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,118 +0,0 @@
|
|||||||
package org.example;
|
|
||||||
import java.util.Scanner;
|
|
||||||
import net.objecthunter.exp4j.Expression;
|
|
||||||
import net.objecthunter.exp4j.ExpressionBuilder;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class NumberleCLI {
|
|
||||||
private static final String GREEN = "\u001B[32m";
|
|
||||||
private static final String YELLOW = "\u001B[33m";
|
|
||||||
|
|
||||||
private static final String White = "\u001B[36m";
|
|
||||||
private static final String GRAY = "\u001B[90m";
|
|
||||||
|
|
||||||
|
|
||||||
private static final INumberleModel model = new NumberleModel();
|
|
||||||
private static final Scanner scanner = new Scanner(System.in);
|
|
||||||
|
|
||||||
private static void GameStart() {
|
|
||||||
while (!model.isGameOver()) {
|
|
||||||
GameStatue();
|
|
||||||
String input = InputInformation();
|
|
||||||
if (input.length()<7){
|
|
||||||
System.out.println("the input length is too short ");
|
|
||||||
}
|
|
||||||
boolean containsOperator = false; // Flag to check if an operator is present
|
|
||||||
|
|
||||||
for (char c : input.toCharArray()) {
|
|
||||||
if ("+-*/".indexOf(c) != -1) {
|
|
||||||
containsOperator = true; // Set flag if an operator is found
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
boolean equal=input.contains("=");
|
|
||||||
|
|
||||||
boolean containsMultiple=input.contains("+-*/");
|
|
||||||
boolean containMultiples=input.contains("+-/");
|
|
||||||
if (!equal){
|
|
||||||
|
|
||||||
System.out.println("no equal please try again ");
|
|
||||||
} else if (containMultiples||containsMultiple) {
|
|
||||||
System.out.println("the Multiple operator");
|
|
||||||
}else if (!containsOperator){
|
|
||||||
System.out.println("at least one operator");
|
|
||||||
}
|
|
||||||
if (input.length()==7) {
|
|
||||||
if (correct(input)){
|
|
||||||
boolean correct = model.input(input);
|
|
||||||
ColoredInput(input, model.getTargetWord());
|
|
||||||
if (correct) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
if (model.isGameWon()) {
|
|
||||||
System.out.println("wow! you won the game ");
|
|
||||||
} else {
|
|
||||||
System.out.println("Game over ! your lose the game !");
|
|
||||||
System.out.println("The right answer is " + model.getTargetWord());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void printCategories() {
|
|
||||||
|
|
||||||
System.out.println("operator: + - * / =");
|
|
||||||
System.out.println("number: 0 1 2 3 4 5 6 7 8 9");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String InputInformation() {
|
|
||||||
System.out.print("please enter your guess: ");
|
|
||||||
return scanner.nextLine();
|
|
||||||
}
|
|
||||||
private static void GameStatue() {
|
|
||||||
// System.out.println("the target equation: " + model.getTargetWord());
|
|
||||||
System.out.println("you have : " + model.getRemainingAttempts()+" "+"chances");
|
|
||||||
System.out.println("your input guess is : " + model.getCurrentGuess());
|
|
||||||
printCategories();
|
|
||||||
}
|
|
||||||
private static void ColoredInput(String input, String target) {
|
|
||||||
|
|
||||||
StringBuilder SE = new StringBuilder();
|
|
||||||
for (int i = 0; i < input.length(); i++) {
|
|
||||||
char c = input.charAt(i);
|
|
||||||
if (i < target.length() && c == target.charAt(i)) {
|
|
||||||
SE.append(GREEN).append(c);
|
|
||||||
} else if (target.contains(String.valueOf(c))) {
|
|
||||||
SE.append(YELLOW).append(c);
|
|
||||||
} else {
|
|
||||||
SE.append(GRAY).append(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
SE.append(White);
|
|
||||||
System.out.println(SE.toString());
|
|
||||||
}
|
|
||||||
private static boolean correct(String input) {
|
|
||||||
String[] EQUAL = input.split("=");
|
|
||||||
if (EQUAL.length != 2) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Expression expLeft = new ExpressionBuilder(EQUAL[0].trim()).build();
|
|
||||||
double leftResult = expLeft.evaluate();
|
|
||||||
Expression expRight = new ExpressionBuilder(EQUAL[1].trim()).build();
|
|
||||||
double rightResult = expRight.evaluate();
|
|
||||||
if (leftResult!=rightResult){
|
|
||||||
System.out.println("the left side is not equal right side ");
|
|
||||||
}
|
|
||||||
return Double.compare(leftResult, rightResult) == 0;
|
|
||||||
}
|
|
||||||
public static void main(String[] args) {
|
|
||||||
model.startNewGame("");
|
|
||||||
GameStart();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,56 +1,43 @@
|
|||||||
package org.example;
|
package org.example;
|
||||||
|
|
||||||
|
// NumberleController.java
|
||||||
public class NumberleController {
|
public class NumberleController {
|
||||||
|
|
||||||
|
|
||||||
private INumberleModel model;
|
private INumberleModel model;
|
||||||
|
private NumberleView view;
|
||||||
|
|
||||||
public NumberleController(INumberleModel model) {
|
public NumberleController(INumberleModel model) {
|
||||||
this.model = model;
|
this.model = model;
|
||||||
}
|
}
|
||||||
public int getRemainingAttempts() {
|
|
||||||
return model.getRemainingAttempts();
|
public void setView(NumberleView view) {
|
||||||
}
|
this.view = view;
|
||||||
public void startNewGame() {
|
|
||||||
model.startNewGame("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void SetFlag(Boolean Flag3){
|
|
||||||
model.setFlag3(Flag3);
|
|
||||||
if (Flag3){
|
|
||||||
model.setTargetword("3*2+1=7");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
public void setflag1(Boolean flag){
|
|
||||||
model.setFlag(flag);;
|
|
||||||
System.out.println("error,the equation is not valid,this will not count as one of tires. so you have "+""+getRemainingAttempts()+" "+"chance ");
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
public void setFlag2(Boolean Flag){
|
|
||||||
|
|
||||||
if (Flag){
|
|
||||||
System.out.println(getTargetWord());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
public String getTargetWord() {
|
|
||||||
return model.getTargetWord();
|
|
||||||
}
|
|
||||||
public void setRemainingAttempts(int val) {
|
|
||||||
model.setRemainingAttempts(val);
|
|
||||||
}
|
|
||||||
public void processInput(String input) {
|
public void processInput(String input) {
|
||||||
model.processInput(input);
|
model.processInput(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isGameOver() {
|
public boolean isGameOver() {
|
||||||
return model.isGameOver();
|
return model.isGameOver();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isGameWon() {
|
public boolean isGameWon() {
|
||||||
return model.isGameWon();
|
return model.isGameWon();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getTargetWord() {
|
||||||
|
return model.getTargetNumber();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
public StringBuilder getCurrentGuess() {
|
||||||
|
return model.getCurrentGuess();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRemainingAttempts() {
|
||||||
|
return model.getRemainingAttempts();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startNewGame() {
|
||||||
|
model.startNewGame();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,200 +1,61 @@
|
|||||||
package org.example;
|
package org.example;// NumberleModel.java
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.Scanner;
|
import java.util.Observable;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
|
|
||||||
|
public class NumberleModel extends Observable implements INumberleModel {
|
||||||
|
private String targetNumber;
|
||||||
|
private StringBuilder currentGuess;
|
||||||
|
private int remainingAttempts;
|
||||||
|
private boolean gameWon;
|
||||||
|
|
||||||
public class NumberleModel extends java.util.Observable implements INumberleModel{
|
@Override
|
||||||
|
public void initialize() {
|
||||||
private String Word;
|
Random rand = new Random();
|
||||||
public ArrayList<String> List=new ArrayList<>();
|
targetNumber = Integer.toString(rand.nextInt(10000000));
|
||||||
public int remainAttempts=6 ;
|
currentGuess = new StringBuilder(" ");
|
||||||
public boolean Gamewon;
|
remainingAttempts = MAX_ATTEMPTS;
|
||||||
public boolean Flag1=false;
|
gameWon = false;
|
||||||
public boolean Flag2=false ;
|
|
||||||
public boolean Flag3;
|
|
||||||
|
|
||||||
public String Formal_World="3+4-2=5";
|
|
||||||
|
|
||||||
private StringBuilder CurrentGuess;
|
|
||||||
|
|
||||||
|
|
||||||
//Start new game you should know this function just from txt file to select the words
|
|
||||||
//which is random to select
|
|
||||||
public void StartGame(String oldTarget){
|
|
||||||
this.FileRead();
|
|
||||||
if (List == null || List.isEmpty()) {
|
|
||||||
System.out.println("The list is null or empty. Please check and fix it.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (oldTarget != null && !oldTarget.isEmpty()) {
|
|
||||||
this.List.removeIf(s -> s.equals(oldTarget));
|
|
||||||
}
|
|
||||||
this.FileRead();
|
|
||||||
Random random = new Random();
|
|
||||||
// System.out.println(List);
|
|
||||||
int index = random.nextInt(List.size());
|
|
||||||
|
|
||||||
if (Flag1){
|
|
||||||
System.out.println("This is not the correct answer.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Flag2){
|
|
||||||
System.out.println(getTargetWord());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Flag3){
|
|
||||||
Word = Formal_World;
|
|
||||||
} else {
|
|
||||||
Word = List.get(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
CurrentGuess = new StringBuilder(Word.length());
|
|
||||||
remainAttempts = Attempt;
|
|
||||||
Gamewon = false;
|
|
||||||
|
|
||||||
setChanged();
|
setChanged();
|
||||||
notifyObservers();
|
notifyObservers();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean ProcessInput(String input){
|
|
||||||
|
|
||||||
assert input.length() <=7 : "Input length should be <= 7";
|
|
||||||
if (input.length() < 7){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
CurrentGuess = new StringBuilder(input);
|
|
||||||
|
|
||||||
if (CurrentGuess.toString().equals(Word)) {
|
|
||||||
Gamewon = true;
|
|
||||||
}
|
|
||||||
setChanged();
|
|
||||||
notifyObservers();
|
|
||||||
return Gamewon;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean input(String input){
|
|
||||||
assert input.length() <=7 : "Input length should be <= 7";
|
|
||||||
if (input.length() < 7){
|
|
||||||
System.out.println("input length must =7");
|
|
||||||
}
|
|
||||||
CurrentGuess = new StringBuilder(input);
|
|
||||||
|
|
||||||
|
|
||||||
if (CurrentGuess.toString().equals(Word)) {
|
|
||||||
Gamewon = true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
remainAttempts--;
|
|
||||||
}
|
|
||||||
return Gamewon;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void FileRead(){
|
|
||||||
try {
|
|
||||||
File file=new File("./equations.txt");
|
|
||||||
assert file.exists():"File should be exists";
|
|
||||||
Scanner scanner =new Scanner(file);
|
|
||||||
|
|
||||||
while(scanner.hasNextLine()){
|
|
||||||
this.List.add(scanner.nextLine());
|
|
||||||
|
|
||||||
}
|
|
||||||
scanner.close();
|
|
||||||
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
System.out.println("File can not read,please try to fix it ");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean processInput(String input) {
|
public boolean processInput(String input) {
|
||||||
return ProcessInput(input);
|
|
||||||
|
remainingAttempts--;
|
||||||
|
setChanged();
|
||||||
|
notifyObservers();
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isGameOver() {
|
public boolean isGameOver() {
|
||||||
assert remainAttempts >= 0 : "remainAttempts should be non-negative";
|
return remainingAttempts <= 0 || gameWon;
|
||||||
return remainAttempts <= 0 || Gamewon ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isGameWon() {
|
public boolean isGameWon() {
|
||||||
return Gamewon;
|
return gameWon;
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setFlag(boolean flag2) {
|
|
||||||
this.Flag2=flag2;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void setFlag3(boolean flag3) {
|
|
||||||
this.Flag3=flag3;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setTargetword(String targetword) {
|
|
||||||
assert targetword != null : "targetword should not be null";
|
|
||||||
assert targetword.length() >= 7 : "targetword length should be >= 7";
|
|
||||||
this.Word=targetword;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getTargetWord() {
|
public String getTargetNumber() {
|
||||||
return Word;
|
return targetNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StringBuilder getCurrentGuess() {
|
public StringBuilder getCurrentGuess() {
|
||||||
return CurrentGuess;
|
return currentGuess;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getRemainingAttempts() {
|
public int getRemainingAttempts() {
|
||||||
assert remainAttempts >= 0 : "remainAttempts should be non-negative";
|
return remainingAttempts;
|
||||||
return remainAttempts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setRemainingAttempts(int val) {
|
public void startNewGame() {
|
||||||
assert val >= 0 : "val should be non-negative";
|
initialize();
|
||||||
remainAttempts = val;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean startNewGame(String oldTarget) {
|
|
||||||
StartGame(oldTarget);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// @Override
|
|
||||||
// public void Delete() {
|
|
||||||
//
|
|
||||||
// if (CurrentGuess.length()>0){
|
|
||||||
// CurrentGuess.deleteCharAt(CurrentGuess.length()-1);
|
|
||||||
// setChanged();
|
|
||||||
// notifyObservers();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,825 +1,77 @@
|
|||||||
package org.example;
|
package org.example;// NumberleView.java
|
||||||
|
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
|
|
||||||
import java.util.Observable;
|
|
||||||
import java.util.Observer;
|
import java.util.Observer;
|
||||||
|
|
||||||
|
public class NumberleView implements Observer {
|
||||||
import net.objecthunter.exp4j.Expression;
|
private final INumberleModel model;
|
||||||
import net.objecthunter.exp4j.ExpressionBuilder;
|
private final NumberleController controller;
|
||||||
|
private final JFrame frame = new JFrame("Numberle");
|
||||||
|
private final JTextField inputTextField = new JTextField(3);;
|
||||||
public class NumberleView extends JFrame implements Observer {
|
private final JLabel attemptsLabel = new JLabel("Attempts remaining: ");
|
||||||
private JPanel Panel;//game panel
|
|
||||||
private JLayeredPane layeredPane;
|
|
||||||
private NumberleController controller;// Controller
|
|
||||||
private final INumberleModel model;// Model
|
|
||||||
public JButton button[][] = new JButton[6][7]; // Game buttons
|
|
||||||
private JTextField inputTextField;
|
|
||||||
private int Row = 0;
|
|
||||||
|
|
||||||
public static final int ROWS = 6;// Total rows
|
|
||||||
public static final int COLS = 7;// Total columns
|
|
||||||
public static final int MAX_INPUT_LENGTH = 7;// Maximum length of input text field
|
|
||||||
private static final Color CORRECT_COLOR = Color.GREEN; // Correct color
|
|
||||||
//The entered text contains letters but in the wrong order
|
|
||||||
private static final Color INCORRECT_COLOR = Color.YELLOW;
|
|
||||||
private final Color defaultColor = UIManager.getColor("Button.background");// Default button color
|
|
||||||
//
|
|
||||||
private JPanel row1;// First row panel
|
|
||||||
private JPanel row2;// Second row panel
|
|
||||||
|
|
||||||
|
|
||||||
public NumberleView(INumberleModel model, NumberleController controller) {
|
public NumberleView(INumberleModel model, NumberleController controller) {
|
||||||
// Parameter validation
|
this.controller = controller;
|
||||||
assert model != null; // Ensure model parameter is not null
|
this.model = model;
|
||||||
assert controller != null; // Ensure controller parameter is not null
|
this.controller.startNewGame();
|
||||||
|
((NumberleModel)this.model).addObserver(this);
|
||||||
// Assigning controller and model
|
initializeFrame();
|
||||||
this.controller = controller; // Assign controller parameter to the controller field
|
this.controller.setView(this);
|
||||||
this.model = model; // Assign model parameter to the model field
|
update((NumberleModel)this.model, null);
|
||||||
|
|
||||||
// Start new game
|
|
||||||
controller.startNewGame(); // Invoke startNewGame method of the controller
|
|
||||||
|
|
||||||
// Initialize GUI components
|
|
||||||
initialize(); // Call the initialize method to set up the GUI components
|
|
||||||
|
|
||||||
// Show game start reminder
|
|
||||||
showGameStartReminder(); // Display a reminder or message indicating the game has started
|
|
||||||
|
|
||||||
// Register view as observer to model
|
|
||||||
((NumberleModel) this.model).addObserver(this); // Register this view as an observer to the model
|
|
||||||
|
|
||||||
// Set view for controller
|
|
||||||
// this.controller.setView(this); // Set this view instance as the view for the controller
|
|
||||||
|
|
||||||
// Update view with initial model state
|
|
||||||
update((NumberleModel) this.model, null); // Call update method to synchronize view with initial model state
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initialize() {
|
public void initializeFrame() {
|
||||||
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
frame.setSize(600, 200);
|
||||||
|
frame.setLayout(new BorderLayout());
|
||||||
|
|
||||||
// Set the look and feel of the user interface to MetalLookAndFeel
|
JPanel center = new JPanel();
|
||||||
try {
|
center.setLayout(new BoxLayout(center, BoxLayout.X_AXIS));
|
||||||
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
|
center.add(new JPanel());
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the title of the frame
|
|
||||||
setTitle("Game");
|
|
||||||
|
|
||||||
// Set default close operation to exit the application when the frame is closed
|
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
|
|
||||||
// Set the frame size to 800x800 pixels
|
|
||||||
setSize(750, 750);
|
|
||||||
setLocationRelativeTo(null);
|
|
||||||
// Make the frame non-resizable
|
|
||||||
setResizable(false);
|
|
||||||
|
|
||||||
// Set the layout of the frame to BorderLayout
|
|
||||||
setLayout(new BorderLayout());
|
|
||||||
|
|
||||||
// Create a panel with a 6x7 grid layout for buttons
|
|
||||||
Panel = new JPanel(new GridLayout(6, 7));
|
|
||||||
|
|
||||||
// Create buttons and add them to the panel
|
|
||||||
for (int i = 0; i < 6; i++) {
|
|
||||||
for (int j = 0; j < 7; j++) {
|
|
||||||
button[i][j] = new JButton("");
|
|
||||||
button[i][j].setPreferredSize(new Dimension(100, 100));
|
|
||||||
Panel.add(button[i][j]);
|
|
||||||
}
|
|
||||||
// Add the panel to the frame's center position
|
|
||||||
add(Panel, BorderLayout.CENTER);
|
|
||||||
Panel.setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create an input panel with a text field
|
|
||||||
JPanel inputPanel = new JPanel();
|
JPanel inputPanel = new JPanel();
|
||||||
inputTextField = new JTextField(7);
|
inputPanel.setLayout(new GridLayout(3, 1));
|
||||||
|
|
||||||
inputPanel.add(inputTextField);
|
inputPanel.add(inputTextField);
|
||||||
add(inputPanel, BorderLayout.NORTH);
|
|
||||||
inputPanel.setVisible(false);
|
|
||||||
|
|
||||||
// Get text from input text field
|
JButton submitButton = new JButton("Submit");
|
||||||
String input = inputTextField.getText();
|
submitButton.addActionListener(e -> {
|
||||||
|
controller.processInput(inputTextField.getText());
|
||||||
|
inputTextField.setText("");
|
||||||
|
});
|
||||||
|
inputPanel.add(submitButton);
|
||||||
|
|
||||||
// Assert that input text field is not null or empty
|
attemptsLabel.setText("Attempts remaining: " + controller.getRemainingAttempts());
|
||||||
assert (input) != null & !input.isEmpty();
|
inputPanel.add(attemptsLabel);
|
||||||
|
center.add(inputPanel);
|
||||||
|
center.add(new JPanel());
|
||||||
|
frame.add(center, BorderLayout.NORTH);
|
||||||
|
|
||||||
// Create a keyboard panel with numeric buttons and operator buttons
|
JPanel keyboardPanel = new JPanel();
|
||||||
JPanel keyBoardPanel = new JPanel();
|
keyboardPanel.setLayout(new BoxLayout(keyboardPanel, BoxLayout.X_AXIS));
|
||||||
keyBoardPanel.setLayout(new BoxLayout(keyBoardPanel, BoxLayout.Y_AXIS));
|
keyboardPanel.add(new JPanel());
|
||||||
row1 = new JPanel();
|
JPanel numberPanel = new JPanel();
|
||||||
row1.setLayout(new FlowLayout());
|
numberPanel.setLayout(new GridLayout(2, 5));
|
||||||
for (int i = 1; i <= 9; i++) {
|
keyboardPanel.add(numberPanel);
|
||||||
|
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
JButton button = new JButton(Integer.toString(i));
|
JButton button = new JButton(Integer.toString(i));
|
||||||
button.setFont(new Font("Serif", Font.PLAIN, 30));
|
button.setEnabled(true);
|
||||||
button.addActionListener(new ButtonClickListener());
|
button.addActionListener(e -> {
|
||||||
row1.add(button);
|
inputTextField.setText(inputTextField.getText() + button.getText());
|
||||||
}
|
});
|
||||||
JButton zeroButton = new JButton("0");
|
button.setPreferredSize(new Dimension(50, 50));
|
||||||
zeroButton.setFont(new Font("Serif", Font.PLAIN, 30));
|
numberPanel.add(button);
|
||||||
zeroButton.addActionListener(new ButtonClickListener());
|
|
||||||
row1.add(zeroButton);
|
|
||||||
|
|
||||||
row2 = new JPanel();
|
|
||||||
row2.setLayout(new FlowLayout());
|
|
||||||
String[] operators = {"Remove", "+", "-", "*", "/", "=", "Enter"};
|
|
||||||
for (String operator : operators) {
|
|
||||||
JButton button = new JButton(operator);
|
|
||||||
button.setFont(new Font("Serif", Font.PLAIN, 30));
|
|
||||||
button.addActionListener(new ButtonClickListener());
|
|
||||||
row2.add(button);
|
|
||||||
}
|
|
||||||
keyBoardPanel.add(row1);
|
|
||||||
keyBoardPanel.add(row2);
|
|
||||||
|
|
||||||
// Add the keyboard panel to the frame's south position
|
|
||||||
add(keyBoardPanel, BorderLayout.SOUTH);
|
|
||||||
|
|
||||||
// Create a button panel with various control buttons
|
|
||||||
JPanel ButtonPanel = new JPanel();
|
|
||||||
JButton Information = new JButton("How to play");
|
|
||||||
ButtonPanel.add(Information);
|
|
||||||
|
|
||||||
// Add the button panel to the frame's north position
|
|
||||||
add(ButtonPanel, BorderLayout.WEST);
|
|
||||||
|
|
||||||
JButton FixFormual = new JButton("Fixed_Formula");
|
|
||||||
JButton display = new JButton("Display");
|
|
||||||
|
|
||||||
|
|
||||||
display.addActionListener(e -> {
|
|
||||||
display();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add action listeners to control buttons
|
|
||||||
FixFormual.addActionListener(e -> {
|
|
||||||
Fixed();
|
|
||||||
});
|
|
||||||
|
|
||||||
Information.addActionListener(e -> {
|
|
||||||
HowtoPlayGame();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
// Add buttons to the button panel
|
|
||||||
ButtonPanel.add(Information);
|
|
||||||
ButtonPanel.add(FixFormual);
|
|
||||||
ButtonPanel.add(display);
|
|
||||||
|
|
||||||
|
|
||||||
// Add the button panel to the frame's north position
|
|
||||||
add(ButtonPanel, BorderLayout.NORTH);
|
|
||||||
|
|
||||||
// Make the frame visible
|
|
||||||
setVisible(true);
|
|
||||||
|
|
||||||
// Call the change method
|
|
||||||
change();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Fixed() {
|
|
||||||
controller.SetFlag(true);
|
|
||||||
System.out.println(controller.getTargetWord());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes all components from row2 panel and replaces them with operator buttons in a specific order.
|
|
||||||
* This method is called when the "Swap" button is clicked.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public void display() {
|
|
||||||
controller.setFlag2(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the buttons based on the input text.
|
|
||||||
* This method is called when there is a change in the input text field.
|
|
||||||
*/
|
|
||||||
public void change() {
|
|
||||||
String input = inputTextField.getText();
|
|
||||||
updateButtonsFromInput(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ActionListener implementation for handling button clicks.
|
|
||||||
*/
|
|
||||||
private class ButtonClickListener implements ActionListener {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
JButton source = (JButton) e.getSource();
|
|
||||||
String buttonText = source.getText();
|
|
||||||
switch (buttonText) {
|
|
||||||
case "Remove":
|
|
||||||
clearDisplay();
|
|
||||||
break;
|
|
||||||
case "Enter":
|
|
||||||
Enter();
|
|
||||||
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
if (inputTextField.getText().length() < MAX_INPUT_LENGTH) {
|
|
||||||
updateDisplay(buttonText);
|
|
||||||
updateButtonsFromInput(inputTextField.getText());
|
|
||||||
}
|
|
||||||
controller.isGameOver();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the color and text of buttons based on the target word and user input.
|
|
||||||
*
|
|
||||||
* @param target The target word as an array of characters.
|
|
||||||
* @param input The user input as a string.
|
|
||||||
*/
|
|
||||||
private void updateButtonsFromTarget(char[] target, String input) {
|
|
||||||
char[] userInput = input.toCharArray();
|
|
||||||
|
|
||||||
for (int col = 0; col < 7; col++) {
|
|
||||||
|
|
||||||
char targetChar = target[col];
|
|
||||||
char inputChar = userInput[col];
|
|
||||||
|
|
||||||
if (targetChar == inputChar) {
|
|
||||||
button[Row][col].setBackground(Color.GREEN);
|
|
||||||
} else if (new String(target).indexOf(inputChar) != -1) {
|
|
||||||
button[Row][col].setBackground(Color.ORANGE);
|
|
||||||
} else {
|
|
||||||
button[Row][col].setBackground(Color.GRAY);
|
|
||||||
}
|
|
||||||
|
|
||||||
button[Row][col].setText(String.valueOf(inputChar));
|
|
||||||
|
|
||||||
|
|
||||||
String buttonText = button[Row][col].getText();
|
|
||||||
|
|
||||||
|
|
||||||
for (Component component : row1.getComponents()) {
|
|
||||||
if (component instanceof JButton) {
|
|
||||||
JButton keyboardButton = (JButton) component;
|
|
||||||
if (keyboardButton.getText().equals(buttonText)) {
|
|
||||||
if (targetChar == inputChar) {
|
|
||||||
keyboardButton.setBackground(Color.GREEN);
|
|
||||||
} else if (new String(target).indexOf(inputChar) != -1) {
|
|
||||||
keyboardButton.setBackground(Color.ORANGE);
|
|
||||||
} else {
|
|
||||||
keyboardButton.setBackground(Color.GRAY);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
for (Component component : row2.getComponents()) {
|
|
||||||
if (component instanceof JButton) {
|
|
||||||
JButton keyboardButton = (JButton) component;
|
|
||||||
if (keyboardButton.getText().equals(buttonText)) {
|
|
||||||
if (targetChar == inputChar) {
|
|
||||||
keyboardButton.setBackground(Color.GREEN);
|
|
||||||
} else if (new String(target).indexOf(inputChar) != -1) {
|
|
||||||
keyboardButton.setBackground(Color.ORANGE);
|
|
||||||
} else {
|
|
||||||
keyboardButton.setBackground(Color.GRAY);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the text of buttons based on the user input.
|
|
||||||
*
|
|
||||||
* @param input The user input as a string.
|
|
||||||
*/
|
|
||||||
private void updateButtonsFromInput(String input) {
|
|
||||||
int rowIndex = Row;
|
|
||||||
|
|
||||||
int colIndex = 0;
|
|
||||||
char[] chars = input.toCharArray();
|
|
||||||
|
|
||||||
for (char c : chars) {
|
|
||||||
if (Character.isDigit(c) || "+-*/=".indexOf(c) != -1) {
|
|
||||||
if (colIndex < 7) {
|
|
||||||
button[rowIndex][colIndex].setText(String.valueOf(c));
|
|
||||||
colIndex++;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int clearIndex = colIndex; clearIndex < 7; clearIndex++) {
|
|
||||||
button[rowIndex][clearIndex].setText("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
keyboardPanel.add(new JPanel());
|
||||||
|
|
||||||
}
|
frame.add(keyboardPanel, BorderLayout.CENTER);
|
||||||
|
frame.setVisible(true);
|
||||||
/**
|
|
||||||
* Handles the action when the "Enter" button is clicked.
|
|
||||||
*/
|
|
||||||
public void Enter() {
|
|
||||||
String input = inputTextField.getText();
|
|
||||||
// Check if input length is less than 7
|
|
||||||
if (input.length() < 7) {
|
|
||||||
controller.setflag1(true);
|
|
||||||
TooShort();
|
|
||||||
return; // Return to avoid further processing
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean containsOperator = false; // Flag to check if an operator is present
|
|
||||||
|
|
||||||
for (char c : input.toCharArray()) {
|
|
||||||
if ("+-*/".indexOf(c) != -1) {
|
|
||||||
containsOperator = true; // Set flag if an operator is found
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (input.length() == 7) {
|
|
||||||
boolean containsEquals = input.contains("=");
|
|
||||||
boolean containsMultiple = input.contains("+-*/");
|
|
||||||
boolean containMultiples = input.contains("+-/");
|
|
||||||
boolean containMultipless = input.contains("+-*");
|
|
||||||
boolean contains = input.contains("+=++");
|
|
||||||
|
|
||||||
if (!containsEquals) {
|
|
||||||
NoEqualSign();
|
|
||||||
} else if (!containsOperator) {
|
|
||||||
Symobol();
|
|
||||||
} else if (containsMultiple || containMultiples || containMultipless) {
|
|
||||||
MultiplyOperator();
|
|
||||||
|
|
||||||
} else if (contains) {
|
|
||||||
MultiplyOperator();
|
|
||||||
} else {
|
|
||||||
boolean isCorrect = validateEquation(input);
|
|
||||||
if (isCorrect) {
|
|
||||||
char[] target = controller.getTargetWord().toCharArray();
|
|
||||||
validateEquation(input);
|
|
||||||
|
|
||||||
controller.setRemainingAttempts(controller.getRemainingAttempts() - input.length() / 7);
|
|
||||||
|
|
||||||
updateButtonsFromInput(input); // Update buttons based on the input4
|
|
||||||
updateButtonsFromTarget(target, input);
|
|
||||||
Row++;
|
|
||||||
|
|
||||||
if (Row > 5) {
|
|
||||||
Row = 0;
|
|
||||||
}
|
|
||||||
controller.processInput(input);
|
|
||||||
|
|
||||||
inputTextField.setText(""); // Clear the input text field
|
|
||||||
if (controller.getRemainingAttempts() == 0) {
|
|
||||||
Youlost();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
|
|
||||||
NotEqual();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates the equation provided by the user.
|
|
||||||
*
|
|
||||||
* @param input The equation input by the user.
|
|
||||||
* @return True if the equation is valid, false otherwise.
|
|
||||||
*/
|
|
||||||
private boolean validateEquation(String input) {
|
|
||||||
String[] parts = input.split("="); // Split the input into left and right sides of the equation
|
|
||||||
if (parts.length != 2) {
|
|
||||||
return false; // Return false if the equation doesn't contain exactly one equals sign
|
|
||||||
}
|
|
||||||
|
|
||||||
// Evaluate the left side of the equation
|
|
||||||
Expression expLeft = new ExpressionBuilder(parts[0].trim()).build();
|
|
||||||
double leftResult = expLeft.evaluate();
|
|
||||||
|
|
||||||
// Evaluate the right side of the equation
|
|
||||||
Expression expRight = new ExpressionBuilder(parts[1].trim()).build();
|
|
||||||
double rightResult = expRight.evaluate();
|
|
||||||
|
|
||||||
// Compare the results of the left and right sides of the equation
|
|
||||||
return Double.compare(leftResult, rightResult) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clears the display by removing the last character from the input text field.
|
|
||||||
*/
|
|
||||||
public void clearDisplay() {
|
|
||||||
String currentText = inputTextField.getText();
|
|
||||||
if (!currentText.isEmpty()) {
|
|
||||||
// Remove the last character from the current text
|
|
||||||
String newText = currentText.substring(0, currentText.length() - 1);
|
|
||||||
inputTextField.setText(newText);
|
|
||||||
// Update buttons based on the new input text
|
|
||||||
updateButtonsFromInput(newText);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the display by appending the given text to the input text field.
|
|
||||||
* If the length of the resulting text exceeds 7 characters, it truncates the text appropriately.
|
|
||||||
*
|
|
||||||
* @param text The text to be added to the input text field.
|
|
||||||
*/
|
|
||||||
public void updateDisplay(String text) {
|
|
||||||
// Check if adding the text will exceed the maximum length
|
|
||||||
if (inputTextField.getText().length() + text.length() > 7) {
|
|
||||||
// Truncate the text to fit within the maximum length
|
|
||||||
text = text.substring(0, 7 - inputTextField.getText().length());
|
|
||||||
}
|
|
||||||
// Append the text to the input text field
|
|
||||||
inputTextField.setText(inputTextField.getText() + text);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Displays a reminder frame indicating that the input is too short.
|
|
||||||
* The reminder frame is displayed for 2 seconds before automatically closing.
|
|
||||||
*/
|
|
||||||
public void TooShort() {
|
|
||||||
// Create a reminder frame
|
|
||||||
JFrame reminderFrame = new JFrame("reminder");
|
|
||||||
reminderFrame.setBounds(625, 300, 400, 200);
|
|
||||||
reminderFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
||||||
|
|
||||||
// Create a label for the reminder message
|
|
||||||
JLabel reminderLabel = new JLabel("Too Short !");
|
|
||||||
reminderLabel.setFont(new Font("Serif", Font.BOLD, 25));
|
|
||||||
reminderLabel.setHorizontalAlignment(JLabel.CENTER);
|
|
||||||
|
|
||||||
// Create a panel to hold the reminder label
|
|
||||||
JPanel reminderPanel = new JPanel(new BorderLayout());
|
|
||||||
reminderPanel.add(reminderLabel, BorderLayout.CENTER);
|
|
||||||
reminderFrame.getContentPane().add(reminderPanel);
|
|
||||||
|
|
||||||
// Make the reminder frame visible
|
|
||||||
reminderFrame.setVisible(true);
|
|
||||||
|
|
||||||
// Set a timer to automatically close the reminder frame after 2 seconds
|
|
||||||
Timer timer = new Timer(2000, e -> {
|
|
||||||
reminderFrame.dispose();
|
|
||||||
});
|
|
||||||
timer.setRepeats(false);
|
|
||||||
timer.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Displays a reminder frame indicating that there must be at least one mathematical symbol
|
|
||||||
* +- * / The reminder frame is displayed for 2 seconds before automatically closing.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public void MultiplyOperator() {
|
|
||||||
// Create a reminder frame
|
|
||||||
JFrame reminderFrame = new JFrame("reminder");
|
|
||||||
reminderFrame.setBounds(625, 300, 400, 200);
|
|
||||||
reminderFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
||||||
|
|
||||||
// Create a label for the reminder message
|
|
||||||
JLabel reminderLabel = new JLabel("Multiple math symbols in a row ");
|
|
||||||
reminderLabel.setFont(new Font("Serif", Font.BOLD, 25));
|
|
||||||
reminderLabel.setHorizontalAlignment(JLabel.CENTER);
|
|
||||||
|
|
||||||
// Create a panel to hold the reminder label
|
|
||||||
JPanel reminderPanel = new JPanel(new BorderLayout());
|
|
||||||
reminderPanel.add(reminderLabel, BorderLayout.CENTER);
|
|
||||||
reminderFrame.getContentPane().add(reminderPanel);
|
|
||||||
|
|
||||||
// Make the reminder frame visible
|
|
||||||
reminderFrame.setVisible(true);
|
|
||||||
|
|
||||||
// Set a timer to automatically close the reminder frame after 2 seconds
|
|
||||||
Timer timer = new Timer(2000, e -> {
|
|
||||||
reminderFrame.dispose();
|
|
||||||
});
|
|
||||||
timer.setRepeats(false);
|
|
||||||
timer.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Displays a reminder frame indicating that there
|
|
||||||
* they must be at least one sign
|
|
||||||
*/
|
|
||||||
public void Symobol() {
|
|
||||||
// Create a reminder frame
|
|
||||||
JFrame reminderFrame = new JFrame("reminder");
|
|
||||||
reminderFrame.setBounds(625, 300, 400, 200);
|
|
||||||
reminderFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
||||||
|
|
||||||
// Create a label for the reminder message
|
|
||||||
JLabel reminderLabel = new JLabel("There must be at least one sign +-*/ !");
|
|
||||||
reminderLabel.setFont(new Font("Serif", Font.BOLD, 25));
|
|
||||||
reminderLabel.setHorizontalAlignment(JLabel.CENTER);
|
|
||||||
|
|
||||||
// Create a panel to hold the reminder label
|
|
||||||
JPanel reminderPanel = new JPanel(new BorderLayout());
|
|
||||||
reminderPanel.add(reminderLabel, BorderLayout.CENTER);
|
|
||||||
reminderFrame.getContentPane().add(reminderPanel);
|
|
||||||
|
|
||||||
// Make the reminder frame visible
|
|
||||||
reminderFrame.setVisible(true);
|
|
||||||
|
|
||||||
// Set a timer to automatically close the reminder frame after 2 seconds
|
|
||||||
Timer timer = new Timer(2000, e -> {
|
|
||||||
reminderFrame.dispose();
|
|
||||||
});
|
|
||||||
timer.setRepeats(false);
|
|
||||||
timer.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Displays a reminder frame explaining how to play the game.
|
|
||||||
* The reminder frame contains instructions on how to guess the hidden mathematical equation in 6 tries
|
|
||||||
* and how the color of the tiles changes to show the proximity to the correct solution.
|
|
||||||
* The reminder frame is displayed until closed by the user.
|
|
||||||
*/
|
|
||||||
public void HowtoPlayGame() {
|
|
||||||
// Create a frame to display the instructions
|
|
||||||
JFrame reminderFrame = new JFrame("How to play");
|
|
||||||
reminderFrame.setBounds(420, 150, 850, 850);
|
|
||||||
reminderFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
||||||
|
|
||||||
// Create a text area to display the instructions
|
|
||||||
JTextArea textArea = new JTextArea();
|
|
||||||
textArea.setLineWrap(true); // Enable automatic line wrapping
|
|
||||||
textArea.setWrapStyleWord(true);
|
|
||||||
textArea.setText("Game Rules. The aim of the game is to guess the hidden mathematical equation and the color of the tiles within 6 attempts. Players need to input a mathematical equation and then make guesses on the correct equation and the color of the tiles based on the hints provided. For example, if the hidden equation is 5 + 75 = 40, and the player inputs 5 + 52 = 10, then according to whether the positions and numbers are correct, certain parts of the tiles will turn green to indicate the correct parts."
|
|
||||||
|
|
||||||
);
|
|
||||||
|
|
||||||
// Set font and size for the text area
|
|
||||||
textArea.setFont(new Font("Serif", Font.ROMAN_BASELINE, 30));
|
|
||||||
|
|
||||||
// Create a panel to hold the text area
|
|
||||||
JPanel reminderPanel = new JPanel(new BorderLayout());
|
|
||||||
reminderPanel.add(textArea, BorderLayout.CENTER);
|
|
||||||
reminderFrame.getContentPane().add(reminderPanel);
|
|
||||||
|
|
||||||
// Make the reminder frame visible
|
|
||||||
reminderFrame.setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Displays a reminder frame indicating that the left side of the equation is not equal to the right side.
|
|
||||||
* Displays a reminder frame indicating that the left side of the equation is not equal to the right side.
|
|
||||||
*/
|
|
||||||
public void NotEqual() {
|
|
||||||
// Create a frame to display the reminder
|
|
||||||
JFrame reminderFrame = new JFrame("reminder");
|
|
||||||
reminderFrame.setBounds(625, 300, 500, 400);
|
|
||||||
reminderFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
||||||
|
|
||||||
// Create a label to display the reminder message
|
|
||||||
JLabel reminderLabel = new JLabel("The left side is not equal to the right");
|
|
||||||
reminderLabel.setFont(new Font("Serif", Font.BOLD, 25));
|
|
||||||
reminderLabel.setHorizontalAlignment(JLabel.CENTER);
|
|
||||||
|
|
||||||
// Create a panel to hold the label
|
|
||||||
JPanel reminderPanel = new JPanel(new BorderLayout());
|
|
||||||
reminderPanel.add(reminderLabel, BorderLayout.CENTER);
|
|
||||||
reminderFrame.getContentPane().add(reminderPanel);
|
|
||||||
|
|
||||||
// Make the reminder frame visible
|
|
||||||
reminderFrame.setVisible(true);
|
|
||||||
|
|
||||||
// Close the reminder frame after a delay
|
|
||||||
Timer timer = new Timer(2000, e -> {
|
|
||||||
reminderFrame.dispose();
|
|
||||||
});
|
|
||||||
timer.setRepeats(false);
|
|
||||||
timer.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Displays a reminder frame indicating the game start and prompts the user to guess the first equation.
|
|
||||||
* The reminder frame is displayed until closed by the user.
|
|
||||||
*/
|
|
||||||
private void showGameStartReminder() {
|
|
||||||
// Create a frame to display the reminder
|
|
||||||
JFrame reminderFrame = new JFrame("reminder");
|
|
||||||
reminderFrame.setBounds(625, 300, 450, 300);
|
|
||||||
reminderFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
||||||
|
|
||||||
// Create a label to display the reminder message
|
|
||||||
JLabel reminderLabel = new JLabel("Guess the first equation!");
|
|
||||||
reminderLabel.setFont(new Font("Serif", Font.BOLD, 25));
|
|
||||||
reminderLabel.setHorizontalAlignment(JLabel.CENTER);
|
|
||||||
|
|
||||||
// Create a panel to hold the label
|
|
||||||
JPanel reminderPanel = new JPanel(new BorderLayout());
|
|
||||||
reminderPanel.setBackground(Color.RED);
|
|
||||||
reminderPanel.add(reminderLabel, BorderLayout.CENTER);
|
|
||||||
reminderFrame.getContentPane().add(reminderPanel);
|
|
||||||
|
|
||||||
// Make the reminder frame visible
|
|
||||||
reminderFrame.setVisible(true);
|
|
||||||
|
|
||||||
// Close the reminder frame after a delay
|
|
||||||
Timer timer = new Timer(2000, e -> {
|
|
||||||
reminderFrame.dispose();
|
|
||||||
});
|
|
||||||
timer.setRepeats(false);
|
|
||||||
timer.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Displays a reminder frame indicating that there is no equal (=) sign in the input.
|
|
||||||
* The reminder frame is displayed until closed by the user.
|
|
||||||
*/
|
|
||||||
public void NoEqualSign() {
|
|
||||||
// Create a frame to display the reminder
|
|
||||||
JFrame reminderFrame = new JFrame("reminder");
|
|
||||||
reminderFrame.setBounds(625, 300, 400, 200);
|
|
||||||
reminderFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
||||||
|
|
||||||
// Create a label to display the reminder message
|
|
||||||
JLabel reminderLabel = new JLabel("No equal (=) sign!");
|
|
||||||
reminderLabel.setFont(new Font("Serif", Font.BOLD, 25));
|
|
||||||
reminderLabel.setHorizontalAlignment(JLabel.CENTER);
|
|
||||||
|
|
||||||
// Create a panel to hold the label
|
|
||||||
JPanel reminderPanel = new JPanel(new BorderLayout());
|
|
||||||
reminderPanel.add(reminderLabel, BorderLayout.CENTER);
|
|
||||||
reminderFrame.getContentPane().add(reminderPanel);
|
|
||||||
|
|
||||||
// Make the reminder frame visible
|
|
||||||
reminderFrame.setVisible(true);
|
|
||||||
|
|
||||||
// Close the reminder frame after a delay
|
|
||||||
Timer timer = new Timer(2000, e -> {
|
|
||||||
reminderFrame.dispose();
|
|
||||||
});
|
|
||||||
timer.setRepeats(false);
|
|
||||||
timer.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Displays a reminder frame indicating that the player has lost the game.
|
|
||||||
* Provides an option to replay the game.
|
|
||||||
*/
|
|
||||||
public void Youlost() {
|
|
||||||
// Create a frame to display the reminder
|
|
||||||
JFrame reminderFrame = new JFrame("reminder");
|
|
||||||
reminderFrame.setBounds(625, 300, 400, 200);
|
|
||||||
reminderFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
||||||
|
|
||||||
// Create a button for replaying the game
|
|
||||||
Button Replay = new Button("Replay");
|
|
||||||
Replay.addActionListener(e -> {
|
|
||||||
controller.startNewGame();
|
|
||||||
System.out.println("you replay the game so the number become " + " " + controller.getTargetWord());
|
|
||||||
for (int row = 0; row < ROWS; row++) {
|
|
||||||
for (int col = 0; col < COLS; col++) {
|
|
||||||
button[row][col].setText("");
|
|
||||||
button[row][col].setBackground(defaultColor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enable buttons for input
|
|
||||||
for (Component component : row1.getComponents()) {
|
|
||||||
if (component instanceof JButton) {
|
|
||||||
component.setEnabled(true);
|
|
||||||
component.setBackground(defaultColor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Enable operator buttons
|
|
||||||
for (Component component : row2.getComponents()) {
|
|
||||||
if (component instanceof JButton) {
|
|
||||||
component.setEnabled(true);
|
|
||||||
component.setBackground(defaultColor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Close the reminder frame
|
|
||||||
reminderFrame.dispose();
|
|
||||||
});
|
|
||||||
Replay.setBounds(10, 20, 10, 10);
|
|
||||||
|
|
||||||
// Create a label to display the reminder message
|
|
||||||
JLabel reminderLabel = new JLabel("You lost!");
|
|
||||||
reminderLabel.setFont(new Font("Serif", Font.BOLD, 25));
|
|
||||||
reminderLabel.setHorizontalAlignment(JLabel.CENTER);
|
|
||||||
|
|
||||||
// Create a panel to hold the label and replay button
|
|
||||||
JPanel reminderPanel = new JPanel(new BorderLayout());
|
|
||||||
reminderPanel.add(reminderLabel, BorderLayout.CENTER);
|
|
||||||
reminderPanel.add(Replay, BorderLayout.NORTH);
|
|
||||||
|
|
||||||
// Add the panel to the reminder frame
|
|
||||||
reminderFrame.getContentPane().add(reminderPanel);
|
|
||||||
|
|
||||||
// Make the replay button and reminder frame visible
|
|
||||||
Replay.setVisible(true);
|
|
||||||
reminderFrame.setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Displays a reminder frame indicating that the player has won the game.
|
|
||||||
* Provides an option to replay the game.
|
|
||||||
*/
|
|
||||||
public void Youwon() {
|
|
||||||
// Create a frame to display the reminder
|
|
||||||
JFrame reminderFrame = new JFrame("reminder");
|
|
||||||
reminderFrame.setBounds(625, 300, 400, 200);
|
|
||||||
reminderFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
||||||
|
|
||||||
// Create a button for restarting the game
|
|
||||||
Button Restart = new Button("Replay");
|
|
||||||
Restart.addActionListener(e -> {
|
|
||||||
Row = 0;
|
|
||||||
// Reset the game board
|
|
||||||
for (int row = 0; row < ROWS; row++) {
|
|
||||||
for (int col = 0; col < COLS; col++) {
|
|
||||||
button[row][col].setText("");
|
|
||||||
button[row][col].setBackground(defaultColor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Enable buttons for input
|
|
||||||
for (Component component : row1.getComponents()) {
|
|
||||||
if (component instanceof JButton) {
|
|
||||||
component.setEnabled(true);
|
|
||||||
component.setBackground(defaultColor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Enable operator buttons
|
|
||||||
for (Component component : row2.getComponents()) {
|
|
||||||
if (component instanceof JButton) {
|
|
||||||
component.setEnabled(true);
|
|
||||||
component.setBackground(defaultColor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Close the reminder frame
|
|
||||||
reminderFrame.dispose();
|
|
||||||
});
|
|
||||||
Restart.setBounds(10, 20, 10, 10);
|
|
||||||
|
|
||||||
// Create a label to display the reminder message
|
|
||||||
JLabel reminderLabel = new JLabel("You won!");
|
|
||||||
reminderLabel.setFont(new Font("Serif", Font.BOLD, 25));
|
|
||||||
reminderLabel.setHorizontalAlignment(JLabel.CENTER);
|
|
||||||
|
|
||||||
// Create a panel to hold the label and restart button
|
|
||||||
JPanel reminderPanel = new JPanel(new BorderLayout());
|
|
||||||
reminderPanel.add(reminderLabel, BorderLayout.CENTER);
|
|
||||||
reminderPanel.add(Restart, BorderLayout.NORTH);
|
|
||||||
|
|
||||||
// Add the panel to the reminder frame
|
|
||||||
reminderFrame.getContentPane().add(reminderPanel);
|
|
||||||
|
|
||||||
// Make the restart button and reminder frame visible
|
|
||||||
Restart.setVisible(true);
|
|
||||||
reminderFrame.setVisible(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
/**
|
public void update(java.util.Observable o, Object arg) {
|
||||||
* update the GUI ,If the view is change the observable will notice changed
|
attemptsLabel.setText("Attempts remaining: " + controller.getRemainingAttempts());
|
||||||
*
|
|
||||||
*/
|
|
||||||
public void update(Observable o, Object arg) {
|
|
||||||
|
|
||||||
|
|
||||||
if (controller.isGameOver()) {
|
|
||||||
if (controller.isGameWon()) {
|
|
||||||
Youwon();
|
|
||||||
controller.startNewGame();
|
|
||||||
System.out.println("The new number is" + " " + controller.getTargetWord());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Component component : row1.getComponents()) {
|
|
||||||
if (component instanceof JButton) {
|
|
||||||
component.setEnabled(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (Component component : row2.getComponents()) {
|
|
||||||
if (component instanceof JButton) {
|
|
||||||
component.setEnabled(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user