-
-
Save rinp/dcdadbf2f7816d3109d8e9625c52647e to your computer and use it in GitHub Desktop.
simply
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package naka; | |
import java.util.Random; | |
/** | |
* Created by rinp on 2016/11/07. | |
*/ | |
public class AlgorithmOne { | |
private static AlgorithmOne algo = new AlgorithmOne(); | |
Condition wolf = Condition.LEFT_FIELD; | |
Condition goat = Condition.LEFT_FIELD; | |
Condition cabbage = Condition.LEFT_FIELD; | |
/** | |
* 右に進む場合trueとなる | |
*/ | |
boolean goRight = true; | |
/** | |
* boat isNot on the river | |
*/ | |
boolean boatCondition = true; | |
private AlgorithmOne() { | |
} | |
protected static AlgorithmOne getInstance() { | |
return algo; | |
} | |
/** | |
* @return 処理が終わる条件 | |
*/ | |
protected boolean allNotThree() { | |
return !(Condition.RIGHT_FIELD == wolf && Condition.RIGHT_FIELD == goat | |
&& Condition.RIGHT_FIELD == cabbage); | |
} | |
/** | |
* @return 右に進むかどうか | |
*/ | |
protected boolean goRight() { | |
return goRight; | |
} | |
/** | |
* boatが逆に進む為の処理 | |
*/ | |
private void phaseChange() { | |
goRight = !goRight; | |
} | |
/** | |
* @return 陸の上かどうか | |
*/ | |
protected boolean getOnTheField() { | |
// 陸上であればtrue | |
return boatCondition; | |
} | |
/** | |
* 陸か川上かどうかの切り替え | |
*/ | |
private void phaseChangeBoat() { | |
boatCondition = !boatCondition; | |
} | |
protected void exeOnTheField(boolean isRight) { | |
// 右に進む場合 | |
if (isRight) { | |
// 全てがいる場合 | |
if (checkAllOne()) { | |
goat = Condition.ON_THE_RIVER; | |
phaseChangeBoat(); | |
} // どちらかを移動させる | |
else if (isNotThereGoatLeftField()) { | |
// ヤギがいない場合にチェンジ | |
if (whichSelect()) { | |
wolf = Condition.ON_THE_RIVER; | |
phaseChangeBoat(); | |
} else { | |
cabbage = Condition.ON_THE_RIVER; | |
phaseChangeBoat(); | |
} | |
} else { | |
// ここにヤギが一匹左にいれば渡す処理 | |
// 違えば以下の処理にする。 | |
if (isAloneLeftGoat()) { | |
goat = Condition.ON_THE_RIVER; | |
} else { | |
boardWolfOrCabbage(); | |
} | |
phaseChangeBoat();// 川に進む | |
} | |
} else { | |
// 1もしくは0しかいない場合 | |
if (isAlone()) { | |
// 船にのる | |
phaseChangeBoat(); | |
} else { | |
// ヤギがいるか確認いるならボートに乗せて進む | |
if (isThereBoatOnTheRightField()) { | |
goat = Condition.ON_THE_RIVER; | |
phaseChangeBoat(); | |
} else { | |
phaseChangeBoat(); | |
} | |
} | |
} | |
} | |
protected void exeOnTheRiver(boolean isRight) { | |
// 右に進む場合 | |
if (isRight) { | |
// 右側に何か渡っているかどうかs | |
if (isNotThree()) { | |
// ボートに乗っているものを渡す | |
onTheFieldToRight(); // 受け渡し | |
phaseChangeBoat(); // 陸上に切り替え | |
phaseChange(); // 左側に進む | |
} else { | |
// ボートの物を渡すs | |
onTheFieldToRight(); | |
phaseChangeBoat(); // 陸上に切り替え | |
phaseChange(); // 左側に進む | |
} | |
} else { | |
// 船の荷物を左側へ渡す | |
onTheFieldToLeft(); | |
// 陸にわたり | |
phaseChangeBoat(); | |
// 右側に進むようにchange | |
phaseChange(); | |
} | |
} | |
/** | |
* @return 全ての物が左の陸上に存在するかどうか | |
*/ | |
private boolean checkAllOne() { | |
return Condition.LEFT_FIELD == wolf && Condition.LEFT_FIELD == goat | |
&& Condition.LEFT_FIELD == cabbage; | |
} | |
/** | |
* @return 右側に渡っているものはいない | |
*/ | |
private boolean isNotThree() { | |
return !(Condition.RIGHT_FIELD == wolf || Condition.RIGHT_FIELD == goat | |
|| Condition.RIGHT_FIELD == cabbage); | |
} | |
/** | |
* 船に乗っているものを右側の岸に渡す | |
*/ | |
private void onTheFieldToRight() { | |
if (Condition.ON_THE_RIVER == wolf) { | |
wolf = Condition.RIGHT_FIELD; | |
} else if (Condition.ON_THE_RIVER == goat) { | |
goat = Condition.RIGHT_FIELD; | |
} else if (Condition.ON_THE_RIVER == cabbage) { | |
cabbage = Condition.RIGHT_FIELD; | |
} | |
} | |
/** | |
* 船に乗っているものを左側の岸に渡す | |
*/ | |
private void onTheFieldToLeft() { | |
if (Condition.ON_THE_RIVER == wolf) { | |
wolf = Condition.LEFT_FIELD; | |
} else if (Condition.ON_THE_RIVER == goat) { | |
goat = Condition.LEFT_FIELD; | |
} else if (Condition.ON_THE_RIVER == cabbage) { | |
cabbage = Condition.LEFT_FIELD; | |
} | |
} | |
/** | |
* @return 1もしくは0 | |
*/ | |
protected boolean isAlone() { | |
int count = 0; | |
// 左にいる | |
if (goRight) { | |
if (Condition.LEFT_FIELD == wolf) { | |
count++; | |
} | |
if (Condition.LEFT_FIELD == goat) { | |
count++; | |
} | |
if (Condition.LEFT_FIELD == cabbage) { | |
count++; | |
} | |
return count <= 1; | |
} else { | |
if (Condition.RIGHT_FIELD == wolf) { | |
count++; | |
} | |
if (Condition.RIGHT_FIELD == goat) { | |
count++; | |
} | |
if (Condition.RIGHT_FIELD == cabbage) { | |
count++; | |
} | |
return count <= 1; | |
} | |
} | |
/** | |
* @param isRight 右側にいるかどうか | |
* @return ヤギが狼に食べられるならtrueを返す | |
*/ | |
private boolean isEatGoat(boolean isRight) { | |
if (isRight) { | |
return Condition.RIGHT_FIELD == wolf && Condition.RIGHT_FIELD == goat; | |
} else { | |
return Condition.LEFT_FIELD == wolf && Condition.LEFT_FIELD == goat; | |
} | |
} | |
/** | |
* @param isRight 右側にいるかどうか | |
* @return キャベツがヤギに食べられるならtrueを返す | |
*/ | |
private boolean isEatCabbage(boolean isRight) { | |
if (isRight) { | |
return Condition.RIGHT_FIELD == goat && Condition.RIGHT_FIELD == cabbage; | |
} else { | |
return Condition.LEFT_FIELD == goat && Condition.LEFT_FIELD == cabbage; | |
} | |
} | |
/** | |
* @return 狼ならtrue | |
*/ | |
private boolean whichSelect() { | |
// 狼かキャベツか好きな方を選択させる | |
Random rnd = new Random(); | |
int i = rnd.nextInt(10); | |
return i <= 4; | |
} | |
private boolean isThereBoatOnTheRightField() { | |
return Condition.RIGHT_FIELD == goat; | |
} | |
/** | |
* @return ヤギが左にいなければtrue | |
*/ | |
private boolean isNotThereGoatLeftField() { | |
return Condition.LEFT_FIELD != goat; | |
} | |
/** | |
* 狼かキャベツが左側にいるので あれば船に乗せる | |
*/ | |
private void boardWolfOrCabbage() { | |
if (Condition.LEFT_FIELD == wolf) { | |
wolf = Condition.ON_THE_RIVER; | |
} else if (Condition.LEFT_FIELD == cabbage) { | |
cabbage = Condition.ON_THE_RIVER; | |
} | |
} | |
/** | |
* @return 左にヤギが一匹のみなら | |
*/ | |
private boolean isAloneLeftGoat() { | |
return Condition.LEFT_FIELD != wolf | |
&& Condition.LEFT_FIELD != cabbage | |
&& Condition.LEFT_FIELD == goat; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package algorithm.one; | |
import java.util.Random; | |
public class AlgorithmOne { | |
private static AlgorithmOne algo = new AlgorithmOne(); | |
String wolf = Condition.LEFTFIELD.getValue(), goat = Condition.LEFTFIELD.getValue(), cabbage = Condition.LEFTFIELD.getValue(); | |
/** | |
* 右に進む場合trueとなる | |
*/ | |
boolean goRight = true; | |
/** | |
* boat isNot on the river | |
*/ | |
boolean boatCondition = true; | |
private AlgorithmOne() { | |
} | |
protected static AlgorithmOne getInstance() { | |
return algo; | |
} | |
/** | |
* @return 処理が終わる条件 | |
*/ | |
protected boolean allNotThree() { | |
if (Condition.RIGHTFIELD.equals(wolf) && Condition.RIGHTFIELD.equals(goat) | |
&& Condition.RIGHTFIELD.equals(cabbage)) { | |
return false; | |
} | |
return true; | |
} | |
/** | |
* @return 右に進むかどうか | |
*/ | |
protected boolean goRight() { | |
return goRight; | |
} | |
/** | |
* boatが逆に進む為の処理 | |
*/ | |
private void phaseChange() { | |
if (goRight) { | |
goRight = false; | |
} else { | |
goRight = true; | |
} | |
} | |
/** | |
* @return 陸の上かどうか | |
*/ | |
protected boolean getOnTheField() { | |
// 陸上であればtrue | |
if (boatCondition) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* 陸か川上かどうかの切り替え | |
*/ | |
private void phaseChangeBoat() { | |
if (boatCondition) { | |
boatCondition = false; | |
} else { | |
boatCondition = true; | |
} | |
} | |
protected void exeOnTheField(boolean isRight) { | |
// 右に進む場合 | |
if (isRight) { | |
// 全てがいる場合 | |
if (checkAllOne()) { | |
goat = Condition.ONTHERIVER.getValue(); | |
phaseChangeBoat(); | |
} // どちらかを移動させる | |
else if (isNotThereGoatLeftField()) {// ヤギがいない場合にチェンジ | |
if (whichSelect()) { | |
wolf = Condition.ONTHERIVER.getValue(); | |
phaseChangeBoat(); | |
} else { | |
cabbage = Condition.ONTHERIVER.getValue(); | |
phaseChangeBoat(); | |
} | |
} else { | |
// ここにヤギが一匹左にいれば渡す処理 | |
// 違えば以下の処理にする。 | |
if (isAloneLeftGoat()) { | |
goat = Condition.ONTHERIVER.getValue(); | |
} else { | |
boardWolfOrCabbage(); | |
} | |
phaseChangeBoat();// 川に進む | |
} | |
} else { | |
// 1もしくは0しかいない場合 | |
if (isAlone()) { | |
// 船にのる | |
phaseChangeBoat(); | |
} else { | |
// ヤギがいるか確認いるならボートに乗せて進む | |
if (isThereBoatOnTheRightField()) { | |
goat = Condition.ONTHERIVER.getValue(); | |
phaseChangeBoat(); | |
} else { | |
phaseChangeBoat(); | |
} | |
} | |
} | |
} | |
protected void exeOnTheRiver(boolean isRight) { | |
// 右に進む場合 | |
if (isRight) { | |
// 右側に何か渡っているかどうかs | |
if (isNotThree()) { | |
// ボートに乗っているものを渡す | |
onTheFieldToRight(); // 受け渡し | |
phaseChangeBoat(); // 陸上に切り替え | |
phaseChange(); // 左側に進む | |
} else { | |
// ボートの物を渡すs | |
onTheFieldToRight(); | |
phaseChangeBoat(); // 陸上に切り替え | |
phaseChange(); // 左側に進む | |
} | |
} else { | |
// 船の荷物を左側へ渡す | |
onTheFieldToLeft(); | |
// 陸にわたり | |
phaseChangeBoat(); | |
// 右側に進むようにchange | |
phaseChange(); | |
} | |
} | |
/** | |
* @return 全ての物が左の陸上に存在するかどうか | |
*/ | |
private boolean checkAllOne() { | |
if (Condition.LEFTFIELD.equals(wolf) && Condition.LEFTFIELD.equals(goat) | |
&& Condition.LEFTFIELD.equals(cabbage)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* @return 右側に渡っているものはいない | |
*/ | |
private boolean isNotThree() { | |
if (Condition.RIGHTFIELD.equals(wolf) || Condition.RIGHTFIELD.equals(goat) | |
|| Condition.RIGHTFIELD.equals(cabbage)) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
/** | |
* 船に乗っているものを右側の岸に渡す | |
*/ | |
private void onTheFieldToRight() { | |
if (Condition.ONTHERIVER.equals(wolf)) { | |
wolf = Condition.RIGHTFIELD.getValue(); | |
} else if (Condition.ONTHERIVER.equals(goat)) { | |
goat = Condition.RIGHTFIELD.getValue(); | |
} else if (Condition.ONTHERIVER.equals(cabbage)) { | |
cabbage = Condition.RIGHTFIELD.getValue(); | |
} | |
} | |
/** | |
* 船に乗っているものを左側の岸に渡す | |
*/ | |
private void onTheFieldToLeft() { | |
if (Condition.ONTHERIVER.equals(wolf)) { | |
wolf = Condition.LEFTFIELD.getValue(); | |
} else if (Condition.ONTHERIVER.equals(goat)) { | |
goat = Condition.LEFTFIELD.getValue(); | |
} else if (Condition.ONTHERIVER.equals(cabbage)) { | |
cabbage = Condition.LEFTFIELD.getValue(); | |
} | |
} | |
/** | |
* @return 1もしくは0 | |
*/ | |
protected boolean isAlone() { | |
int count = 0; | |
// 左にいる | |
if (goRight) { | |
if (Condition.LEFTFIELD.equals(wolf)) { | |
count++; | |
} | |
if (Condition.LEFTFIELD.equals(goat)) { | |
count++; | |
} | |
if (Condition.LEFTFIELD.equals(cabbage)) { | |
count++; | |
} | |
if (count <= 1) { | |
return true; | |
} else { | |
return false; | |
} | |
} else { | |
if (Condition.RIGHTFIELD.equals(wolf)) { | |
count++; | |
} | |
if (Condition.RIGHTFIELD.equals(goat)) { | |
count++; | |
} | |
if (Condition.RIGHTFIELD.equals(cabbage)) { | |
count++; | |
} | |
if (count <= 1) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} | |
/** | |
* @param isRight | |
* 右側にいるかどうか | |
* @return ヤギが狼に食べられるならtrueを返す | |
*/ | |
private boolean isEatGoat(boolean isRight) { | |
if (isRight) { | |
if (Condition.RIGHTFIELD.equals(wolf) && Condition.RIGHTFIELD.equals(goat)) { | |
return true; | |
} else { | |
return false; | |
} | |
} else { | |
if (Condition.LEFTFIELD.equals(wolf) && Condition.LEFTFIELD.equals(goat)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} | |
/** | |
* @param isRight | |
* 右側にいるかどうか | |
* @return キャベツがヤギに食べられるならtrueを返す | |
*/ | |
private boolean isEatCabbage(boolean isRight) { | |
if (isRight) { | |
if (Condition.RIGHTFIELD.equals(goat) && Condition.RIGHTFIELD.equals(cabbage)) { | |
return true; | |
} else { | |
return false; | |
} | |
} else { | |
if (Condition.LEFTFIELD.equals(goat) && Condition.LEFTFIELD.equals(cabbage)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} | |
/** | |
* @return 狼ならtrue | |
*/ | |
private boolean whichSelect() { | |
// 狼かキャベツか好きな方を選択させる | |
Random rnd = new Random(); | |
int i = rnd.nextInt(10); | |
if (i <= 4) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
private boolean isThereBoatOnTheRightField() { | |
if (Condition.RIGHTFIELD.equals(goat)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* @return ヤギが左にいなければtrue | |
*/ | |
private boolean isNotThereGoatLeftField() { | |
if (Condition.LEFTFIELD.equals(goat)) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
/** | |
* 狼かキャベツが左側にいるので あれば船に乗せる | |
*/ | |
private void boardWolfOrCabbage() { | |
if (Condition.LEFTFIELD.equals(wolf)) { | |
wolf = Condition.ONTHERIVER.getValue(); | |
} else if (Condition.LEFTFIELD.equals(cabbage)) { | |
cabbage = Condition.ONTHERIVER.getValue(); | |
} | |
} | |
/** | |
* @return 左にヤギが一匹のみなら | |
*/ | |
private boolean isAloneLeftGoat() { | |
if (!Condition.LEFTFIELD.equals(wolf) && !Condition.LEFTFIELD.equals(cabbage) && Condition.LEFTFIELD.equals(goat)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package naka; | |
/** | |
* Created by rinp on 2016/11/07. | |
*/ | |
public enum Condition { | |
/** | |
* 左側の陸上 | |
*/ | |
LEFT_FIELD, | |
/** | |
* 川上 | |
*/ | |
ON_THE_RIVER, | |
/** | |
* 右側の陸上 | |
*/ | |
RIGHT_FIELD; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment