#pragma once
//フィールドの大きさ
#define WIDTH 10
#define HEIGHT 10
//キーコードの定義
#define LEFT 0x4b
#define RIGHT 0x4d
#define DOWN 0x50
#define UP 0x48
//10*10マスのフィールドを表す変数
int field[HEIGHT][WIDTH] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 1, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 1, 3, 1, 1, 0, 1},
{1, 0, 1, 1, 1, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 1, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
/*
迷路探索プログラム
maizesearch.cpp
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include "globaldata.h"
//迷路探索クラス
class MAZESEARCH{
private:
int i, j; //ループ変数
public:
MAZESEARCH(){};
~MAZESEARCH(){};
//フィールドの開始地点を設定する関数
void SetField(int x, int y){
if (field[y][x] != 1)
field[y][x] = 2;
}
//フィールドを表示する関数
void Output(){
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++)
printf("%d",field[i][j]);
printf("\n");
}
printf("\n");
}
//移動するマスがゴール地点か判定する関数
int goal(int x, int y){
if (field[y][x] == 3) {
printf("GOAL!!\n");
return true;
}
return false;
}
};
//プレイヤークラス
class PLAYER {
private:
int x, y; //X,Y座標
public:
PLAYER(){};
~PLAYER(){};
//座標の初期化する関数
void init(){
x = 0;
y = 0;
srand((unsigned int)time(NULL));
x = rand() % 10 ;
y = rand() % 10 ;
}
//X座標を取得する関数
int getX(){
return x;
}
//Y座標を取得する関数
int getY(){
return y;
}
//上方向に移動する関数
void up(int current_x, int current_y){
if (field[current_y - 1][x] == 0) {
y = current_y - 1;
field[y][x] = 2;
field[current_y][current_x] = 0;
}
}
//下方向に移動する関数
void down(int current_x, int current_y){
if (field[current_y + 1][x] == 0) {
y = current_y + 1;
field[y][x] = 2;
field[current_y][current_x] = 0;
}
}
//右方向に移動する関数
void right(int current_x, int current_y){
if (field[y][current_x + 1] == 0) {
x = current_x + 1;
field[y][x] = 2;
field[current_y][current_x] = 0;
}
}
//左方向に移動する関数
void left(int current_x, int current_y){
if (field[y][current_x - 1] == 0){
x = current_x - 1;
field[y][x] = 2;
field[current_y][current_x] = 0;
}
}
};
int main(void)
{
MAZESEARCH maze;
PLAYER player;
int fin = 0; //終了フラグ
//X座標、Y座標を初期化する
player.init();
//スタート地点をセットする
maze.SetField(player.getX(), player.getY());
printf("START\n");
//フィールドを表示する
maze.Output();
/*
下記の処理を繰り返す
(1) キーが入力される
(2-1) 「q」キーの場合は、終了フラグを立て、ループを抜ける
(2-2) 移動先にゴールがあれば、終了フラグを立て、ループを抜ける
(2-3) 移動先が、移動可能エリアの場合は、移動する
(2-4) フィールド全体を表示する
*/
while (1) {
if (_kbhit()) {
switch(_getch()) {
//上方向へ移動
case UP:
printf("MOVE UP\n");
fin = maze.goal(player.getX(), player.getY() - 1);
player.up(player.getX(), player.getY());
maze.Output();
break;
//下方向へ移動
case DOWN:
printf("MOVE DOWN\n");
fin = maze.goal(player.getX(), player.getY() + 1);
player.down(player.getX(), player.getY());
maze.Output();
break;
//右方向へ移動
case RIGHT:
printf("MOVE RIGHT\n");
fin = maze.goal(player.getX() + 1, player.getY());
player.right(player.getX(), player.getY());
maze.Output();
break;
//左方向へ移動
case LEFT:
printf("MOVE LEFT\n");
fin = maze.goal(player.getX() - 1, player.getY());
player.left(player.getX(), player.getY());
maze.Output();
break;
//プログラムを終了
case 'q':
printf("FINISH\n");
fin = 1;
break;
default:
break;
}
}
if (fin == 1) {
break;
}
}
return 0;
}
Copyright © ITmedia, Inc. All Rights Reserved.
組み込み開発の記事ランキング
コーナーリンク