## c语言实现三子棋！

前言 ：用c语言实现三子棋可分为六个步骤！

1：首先是小游戏开始的准备工作-----

首先我们要进入小游戏至少要选择一次，我们用一个do--while语句来实现！

然后建立一个简易菜单-----

``````void menu()
{
printf("***************************\n");
printf("******    1.play     ******\n");
printf("******    0.exit       ****\n");
printf("***************************\n");
}``````

之后再用一个选择语句供玩家选择-----

``````	do
{
printf("请输入选项->\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误，请重新输入->\n");
break;
}
} while (input);``````

此时已经成型，进入下一个步骤即可！

2：创建棋盘（一个二维数组),对其初始化----

``````//初始化棋盘
init(board, ROW, COL);``````

``````void init(char board[ROW][COL],int row,int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}``````

3：打印棋盘----

``````//打印棋盘
display(board, ROW, COL);``````

``````         |   |
---|---|---
|   |
---|---|---
|   |
``````

``````void display(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if(j<col-1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col-1)
printf("|");
}
printf("\n");
}
}
}``````

4：玩家开始下棋，且打印棋盘---

``````	//玩家下棋
playermove(board, ROW, COL);
display(board, ROW, COL);``````

``````void playermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家下棋->\n");
scanf("%d%d", &x, &y);
if (x <= row && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else printf("位子被占，请重新输入->\n");
}
else printf("输入非法，请重新输入->\n");
}
}``````

5：轮到电脑开始下棋，且打印棋盘---

``````	//电脑下棋
compmove(board, ROW, COL);
display(board, ROW, COL);``````

``````void compmove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋->\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
``````

rand()----会生成随机数。

rand函数的使用使用需要引----"srand"!

``srand((unsigned int)time(NULL));``

``````int main()
{
srand((unsigned int)time(NULL));``````

``````#include<stdlib.h>
#include<time.h>``````

6：玩家或电脑每次下完棋都要判断输赢---

``````//判断输赢
ret=iswin(board, ROW, COL);``````

``````while (1)
{
//玩家下棋
playermove(board, ROW, COL);
display(board, ROW, COL);
//判断输赢
ret=iswin(board, ROW, COL);
if (ret != 'c')
break;
//电脑下棋
compmove(board, ROW, COL);
display(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'c')
break;
}
if (ret == '*')
printf("玩家赢\n");
else if (ret == "#")
printf("电脑赢\n");
else printf("平局\n");``````

``````int flat(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}

char iswin(char board[ROW][COL], int row, int col)
{
int i = 0;
//行
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
return board[i][1];
}
//列
int j = 0;
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
return board[1][j];
}
//对角
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][1];
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
else if (flat(board, ROW, COL) == 1)
return 'q';
else return 'c';
}``````

-----test.c---文件名--起始文件-

``````#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
{
printf("***************************\n");
printf("******    1.play     ******\n");
printf("******    0.exit       ****\n");
printf("***************************\n");
}
void game()
{
char board[ROW][COL] = { 0 };
//初始化棋盘
init(board, ROW, COL);
//打印棋盘
display(board, ROW, COL);
int ret = 0;
while (1)
{
//玩家下棋
playermove(board, ROW, COL);
display(board, ROW, COL);
//判断输赢
ret=iswin(board, ROW, COL);
if (ret != 'c')
break;
//电脑下棋
compmove(board, ROW, COL);
display(board, ROW, COL);
ret = iswin(board, ROW, COL);
if (ret != 'c')
break;
}
if (ret == '*')
printf("玩家赢\n");
else if (ret == "#")
printf("电脑赢\n");
else printf("平局\n");
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
printf("请输入选项->\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误，请重新输入->\n");
break;
}
} while (input);
return 0;
}``````

----game.h----头文件名--负责函数的声明

``````#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
void init(char board[ROW][COL],int row,int col);
void display(char board[ROW][COL], int row, int col);
void playermove(char board[ROW][COL], int row, int col);
void compmove(char board[ROW][COL], int row, int col);
char iswin(char board[ROW][COL], int row, int col);
``````

-----game.c----文件名---负责函数的实现

``````#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init(char board[ROW][COL],int row,int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}

void display(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if(j<col-1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col-1)
printf("|");
}
printf("\n");
}
}
}

void playermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家下棋->\n");
scanf("%d%d", &x, &y);
if (x <= row && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else printf("位子被占，请重新输入->\n");
}
else printf("输入非法，请重新输入->\n");
}
}

void compmove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋->\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}

int flat(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}

char iswin(char board[ROW][COL], int row, int col)
{
int i = 0;
//行
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
return board[i][1];
}
//列
int j = 0;
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
return board[1][j];
}
//对角
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][1];
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
else if (flat(board, ROW, COL) == 1)
return 'q';
else return 'c';
}``````