ガチホ〇撲滅教

人の行く裏に道あり花の山

〜続々〜リハビリプログラムAOJ【0502】

またまた、リハビリプログラム

今回は、(サイコロ | Aizu Online Judge)

  

本日(記事をあげた時点では昨日)2つめのコードであるがやはり、リハビリを重ねるごとにコーディングのスピードがあがっているのを感じた。1日に解ける問題数がしっかりと増えてきているのはリハビリの効果が出始めているとみてとらえていいと感じる。

また、今年の夏休みは、5日に1度、直前の5日間を振り返って

今後どのようにしていくかを反省していきたいと考えている(受験生か!!)

 

ちなみに、この問題は 

サイコロの上面をtop

サイコロの前面をfront

サイコロの右面をrs (right-sideとする)

 

この3面さえわかれば

下は7-top

後ろは7-front

左は7-rsとなるため

 

結果、6面が把握できる事になる。

(3面の情報を把握する事によりサイコロ全ての面の情報を把握する事が可能)

このデータ管理によりプログラミングの労力を減らす事ができる。

このとき、注意するのが次の3面の情報が前の情報に依存するため

データの代入中には、データを2つ用意する事が必要である。

 

次の各面の数字と、現在の各面の数字

 

ページを見ると各動作によるサイコロの挙動は明らかである。

以下に擬似コードを記す。

(次の各面の値) = (前の各面の値を用いて処理)

 

操作Northによって

top = front

front = 7-top

rs = rs (変わらない)

 

操作Eastによって

top = 7-rs

front = front(変わらない)

rs = top

 

操作Westによって

top = rs

front = front(変わらない)

rs = 7-top

 

操作Southによって

top = 7 - front

front = top

rs = rs (変わらない)

 

操作Rightによって

top = top

front = rs

rs = 7 - front

 

操作Leftによって

top = top

front = 7 - rs

rs = front

 

となる。が、実行結果はミスっていたため(擬似コードの挙動予測はあってると思う)

デバッグを行った。

 

~修正後のコード~

 

~FileRead_0502.java~

 

package dice_0502;

 

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

 

public class FileRead_0502 {

ArrayList<String> direction = new ArrayList<String>();

 

/**

* @return the direction

*/

public ArrayList<String> getDirection() {

return direction;

}

 

public void fileRead(String filename){

FileReader fr = null;

    BufferedReader br = null;

    try {

        fr = new FileReader(filename);

        br = new BufferedReader(fr);

       

        String line;

        int i;

        while*1 != null){ //1行読む

        i = Integer.parseInt(line);  

        if(i == 0) break; //EoFの0がきたら終了

        for(int k = 0; k < i; k++){

        line = br.readLine();

        direction.add(line);

        if(k == i-1){

        direction.add(null);

        }

        }

        }

    } catch (FileNotFoundException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        try {

            br.close();

            fr.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

}

 

 

 

~Dice.java~

 

package dice_0502;

 

public class Dice {

private static FileRead_0502 file = new FileRead_0502();

 

private int top = 1, front = 2, rs = 3;

private int p_t = 1, p_f = 2, p_r = 3;

privateintpoint = 1;

 

public void lotation(){

for(int i = 0; i < file.getDirection().size(); i++){

if(file.getDirection().get(i) == null){

System.out.println(point);

point = 1;

top = 1; front = 2; rs = 3;

p_t = 1; p_f = 2; p_r = 3;

continue;

}

if(file.getDirection().get(i).equals("North")){

this.north();

}

if(file.getDirection().get(i).equals("East")){

this.east();

}

if(file.getDirection().get(i).equals("West")){

this.west();

}

if(file.getDirection().get(i).equals("South")){

this.south();

}

if(file.getDirection().get(i).equals("Right")){

this.right();

}

if(file.getDirection().get(i).equals("Left")){

this.left();

}

p_t = top;

p_f = front;

p_r = rs;

point += top;

}

}

 

public void north(){

top = p_f;

front = 7-p_t;

//rsは変更なし

}

public void east(){

top = 7-p_r;

//frontは変更なし

rs = p_t;

}

public void west(){

top = p_r;

//frontは変更なし

rs = 7-p_t;

}

public void south(){

top = 7-p_f;

front = p_t;

//rsは変更なし

}

public void right(){

//topは変更なし

front = p_r;

rs = 7-p_f;

}

public void left(){

//topは変更なし

front = 7-p_r;

rs = p_f;

}

 

public static void main(String args[]){

file.fileRead(args[0]);

Dice dice = new Dice();

dice.lotation();

}

}

 

ミスってたとこ…

・そもそもの一番最初の1が上の時を足し忘れていた。

・2回目のサイコロの回転の開始時に、サイコロの状態を初期化し忘れていた事。修正前は、前回のサイコロの回転の最後の状態から開始していた。

 

この2点を修正することによって、ちゃんとした実行結果を得られる事ができた。

*1:line = br.readLine(