移動
Posted: 2006年10月15日(日) 11:46
こんにちは
座標で考えてみました。
可読性って重要ですね。
でも、自分の書いたのが他の人が見ても可読性があるのか
心配なんですよ。
改善した方が良いよって指摘して下さると嬉しいです。
#include <windows.h>
#include <stdio.h>
#define MAX 20
//空白の挿入
void kouran(int i){
for(int ix = 0; ix < i; ix++){
::printf(" ");
}
}
//改行
void jyouge(int i){
for(int iy = 0; iy < i; iy++){
::printf("\n");
}
}
//向き
enum direction{
top = 0, //上
bottom = 1, //下
right = 2, //右
left = 3, //左
};
//キャラクター
void kyara(int x, int y, direction z){
char ckya[4][3][8] = {
{" ↑ \n", "↑↑↑\n"," ↑ \n"},
{" ↓ \n", "↓↓↓\n"," ↓ \n"},
{" → \n", "→→→\n"," → \n"},
{" ← \n", "←←←\n"," ← \n"},
};
//表示出来る範囲内か?
const int Y_SIZE = 3;
if(y + Y_SIZE < 0 || y - Y_SIZE > MAX){
return;
}
const int X_SIZE = 6;
if(x + X_SIZE < 0 || x - X_SIZE > MAX){
return;
}
//キャラの表示範囲を決定する
int icy = 0;
int i_end_y = Y_SIZE;
if(y < 0){
icy = ::abs(y);
}else if(y > MAX){
i_end_y = Y_SIZE - (y - MAX);
}
int icx = 0;
int i_end_x = X_SIZE;
if(x < 0){
icx = ::abs(x);
if(icx % 2){
icx--;
}
}else if(x > MAX){
i_end_x = X_SIZE - (x - MAX);
if(i_end_x % 2){
i_end_x--;
}
}
::jyouge(y);
for(int iy = icy; iy < i_end_y; iy++){
::kouran(x);
for(int ix = icx; ix < i_end_x; ix+=2){
char c[3];
c[0] = ckya[z][iy][ix];
c[1] = ckya[z][iy][ix + 1];
c[2] = '\0';
::printf(c);
}
::printf("\n");
}
}
int main(void){
//座標と向き
int x = 0, y = 0;
direction z = ::bottom;
//入力されたコード
int i = 0;
//見本として表示
::kyara(x, y, z);
while(::printf("上:8 下:2 左:4 右:6 終:0\n"), ::scanf("%d", &i), i != 0){
::system("cls");
if(i == 8){
y--;
z = ::top;
}
if(i == 2){
y++;
z = ::bottom;
}
if(i == 4){
x--;
z = ::left;
}
if(i == 6){
x++;
z = ::right;
}
::kyara(x, y, z);
}
return 0;
}
座標で考えてみました。
可読性って重要ですね。
でも、自分の書いたのが他の人が見ても可読性があるのか
心配なんですよ。
改善した方が良いよって指摘して下さると嬉しいです。
#include <windows.h>
#include <stdio.h>
#define MAX 20
//空白の挿入
void kouran(int i){
for(int ix = 0; ix < i; ix++){
::printf(" ");
}
}
//改行
void jyouge(int i){
for(int iy = 0; iy < i; iy++){
::printf("\n");
}
}
//向き
enum direction{
top = 0, //上
bottom = 1, //下
right = 2, //右
left = 3, //左
};
//キャラクター
void kyara(int x, int y, direction z){
char ckya[4][3][8] = {
{" ↑ \n", "↑↑↑\n"," ↑ \n"},
{" ↓ \n", "↓↓↓\n"," ↓ \n"},
{" → \n", "→→→\n"," → \n"},
{" ← \n", "←←←\n"," ← \n"},
};
//表示出来る範囲内か?
const int Y_SIZE = 3;
if(y + Y_SIZE < 0 || y - Y_SIZE > MAX){
return;
}
const int X_SIZE = 6;
if(x + X_SIZE < 0 || x - X_SIZE > MAX){
return;
}
//キャラの表示範囲を決定する
int icy = 0;
int i_end_y = Y_SIZE;
if(y < 0){
icy = ::abs(y);
}else if(y > MAX){
i_end_y = Y_SIZE - (y - MAX);
}
int icx = 0;
int i_end_x = X_SIZE;
if(x < 0){
icx = ::abs(x);
if(icx % 2){
icx--;
}
}else if(x > MAX){
i_end_x = X_SIZE - (x - MAX);
if(i_end_x % 2){
i_end_x--;
}
}
::jyouge(y);
for(int iy = icy; iy < i_end_y; iy++){
::kouran(x);
for(int ix = icx; ix < i_end_x; ix+=2){
char c[3];
c[0] = ckya[z][iy][ix];
c[1] = ckya[z][iy][ix + 1];
c[2] = '\0';
::printf(c);
}
::printf("\n");
}
}
int main(void){
//座標と向き
int x = 0, y = 0;
direction z = ::bottom;
//入力されたコード
int i = 0;
//見本として表示
::kyara(x, y, z);
while(::printf("上:8 下:2 左:4 右:6 終:0\n"), ::scanf("%d", &i), i != 0){
::system("cls");
if(i == 8){
y--;
z = ::top;
}
if(i == 2){
y++;
z = ::bottom;
}
if(i == 4){
x--;
z = ::left;
}
if(i == 6){
x++;
z = ::right;
}
::kyara(x, y, z);
}
return 0;
}