腾讯电脑管家企业平台:http://zhidao.baidu.com/c/guanjia/
“推箱子”C代码:
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
#include<windows.h>
int m =0 //m代表第几关
struct maps{short a[9][11]}
struct maps map[5]={ 0,0,0,0,0,0,0,0,0,0,0, //共5关,每关9行11列
0,1,1,1,1,1,1,1,0,0,0,
0,1,0,0,0,0,0,1,1,1,0,
1,1,4,1,1,1,0,0,0,1,0, //0空地,1墙
1,5,0,0,4,0,0,4,0,1,0, //4是箱子,5是人
1,0,3,3,1,0,4,0,1,1,0, //3是目的地
1,1,3,3,1,0,0,0,1,0,0, //7是箱子在目的地(4+3)
0,1,1,1,1,1,1,1,1,0,0, //8是人在目的地(5+3)
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,0,0,0,
0,0,1,5,0,1,1,1,0,0,0,
0,0,1,0,4,0,0,1,0,0,0,
0,1,1,1,0,1,0,1,1,0,0,
0,1,3,1,0,1,0,0,1,0,0,
0,1,3,4,0,0,1,0,1,0,0,
0,1,3,0,0,0,4,0,1,0,0,
0,1,1,1,1,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,1,0,
0,0,1,1,0,0,1,0,5,1,0,
0,0,1,0,0,0,1,0,0,1,0,
0,0,1,4,0,4,0,4,0,1,0,
0,0,1,0,4,1,1,0,0,1,0,
1,1,1,0,4,0,1,0,1,1,0,
1,3,3,3,3,3,0,0,1,0,0,
1,1,1,1,1,1,1,1,1,0,0,
0,1,1,1,1,1,1,1,1,1,0,
0,1,0,0,1,1,0,0,0,1,0,
0,1,0,0,0,4,0,0,0,1,0,
0,1,4,0,1,1,1,0,4,1,0,
0,1,0,1,3,3,3,1,0,1,0,
1,1,0,1,3,3,3,1,0,1,1,
1,0,4,0,0,4,0,0,4,0,1,
1,0,0,0,0,0,1,0,5,0,1,
1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,0,0,
0,1,1,1,0,0,0,0,1,0,0,
1,1,3,0,4,1,1,0,1,1,0,
1,3,3,4,0,4,0,0,5,1,0,
1,3,3,0,4,0,4,0,1,1,0,
1,1,1,1,1,1,0,0,1,0,0,
0,0,0,0,0,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0 }
void DrMap( ) //绘制地图
{ CONSOLE_CURSOR_INFO cursor_info={1,0} //隐藏光标的设置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info)
printf("\n\n \t\t\b推箱子")
printf("\n \t")
for (int i = 0i <9i++)
{for (int j = 0j <11j++)
{switch (map[m].a[i][j])
{case 0: printf(" ")break
case 1: printf("■")break
case 3: printf("◎")break
case 4: printf("□")break
case 5: printf("♀")break //5是人
case 7: printf("□")break //4 + 3箱子在目的地中
case 8: printf("♀")break // 5 + 3人在目的地中
}
}
printf("\n\t")
}
}
void gtxy(int x, int y) //控制光标位置的函数
{ COORD coord
coord.X = x
coord.Y = y
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord)
}
void start( ) //开始游戏
{ int r, c //人的下标
for (int i = 0i <9i++)
{ for (int j = 0j <11j++)
{if (map[m].a[i][j] == 5||map[m].a[i][j]==8) { r = i c = j} } //i j 人的下标
}
char key
key = getch( )
switch (key)
{case 'W':
case 'w':
case 72:
if (map[m]. a[r - 1][c] == 0|| map[m]. a [r - 1][c] == 3)
{ gtxy(2*c+8,r-1+3)printf("♀") // gtxy(2*c+8,r-1+3)是到指定位置输出字符
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r - 1][c] += 5 map[m]. a [r][c] -= 5}
else if (map[m]. a [r - 1][c] == 4 || map[m]. a [r - 1][c] == 7)
{ if (map[m]. a [r - 2][c] == 0 || map[m]. a [r - 2][c] == 3)
{ gtxy(2*c+8,r-2+3)printf("□")gtxy(2*c+8,r-1+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r - 2][c] += 4 map[m]. a [r - 1][c] += 1
map[m]. a [r][c] -= 5}
} break
case 'S':
case 's':
case 80:
if (map[m]. a [r + 1][c] == 0 || map[m]. a [r + 1][c] == 3)
{ gtxy(2*c+8,r+1+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r + 1][c] += 5 map[m]. a [r][c] -= 5}
else if (map[m]. a [r + 1][c] == 4 || map[m]. a [r+ 1][c] == 7)
{ if (map[m]. a [r + 2][c] == 0 || map[m]. a [r + 2][c] == 3)
{ gtxy(2*c+8,r+2+3)printf("□")gtxy(2*c+8,r+1+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r + 2][c] += 4map[m]. a [r + 1][c] += 1
map[m]. a [r][c] -= 5}
}break
case 'A':
case 'a':
case 75:
if (map[m]. a [r ][c - 1] == 0 || map[m]. a [r ][c - 1] == 3)
{ gtxy(2*(c-1)+8,r+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r ][c - 1] += 5map[m]. a [r][c] -= 5}
else if (map[m]. a [r][c - 1] == 4 || map[m]. a [r][c - 1] == 7)
{if (map[m]. a [r ][c - 2] == 0 || map[m]. a [r ][c - 2] == 3)
{ gtxy(2*(c-2)+8,r+3)printf("□")gtxy(2*(c-1)+8,r+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r ][c - 2] += 4map[m]. a [r ][c - 1] += 1
map[m]. a [r][c] -= 5}
}break
case 'D':
case 'd':
case 77:
if (map[m]. a [r][c + 1] == 0 || map[m]. a [r][c + 1] == 3)
{ gtxy(2*(c+1)+8,r+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8) {gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r][c + 1] += 5 map[m]. a [r][c] -= 5}
else if (map[m]. a [r][c + 1] == 4 || map[m]. a [r][c + 1] == 7)
{ if (map[m]. a [r][c + 2] == 0 || map[m]. a [r][c + 2] == 3)
{ gtxy(2*(c+2)+8,r+3)printf("□")gtxy(2*(c+1)+8,r+3)printf("♀")
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3)printf(" ")}
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3)printf("◎")}
map[m]. a [r][c + 2] += 4map[m]. a [r][c + 1] += 1
map[m]. a [r][c] -= 5}
}break
}
}
int ifwan( ) //是否完成(1是0否)
{ if(m==0){if(map[m].a[5][2]==7&&map[m].a[5][3]==7&&
map[m].a[6][2]==7&&map[m].a[6][3]==7) return 1}
if(m==1){if(map[m].a[5][2]==7&&map[m].a[6][2]==7&&
map[m].a[7][2]==7) return 1}
if(m==2){if(map[m].a[7][1]==7&&map[m].a[7][2]==7&&map[m].a[7][3]==7&&
map[m].a[7][4]==7&&map[m].a[7][5]==7) return 1}
if(m==3){if(map[m].a[4][4]==7&&map[m].a[4][5]==7&&map[m].a[4][6]==7&&
map[m].a[5][4]==7&&map[m].a[5][5]==7&&map[m].a[5][6]==7) return 1}
if(m==4){if(map[m].a[3][2]==7&&map[m].a[4][1]==7&&map[m].a[4][2]==7&&
map[m].a[5][1]==7&&map[m].a[5][2]==7) return 1}
return 0
}
int main( ) //主函数
{ while (1)
{ system("cls")
DrMap( )
while (1)
{ start( )
if(ifwan()){printf(" }7")break} //完成后响铃
m+=1
}
return 0
}
五子棋的代码:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
usingnamespacestd
constintN=15 //15*15的棋盘
constcharChessBoardflag='' //棋盘标志
constcharflag1='o' //玩家1或电脑的棋子标志
constcharflag2='X' //玩家2的棋子标志
typedefstructCoordinate //坐标类
{
intx //代表行
inty //代表列
}Coordinate
classGoBang //五子棋类
{
public:
GoBang() //初始化
{
InitChessBoard()
}
voidPlay() //下棋
{
CoordinatePos1 //玩家1或电脑
CoordinatePos2 //玩家2
intn=0
while(1)
{
intmode=ChoiceMode()
while(1)
{
if(mode==1) //电脑vs玩家
{
ComputerChess(Pos1,flag1) //电脑下棋
if(GetVictory(Pos1,0,flag1)==1) //0表示电脑,真表示获胜
break
PlayChess(Pos2,2,flag2) //玩家2下棋
if(GetVictory(Pos2,2,flag2)) //2表示玩家2
break
}
else //玩家1vs玩家2
{
PlayChess(Pos1,1,flag1) //玩家1下棋
if(GetVictory(Pos1,1,flag1)) //1表示玩家1
break
PlayChess(Pos2,2,flag2) //玩家2下棋
if(GetVictory(Pos2,2,flag2)) //2表示玩家2
break
}
}
cout<<"***再来一局***"<<endl
cout<<"yorn:"
charc='y'
cin>>c
if(c=='n')
break
}
}
protected:
intChoiceMode() //选择模式
{
inti=0
system("cls") //系统调用,清屏
InitChessBoard() //重新初始化棋盘
cout<<"***0、退出 1、电脑vs玩家 2、玩家vs玩家***"<<endl
while(1)
{
cout<<"请选择:"
cin>>i
if(i==0) //选择0退出
exit(1)
if(i==1||i==2)
returni
cout<<"输入不合法"<<endl
}
}
voidInitChessBoard() //初始化棋盘
{
for(inti=0i<N+1++i)
{
for(intj=0j<N+1++j)
{
_ChessBoard[i][j]=ChessBoardflag
}
}
}
voidPrintChessBoard() //打印棋盘,这个函数可以自己调整
{
system("cls") //系统调用,清空屏幕
for(inti=0i<N+1++i)
{
for(intj=0j<N+1++j)
{
if(i==0) //打印列数字
{
if(j!=0)
printf("%d ",j)
else
printf(" ")
}
elseif(j==0) //打印行数字
printf("%2d",i)
else
{
if(i<N+1)
{
printf("%c|",_ChessBoard[i][j])
}
}
}
cout<<endl
cout<<" "
for(intm=0m<Nm++)
{
printf("--|")
}
cout<<endl
}
}
voidPlayChess(Coordinate&pos,intplayer,intflag) //玩家下棋
{
PrintChessBoard() //打印棋盘
while(1)
{
printf("玩家%d输入坐标:",player)
cin>>pos.x>>pos.y
if(JudgeValue(pos)==1) //坐标合法
break
cout<<"坐标不合法,重新输入"<<endl
}
_ChessBoard[pos.x][pos.y]=flag
}
voidComputerChess(Coordinate&pos,charflag) //电脑下棋
{
PrintChessBoard() //打印棋盘
intx=0
inty=0
while(1)
{
x=(rand()%N)+1 //产生1~N的随机数
srand((unsignedint)time(NULL))
y=(rand()%N)+1 //产生1~N的随机数
srand((unsignedint)time(NULL))
if(_ChessBoard[x][y]==ChessBoardflag) //如果这个位置是空的,也就是没有棋子
break
}
pos.x=x
pos.y=y
_ChessBoard[pos.x][pos.y]=flag
}
intJudgeValue(constCoordinate&pos) //判断输入坐标是不是合法
{
if(pos.x>0&&pos.x<=N&&pos.y>0&&pos.y<=N)
{
if(_ChessBoard[pos.x][pos.y]==ChessBoardflag)
{
return1 //合法
}
}
return0 //非法
}
intJudgeVictory(Coordinatepos,charflag) //判断有没有人胜负(底层判断)
{
intbegin=0
intend=0
intbegin1=0
intend1=0
//判断行是否满足条件
(pos.y-4)>0?begin=(pos.y-4):begin=1
(pos.y+4)>N?end=N:end=(pos.y+4)
for(inti=pos.x,j=beginj+4<=endj++)
{
if(_ChessBoard[i][j]==flag&&_ChessBoard[i][j+1]==flag&&
_ChessBoard[i][j+2]==flag&&_ChessBoard[i][j+3]==flag&&
_ChessBoard[i][j+4]==flag)
return1
}
//判断列是否满足条件
(pos.x-4)>0?begin=(pos.x-4):begin=1
(pos.x+4)>N?end=N:end=(pos.x+4)
for(intj=pos.y,i=begini+4<=endi++)
{
if(_ChessBoard[i][j]==flag&&_ChessBoard[i+1][j]==flag&&
_ChessBoard[i+2][j]==flag&&_ChessBoard[i+3][j]==flag&&
_ChessBoard[i+4][j]==flag)
return1
}
intlen=0
//判断主对角线是否满足条件
pos.x>pos.y?len=pos.y-1:len=pos.x-1
if(len>4)
len=4
begin=pos.x-len //横坐标的起始位置
begin1=pos.y-len //纵坐标的起始位置
pos.x>pos.y?len=(N-pos.x):len=(N-pos.y)
if(len>4)
len=4
end=pos.x+len //横坐标的结束位置
end1=pos.y+len //纵坐标的结束位置
for(inti=begin,j=begin1(i+4<=end)&&(j+4<=end1)++i,++j)
{
if(_ChessBoard[i][j]==flag&&_ChessBoard[i+1][j+1]==flag&&
_ChessBoard[i+2][j+2]==flag&&_ChessBoard[i+3][j+3]==flag&&
_ChessBoard[i+4][j+4]==flag)
return1
}
//判断副对角线是否满足条件
(pos.x-1)>(N-pos.y)?len=(N-pos.y):len=pos.x-1
if(len>4)
len=4
begin=pos.x-len //横坐标的起始位置
begin1=pos.y+len //纵坐标的起始位置
(N-pos.x)>(pos.y-1)?len=(pos.y-1):len=(N-pos.x)
if(len>4)
len=4
end=pos.x+len //横坐标的结束位置
end1=pos.y-len //纵坐标的结束位置
for(inti=begin,j=begin1(i+4<=end)&&(j-4>=end1)++i,--j)
{
if(_ChessBoard[i][j]==flag&&_ChessBoard[i+1][j-1]==flag&&
_ChessBoard[i+2][j-2]==flag&&_ChessBoard[i+3][j-3]==flag&&
_ChessBoard[i+4][j-4]==flag)
return1
}
for(inti=1i<N+1++i) //棋盘有没有下满
{
for(intj=1j<N+1++j)
{
if(_ChessBoard[i][j]==ChessBoardflag)
return0 //0表示棋盘没满
}
}
return-1 //和棋
}
boolGetVictory(Coordinate&pos,intplayer,intflag) //对JudgeVictory的一层封装,得到具体那个玩家获胜
{
intn=JudgeVictory(pos,flag) //判断有没有人获胜
if(n!=0) //有人获胜,0表示没有人获胜
{
PrintChessBoard()
if(n==1) //有玩家赢棋
{
if(player==0) //0表示电脑获胜,1表示玩家1,2表示玩家2
printf("***电脑获胜***\n")
else
printf("***恭喜玩家%d获胜***\n",player)
}
else
printf("***双方和棋***\n")
returntrue //已经有人获胜
}
returnfalse //没有人获胜
}
private:
char_ChessBoard[N+1][N+1]
}
扩展资料:设计思路
1、进行问题分析与设计,计划实现的功能为,开局选择人机或双人对战,确定之后比赛开始。
2、比赛结束后初始化棋盘,询问是否继续比赛或退出,后续可加入复盘、悔棋等功能。
3、整个过程中,涉及到了棋子和棋盘两种对象,同时要加上人机对弈时的AI对象,即涉及到三个对象。
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)