人人对战:
#include<iostream>
using namespace std;
void wz()
{
cout<<"下面开始五子棋比赛,一号选手用方块符号,二号用心型符号:"<<endl<<endl;
for(int i=3;i>0;i--)
{
cout<<i<<" ";
}
cout<<"开始-----------"<<endl<<endl;
}
int hs()
{
int x,y,i=0,j=1;
char d[80][80];
//初始化---
for(x=0;x<25;x+=3)
{
for(y=0;y<80;y+=3)
{
d[x][y]='+';
}
}
//第一行数字--
for(x=0;x<10;x++)
cout<<x<<" ";
for(x=10;x<27;x++)
cout<<x<<" ";
cout<<endl;
//输出棋盘--
for(x=0;x<25;x+=3)
{
cout<<j<<" ";j++;
for(y=0;y<76;y+=3)
{
cout<<d[x][y]<<" ";
}
cout<<endl;
}
while(1)
{
int a,b;
if(i%2==0)
cout<<"由玩家一下棋:"<<endl;
else
cout<<"由玩家二下棋:"<<endl;
cin>>a>>b;
if(i%2==1)
{
if(d[(a-1)*3][(b-1)*3]=='+')
d[(a-1)*3][(b-1)*3]=3;
else
{
cout<<"-----------------------------不能覆盖棋子:"<<endl;
continue;
}
}
else
{
if(d[(a-1)*3][(b-1)*3]=='+')
d[(a-1)*3][(b-1)*3]=1;
else
{
cout<<"-----------------------------不能覆盖棋子:"<<endl;
continue;
}
}
i++;
j=1;
//第一行数字---
for(x=0;x<10;x++)
cout<<x<<" ";
for(x=10;x<27;x++)
cout<<x<<" ";
cout<<endl;
for(x=0;x<25;x+=3)
{
cout<<j<<" ";j++;
for(y=0;y<76;y+=3)
{
cout<<d[x][y]<<" ";
}
cout<<endl;
}
//判断是否是五个
if(//横着
d[(a-1)*3][(b-1)*3]==1&&d[(a-1)*3][(b)*3]==1&&d[(a-1)*3][(b+1)*3]==1&&d[(a-1)*3][(b+2)*3]==1&&d[(a-1)*3][(b+3)*3]==1
||d[(a-1)*3][(b-2)*3]==1&&d[(a-1)*3][(b-1)*3]==1&&d[(a-1)*3][(b)*3]==1&&d[(a-1)*3][(b+1)*3]==1&&d[(a-1)*3][(b+2)*3]==1
||d[(a-1)*3][(b-3)*3]==1&&d[(a-1)*3][(b-2)*3]==1&&d[(a-1)*3][(b-1)*3]==1&&d[(a-1)*3][(b)*3]==1&&d[(a-1)*3][(b+1)*3]==1
||d[(a-1)*3][(b-4)*3]==1&&d[(a-1)*3][(b-3)*3]==1&&d[(a-1)*3][(b-2)*3]==1&&d[(a-1)*3][(b-1)*3]==1&&d[(a-1)*3][(b)*3]==1
||d[(a-1)*3][(b-5)*3]==1&&d[(a-1)*3][(b-4)*3]==1&&d[(a-1)*3][(b-3)*3]==1&&d[(a-1)*3][(b-2)*3]==1&&d[(a-1)*3][(b-1)*3]==1
//从左上到右下
||d[(a-1)*3][(b-1)*3]==1&&d[a*3][b*3]==1&&d[(a+1)*3][(b+1)*3]==1&&d[(a+2)*3][(b+2)*3]==1&&d[(a+3)*3][(b+3)*3]==1
||d[(a-2)*3][(b-2)*3]==1&&d[(a-1)*3][(b-1)*3]==1&&d[a*3][b*3]==1&&d[(a+1)*3][(b+1)*3]==1&&d[(a+2)*3][(b+2)*3]==1
||d[(a-3)*3][(b-3)*3]==1&&d[(a-2)*3][(b-2)*3]==1&&d[(a-1)*3][(b-1)*3]==1&&d[a*3][b*3]==1&&d[(a+1)*3][(b+1)*3]==1
||d[(a-4)*3][(b-4)*3]==1&&d[(a-3)*3][(b-3)*3]==1&&d[(a-2)*3][(b-2)*3]==1&&d[(a-1)*3][(b-1)*3]==1&&d[a*3][b*3]==1
||d[(a-5)*3][(b-5)*3]==1&&d[(a-4)*3][(b-4)*3]==1&&d[(a-3)*3][(b-3)*3]==1&&d[(a-2)*3][(b-2)*3]==1&&d[(a-1)*3][(b-1)*3]==1
//从右上到左下
||d[(a-1)*3][(b-1)*3]==1&&d[(a-2)*3][b*3]==1&&d[a-3][b+1]==1&&d[a-4][b+2]==1&&d[a-5][b+3]==1
||d[a*3][(b-2)*3]==1&&d[(a-1)*3][(b-1)*3]==1&&d[(a-2)*3][b*3]==1&&d[(a-3)*3][(b+1)*3]==1&&d[(a-4)*3][(b+2)*3]==1
||d[(a+1)*3][(b-3)*3]==1&&d[a*3][(b-2)*3]==1&&d[(a-1)*3][(b-1)*3]==1&&d[(a-2)*3][b*3]==1&&d[(a-3)*3][(b+1)*3]==1
||d[(a+2)*3][(b-4)*3]==1&&d[(a+1)*3][(b-3)*3]==1&&d[a*3][(b-2)*3]==1&&d[(a-1)*3][(b-1)*3]==1&&d[(a-2)*3][b*3]==1
||d[(a+3)*3][(b-5)*3]==1&&d[(a+2)*3][(b-4)*3]==1&&d[(a+1)*3][(b-3)*3]==1&&d[a*3][(b-2)*3]==1&&d[(a-1)*3][(b-1)*3]==1
//竖着
||d[(a-1)*3][(b-1)*3]==1&&d[a*3][(b-1)*3]==1&&d[(a+1)*3][(b-1)*3]==1&&d[(a+2)*3][(b-1)*3]==1&&d[(a+3)*3][(b-1)*3]==1
||d[(a-2)*3][(b-1)*3]==1&&d[(a-1)*3][(b-1)*3]==1&&d[a*3][(b-1)*3]==1&&d[(a+1)*3][(b-1)*3]==1&&d[(a+2)*3][(b-1)*3]==1
||d[(a-3)*3][(b-1)*3]==1&&d[(a-2)*3][(b-1)*3]==1&&d[(a-1)*3][(b-1)*3]==1&&d[a*3][(b-1)*3]==1&&d[(a+1)*3][(b-1)*3]==1
||d[(a-4)*3][(b-1)*3]==1&&d[(a-3)*3][(b-1)*3]==1&&d[(a-2)*3][(b-1)*3]==1&&d[(a-1)*3][(b-1)*3]==1&&d[a*3][(b-1)*3]==1
||d[(a-5)*3][(b-1)*3]==1&&d[(a-4)*3][(b-1)*3]==1&&d[(a-3)*3][(b-1)*3]==1&&d[(a-2)*3][(b-1)*3]==1&&d[(a-1)*3][(b-1)*3]==1
)
{
cout<<endl<<"玩家一获胜"<<endl;break;
}
if(//横着
d[(a-1)*3][(b-1)*3]==3&&d[(a-1)*3][(b)*3]==3&&d[(a-1)*3][(b+1)*3]==3&&d[(a-1)*3][(b+2)*3]==3&&d[(a-1)*3][(b+3)*3]==3
||d[(a-1)*3][(b-2)*3]==3&&d[(a-1)*3][(b-1)*3]==3&&d[(a-1)*3][(b)*3]==3&&d[(a-1)*3][(b+1)*3]==3&&d[(a-1)*3][(b+2)*3]==3
||d[(a-1)*3][(b-3)*3]==3&&d[(a-1)*3][(b-2)*3]==3&&d[(a-1)*3][(b-1)*3]==3&&d[(a-1)*3][(b)*3]==3&&d[(a-1)*3][(b+1)*3]==3
||d[(a-1)*3][(b-4)*3]==3&&d[(a-1)*3][(b-3)*3]==3&&d[(a-1)*3][(b-2)*3]==3&&d[(a-1)*3][(b-1)*3]==3&&d[(a-1)*3][(b)*3]==3
||d[(a-1)*3][(b-5)*3]==3&&d[(a-1)*3][(b-4)*3]==3&&d[(a-1)*3][(b-3)*3]==3&&d[(a-1)*3][(b-2)*3]==3&&d[(a-1)*3][(b-1)*3]==3
//从左上到右下
||d[(a-1)*3][(b-1)*3]==3&&d[a*3][b*3]==3&&d[(a+1)*3][(b+1)*3]==3&&d[(a+2)*3][(b+2)*3]==3&&d[(a+3)*3][(b+3)*3]==3
||d[(a-2)*3][(b-2)*3]==3&&d[(a-1)*3][(b-1)*3]==3&&d[a*3][b*3]==3&&d[(a+1)*3][(b+1)*3]==3&&d[(a+2)*3][(b+2)*3]==3
||d[(a-3)*3][(b-3)*3]==3&&d[(a-2)*3][(b-2)*3]==3&&d[(a-1)*3][(b-1)*3]==3&&d[a*3][b*3]==3&&d[(a+1)*3][(b+1)*3]==3
||d[(a-4)*3][(b-4)*3]==3&&d[(a-3)*3][(b-3)*3]==3&&d[(a-2)*3][(b-2)*3]==3&&d[(a-1)*3][(b-1)*3]==3&&d[a*3][b*3]==3
||d[(a-5)*3][(b-5)*3]==3&&d[(a-4)*3][(b-4)*3]==3&&d[(a-3)*3][(b-3)*3]==3&&d[(a-2)*3][(b-2)*3]==3&&d[(a-1)*3][(b-1)*3]==3
//从右上到左下
||d[(a-1)*3][(b-1)*3]==3&&d[(a-2)*3][b*3]==3&&d[a-3][b+1]==3&&d[a-4][b+2]==3&&d[a-5][b+3]==3
||d[a*3][(b-2)*3]==3&&d[(a-1)*3][(b-1)*3]==3&&d[(a-2)*3][b*3]==3&&d[(a-3)*3][(b+1)*3]==3&&d[(a-4)*3][(b+2)*3]==3
||d[(a+1)*3][(b-3)*3]==3&&d[a*3][(b-2)*3]==3&&d[(a-1)*3][(b-1)*3]==3&&d[(a-2)*3][b*3]==3&&d[(a-3)*3][(b+1)*3]==3
||d[(a+2)*3][(b-4)*3]==3&&d[(a+1)*3][(b-3)*3]==3&&d[a*3][(b-2)*3]==3&&d[(a-1)*3][(b-1)*3]==3&&d[(a-2)*3][b*3]==3
||d[(a+3)*3][(b-5)*3]==3&&d[(a+2)*3][(b-4)*3]==3&&d[(a+1)*3][(b-3)*3]==3&&d[a*3][(b-2)*3]==3&&d[(a-1)*3][(b-1)*3]==3
//竖着
||d[(a-1)*3][(b-1)*3]==3&&d[a*3][(b-1)*3]==3&&d[(a+1)*3][(b-1)*3]==3&&d[(a+2)*3][(b-1)*3]==3&&d[(a+3)*3][(b-1)*3]==3
||d[(a-2)*3][(b-1)*3]==3&&d[(a-1)*3][(b-1)*3]==3&&d[a*3][(b-1)*3]==3&&d[(a+1)*3][(b-1)*3]==3&&d[(a+2)*3][(b-1)*3]==3
||d[(a-3)*3][(b-1)*3]==3&&d[(a-2)*3][(b-1)*3]==3&&d[(a-1)*3][(b-1)*3]==3&&d[a*3][(b-1)*3]==3&&d[(a+1)*3][(b-1)*3]==3
||d[(a-4)*3][(b-1)*3]==3&&d[(a-3)*3][(b-1)*3]==3&&d[(a-2)*3][(b-1)*3]==3&&d[(a-1)*3][(b-1)*3]==3&&d[a*3][(b-1)*3]==3
||d[(a-5)*3][(b-1)*3]==3&&d[(a-4)*3][(b-1)*3]==3&&d[(a-3)*3][(b-1)*3]==3&&d[(a-2)*3][(b-1)*3]==3&&d[(a-1)*3][(b-1)*3]==3
)
{
cout<<endl<<"玩家二获胜"<<endl;break;
}
}
return 0;
}
int main()
{
char a;
wz();
hs();
while(1)
{
cout<<"按P重新开始:";
cin>>a;
if(a=='p')
hs();
else
break;
}
return 0;
}
人机对战暂时没想出来,有知道的希望指教一下!!感激----
今天在学习移位运算符和取反运算符的时候,发现一些问题!现在呢在这里说一下自己的感悟!
先看下面一道 取反和移位相结合的问题
#include<stdio.h>
int main()
{
char a=0x11;
char b;
b=~a<<2;
printf("%d\n",b);
b=~(a<<2);
printf("%d\n",b);
b=~a>>2;
printf("%d\n",b);
b=~(a>>2);
printf("%d\n",b);
return 0;
}
输出结果是:
-72
-69
-5
-5
刚开始在想为什么相同的操作(只是移位方向不同),为什么上面两个结果不一样,而下面两个结果却是一样的呢。有些人在疑问是不是因为下面两个表达式都是先移位后取反的呢?我可以肯定不是这样的,但是不能解释这个结果。这个问题我纠结了半天! 不过对下面的知识了解之后便豁然开朗了。
移位运算符分为左移运算符和右移运算符。
当对一个数执行左移运算的时候,右边的空位是用0补齐。
而当对一个数执行右移运算的时候,左边的空位是用符号位补齐的(这是我之前没注意的地方,惭愧)。
解体步骤如下:
a=0x11=0001 0001
~a=1110 1110
a<<2=0100 0100
a>>2=0000 0100
~a<<2=1011 1000
现在 1011 1000是补码,转化成原码之后是
1100 1000=-72 -72是十进制
~(a<<2)=0100 0100
转化为原码之后是 1100 0101=-69
~a>>2=1111 1011(注意现在是用1补齐的,因为~a的符号位是1)
转化为原码之后是 1000 0101=-5
~(a>>2)=1111 1011
转化为原码之后是 1000 0101=-5
明白之后心情很好,所以在这里分享一下。呵呵