目录
三子棋
test.c —— 测试游戏
game.h —— 游戏函数的声明
game.c —— 游戏函数的实现
一、test.c (测试游戏)
#include"game.h"
void menu()
{
printf("***********************\n");
printf("******* 1.play ********\n");
printf("******* 0.eixt ********\n");
printf("***********************\n");
}
void game()
{
char board[ROW][COL] = { 0 };
//初始化棋盘
InitBoard(board,ROW,COL);
//打印棋盘
DisPlayBoard(board, ROW, COL);
char ret = 0;
while(1)
{
//玩家下棋
PlayerMove(board, ROW, COL);
DisPlayBoard(board, ROW, COL);
//判断输赢
ret=Judge(board, ROW, COL);
if (ret != 'c')
{
break;
}
//电脑下棋
ComputerMove(board, ROW, COL);
DisPlayBoard(board, ROW, COL);
//判断输赢
ret = Judge(board, ROW, COL);
if (ret != 'c')
{
break;
}
}
if (ret == 'd')
{
printf("平局\n");
}
else if (ret == '*')
{
printf("玩家胜利\n");
}
else if (ret == '#')
{
printf("电脑胜利\n");
}
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("eixt\n");
break;
default:
printf("Re enter\n");
}
} while (input);
return 0;
}
二、game.h(游戏函数的声明)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define ROW 3
#define COL 3
void InitBoard(char board[ROW][COL], int row, int col);
void DisPlayBoard(char board[ROW][COL], int row, int col);
void PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
char Judge(char board[ROW][COL], int row, int col);
int Empty(char board[ROW][COL], int row, int col);
三、game.c(游戏函数的实现)
1.棋盘的初始化
void InitBoard(char board[ROW][COL], int row, int col)
{
memset(&board[0][0],' ', sizeof(char) * row * col);//以字节为单位设置内存
}
2.棋盘的打印
void DisPlayBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row;i++)
{
for (int j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < row - 1)
{
printf("|");
}
}
printf("\n");
for (int j = 0; j < col; j++)
{
printf("---");
if(j<col-1)
printf("|");
}
printf("\n");
}
}
3.玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col)
{
while(1)
{
int x1, y1 = 0;
printf("玩家下棋\n");
printf("请输入坐标:\n");
scanf("%d %d", &x1, &y1);
int x = x1 - 1;
int y = y1 - 1;
if (x >= 0 && x < row && y >= 0 && y < row)
{
if (board[x][y] == ' ')
{
board[x][y] = '*';
break;
}
else
{
printf("该坐标以落子,请重新选择\n");
}
}
else
{
printf("坐标超出棋盘限制,请重新选择\n");
}
}
}
4.电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{
printf("电脑下棋\n");
while(1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
5.判断棋盘中是否还有棋子
int Empty(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 1;
}
}
}
return 0;
}
6.判断输赢
char Judge(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (j - 1 >= 0 && j + 1 >= 0 && i + 1 >= 0 && i - 1 >= 0
&& i - 1 < row && j - 1<col && i + 1< row && j + 1 < col
&& board[i][j] == board[i - 1][j - 1] && board[i][j] == board[i + 1][j + 1] && board[i][j]!=' ')// \方向
{
return board[i][j];
}
if (j - 1 >= 0 && j + 1 >= 0 && i + 1 >= 0 && i - 1 >= 0
&& i - 1 < row && j - 1 < col && i + 1 < row && j + 1 < col
&& board[i][j] == board[i - 1][j + 1] && board[i][j] == board[i + 1][j - 1] && board[i][j] != ' ')// /方向
{
return board[i][j];
}
if (j - 1 >= 0 && j + 1 >= 0
&& j - 1 < col && j + 1 < col
&& board[i][j] == board[i][j + 1] && board[i][j] == board[i][j - 1] && board[i][j] != ' ')// —方向
{
return board[i][j];
}
if (i + 1 >= 0 && i - 1 >= 0
&& i - 1 < row && i + 1 < row
&& board[i][j] == board[i+1][j] && board[i][j] == board[i-1][j] && board[i][j] != ' ')// | 方向
{
return board[i][j];
}
}
}
if (Empty(board, row, col) == 1)//如果棋盘不满
{
return 'c';
}
return 'd';//平局
}
四、完整代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define ROW 3
#define COL 3
void InitBoard(char board[ROW][COL], int row, int col);
void DisPlayBoard(char board[ROW][COL], int row, int col);
void PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
char Judge(char board[ROW][COL], int row, int col);
int Empty(char board[ROW][COL], int row, int col);
void InitBoard(char board[ROW][COL], int row, int col)
{
memset(&board[0][0],' ', sizeof(char) * row * col);//以字节为单位设置内存
}
void DisPlayBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row;i++)
{
for (int j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < row - 1)
{
printf("|");
}
}
printf("\n");
for (int j = 0; j < col; j++)
{
printf("---");
if(j<col-1)
printf("|");
}
printf("\n");
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
while(1)
{
int x1, y1 = 0;
printf("玩家下棋\n");
printf("请输入坐标:\n");
scanf("%d %d", &x1, &y1);
int x = x1 - 1;
int y = y1 - 1;
if (x >= 0 && x < row && y >= 0 && y < row)
{
if (board[x][y] == ' ')
{
board[x][y] = '*';
break;
}
else
{
printf("该坐标以落子,请重新选择\n");
}
}
else
{
printf("坐标超出棋盘限制,请重新选择\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
printf("电脑下棋\n");
while(1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int Empty(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 1;
}
}
}
return 0;
}
char Judge(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (j - 1 >= 0 && j + 1 >= 0 && i + 1 >= 0 && i - 1 >= 0
&& i - 1 < row && j - 1<col && i + 1< row && j + 1 < col
&& board[i][j] == board[i - 1][j - 1] && board[i][j] == board[i + 1][j + 1] && board[i][j]!=' ')// \方向
{
return board[i][j];
}
if (j - 1 >= 0 && j + 1 >= 0 && i + 1 >= 0 && i - 1 >= 0
&& i - 1 < row && j - 1 < col && i + 1 < row && j + 1 < col
&& board[i][j] == board[i - 1][j + 1] && board[i][j] == board[i + 1][j - 1] && board[i][j] != ' ')// /方向
{
return board[i][j];
}
if (j - 1 >= 0 && j + 1 >= 0
&& j - 1 < col && j + 1 < col
&& board[i][j] == board[i][j + 1] && board[i][j] == board[i][j - 1] && board[i][j] != ' ')// —方向
{
return board[i][j];
}
if (i + 1 >= 0 && i - 1 >= 0
&& i - 1 < row && i + 1 < row
&& board[i][j] == board[i+1][j] && board[i][j] == board[i-1][j] && board[i][j] != ' ')// | 方向
{
return board[i][j];
}
}
}
if (Empty(board, row, col) == 1)//如果棋盘不满
{
return 'c';
}
return 'd';//平局
}
void menu()
{
printf("***********************\n");
printf("******* 1.play ********\n");
printf("******* 0.eixt ********\n");
printf("***********************\n");
}
void game()
{
char board[ROW][COL] = { 0 };
//初始化棋盘
InitBoard(board,ROW,COL);
//打印棋盘
DisPlayBoard(board, ROW, COL);
char ret = 0;
while(1)
{
//玩家下棋
PlayerMove(board, ROW, COL);
DisPlayBoard(board, ROW, COL);
//判断输赢
ret=Judge(board, ROW, COL);
if (ret != 'c')
{
break;
}
//电脑下棋
ComputerMove(board, ROW, COL);
DisPlayBoard(board, ROW, COL);
//判断输赢
ret = Judge(board, ROW, COL);
if (ret != 'c')
{
break;
}
}
if (ret == 'd')
{
printf("平局\n");
}
else if (ret == '*')
{
printf("玩家胜利\n");
}
else if (ret == '#')
{
printf("电脑胜利\n");
}
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("eixt\n");
break;
default:
printf("Re enter\n");
}
} while (input);
return 0;
}