文章目录
什么是三子棋?
三子棋就是在3*3的棋盘中,双方进行围堵和想法,有一方先达到连着3颗棋子就算胜利;
三子棋需要掌握到C语言的哪个地方?
据笔者完成三子棋的过程来看,三子棋只需要掌握到二维数组即可。
如何实现三子棋?
1.头文件(函数声明)
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 3
#define COL 3
void menu();
void Init_board(char board[ROW][COL],int row,int col);//初始化数组为空格
void display_board(char board[ROW][COL],int row, int col);//展示数组
void Player(char board[ROW][COL], int row, int col);//玩家下棋
void Computer(char board[ROW][COL],int row,int col);//电脑下棋
char Iswin(char board[ROW][COL],int row,int col);//判断输赢
2.game.c(函数实现)
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("*****************************\n");
printf("********1.play ************\n");
printf("********0.exit ************\n");
printf("*****************************\n");
}
void Init_board(char board[ROW][COL], int row, int col)//初始化数组为空格
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void display_board(char board[ROW][COL], int row, int col)//展示棋盘
{
int i, j;
for (i = 0; i < row; i++)
{
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 Player(char board[ROW][COL], int row, int col)//玩家下棋
{
int x, y;
while (1)
{
printf("玩家下棋:>");
scanf("%d%d", &x, &y);
//判断合法性
if ((x >= 1 && x <= row)&& (y >= 1 && y <= col))
{
//判断是否已有棋子
if (board[x-1][y-1] == ' ')
{
board[x-1][y-1] = '*';
break;
}
else
{
printf("这个位置已被占用,请重新输入\n");
}
}
else
{
printf("输入非法,请重新输入:>\n");
}
}
}
void Computer(char board[ROW][COL], int row, int col)//电脑下棋
{
printf("电脑下:>\n");
int x, y;
while (1)
{
x = rand() % row;
y = rand() % col;
//判断是否已有棋子
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
else
{
;
}
}
}
char Iswin(char board[ROW][COL], int row, int col)//判断输赢
{
//玩家赢
int i;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '*')
{
return '*';
}
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] == '*')
{
return '*';
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == '*')
{
return '*';
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] == '*')
{
return '*';
}
//电脑赢
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '#')
{
return '#';
}
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] == '#')
{
return '#';
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == '#')
{
return '#';
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] == '#')
{
return '#';
}
//平局
int flag = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ');
flag++;
}
}
if (flag == 0)
{
return 'Q';
}
else
return 'C';
}
3.text.c(三子棋的测试)
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void game()
{
char ret;
char board[ROW][COL] = {"0"};//创建数组
Init_board(board, ROW, COL);//初始化数组为空格
display_board(board, ROW, COL);//展示棋盘
while (1)
{
Player(board, ROW, COL);//玩家下棋
ret = Iswin(board, ROW, COL);//判断输赢
if (ret != 'C')
break;
display_board(board, ROW, COL);//打印棋盘
Computer(board, ROW, COL);//电脑下棋
ret = Iswin(board, ROW, COL);//判断输赢
if (ret != 'C')
break;
display_board(board, ROW, COL);//打印棋盘
}
if (ret == '*')
{
printf("玩家赢\n");
display_board(board, ROW, COL);//打印棋盘
}
else if (ret == '#')
{
printf("电脑赢\n");
display_board(board, ROW, COL);//打印棋盘
}
else
{
printf("平局\n");
display_board(board, ROW, COL);//打印棋盘s
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//设置时间戳
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("游戏结束\n");
break;
default:
printf("输入错误,请重新选择:>");
break;
}
} while (input);
return 0;
}
三子棋代码的总览思路
主函数的代码块是基本的套路,知道怎么做即可。
如下:
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//设置时间戳
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("游戏结束\n");
break;
default:
printf("输入错误,请重新选择:>");
break;
}
} while (input);
return 0;
}
三子棋总览如下,即game()函数的实现:
-
创建二维数组
-
初始化全为空格
-
打印棋盘
-
While(1)
{
玩家下棋
判断输赢
展示棋盘电脑下棋
判断输赢
展示棋盘
}
加油呀!