1.井字棋
代码:
#include<iostream>
#include<stdlib.h>
#include <time.h>
using namespace std;
char r='d';
char f='d';
int jiru1[9]={0}; //人人游戏时用来记录是否在此处下过棋
int i=1;
char game[3][3]; //用来下棋
int x,y;
int a[8]; //人机游戏时记录每行,列,斜行有多少子
int b[8]; //人机游戏时记录机器每行,列,斜行有多少子
int c[8]; //人机游戏时记录人每行,列,斜行有多少子
int jiru[3][3]={0}; //人机游戏时用来记录是否在此处下过棋
int qi[3][3]={0}; //人人游戏时用来记录对手是否在此处下过棋
int ren[3][3]={0}; //人人游戏时用来记录人是否在此处下过棋
void zhuanhua(int t) //把输入的1~9数字转化成坐标
{
int h,g;
h=t%3;
g=(t-h)/3;
x=g;
y=h-1;
}
void suan() //人机游戏时计算每行,列,斜行有多少子
{
int a[8],b[8],c[8];
a[1]=jiru[0][0]+jiru[0][1]+jiru[0][2];
a[2]=jiru[1][0]+jiru[1][1]+jiru[1][2];
a[3]=jiru[2][0]+jiru[2][1]+jiru[2][2];
a[4]=jiru[0][0]+jiru[1][0]+jiru[2][0];
a[5]=jiru[0][1]+jiru[1][1]+jiru[2][1];
a[6]=jiru[0][2]+jiru[1][2]+jiru[2][2];
a[7]=jiru[0][0]+jiru[1][1]+jiru[2][2];
a[8]=jiru[0][2]+jiru[1][1]+jiru[2][0];
b[1]=qi[0][0]+qi[0][1]+qi[0][2];
b[2]=qi[1][0]+qi[1][1]+qi[1][2];
b[3]=qi[2][0]+qi[2][1]+qi[2][2];
b[4]=qi[0][0]+qi[1][0]+qi[2][0];
b[5]=qi[0][1]+qi[1][1]+qi[2][1];
b[6]=qi[0][2]+qi[1][2]+qi[2][2];
b[7]=qi[0][0]+qi[1][1]+qi[2][2];
b[8]=qi[0][2]+qi[1][1]+qi[2][0];
c[1]=ren[0][0]+ren[0][1]+ren[0][2];
c[2]=ren[1][0]+ren[1][1]+ren[1][2];
c[3]=ren[2][0]+ren[2][1]+ren[2][2];
c[4]=ren[0][0]+ren[1][0]+ren[2][0];
c[5]=ren[0][1]+ren[1][1]+ren[2][1];
c[6]=ren[0][2]+ren[1][2]+ren[2][2];
c[7]=ren[0][0]+ren[1][1]+ren[2][2];
c[8]=ren[0][2]+ren[1][1]+ren[2][0];
}
void chushihua() //把数据初始化
{
i=1;
int j,k;
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
game[j][k]='/';
}
}
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
jiru[j][k]=0;
qi[j][k]=0;
ren[j][k]=0;
}
}
}
void shuru() //输入数据
{
int k,j;
for(k=0;k<3;k++)
{
for(j=0;j<3;j++)
{
cout << game[k][j] << " ";
}
cout << endl;
}
}
void renxia(int t,int s) //人落子的情况
{
if(i%2==1)
{
game[t][s]='x';
jiru[t][s]=1;
ren[t][s]=1;
system("cls");
shuru();
}
else
{
game[t][s]='o';
jiru[t][s]=1;
ren[t][s]=1;
system("cls");
shuru();
}
}
void huiqi1(int t,int s) //人机游戏时人悔棋的情况
{
game[t][s]='/';
jiru[t][s]=0;
ren[t][s]=0;
system("cls");
shuru();
}
void suiji() //产生随机数
{
int choice;
srand((unsigned int)time(NULL));
choice=rand()%10;
int h,g;
if(choice==10)
{
choice=9;
}
else if(choice==0)
{
choice=1;
}
h=choice%3;
g=(choice-h)/3;
game[g][h-1]='x';
jiru[g][h-1]=1;
qi[g][h-1]=1;
}
void jixia() //人机游戏时机器下子
{
if(i==1)
{
suiji();
system("cls");
shuru();
}
else if(i==3)
{
int p;
for(p=1;p<10;p++)
{
int h,g;
h=p%3;
g=(p-h)/3;
if(jiru[g][h-1]==0)
{
game[g][h-1]='x';
jiru[g][h-1]=1;
qi[g][h-1]=1;
break;
}
}
system("cls");
shuru();
}
else
{
suan();
int u,v=0;
for(u=1;u<4;u++)
{
if((a[u]==2)&&((b[u]==2)||(c[u]==2)))
{
int z;
for(z=0;z<3;z++)
{
if(jiru[u-1][z]==0)
{
game[u-1][z]='x';
jiru[u-1][z]=1;
qi[u-1][z]=1;
}
}
system("cls");
shuru();
v++;
break;
}
}
if(v==0)
{
for(u=4;u<7;u++)
{
if((a[u]==2)&&((b[u]==2)||(c[u]==2)))
{
int z;
for(z=0;z<3;z++)
{
if(jiru[z][u-4]==0)
{
game[z][u-4]='x';
jiru[z][u-4]=1;
qi[z][u-4]=1;
}
}
system("cls");
shuru();
v++;
break;
}
}
if(v==0)
{
if((a[7]==2)&&((b[7]==2)||(c[7]==2)))
{
if(jiru[0][0]==0)
{
game[0][0]='x';
jiru[0][0]=1;
qi[0][0]=1;
}
else if(jiru[1][1]==0)
{
game[1][1]='x';
jiru[1][1]=1;
qi[1][1]=1;
}
else if(jiru[2][2]==0)
{
game[2][2]='x';
jiru[2][2]=1;
qi[2][2]=1;
}
system("cls");
shuru();
v++;
}
}
if(v==0)
{
if((a[8]==2)&&((b[8]==2)||(c[8]==2)))
{
if(jiru[0][2]==0)
{
game[0][2]='x';
jiru[0][2]=1;
qi[0][2]=1;
}
else if(jiru[1][1]==0)
{
game[1][1]='x';
jiru[1][1]=1;
qi[1][1]=1;
}
else if(jiru[2][0]==0)
{
game[2][0]='x';
jiru[2][0]=1;
qi[2][0]=1;
}
system("cls");
shuru();
v++;
}
if(v==0)
{
for(u=1;u<4;u++)
{
if((a[u]==1)&&(b[u]==1))
{
int z;
for(z=0;z<3;z++)
{
if(jiru[u-1][z]==0)
{
game[u-1][z]='x';
jiru[u-1][z]=1;
qi[u-1][z]=1;
system("cls");
shuru();
v++;
break;
}
}
}
}
if(v==0)
{
for(u=4;u<7;u++)
{
if((a[u]==1)&&(b[u]==1))
{
int z;
for(z=0;z<3;z++)
{
if(jiru[z][u-4]==0)
{
game[z][u-4]='x';
jiru[z][u-4]=1;
qi[z][u-4]=1;
system("cls");
shuru();
v++;
break;
}
}
}
}
if(v==0)
{
if((a[7]==1)&&(b[7]==1)&&(c[7]==0))
{
if((jiru[0][0]==1)&&(jiru[1][1]==0)){game[1][1]='x';jiru[1][1]=1;qi[1][1]=1;}
else if((jiru[1][1]==1)&&(jiru[2][2]==0)){game[2][2]='x';jiru[2][2]=1;qi[2][2]=1;}
else if((jiru[2][2]==1)&&(jiru[1][1]==0)){game[1][1]='x';jiru[1][1]=1;qi[1][1]=1;}
system("cls");
shuru();
v++;
}
if(v==0)
{
if((a[8]==1)&&(b[8]==1)&&(c[8]==0))
{
if((jiru[0][2]==1)&&(jiru[1][1]==0)){game[1][1]='x';jiru[1][1]=1;qi[1][1]=1;}
else if((jiru[1][1]==1)&&(jiru[0][2]==0)){game[0][2]='x';jiru[0][2]=1;qi[0][2]=1;}
else if((jiru[2][0]==1)&&(jiru[1][1]==0)){game[1][1]='x';jiru[1][1]=1;qi[1][1]=1;}
system("cls");
shuru();
v++;
}
if(v==0)
{
int p;
for(p=1;p<9;p++)
{
int h,g;
h=p%3;
g=(p-h)/3;
if((jiru[g][h-1]==0)&&(ren[g][h-1]==0))
{
game[g][h-1]='x';
jiru[g][h-1]=1;
qi[g][h-1]=1;
system("cls");
shuru();
v++;
break;
}
}
system("cls");
shuru();
}
}
}
}
}
}
}
}
}
void jixianzou() //人机对战的情况
{
if((game[0][0]=='x'&&game[0][1]=='x'&&game[0][2]=='x')||(game[1][0]=='x'&&game[1][1]=='x'&&game[1][2]=='x')||(game[2][0]=='x'&&game[2][1]=='x'&&game[2][2]=='x')||(game[0][0]=='x'&&game[1][0]=='x'&&game[2][0]=='x')||(game[0][1]=='x'&&game[1][1]=='x'&&game[2][1]=='x')||(game[0][2]=='x'&&game[1][2]=='x'&&game[2][2]=='x'))
{
cout << "玩家失败" << endl;
}
else if((game[0][0]=='x'&&game[1][1]=='x'&&game[2][2]=='x')||(game[0][2]=='x'&&game[1][1]=='x'&&game[2][0]=='x'))
{
cout << "玩家失败" << endl;
}
else if((game[0][0]=='o'&&game[0][1]=='o'&&game[0][2]=='o')||(game[1][0]=='o'&&game[1][1]=='o'&&game[1][2]=='o')||(game[2][0]=='o'&&game[2][1]=='o'&&game[2][2]=='o')||(game[0][0]=='o'&&game[1][0]=='o'&&game[2][0]=='o')||(game[0][1]=='o'&&game[1][1]=='o'&&game[2][1]=='o')||(game[0][2]=='o'&&game[1][2]=='o'&&game[2][2]=='o'))
{
cout << "玩家获胜" << endl;
}
else if((game[0][0]=='o'&&game[1][1]=='o'&&game[2][2]=='o')||(game[0][2]=='o'&&game[1][1]=='o'&&game[2][0]=='o'))
{
cout << "玩家获胜" << endl;
}
else if(i==10)
{
cout << "平局" << endl;
}
else
{
if(i%2==1)
{
jixia();
i++;
jixianzou();
}
int t;
cin >> t;
zhuanhua(t);
if((t<0)||t>10||jiru[x][y]==1)
{
cout << "输入错误,请重新输入" << endl;
jixianzou();
}
else
{
renxia(x,y);
cout<< "是否悔棋(Y or N),是否重新开始(C or N),是否结束(T or N)" << endl;
cin >> f;
if(f=='Y')
{
huiqi1(x,y);
jixianzou();
}
else if(f=='C')
{
system("cls");
chushihua();
shuru();
jixianzou();
}
else if(f=='T')
{
}
else
{
renxia(x,y);
i++;
jixianzou();
}
}
}
}
void ren1(int choice) //人人对战时人下子
{
if(i%2==1)
{
int h,g;
h=choice%3;
g=(choice-h)/3;
game[g][h-1]='x';
jiru1[choice-1]=1;
system("cls");
shuru();
}
else
{
int h,g;
h=choice%3;
g=(choice-h)/3;
game[g][h-1]='o';
jiru1[choice-1]=1;
system("cls");
shuru();
}
}
void huiqi(int choice) //人人对战时人悔棋
{
int h,g;
h=choice%3;
g=(choice-h)/3;
game[g][h-1]='/';
jiru1[choice-1]=0;
system("cls");
shuru();
}
void xiaqi() //人人对战时的下棋情况
{
if((game[0][0]=='x'&&game[0][1]=='x'&&game[0][2]=='x')||(game[1][0]=='x'&&game[1][1]=='x'&&game[1][2]=='x')||(game[2][0]=='x'&&game[2][1]=='x'&&game[2][2]=='x')||(game[0][0]=='x'&&game[1][0]=='x'&&game[2][0]=='x')||(game[0][1]=='x'&&game[1][1]=='x'&&game[2][1]=='x')||(game[0][2]=='x'&&game[1][2]=='x'&&game[2][2]=='x'))
{
cout << "玩家1获胜" << endl;
}
else if((game[0][0]=='x'&&game[1][1]=='x'&&game[2][2]=='x')||(game[0][2]=='x'&&game[1][1]=='x'&&game[2][0]=='x'))
{
cout << "玩家1获胜" << endl;
}
else if((game[0][0]=='o'&&game[0][1]=='o'&&game[0][2]=='o')||(game[1][0]=='o'&&game[1][1]=='o'&&game[1][2]=='o')||(game[2][0]=='o'&&game[2][1]=='o'&&game[2][2]=='o')||(game[0][0]=='o'&&game[1][0]=='o'&&game[2][0]=='o')||(game[0][1]=='o'&&game[1][1]=='o'&&game[2][1]=='o')||(game[0][2]=='o'&&game[1][2]=='o'&&game[2][2]=='o'))
{
cout << "玩家2获胜" << endl;
}
else if((game[0][0]=='o'&&game[1][1]=='o'&&game[2][2]=='o')||(game[0][2]=='o'&&game[1][1]=='o'&&game[2][0]=='o'))
{
cout << "玩家2获胜" << endl;
}
else if(i==10)
{
cout << "平局" << endl;
}
else
{
int m;
cin >> m;
if(m>9||m<1||jiru1[m-1]==1)
{
cout << "输入错误,请重新输入" << endl;
xiaqi();
}
else
{
ren1(m);
cout << "(是否悔棋 Y or N)(是否重新开始 C or N)或(是否结束游戏 T or N)" << endl;
cin >> r;
if(r=='Y')
{
huiqi(m);
xiaqi();
}
else if(r=='C')
{
i=1;
int j,k;
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
game[j][k]='/';
}
}
for(j=0;j<9;j++)
{
jiru1[j]=0;
}
system("cls");
shuru();
xiaqi();
}
else if(r=='T')
{
}
else
{
i++;
system("cls");
shuru();
xiaqi();
}
}
}
}
int main() //主函数
{
cout << " 欢迎进入井字棋游戏!" << endl;
cout << " 本游戏用数字1~9表示下棋位置 " << endl;
while(1)
{
if((r=='T')||(f=='T'))
{
break;
}
cout << " 是否下棋 Y or N " << endl;
char y;
cin >> y;
if(y=='N')
{
break;
}
else
{
i=1;
int j,k;
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
game[j][k]='/';
}
}
for(j=0;j<9;j++)
{
jiru1[j]=0;
}
cout << " 人机下棋 or 人人下棋 ( 1 or 2 )" << endl;
int u;
cin >> u;
if(u==2)
{
shuru();
xiaqi();
}
if(u==1)
{
chushihua();
shuru();
jixianzou();
}
}
}
return 0;
}
提示:每次结束后都要选择是否悔棋,要不计算机下不了,还有计算机AI很低,见谅(电脑随机生成棋子位置的原因)。
代码:
#include<iostream>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
const int N=21; //N为蛇所能移动的正方形的边长
void Get_xy(int x,int y) //定位光标位置
{
HANDLE hout;
COORD pos;
pos.X=x*2;
pos.Y=y;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout,pos);
}
void Color(int num) //设置颜色
{
HANDLE hout;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hout,num);
}
void Initial() //初始化
{
int i,j;
int wall[N+2][N+2]={{0}};
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
wall[i][j]=1;
Color(11);
for(i=0;i<N+2;i++)
{
for(j=0;j<N+2;j++)
{
if(wall[i][j])
cout<<"■";
else cout<<"□";
}
cout<<endl;
}
Get_xy(N+3,1); Color(20);
cout<<"按'W','S','A','D'进行操作"<<endl;
Get_xy(N+3,2); Color(20);
cout<<"按任意键暂停"<<endl;
Get_xy(N+3,3); Color(20);
cout<<"得分:"<<endl;
}
void game()
{
int** snake=NULL; //snake为存储蛇身的每一点的位置的数组
int len=1;
int i;
int score=0;
int apple[2];
int tail[2];
char ch='p';
Initial();
//获得一个随机的食物位置
srand((unsigned)time(NULL));
apple[0]=rand()%N+1;
apple[1]=rand()%N+1;
Get_xy(apple[0],apple[1]);
Color(12);
cout<<"●"<<endl;
//给snake数组分配内存空间
snake=(int**)realloc(snake,sizeof(int*)*len);
for(i=0;i<len;i++)
snake[i]=(int*)malloc(sizeof(int)*2);
//开始将蛇置于页面正中间
snake[0][0]=N/2;
snake[0][1]=N/2+1;
Get_xy(snake[0][0],snake[0][1]); Color(14);
cout<<"⊙"<<endl;
int flag=1; //
while(1)
{
//每移动一次,就把上次的尾巴还原为背景色
if(flag)
{
tail[0]=snake[len-1][0];
tail[1]=snake[len-1][1];
Get_xy(tail[0],tail[1]);
Color(11);
cout<<"■"<<endl;
}
flag=1;
for(i=len-1;i>0;i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
Get_xy(snake[i][0],snake[i][1]);
Color(14);
cout<<"★"<<endl;
}
/*====================================================================
函数名:kbhit()(VC++6.0下为_kbhit())
功 能及返回值: 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0
用 法:int kbhit(void);
包含头文件: include <conio.h>
=====================================================================*/
if(kbhit())
{
Get_xy(0,N+3);
ch=getche();
}
switch(ch)
{
case 'W':
case 'w': snake[0][1]--; break;
case 'S':
case 's': snake[0][1]++; break;
case 'A':
case 'a': snake[0][0]--; break;
case 'D':
case 'd': snake[0][0]++; break;
default :break;
}
for(i=1;i<len;i++)
{
//蛇咬到蛇身,游戏结束!
if(snake[0][0]==snake[i][0] && snake[0][1]==snake[i][1])
{
Get_xy(N/2,N/2); Color(30);
cout<<"Game over!"<<endl;
exit(0);
}
}
Get_xy(snake[0][0],snake[0][1]);
Color(14); cout<<"⊙"<<endl;
/*==================================================================
函数名: sleep
功 能: 执行挂起一段时间
用 法: unsigned sleep(unsigned milliseconds);
在VC中使用带上头文件
#include <windows.h>
在gcc编译器中,使用的头文件因gcc版本的不同而不同
#include <unistd.h>
===================================================================*/
//分数越高,蛇的移动速度越快
Sleep(fabs(200.0-0.5*score));
//蛇吃到食物
if(snake[0][0]==apple[0] && snake[0][1]==apple[1])
{
flag=0; score++; len++; srand((unsigned)time(NULL));
snake=(int**)realloc(snake,sizeof(int*)*len);
snake[len-1]=(int*)malloc(sizeof(int)*2);
Get_xy(N+6,3); Color(20); cout<<score<<endl;
apple[0]=rand()%N+1; apple[1]=rand()%N+1;
Get_xy(apple[0],apple[1]);
Color(12);
cout<<"●"<<endl;
}
//蛇头撞墙了,游戏结束!
if(snake[0][0]==0 || snake[0][0]==N || snake[0][1]==0 || snake[0][1]==N)
{
Get_xy(N/2,N/2); Color(30);
cout<<"Game Over!"<<endl;
for(i=0;i<len;i++)
free(snake[i]);
Sleep(INFINITE);
exit(0);
}
//游戏通关!
if(len>=N*N/20)
{
Get_xy(N/2,N/2); Color(30);
cout<<"Win!"<<endl;
for(i=0;i<len;i++)
free(snake[i]);
Sleep(INFINITE);
exit(0);
}
}
}
int main()
{
game();
return 0;
}
总结:贪吃蛇在构想时原理很简单,但要实现起来难,这是网上已公开的贪吃蛇代码,我收录下来,慢慢学习。而且已纠正网上的代码在电脑上无法运行的问题。