A. Introduction
How to code a problem like Scrabble?
The answer is: like peeling an onion - one layer at a time, always starting from the easiest part!
Layer 1: a method that takes a letter and returns the points of that letter. Don't worry about the letter's position on the game board yet. Input - a letter, String; output - the letter's points, int
Fully test Layer 1 before moving on!
Layer 2: a method that takes a square's number and returns if this square will double or triple of the value of a letter in this square. Don't worry about the word value yet. Input - a square's number, int; output - 1, 2, or 3, int
Fully test Layer 2 before moving on!
Layer 3: a method that takes a square's number and returns if the value of a word starting from this square will double or triple. Word is always 4-letter long. Input - a square's number, int; output - 1, 2, or 3, int
Fully test Layer 3 before moving on!
Assembly: 1) Word value = (sum of (letterPoints*letterMultiplier)) * wordMultiplier
2) repeat 5 times
Finishing up: 1) read data from a file instead of standard input.
2) test out difference delimiters
Now you try :) Here comes Scrabble Intermediate. Again the code base below is for your convenience. Please add/modify methods to bring Scrabble to the next level!
Code base:
---------------------------------------------Scrabble Junior Version-----------------------------------
import java.util.*; class Main { public static int points(String letter){ int points=0; switch(letter){ case "A": points=1;break; case "E": points=1;break; case "D": points=2;break; case "R": points=2;break; case "B": points=3;break; case "M": points=3;break; case "V": points=4;break; case "Y": points=4;break; case "J": points=8;break; case "X": points=8; } return points; } public static int letterMul(int num){ int mul =1; if(num==3||num==9||num==15||num==21||num==27||num==33||num ==39){ mul=2; } else if (num==5||num==10||num==20||num==25||num==30||num==35||num==40){ mul=3; } return mul; } public static int wordMul(int num){ int mul =1; if(num==7||num==14||num==28){ mul=2; } else if (num==8||num==16||num==24||num==32){ mul=3; } return mul; } public static void main(String[] args) { Scanner input = new Scanner(System.in); String[] letters = new String[4]; for(int i=0;i<4;i++){ letters[i]=input.next(); } input.nextLine(); for (int k=0;k<5;k++){ int total=0; int word =0; int start=input.nextInt(); for(int i=0;i<4;i++){ total+=points(letters[i])*letterMul(start+i); if(wordMul(start+i)>word){ word=wordMul(start+i); } // System.out.println("i: " + i +" word: "+ word); } System.out.println(total*word); } } }
import java.util.*; class Main { public static int points(String letter){ int points=0; switch(letter){ case "A": points=1;break; case "E": points=1;break; case "D": points=2;break; case "R": points=2;break; case "B": points=3;break; case "M": points=3;break; case "V": points=4;break; case "Y": points=4;break; case "J": points=8;break; case "X": points=8; } return points; } public static int letterMul(int num){ int mul =1; if(num==3||num==9||num==15||num==21||num==27||num==33||num ==39){ mul=2; } else if (num==5||num==10||num==20||num==25||num==30||num==35||num==40){ mul=3; } return mul; } public static int wordMul(int num){ int mul =1; if(num==7||num==14||num==28){ mul=2; } else if (num==8||num==16||num==24||num==32){ mul=3; } return mul; } public static void main(String[] args) { Scanner input = new Scanner(System.in); String[] letters = new String[4]; for(int i=0;i<4;i++){ letters[i]=input.next(); } input.nextLine(); for (int k=0;k<6;k++){ int total=0; int word =1; int start=input.nextInt(); String hotspot= input.next(); for(int i=0;i<4;i++){ total+=points(letters[i])*letterMul(start+i); if (hotspot == "H"){ if(wordMul(start+i)>word){ word=wordMul(start+i); } } if(hotspot == "V"){ if(wordMul(start+(i*10))>word){ word=wordMul(start+(i*10)); } System.out.println(total*word); } } } } }
import java.util.*;
class Scrabblev2 {
public static int points(String letter){
int points=0;
switch(letter){
case "A": points=1;break;
case "E": points=1;break;
case "D": points=2;break;
case "R": points=2;break;
case "B": points=3;break;
case "M": points=3;break;
case "V": points=4;break;
case "Y": points=4;break;
case "J": points=8;break;
case "X": points=8;
}
return points;
}
public static int letterMul(int num){
int mul =1;
if(num==3||num==9||num==15||num==21||num==27||num==33||num ==39){
mul=2;
}
else if (num==5||num==10||num==20||num==25||num==30||num==35||num==40){
mul=3;
}
return mul;
}
public static int wordMul(int num){
int mul =1;
if(num==7||num==14||num==28){
mul=2;
}
else if (num==8||num==16||num==24||num==32){
mul=3;
}
return mul;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] letters = new String[4];
for(int i=0;i<4;i++){
letters[i]=input.next();
}
input.nextLine();
for (int k=0;k<6;k++){
int total=0;
int word =1;
int start=input.nextInt();
String direc = input.next();
for(int i=0;i<4;i++){
total+=points(letters[i])*letterMul(start+i);
if(direc == "H")
{
if(wordMul(start+i)>word){
word=wordMul(start+i);
}
}
if(direc == "V")
{
if(wordMul(start+(i*10))>word){
word=wordMul(start+(i*10));
}
}
//System.out.println("i: " + i +" word: "+ word);
}
System.out.println(total*word);
}
}
}
import java.util.*;
import java.io.*;
class Scrabble {
public static int getLetterValue(String letter){
int lettervalue = 0;
switch (letter){
case "A": lettervalue = 1; break;
case "E": lettervalue = 1; break;
case "D": lettervalue = 2; break;
case "R": lettervalue = 2; break;
case "B": lettervalue = 3; break;
case "M": lettervalue = 3; break;
case "V": lettervalue = 4; break;
case "Y": lettervalue = 4; break;
case "J": lettervalue = 8; break;
case "X": lettervalue = 8; break;
default: break;
}
return lettervalue;
}
public static boolean isHorizontal(String input){
boolean horizontal = false;
if(input.equals("H")){horizontal=true;}
return horizontal;
}
public static int getLetterMultiplier(int position){
int multiplier = 1;
if(position%3 == 0 && position%2 != 0){multiplier = 2;}
else if(position%5 == 0){multiplier = 3;}
return multiplier;
}
public static int getWordValue(String word, int firstLetterPosition, boolean horizontal){
int wordvalue = 0;
if(horizontal){
for(int i=0; i<4; i++){
wordvalue += getLetterValue(word.charAt(i)+"")*getLetterMultiplier(firstLetterPosition+i);
}
}
else{
for(int i=0; i<4; i++){
wordvalue += getLetterValue(word.charAt(i)+"")*getLetterMultiplier(firstLetterPosition+ i*10);
}
}
return wordvalue;
}
public static int getWordMultiplier(int firstLetterPosition, boolean horizontal){
int multiplier = 1;
if(horizontal){
for(int i=firstLetterPosition; i<firstLetterPosition+4; i++){
if(i%7 == 0 && getLetterMultiplier(i)==1){multiplier = 2;}
else if(i%8 == 0 && getLetterMultiplier(i)==1 && multiplier == 1){multiplier = 3;}
}
}
else{
for(int i=0; i<4; i++){
if((i*10 +firstLetterPosition)%7 == 0 && getLetterMultiplier(i*10 +firstLetterPosition)==1){multiplier = 2;}
else if((i*10 +firstLetterPosition)%8 == 0 && getLetterMultiplier(i*10 +firstLetterPosition)==1 && multiplier == 1){multiplier = 3;}
}
}
return multiplier;
}
public static int getPoints(String word, int firstLetterPosition, boolean horizontal){
return getWordValue(word, firstLetterPosition, horizontal) * getWordMultiplier(firstLetterPosition, horizontal);
}
public static void main(String[] args) throws IOException{
Scanner Howard = new Scanner(new FileReader("data/input.txt"));
String input = Howard.nextLine().replaceAll(", ", "");
for(int i=0; i<5; i++){
String info[] = Howard.nextLine().split(", ");
int position = Integer.parseInt(info[0]);
boolean horizontal = isHorizontal(info[1]);
System.out.println(getPoints(input, position, horizontal));
}
}
}
https://repl.it/@DaveyYu/Scrabble
https://repl.it/@Brainseater905/ClientsideStainedState
wait... is this for sunday class