1.程序代码
/*1.vboard[x][y]
x表示数组行,Y表示数组列
索引基于0
2.在控制台输入的行、列都基于1
3.程序在华为NOVA7开发、调试
4.C编译器必须支持UTF-8
*/
#include <stdio.h>
#define SIZE 15
#define HEI 0X48 //'H'
#define BAI 0X42 //'B'
#define BLK '0'
#define NUL 0
char *pos="┼";
char *black="●";
char *white="○";
int step=0;
int count=0;
int qizi[2]={
HEI,BAI};
int record[SIZE*SIZE]= {
-1}; //记录
char vboard[SIZE][SIZE+1];
int players[2];//记录谁执黑谁执白
//初始化;
void init();
//显示棋盘
void showConsole();
//显示虚拟棋盘
void debugVboard();
//下子先后
void inputStep();
//棋子记录
void recordStep(int x,int y);
//打印棋子记录
void printRecord();
void setValue(int x,int y,int v);
//下一步棋
int player(int wc);
//轮流下棋
void alternatePlay();
int main()
{
init();
initVboard();
alternatePlay();
/*
setValue(15,15,HEI);
setValue(14,15,BAI);
showConsole();
*/
//debugVboard();
//searchVboard();
return 0;
}
init()
{
count=0;
}
void showConsole()
{
char ind[16]="123456789ABCDEF\0";
char *ind2="ABCDEFGHIJKLMNO";
for(int i=0; i<SIZE; i++)
{
//打印左侧列索引
printf("%c",ind[i]);
//打印棋子
for(int j=0; j<SIZE; j++)
{
if(vboard[i][j]=='*')
printf("%s ",pos);
else if(vboard[i][j]=='H')
printf("%s ",black);
else if(vboard[i][j]=='B')
printf("%s ",white);
else printf("%s ",pos);
}
printf("\n");
}
//打印底部行索引
for(int k=0; k<SIZE; k++)
{
printf("%2c",*(ind2+k));
}
putchar('\n');
}
void initVboard()
{
for(int i=0; i<SIZE; i++)
{
for(int j=0; j<SIZE; j++)
{
vboard[i][j]='*';
}
vboard[i][SIZE]=0;
}
}
void debugVboard()
{
for(int i=0; i<SIZE; i++)
{
for(int j=0; j<SIZE; j++)
{
printf("%2c",vboard[i][j]);
}
putchar('\n');
}
}
//索引基于1
void setValue(int x,int y,int v)
{
vboard[x-1][y-1]=v;
}
void printRecord()
{
for(int i=0; i<SIZE; i++)
{
for(int j=0; j<SIZE; j++)
{
//黑棋
if(vboard[i][j]=='H')
{
printf("● ROW:%d COL:%d\n",i+1,j+1);
}
//白棋
if(vboard[i][j]=='B')
{
printf("○ ROW:%d COL:%d\n",i+1,j+1);
}
}
}
}
//用一维数组记录225步
void recordStep(int x,int y)
{
int i;
i=step;
if(i<225 && record[i]==-1)
{
record[i]=15*x+y;
printf("%d %d %d\n",step,record[i],__LINE__);
step++;
}
}
//下子先后
void inputStep()
{
int whofirst=-1;
while(whofirst<0)
{
printf("1白先,2黑先,请选择:");
scanf("%d",whofirst);
}
switch(whofirst)
{
case 2:
{
//默认黑先
players[0]=HEI;
players[1]=BAI;
break;
}
default:
{
players[0]=BAI;
players[1]=HEI;
}
}
}
//黑白
int player(int wc)
{
int x,y;
printf("%d",count+1);
if(wc==HEI)
printf("%srow,column:",black);
else if(wc==BAI)
printf("%srow,column:",white);
else{
}
scanf("%d,%d",&x,&y);
getchar();
//printf("ascci=%d",c);
if((x>0&&x<=15)&&(y>0&&y<=15))
{
setValue(x,y,wc);
//recordStep(x,y);
return 0;
}
else
{
}
return 1;
}
//轮流下棋
void alternatePlay()
{
int flag=0;
int alter=0;
int heibai;
while(flag==0)
{
showConsole();
alter=count%2;
heibai=qizi[alter];
flag=player(heibai);
count++;
}
}
2.控制台输入
2.1输入黑棋
第一步,在15行15列输入黑棋。格式为:15,15 回车。
2.2输入白棋
比如在14行15列输入白棋。格式为:14,15 回车。