Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I take info(letters and ints) from a file, and store that information for further use?

I'm going to try to be as specific as possible, but please don't be upset if this isn't perfect, this is my first post.

I'm creating a scrabble scoring class, that takes in two files: a letter value txt file and a txt file that contains some words that we will later determine each word's value based off the letters it contains, just like in a game of Scrabble.

The letter value text file will have 26 lines, each line having a random letter of the alphabet, a space, and a certain letter value integer that corresponds with said letter in said line. Like this:

A 1

E 1

F 3

.

.

.

Z 15

I know how to read each line and store that, but I don't know how to set this up so that it matches each letter in the file with it's corresponding number value. I'm almost thinking of doing two arrays... Any help is appreciated. Let me know if you need more information.

like image 569
JimBobCooter Avatar asked Dec 21 '25 03:12

JimBobCooter


1 Answers

You can use a Map to look up point values. Then you don't have to worry about converting characters to an index, etc.

Map<Integer, Integer> points = new HashMap<>();
Pattern p = Pattern.compile("([A-Z])\\s+(\\d+)");
for (String line : Files.readAllLines(Paths.get("points.txt"))) {
  Matcher m = p.matcher(line);
  if (!m.matches())
    throw new IllegalArgumentException(line);
  points.put((int) m.group(1).charAt(0), Integer.valueOf(m.group(2)));
}

Then you can do fun things with your points map, like this:

int score = "HELLO".chars().map(points::get).sum();

Of course, you'll need to account for the bonus due to each letter's location, etc., but this might help you get started.

like image 107
erickson Avatar answered Dec 23 '25 16:12

erickson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!