三子棋,程序员入门必须写的东西,包含了C语言初级阶段所有的知识点。比如:do...while()循环,while循环,for循环,if...else语句,switch语句,二维数组,函数等等,如果要写出三子棋,这些知识点必不可少。
三子棋源代码:
#include
#include
#include
#include"game.h"
#include
void game()
{
int panduan = 0;
char gezi[ROWS][COLS] = { 0 };
init_board(gezi, ROWS, COLS);
display_board(gezi, ROWS, COLS);
while (1)
{
printf("电脑走\n");
computer_move(gezi, ROWS, COLS);
display_board(gezi, ROWS, COLS);
panduan = check_win(gezi, ROWS, COLS);
if (panduan != 'q')
{
break;
}
printf("玩家走\n");
player_move(gezi, ROWS, COLS);
display_board(gezi, ROWS, COLS);
panduan = check_win(gezi, ROWS, COLS);
if (panduan != 'q')
{
break;
}
}
if (panduan == '*')
{
printf("电脑赢了!!!!!!!!!\n");
printf("是否重新开始?\n");
printf("1,继续\n");
printf("2,退出\n");
}
if (panduan == '0')
{
printf("恭喜你!你赢了!!!!!!\n");
printf("是否重新开始?\n");
printf("1,继续\n");
printf("2,退出\n");
}
if (panduan == 'm')
{
printf("抱歉你和电脑打平了\n");
printf("是否重新开始?\n");
printf("1,继续\n");
printf("2,退出\n");
}
}
void memu()
{
printf("*******************\n");
printf("***** 三子棋 *****\n");
printf("*******************\n");
printf("***** 1.play *****\n");
printf("***** 0.exit *****\n");
printf("*******************\n");
}
void test()
{
memu();
int input = 0;
do
{
srand((unsigned)time(NULL));
printf("请输出数字\n");
scanf_s("%d", &input);
switch (input)
{
case 0:
printf("退出游戏\n");
break;
case 1:
printf("开始游戏\n");
game();
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
}
int main()
{
test();
system("pause");
return 0;
}
#include
#include
#include"game.h"
#include
void init_board(char gezi[ROWS][COLS], int rows, int cols)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
gezi[i][j] = ' ';
}
}
}
void display_board(char gezi[ROWS][COLS], int rows, int cols)
{
{
int i = 0;
for (i = 0; i < rows; i++)
{
printf("| %c | %c | %c |\n", gezi[i][0], gezi[i][1], gezi[i][2]);
if (i <= rows - 1)
{
printf("|----|----|----|\n");
}
}
}
}
(表格截图)
void computer_move(char gezi[ROWS][COLS], int rows, int cols)
{
while (1)
{
int x = rand() % 3;
int y = rand() % 3;
if (gezi[x][y] != '*'&&gezi[x][y] != '0')
{
gezi[x][y] = '*';
return;
}
}
}
(随机生成一个坐标)
void player_move(char gezi[ROWS][COLS], int rows, int cols)
{
printf("请输出坐标:\n");
int x = 0;
int y = 0;
do
{
scanf_s("%d %d", &x, &y);
if (gezi[x - 1][y - 1] == '*')
{
printf("这个坐标被占了,重新输入\n");
}
else if ((x - 1) > 3 || (y - 1) > 3 || (x - 1) < 0 || (y - 1) < 0)
{
printf("输出错误,请重新输入\n");
}
else
{
gezi[x - 1][y - 1] ='0';
break;
}
} while (1);
}
static int is_full( char gezi[ROWS][COLS],int rows,int cols)\\全局静态变量是显式用static修饰的全局变量,作用域是声明此变量所在的文件,也就是只能用于check_win函数中。没有满的话返回0,满的话返回1。
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
if (gezi[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
int check_win(char gezi[ROWS][COLS], int rows, int cols)
{
int i = 0;
int k = 1;
for (i = 0; i < rows; i++)
{
if (gezi[i][0] == gezi[i][1] && gezi[i][1] == gezi[i][2] && gezi[i][1] != ' ')
{
return gezi[i][1];
}
}
for (i = 0; i < cols; i++)
{
if (gezi[0][i] == gezi[1][i] && gezi[1][i] == gezi[2][i] && gezi[1][i] != ' ')
{
return gezi[1][i];
}
}
if (gezi[0][0] == gezi[1][1] && gezi[1][1] == gezi[2][2] && gezi[1][1] != ' ')
{
return gezi[1][1];
}
if (gezi[0][2] == gezi[1][1] && gezi[1][1] == gezi[2][0] && gezi[1][1] != ' ')
{
return gezi[1][1];
}
if (is_full(gezi, rows, cols)==1)
{
return 'm';
}
return 'q';
}
#ifndef _Game_H_
#define _Game_H_
#define ROWS 3
#define COLS 3
void init_board(char gezi[ROWS][COLS], int rows, int cols);
void display_board(char gezi[ROWS][COLS], int rows, int cols);
void computer_move(char gezi[ROWS][COLS], int rows, int cols);
void player_move(char gezi[ROWS][COLS], int rows, int cols);
int check_win(char gezi[ROWS][COLS], int rows, int cols);
#endif
代码演示: