-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 03a8713
Showing
11 changed files
with
440 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# 24-Cards-Game Solver using Brute Force Algorithm | ||
|
||
The 24 card game is an arithmetic card game with the goal of finding ways to change four random numbers so that the final result is a total of 24. The game is quite popular due to its ability to improve counting skills and sharpen the mind to think quickly and accurately. The 24 Card Game is usually played using a deck of playing cards. Playing cards consist of 52 cards divided into four suits (spades, hearts, diamonds and clubs) each consisting of 13 cards (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, and King). | ||
|
||
This program shows all possibility of operation combinations through 4 cards number so that the result of the expression could be equal to 24. Technically this program use Brute Force Algorithm to find the solutions. | ||
|
||
## Requirements | ||
|
||
1. Java (java 19.0.2 recommended) | ||
|
||
## Run | ||
|
||
1. Clone this repository | ||
2. Go to root directory | ||
3. Run `.\main.bat` in terminal | ||
|
||
## Author | ||
|
||
[`13521046 Jeffrey Chow`](https://github.com/JeffreyChow19) |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
cd src | ||
javac -d ../bin *.java | ||
cd ../bin | ||
java main | ||
cd .. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package datatype; | ||
|
||
public class StrDoub { | ||
public String expr; | ||
public Double res; | ||
|
||
public StrDoub (String expr, Double res){ | ||
this.expr = expr; | ||
this.res = res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
import java.util.*; | ||
import java.security.SecureRandom; | ||
import java.io.*; | ||
import datatype.*; | ||
|
||
public class main extends shortcut { | ||
// global variables | ||
public static Scanner scan = new Scanner(System.in); | ||
public static long startTime = 0; | ||
public static long endTime = 0; | ||
|
||
public static void main(String[] args) { | ||
// splash screen | ||
println("\nWelcome to 24 Games Solver"); | ||
println("=========================="); | ||
println(); | ||
|
||
// menu options | ||
println("Options: "); | ||
println("[0] Exit"); | ||
println("[1] Input Cards from Console"); | ||
println("[2] Auto Generate cards"); | ||
println(); | ||
|
||
int option; | ||
|
||
do { // input validation | ||
print("Enter option [0-2]: "); | ||
option = scan.nextInt(); | ||
scan.nextLine(); | ||
} while (option < 0 || option > 2); | ||
|
||
println(); | ||
|
||
switch(option){ | ||
case 1: | ||
userInput(); | ||
break; | ||
case 2: | ||
autoInput(); | ||
break; | ||
default: | ||
println("See ya! Thank you mate!"); | ||
System.exit(0); | ||
break; | ||
} | ||
|
||
println("\nExecution time : " + (endTime-startTime) + " miliseconds"); | ||
} | ||
|
||
public static void userInput(){ | ||
println("User Input"); | ||
println("=========="); | ||
println(); | ||
|
||
String[] inputs; | ||
String input; | ||
boolean reinput; | ||
|
||
do{ // inputs validation | ||
reinput = false; | ||
|
||
// user input | ||
print("Input : "); | ||
input = scan.nextLine(); | ||
|
||
// trim and split the input | ||
inputs = Arrays.copyOfRange(input.trim().split("[ ]+"), 0, 4); | ||
|
||
// validation of each input | ||
int i = 0; | ||
while (!reinput && i < inputs.length){ | ||
int ascii = (int)(inputs[i].toLowerCase().charAt(0)); | ||
if (inputs[i].length() > 1){ | ||
reinput = (inputs[i] != "10"); | ||
} else if ((ascii == 97) || (ascii == 106) || (ascii == 107) || (ascii == 113) || (ascii >= 50 && ascii <= 57)) { | ||
reinput = false; | ||
} else { | ||
reinput = true; | ||
} | ||
i++; | ||
} | ||
|
||
} while (reinput); | ||
|
||
parseInput(inputs); | ||
|
||
} | ||
|
||
public static void autoInput(){ | ||
println("Auto Generate Cards"); | ||
println("==================="); | ||
println(); | ||
|
||
String[] inputs = new String[4]; | ||
String[] cards = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; | ||
|
||
// randomizer | ||
SecureRandom rand = new SecureRandom(); | ||
|
||
print("Your cards are : "); | ||
|
||
// random 4 cards | ||
for (int i = 0; i < 4; i++){ | ||
inputs[i] = cards[rand.nextInt(13)]; | ||
print(inputs[i] + ' '); | ||
} | ||
|
||
println(); | ||
|
||
parseInput(inputs); | ||
} | ||
|
||
public static void parseInput(String[] inputs){ | ||
int[] numbers = new int[4]; | ||
|
||
// create dictionary | ||
Hashtable<String, Integer> dict = new Hashtable<String, Integer>(); | ||
dict.put("A", 1); | ||
dict.put("a", 1); | ||
dict.put("2", 2); | ||
dict.put("3", 3); | ||
dict.put("4", 4); | ||
dict.put("5", 5); | ||
dict.put("6", 6); | ||
dict.put("7", 7); | ||
dict.put("8", 8); | ||
dict.put("9", 9); | ||
dict.put("10", 10); | ||
dict.put("J", 11); | ||
dict.put("j", 11); | ||
dict.put("Q", 12); | ||
dict.put("q", 12); | ||
dict.put("K", 13); | ||
dict.put("k", 13); | ||
|
||
// parse | ||
for (int i = 0; i < inputs.length; i++){ | ||
numbers[i] = dict.get(inputs[i]); | ||
} | ||
|
||
// start timer | ||
startTime = System.currentTimeMillis(); | ||
|
||
// call for solution | ||
solution.solution(numbers); | ||
|
||
} | ||
|
||
public static void output(Set<StrDoub> solutions){ | ||
// end timer | ||
endTime = System.currentTimeMillis(); | ||
|
||
println("\nOutput options : "); | ||
println("[1] Console"); | ||
println("[2] Text File"); | ||
println(); | ||
|
||
int option; | ||
|
||
do { // input validation | ||
print("Enter option [1-2]: "); | ||
option = scan.nextInt(); | ||
scan.nextLine(); | ||
} while (option < 1 || option > 2); | ||
|
||
switch(option){ | ||
case 1: | ||
outputConsole(solutions); | ||
break; | ||
case 2: | ||
try { | ||
outputFile(solutions); | ||
} catch (IOException ex) { | ||
println("Failed to create file. Please try again..."); | ||
output(solutions); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
public static void outputConsole(Set<StrDoub> solutions){ | ||
println((solutions.size() == 0) ? "There is no solution" : ("There are " + solutions.size() + " solutions\n")); | ||
int i = 1; | ||
|
||
// print all results | ||
for (StrDoub solution : solutions){ | ||
print(i + ". "); | ||
println(solution.expr); | ||
i++; | ||
} | ||
} | ||
|
||
public static void outputFile(Set<StrDoub> solutions) throws IOException{ | ||
// user input filename | ||
print("Output file name [___.txt] : "); | ||
String filename = scan.nextLine(); | ||
|
||
// initialize buffer | ||
String output = ""; | ||
|
||
// header | ||
output += (solutions.size() == 0) ? "There is no solution" : ("There are " + solutions.size() + " solutions\n"); | ||
|
||
// write contents | ||
int i = 1; | ||
for (StrDoub solution : solutions){ | ||
output += (i + ". " + solution.expr +'\n'); | ||
i++; | ||
} | ||
|
||
// write to file | ||
FileWriter writer = new FileWriter("../test/" + filename); | ||
writer.write(output); | ||
writer.close(); | ||
|
||
// success message | ||
print("Successfully added "+ filename +" to test folder."); | ||
} | ||
|
||
} | ||
|
||
class shortcut { | ||
|
||
public static void print(char item){ | ||
System.out.print(item); | ||
} | ||
|
||
public static void print(int item){ | ||
System.out.print(item); | ||
} | ||
|
||
public static void print(String item){ | ||
System.out.print(item); | ||
} | ||
|
||
public static void println(){ | ||
System.out.println(); | ||
} | ||
|
||
public static void println(char item){ | ||
System.out.println(item); | ||
} | ||
|
||
public static void println(int item){ | ||
System.out.println(item); | ||
} | ||
|
||
public static void println(String item){ | ||
System.out.println(item); | ||
} | ||
|
||
public static void printArr(int[] arr){ | ||
Arrays.stream(arr).forEach(System.out::print); | ||
println(); | ||
} | ||
} |
Oops, something went wrong.