vs2015
进行分层设计:数据层,控制层,显示层。
模仿工业生产,显示最短路径3表示,AImoveout函数显示1移动过程。
代码结构图:
AI.h
#include "data.h"
#include "control.h"
int AIout(int AIdata[N][N],int i,int j);
void AImoveout();
control.h
#pragma once
#include "define.h"
#include "data.h"
void run(char ch);
data.h
#pragma once
#include "define.h"
//extern 全局变量 extern 可有可无
extern int Realdata[N][N]; // extern 引用全局变量,全局变量的声明
extern int AIdata[N][N];
extern int ipos; //初始位置
extern int jpos;
extern int canout;
define.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#define N 10
view.h
#pragma once
#include "define.h"
void show(int a[N][N]);
AI.c
#include "AI.h"
int AIout(int AIdata[N][N], int i, int j) {
AIdata[i][j] = 3;//避免回头路
if (i == N-1 && j==N-1 )
{
canout = 1; //可以走出来
printf("\n恭喜走出来\n");
}
else
{
//右,下,左,上
if (j+1 <= 9 && AIdata[i][j+1] < 2 && canout !=1)
{
AIout(AIdata,i,j+1);
}
if (i + 1 <= 9 && AIdata[i+1][j] < 2 && canout != 1)
{
AIout(AIdata, i + 1, j);
}
if (j - 1 >= 0 && AIdata[i][j-1] < 2 && canout != 1)
{
AIout(AIdata, i, j-1);
}
if (i- 1 >= 0 && AIdata[i-1][j] < 2 && canout != 1)
{
AIout(AIdata, i-1, j);
}
if (canout != 1) {
AIdata[i][j] = 0; //走不通,设置0
}
}
return canout;//代表是否成功
}
void AImoveout() {
AIdata[0][0] = 1;
while (ipos!= N-1 && jpos != N -1)
{
if (ipos - 1 >= 0 && AIdata[ipos - 1][jpos] == 3) {
AIdata[ipos - 1][jpos] = 0;
run('w');
}
if (ipos + 1 <= 9 && AIdata[ipos + 1][jpos] == 3) {
AIdata[ipos + 1][jpos] = 0;
run('s');
}
if (jpos + 1 <= 9 && AIdata[ipos][jpos + 1] == 3) {
AIdata[ipos][jpos + 1] = 0;
run('d');
}
if (jpos - 1 >= 0 && AIdata[ipos][jpos - 1] == 3) {
AIdata[ipos][jpos - 1] = 0;
run('a');
}
}
}
control.c
#include "control.h"
void run(char ch) { //w s a d
switch (ch) {
case 'w':
if (ipos-1>= 0 && Realdata[ipos - 1][jpos] < 2) {
int temp = Realdata[ipos][jpos];
Realdata[ipos][jpos] = Realdata[ipos - 1][jpos];
Realdata[ipos - 1][jpos] = temp;//交换数据
ipos -= 1;
}
break;
case 's':
if (ipos + 1 <= N - 1 && Realdata[ipos + 1][jpos] < 2) {
int temp = Realdata[ipos][jpos];
Realdata[ipos][jpos] = Realdata[ipos + 1][jpos];
Realdata[ipos + 1][jpos] = temp;//交换数据
ipos += 1;
}
break;
case 'a':
if (jpos - 1 >= 0 && Realdata[ipos][jpos - 1] < 2) {
int temp = Realdata[ipos][jpos];
Realdata[ipos][jpos] = Realdata[ipos ][jpos - 1];
Realdata[ipos][jpos-1] = temp;//交换数据
jpos -= 1;
}
break;
case 'd':
if (jpos + 1 <= N-1 && Realdata[ipos][jpos + 1] < 2) {
int temp = Realdata[ipos][jpos];
Realdata[ipos][jpos] = Realdata[ipos][jpos + 1];
Realdata[ipos][jpos + 1] = temp;//交换数据
jpos += 1;
}
break;
default:
break;
}
show(Realdata);
}
data.c
#include "define.h"
int Realdata[N][N] = {
{ 1,0,2,0,0,0,0,0,0,0 },
{ 0,0,0,2,0,0,2,0,0,0 },
{ 0,0,0,0,2,0,0,2,2,0 },
{ 0,0,0,0,2,0,2,0,0,0 },
{ 0,0,0,0,2,0,0,0,0,2 },
{ 2,2,2,0,0,2,0,2,0,0 },
{ 0,0,0,0,0,2,0,2,0,2 },
{ 0,0,0,0,0,0,0,2,0,0 },
{ 0,0,0,0,0,0,0,2,2,0 },
{ 0,0,0,0,0,0,0,0,2,0 }
};
int AIdata[N][N] = {
{ 1,0,2,0,0,0,0,0,0,0 },
{ 0,0,0,2,0,0,2,0,0,0 },
{ 0,0,0,0,2,0,0,2,2,0 },
{ 0,0,0,0,2,0,2,0,0,0 },
{ 0,0,0,0,2,0,0,0,0,2 },
{ 2,2,2,0,0,2,0,2,0,0 },
{ 0,0,0,0,0,2,0,2,0,2 },
{ 0,0,0,0,0,0,0,2,0,0 },
{ 0,0,0,0,0,0,0,2,2,0 },
{ 0,0,0,0,0,0,0,0,2,0 }
};
int ipos=0; //初始位置
int jpos=0;
int canout = 0;// 0代表走不出来,1走出来
view.c
#include "view.h"
void show(int a[N][N]) {
printf("\n-----------------------------\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%4d", a[i][j]);
}
printf("\n");
}
}
main.c
#include "data.h"
#include "view.h"
#include "control.h"
#include "AI.h"
#pragma comment(lib, "legacy_stdio_definitions.lib")
void main() {
int flag = AIout(AIdata, 0,0);
if (flag==1)
{
printf("\n可以走出");
show(AIdata);
AImoveout();
}
else {
printf("\n不可以走出");
}
//show(Realdata); //显示初始情况
//while (1) {
// char ch = getchar();
// getchar();
// run(ch);
// show(Realdata);
//}
system("pause");
}