-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq16p15.java
More file actions
77 lines (72 loc) · 2.21 KB
/
Copy pathq16p15.java
File metadata and controls
77 lines (72 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.Scanner;
public class q16p15{
public static void main(String[] args){
String robot = "RGGB";
Scanner scanner = new Scanner( System.in);
//System.out.print( "Type some data for the program: " );
//String input = scanner.nextLine();
boolean allHit = false;
while(!allHit){
System.out.print( "Type some data for the program: " );
String input = scanner.nextLine();
//System.out.println(">> "+ input);
allHit = compare(input, robot);
}
System.out.println("--end--");
}
public static boolean compare(String input, String robot){
int len = robot.length();
if(input.length() != len){
System.out.println("Input size error!");
return false;
}
int[] counter = new int[len];
int hits = 0;
for(int i = 0; i<len; i++){
if(input.charAt(i) == robot.charAt(i)){
hits++;
}else{
counter[code(robot.charAt(i))]++;
}
}
if(hits == 4){
System.out.println("All fit!");
return true;
}
int pseudoHits = 0;
for(int i = 0; i<len; i++){
if(input.charAt(i)!=robot.charAt(i) && counter[code(input.charAt(i))]>0){
pseudoHits++;
counter[code(input.charAt(i))]--;
}
}
if(hits > 0 && pseudoHits >0){
System.out.println("Hit & Pseudo-hit");
return false;
}else if(hits == 0 && pseudoHits>0){
System.out.println("Only Pseudo-hit");
return false;
}else if(hits==0 && pseudoHits==0){
System.out.println("None");
return false;
}else if(hits > 0 && pseudoHits==0){
System.out.println("Only Hit");
return false;
}
return false;
}
public static int code(char c){
switch(c){
case 'B':
return 0;
case 'G':
return 1;
case 'R':
return 2;
case 'Y':
return 3;
default:
return -1;
}
}
}