final class head{
public static final byte UP = 0;
public static final byte RIGHT = 1;
public static final byte DOWN = 2;
public static final byte LEFT = 3;
public static final byte LOOT = 0;
public static final byte BLOK = 1;
public static final byte GOAL = 127;
}
class set{
public static void main(String angs[]){
map map = new map();
map.print();
map.new_player();
}
}
class people{
private byte pos_x = 1;
private byte pos_y = 1;
private byte pos_l = head.UP;
//左を向いて歩く
public void work_left(){
pos_l = (byte)((pos_l+3)%4);
work_right();
}
//一歩歩くがダメなら右を向く
private void work_right(){
byte go_ans = 0;
switch(pos_l) {
case head.UP:
go_ans = go_up();
break;
case head.RIGHT:
go_ans = go_right();
break;
case head.DOWN:
go_ans = go_down();
break;
case head.LEFT:
go_ans = go_left();
break;
default:
System.out.print("向きエラー:"+pos_l);
go_ans = 1;
break;
}
if(go_ans == 1)
{
pos_l++;
work_right();
}
}
//指定方向に歩く
private byte go_up(){
if(map.pos(pos_x,(byte)(pos_y-1)) == head.BLOK)
return 1;
pos_y--;
return 0;
}
//指定方向に歩く
private byte go_right(){
if(map.pos((byte)(pos_x+1),pos_y) == head.BLOK)
return 1;
pos_x++;
return 0;
}
//指定方向に歩く
private byte go_down(){
if(map.pos(pos_x,(byte)(pos_y+1)) == head.BLOK)
return 1;
pos_y++;
return 0;
}
//指定方向に歩く
private byte go_left(){
if(map.pos((byte)(pos_x-1),pos_y) == head.BLOK)
return 1;
pos_x--;
return 0;
}
//現在地出力
public void print(){
System.out.println(pos_x+","+pos_y);
}
//現在地返却
public byte pos_x(){
return pos_x;
}
//現在地返却
public byte pos_y(){
return pos_y;
}
}
class map{
static byte[][] flor;
static byte max_x = 0;
static byte max_y = 0;
map(){
this((byte)8,(byte)8);
}
map(byte x,byte y){
flor = new byte[x][y];
max_x = x;
max_y = y;
for(int i = 0;i < max_x; i++)
{
flor[0][i] = head.BLOK;
flor[max_y-1][i] = head.BLOK;
}
for(int i = 0;i < max_y; i++)
{
flor[i][0] = head.BLOK;
flor[i][max_x-1] = head.BLOK;
}
for(int i = 2;i < max_y-2; i++)
for(int j = 2;j < max_x-2; j++)
flor[i][j] = head.BLOK;
flor[max_y-2][max_x-2] = head.GOAL;
}
//一人作って歩かせる
public void new_player() {
people player = new people();
while(pos(player.pos_x(),player.pos_y()) != head.GOAL)
{
print(player);
player.work_left();
}
}
//現在地情報返却
public static byte pos(byte x,byte y){
return flor[y][x];
}
//マップ構造出力
public void print(){
for(int i = 0; i < max_y; i++)
{
for(int j = 0; j < max_x; j++)
switch(flor[i][j]){
case head.LOOT:
System.out.print(" ");
break;
case head.BLOK:
System.out.print("■");
break;
case head.GOAL:
System.out.print("G");
break;
default:
System.out.print("マップエラー");
break;
}
System.out.println();
}
}
//マップ構造出力(人付)
public void print(people player){
for(int i = 0; i < max_y; i++)
{
for(int j = 0; j < max_x; j++)
{
if((player.pos_x() != j) || (player.pos_y() != i))
{
switch(flor[i][j]){
case head.LOOT:
System.out.print(" ");
break;
case head.BLOK:
System.out.print("■");
break;
case head.GOAL:
System.out.print("G");
break;
default:
System.out.print("マップエラー");
break;
}
}
else
{
System.out.print("○");
}
}
System.out.println();
}
}
}
…が、勢いに任せて実行したは良いものの
► スポイラーを表示
しばらくエラーを出し続けた後一応ゴールにたどり着けているようなのですが明らかにおかしいです。
どうも一回目から向きがおかしくなっているらしいのですが…
0に3を加算して何で4になってしまうのでしょうか?
どなたか原因を教えてくださると助かります。