C语言完成三子棋
#define ROW 3#define COL 3
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
void filtey(char srred, int row, int col);
void usvtfy(char srred, int row, int col);
void playermove(char srred, int row, int col);
void computermove(char srred, int row, int col);
int inwin(char srred, int row, int col);
#include "三子棋.h"
void visr()
{
printf("****************\n");
printf("*****1.play*****\n");
printf("*****0.end *****\n");
printf("****************\n");
}
void game_play()
{
char ret=0;
char C=67;
char srred;
filtey(srred,ROW,COL); //将字符串数组srred初始化为‘ ’;
usvtfy(srred,ROW,COL); //打印下棋表格;
while (1)
{
playermove(srred, ROW, COL); //玩家下棋;
usvtfy(srred, ROW, COL);
ret = inwin(srred, ROW, COL);
if (ret!=C)
{
break;
}
computermove(srred, ROW, COL); //电脑下棋;
usvtfy(srred, ROW, COL);
ret = inwin(srred, ROW, COL);
if (ret!=C)
{
break;
}
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else
{
printf("平局\n");
}
}
void stdio()
{
int inpot = 0;
srand((unsigned int)time(NULL));
do
{
printf("请选择\n");
scanf_s("%d", &inpot);
switch (inpot)
{
case(1):
printf("三子棋开始\n");
game_play();
break;
case(0):
printf("game end\n");
break;
default:
printf("请重新输入");
break;
}
}
while (inpot);
}
int main()
{
visr();
stdio();
}
#include "三子棋.h"
void filtey(char srred,int row,int col) //初始化为‘ ’;
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
srred = ' ';
}
}
}
void usvtfy(char srred, int row, int col) //打印表格;
{
int i,j,y;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", srred);
if (j < col - 1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
for (y = 0; y < row; y++)
{
printf("___");
if (y < col - 1)
printf("|");
}
printf("\n");
}
if (i == row - 1)
{
for (y = 0; y < row; y++)
{
printf(" ");
if (y < col - 1)
printf("|");
}
printf("\n");
}
}
}
void playermove(char srred, int row, int col) //玩家下棋;
{
int x, y;
printf("玩家走\n");
while (1)
{
printf("请输入坐标>");
scanf_s("%d,%d",&x,&y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (srred == ' ')
{
srred = '*'; break;
}
else
{
printf("这个坐标已被占用\n");
}
}
else
{
printf("你输入的坐标不合法\n");
}
}
}
void computermove(char srred, int row, int col) //电脑下棋;
{
int x, y;
printf("电脑走>\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (srred == ' ')
{
srred = '#';
break;
}
}
}
int max(char srred, int row, int col) //检查是否继续下棋;
{
int x, y;
for (x = 0; x < row; x++)
{
for (y = 0; y < col; y++)
{
if (srred == ' ')
{
return 0;
}
}
}
return 1;
}
int inwin(char srred, int row, int col) //检查输赢;
{
int i;
for (i = 0; i < row; i++)
{
if (srred == srred && srred == srred && srred != ' ')
{
return(srred);
}
}
for (i = 0; i < col; i++)
{
if (srred == srred && srred == srred && srred != ' ')
return(srred);
}
if (srred == srred && srred == srred && srred != ' ')
{
return(srred);
}
if (srred == srred && srred == srred && srred != ' ')
{
return(srred);
}
if (1 == max(srred, ROW, COL))
{
return 'Q';
}
return 'C';
}
页:
[1]