Here is a game - see if you can figure out the rules :). Then think about input, output, and the steps to solve the game Challenge of the week: finish coding the game above
import java.util.Scanner; class Main { public static boolean checkA(int CordRow,int CordCol,int SecNum,int TrdNum){ if(SecNum == 2 && TrdNum == 1 && CordCol+1+2<= 10 || SecNum == 1 && TrdNum == 2 && CordCol-2>= 1) return true; else return false; } public static boolean checkB(int CordRow,int CordCol,int SecNum,int TrdNum){ if(SecNum == 2 && TrdNum == 1 && CordCol+1+2<= 10 || SecNum == 1 && TrdNum == 2 && CordCol-2>= 1) return true; else return false; } public static boolean checkC(int CordRow,int CordCol,int SecNum,int TrdNum){ if(SecNum == 2 && TrdNum == 1 && CordCol+1+2<= 10 || SecNum == 1 && TrdNum == 2 && CordCol-2>= 1) return true; else return false; } public static boolean checkD(int CordRow,int CordCol,int SecNum,int TrdNum){ if(SecNum == 2 && TrdNum == 1 && CordCol+1+2<= 10 || SecNum == 1 && TrdNum == 2 && CordCol-2>= 1) return true; else return false; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String InputLetter = in.next(); String InputNumber = in.next(); String Cord = InputLetter.concat(InputNumber); int CordRow = Cord.charAt(0)-64; int CordCol = Cord.charAt(1)-48; String other = Cord; boolean none = true; int SecNum = in.nextInt(); int TrdNum = in.nextInt(); if(checkA(CordRow,CordCol,SecNum,TrdNum)){ System.out.print("A "); none = false; } if(checkB(CordRow,CordCol,SecNum,TrdNum)){ System.out.print("B "); none = false; } if(checkC(CordRow,CordCol,SecNum,TrdNum)){ System.out.print("C "); none = false; } if(checkD(CordRow,CordCol,SecNum,TrdNum)){ System.out.print("D "); none = false; } if(none == true){ System.out.println("NONE"); } } }
Thanks Rick! Works great! I am including the complete solution game board for everyone's reference.
import java.util.Scanner;
class Board{
int[] aPos;
int tile;
Board(int[] aPos, int tile){
this.aPos = aPos;
this.tile = tile;
}
public boolean check(int[] pos){
if(pos[0]==aPos[0] && (pos[1]==aPos[1] || pos[1]==aPos[1]+1))
return false;
else if(pos[0]==aPos[0]-1 && (pos[1]==aPos[1] || pos[1]==aPos[1]+1))
return false;
else if(pos[0]==aPos[0] && pos[1]==aPos[1]-1)
return false;
else if(pos[0]==aPos[0] && pos[1]==aPos[1]+2)
return false;
else if(pos[0]==aPos[0]+1 && (pos[1]==aPos[1] || pos[1]==aPos[1]+1))
return false;
else if(pos[0]>9 || pos[0]<0 || pos[1]>9 || pos[1]<0)
return false;
else
return true;
}
public boolean placeA(int[] temPos){
int pos[] = temPos.clone();
if(tile == 2)
pos[1]--;
if(check(pos)){
pos[1]++;
return check(pos);
}
else
return false;
}
public boolean placeB(int[] temPos){
int pos[] = temPos.clone();
if(tile != 1){
pos[1]--;
if(tile == 3)
pos[0]++;
}
if(check(pos)){
pos[1]++;
if(check(pos)){
pos[0]--;
return check(pos);
}
else
return false;
}
else
return false;
}
public boolean placeC(int[] temPos){
int pos[] = temPos.clone();
if(tile != 1){
pos[0]++;
if(tile == 2)
pos[1]++;
if(tile == 4)
pos[1]--;
}
if(check(pos)){
pos[0]--;
if(check(pos)){
pos[1]--;
if(check(pos)){
pos[1] += 2;
return check(pos);
}
else
return false;
}
else
return false;
}
else
return false;
}
public boolean placeD(int[] temPos){
int pos[] = temPos.clone();
if(tile != 1){
pos[0]++;
if(tile != 3){
pos[1]++;
if(tile == 4)
pos[0]++;
}
}
if(check(pos)){
pos[0]--;
if(check(pos)){
pos[1]--;
if(check(pos)){
pos[0] --;
return check(pos);
}
else{
return false;
}
}
else{
return false;
}
}
else{
return false;
}
}
public String[] getOutputs(int aTile){
boolean[] booleanOutputs = new boolean[4];
int[] pos1 = new int[2];
int[] pos2 = new int[2];
if(aTile == 1){
pos1[0] = aPos[0]+1;
pos1[1] = aPos[1]-1;
pos2[0] = aPos[0]-1;
pos2[1] = aPos[1]-1;
}
else{
pos1[0] = aPos[0]+1;
pos1[1] = aPos[1]+2;
pos2[0] = aPos[0]-1;
pos2[1] = aPos[1]+2;
}
if(placeA(pos1) || placeA(pos2))
booleanOutputs[0] = true;
if(placeB(pos1) || placeB(pos2))
booleanOutputs[1] = true;
if(placeC(pos1) || placeC(pos2))
booleanOutputs[2] = true;
if(placeD(pos1) || placeD(pos2))
booleanOutputs[3] = true;
int count = 0;
for(boolean i : booleanOutputs)
if(i)
count++;
String outputs[] = new String[count];
int i = 0;
if(booleanOutputs[0]){
outputs[i] = "A";
i++;
}
if(booleanOutputs[1]){
outputs[i] = "B";
i++;
}
if(booleanOutputs[2]){
outputs[i] = "C";
i++;
}
if(booleanOutputs[3]){
outputs[i] = "D";
i++;
}
return outputs;
}
}
class Main {
public static int translate(String input){
int output = 0;
switch(input){
case "A": output = 0; break;
case "B": output = 1; break;
case "C": output = 2; break;
case "D": output = 3; break;
case "E": output = 4; break;
case "F": output = 5; break;
case "G": output = 6; break;
case "H": output = 7; break;
case "I": output = 8; break;
case "J": output = 9; break;
}
return output;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
for(int i=0; i<5; i++){
String inputs[] = in.nextLine().split(", ");
int[] aPos = {translate(inputs[0].charAt(0)+""),inputs[0].charAt(1)-49};
Board blokus = new Board(aPos,Integer.parseInt(inputs[2]));
String outputs[] = blokus.getOutputs(Integer.parseInt(inputs[1]));
if(outputs.length!=0){
for(int j=0; j<outputs.length; j++){
System.out.print(outputs[j]);
if(j!=outputs.length-1)
System.out.print(", ");
}
System.out.println("");
}
else
System.out.println("NONE");
}
}
}