三子棋游戏:
1,头文件
#define ROW 3
#define COL 3
#define ROWS ROW
#define COLS COL
void Init_board(char board[ROWS][COLS], int rows, int cols, char set);
void print(char board[ROWS][COLS], int row, int col);
void player(char board[ROWS][COLS], int row, int col);
void computer(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int ensure(char arr[ROWS][COLS], int row, int col);
#endif //__GAME_H__
2,源文件
#include"game.h"
void init_board(char arr[ROWS][COLS], int row, int col)
{
memset(arr, ' ', ROWS*COLS*sizeof(char));
}
//打印棋盘
void print(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
printf("%c|%c|%c\n", arr[i][0], arr[i][1], arr[i][2]);
if (i < row-1)
{
printf("------\n");
}
}
}
//玩者走
void player(char arr[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
printf("输入要放的棋子坐标");
scanf("%d %d", &x, &y);
while (1)
{
if ((x<4 && x>0) && (y<4 && y>0))
{
if (arr[x - 1][y - 1] == ' ')
{
arr[x - 1][y - 1] = 'O';//若此位置没被占用,则放入O
break;
}
else
{
printf("此位置已被占用,请重新输入:");
scanf("%d %d", &x, &y);
}
}
else
{
printf("坐标输入错误,请重新输入:");
scanf("%d %d", &x, &y);
}
}
ensure(arr, ROWS, COLS);
}
//电脑走
void computer(char arr[ROWS][COLS], int row, int col)
{
while (1)
{
int x = rand() % 4;
int y = rand() % 4;
if (arr[x - 1][y - 1] == ' ')
{
arr[x - 1][y - 1] = 'X';
break;
}
}
ensure(arr, ROWS, COLS);
}
//判断输赢
int ensure(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
if ((arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2]) && (arr[i][1] != ' '))
return 0;
}
for (j = 0; j < col; j++)
{
if ((arr[0][j] == arr[1][j]) && (arr[1][j] == arr[2][j]) && (arr[1][j] != ' '))
return 0;
}
if ((((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2])) || ((arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0]))) && arr[1][1] != ' ')
return 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (arr[i][j] == ' ')
return 1;
}
}
return 'p';
}
//进入游戏
#include"game.h"
//打印菜单
void menu()
{
printf("******** 1.play ********\n");
printf("******** 0.exist ********\n");
}
void playgame()
{
int i = 0;
char arr[ROWS][COLS] = { 0 };
init_board(arr, ROWS, COLS);
print(arr, ROWS, COLS);
srand((unsigned int)time(NULL));
printf("1,电脑先走。2,玩家先走。请选择:");
scanf("%d", &i);
switch(i)
{
case 1:computer(arr, ROWS, COLS);
print(arr, ROWS, COLS);
case 2:player(arr, ROWS, COLS);
print(arr, ROWS, COLS);
}
while (1)
{
computer(arr, ROWS, COLS);
print(arr, ROWS, COLS);
if (ensure(arr, ROWS, COLS) == 0)
{
printf("电脑赢\n");
break;
}
else if (ensure(arr, ROWS, COLS) == 'p')
{
printf("平局\n");
break;
}
player(arr, ROWS, COLS);
print(arr, ROWS, COLS);
if (ensure(arr, ROWS, COLS) == 0)
{
printf("玩家赢\n");
break;
}
else if (ensure(arr, ROWS, COLS) == 'p')
{
printf("平局\n");
}
}
}
//选择是否进入游戏
void test()
{
int input;
do
{
menu();
printf("请选择\n");
scanf("%d", &input);
switch (input)
{case 0:
printf("游戏结束\n");
break;
case 1:
playgame();
break;
default:
printf("选择错误\n");
break;
}
}
while (input);
}
//主函数
int main()
{
menu();
test();
system("pause");
return 0;
}