Skip to content

Commit a86bc51

Browse files
committed
the tame of thrones code is added
this code is submitted to geektrust and is also kept in the secure web for furthur generations to come
1 parent 01acf31 commit a86bc51

36 files changed

+1937
-7485
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
public class AIR implements Kingdom {
3+
private static final String King = "Roy";
4+
private static final String emblem;
5+
6+
private static Kingdom ally = null;
7+
static {
8+
emblem = "owl";
9+
}
10+
11+
public static String getKing() {
12+
return King;
13+
}
14+
15+
@Override
16+
public String ruler() {
17+
//there has to be one ruler for every state
18+
return King;
19+
}
20+
21+
@Override
22+
public void getMessage(Kingdom sender, String message) {
23+
if (checkMessage(message)){
24+
setAlly(sender);
25+
}
26+
}
27+
28+
private boolean checkMessage(String message) {
29+
char[] EmblemArray = emblem.toCharArray();
30+
/**
31+
* this is a brute force technique and will be improved if required
32+
*/
33+
34+
for (char emlm : EmblemArray){
35+
if(containsChar(message, emlm) && (occurances(message, emlm, 0) >= occurances(emblem, emlm, 0))){
36+
// this computation is resource extensive and there fore must be thought of if it goes to production
37+
}else{
38+
return false;
39+
}
40+
}
41+
return true;
42+
}
43+
public boolean containsChar(String s, char search) {
44+
if (s.length() == 0)
45+
return false;
46+
else
47+
return s.charAt(0) == search || containsChar(s.substring(1), search);
48+
}
49+
public int occurances(String s, char search, int initSize){
50+
if(s.length() == 0)
51+
return initSize;
52+
//go to the end of the string
53+
initSize = occurances(s.substring(1), search, initSize);
54+
55+
if (s.charAt(0) == search)
56+
initSize +=1;
57+
58+
return initSize;
59+
}
60+
61+
@Override
62+
public void setAlly(Kingdom alliegience) {
63+
if(ally == null){
64+
ally = alliegience;
65+
}else {
66+
/**
67+
* this is the primary prevalent constraint given to the code
68+
* to make codes less redundant to external influendes ... once its set it should not
69+
* change
70+
*
71+
* later other antilocking systems will be introduced
72+
*/
73+
}
74+
}
75+
76+
@Override
77+
public Kingdom getAlly() {
78+
if (ally==null){
79+
return null;
80+
81+
}
82+
return ally;
83+
}
84+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
2+
3+
import java.util.HashSet;
4+
import java.util.Scanner;
5+
import java.util.Set;
6+
7+
public class Commencer {
8+
public static void main(String[] args) {
9+
10+
/**
11+
* all the stock states taking part in the game of thrones
12+
* this has been done to remove cases of litter objects all around
13+
*/
14+
SPACE space =new SPACE();
15+
/**
16+
* space kingdom is the present contender to the throne but this
17+
* code has been adapted and tested to be easily replaced by any
18+
* other kingdom who wants to send secret messages to others
19+
*
20+
* this code does not support multiple kingdoms sending messages to each other
21+
* later support for this functionality might be added
22+
*/
23+
AIR air = new AIR();
24+
ICE ice =new ICE();
25+
FIRE fire =new FIRE();
26+
LAND land =new LAND();
27+
WATER water =new WATER();
28+
29+
/**
30+
* we have a director node that acts as the commutator service
31+
*/
32+
Convenor convenor =new Convenor();
33+
Scanner scanner = new Scanner(System.in);
34+
/**
35+
* find who is the present king of southeros
36+
*/
37+
38+
findPresentKingOfSoutheros();
39+
boolean reentrancy = true; //this states how the input loop will be blocked
40+
Set<Kingdom> alligiances = new HashSet<Kingdom>();
41+
while (reentrancy){
42+
System.out.println("give the message ->> [ STATE- in caps] [message]");
43+
String state= scanner.next();
44+
String message = scanner.nextLine();
45+
/**
46+
* space council will send the messages all other kingdoms to get tbeir allegiance
47+
*/
48+
49+
switch (state){
50+
case "ICE":
51+
convenor.sendMessage(space, ice,message);
52+
if(convenor.checkAlligience(space, ice)){
53+
alligiances.add(ice);
54+
}
55+
break;
56+
case "AIR":
57+
convenor.sendMessage(space, air,message);
58+
if(convenor.checkAlligience(space, air)){
59+
alligiances.add(air);
60+
}
61+
break;
62+
case "FIRE":
63+
convenor.sendMessage(space, fire,message);
64+
if(convenor.checkAlligience(space, fire)){
65+
alligiances.add(fire);
66+
}
67+
break;
68+
case "LAND":
69+
convenor.sendMessage(space, land,message);
70+
if(convenor.checkAlligience(space, land)){
71+
alligiances.add(land);
72+
}
73+
break;
74+
case "WATER":
75+
convenor.sendMessage(space, water,message);
76+
if(convenor.checkAlligience(space, water)){
77+
alligiances.add(water);
78+
}
79+
break;
80+
81+
82+
}
83+
84+
/**
85+
* declare the king of southeros
86+
*/
87+
if(alligiances.size() >=3){
88+
convenor.declareKingOfSoutheros(space.ruler());
89+
//give the outputs
90+
findPresentKingOfSoutheros();
91+
System.out.println("Allies of ruler ::" );
92+
displayAlliances(alligiances);
93+
}else {
94+
System.out.println("Minimum alligience level of allies not met yet ");
95+
System.out.println("you need to send more messages to confirm your seat ");
96+
}
97+
/**
98+
* check to set reentrancy
99+
*/
100+
System.out.println();
101+
System.out.println("Continue sending [y/n]");
102+
String flag = scanner.next();
103+
boolean flagNextCase = true;
104+
while (flagNextCase){
105+
flag = scanner.next();
106+
if(flag.equalsIgnoreCase("n")){
107+
flagNextCase = false;
108+
reentrancy =false;
109+
110+
}else if(flag.equalsIgnoreCase("y")){
111+
System.out.println("Ok lets try another input :: ");
112+
reentrancy = true;
113+
flagNextCase =false;
114+
}else {
115+
System.out.println("try again >> ");
116+
}
117+
}
118+
119+
120+
}
121+
/**
122+
* display all the data all over again
123+
*/
124+
System.out.println("the final results of the several proxy messages >> ");
125+
findPresentKingOfSoutheros();
126+
System.out.println("Allies of ruler ::" );
127+
displayAlliances(alligiances);
128+
}
129+
130+
131+
private static void displayAlliances(Set<Kingdom> alligiances) {
132+
for(Kingdom kingdom: alligiances){
133+
System.out.println(kingdom.getClass().getName());
134+
}
135+
}
136+
137+
private static void findPresentKingOfSoutheros() {
138+
System.out.println("present king of Southeros " + presentKingOfSoutheros());
139+
}
140+
141+
private static String presentKingOfSoutheros() {
142+
return new Southeros().ruler();
143+
}
144+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
public class Convenor {
3+
Kingdom kingdom;
4+
public void sendMessage(Kingdom sender, Kingdom receiver , String message){
5+
receiver.getMessage(sender, message.toLowerCase());
6+
}
7+
public boolean checkAlligience(Kingdom from, Kingdom to){
8+
if(to.getAlly() != null){
9+
return from.getClass().getName().equals(to.getAlly().getClass().getName());
10+
}
11+
return false;
12+
}
13+
14+
public void declareKingOfSoutheros(String king) {
15+
new Southeros().declareRuler(king);
16+
}
17+
public String presentKingOfKingdom(Kingdom kingdom){
18+
return kingdom.ruler();
19+
}
20+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
3+
public class FIRE implements Kingdom{
4+
private static final String King = "ShotKing";
5+
private static final String emblem;
6+
7+
private static Kingdom ally;
8+
9+
static {
10+
ally = null;
11+
emblem = "dragon";
12+
}
13+
14+
public static String getKing() {
15+
return King;
16+
}
17+
18+
@Override
19+
public String ruler() {
20+
//there has to be one ruler for every state
21+
return King;
22+
}
23+
24+
@Override
25+
public void getMessage(Kingdom sender, String message) {
26+
if (checkMessage(message)){
27+
setAlly(sender);
28+
}
29+
}
30+
31+
private boolean checkMessage(String message) {
32+
char[] EmblemArray = emblem.toCharArray();
33+
/**
34+
* this is a brute force technique and will be improved if required
35+
*/
36+
37+
for (char emlm : EmblemArray){
38+
if(containsChar(message, emlm) && (occurances(message, emlm, 0) >= occurances(emblem, emlm, 0))){
39+
// this computation is resource extensive and there fore must be thought of if it goes to production
40+
}else{
41+
return false;
42+
}
43+
}
44+
return true;
45+
}
46+
public boolean containsChar(String s, char search) {
47+
if (s.length() == 0)
48+
return false;
49+
else
50+
return s.charAt(0) == search || containsChar(s.substring(1), search);
51+
}
52+
public int occurances(String s, char search, int initSize){
53+
if(s.length() == 0)
54+
return initSize;
55+
//go to the end of the string
56+
initSize = occurances(s.substring(1), search, initSize);
57+
58+
if (s.charAt(0) == search)
59+
initSize +=1;
60+
61+
return initSize;
62+
}
63+
64+
@Override
65+
public void setAlly(Kingdom alliegience) {
66+
if(ally == null){
67+
ally = alliegience;
68+
}else {
69+
/**
70+
* this is the primary prevalent constraint given to the code
71+
* to make codes less redundant to external influendes ... once its set it should not
72+
* change
73+
*
74+
* later other antilocking systems will be introduced
75+
*/
76+
}
77+
}
78+
@Override
79+
public Kingdom getAlly() {
80+
if (ally==null){
81+
return null;
82+
83+
}
84+
return ally;
85+
}
86+
87+
}

0 commit comments

Comments
 (0)