Skip to content

Instantly share code, notes, and snippets.

@naka-Eff
Last active November 7, 2016 12:10
Show Gist options
  • Save naka-Eff/d1de4f8c645f0cb9dcdd400b8ef947ab to your computer and use it in GitHub Desktop.
Save naka-Eff/d1de4f8c645f0cb9dcdd400b8ef947ab to your computer and use it in GitHub Desktop.
package algorithm.one;
public class start {
public static void main(String[] args) {
AlgorithmOne access = AlgorithmOne.getInstance();
// 全て渡りきるまで処理は続く
while (access.allNotThree()) {
// 右に進む場合
if (access.goRight()) {
// 陸上処理
if (access.getOnTheField()) {
access.exeOnTheField(true);
} // 川上処理
else {
access.exeOnTheRiver(true);
}
} // 左に進む
else {
// 陸上か川上か
if (access.getOnTheField()) {
access.exeOnTheField(false);
} else {
access.exeOnTheRiver(false);
}
}
}
System.out.println("クリア ");
System.out.println("wolf is :"+access.wolf);
System.out.println("goat is :"+access.goat);
System.out.println("cabbage is :"+access.cabbage);
}
}
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;
}
}
}
package algorithm.one;
public enum Condition {
/**
* 左側の陸上
*/
LEFTFIELD("1"),
/**
* 川上
*/
ONTHERIVER("2"),
/**
* 右側の陸上
*/
RIGHTFIELD("3");
/** value値 */
private String value;
/**
* コンストラクタ
*
* @param value
* SelectValueのvalue値
*/
Condition(String value) {
this.value = value;
}
/**
* value値の取得。
*
* @return 対象のSelectValueのvalue値
*/
public String getValue() {
return this.value;
}
/**
* 現状の状態値の比較
*
* @param sv
* 比較対象となるselectValue
* @return true value値が一致 /false その他
*/
public boolean equals(String str) {
if (null == str) {
return false;
} else if ("".equals(str)) {
return false;
}
return str.equals(this.value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment