三子棋,是黑白棋的一种。三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙、井字棋等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。但是,有很多时候会出现和棋的情况。
而我们在电脑上如何一个人也能玩三子棋呢,这就需要电脑随机生成一个坐标,以辅助我们能够顺利完成下棋。
1、在菜单界面选择开始或退出游戏。
2、创建并初始化棋盘。
3、打印棋盘。
4、开始玩游戏,玩家下棋,需输入行坐标和列坐标来落棋,’ * ‘表示玩家下棋。
5、根据函数返回值判断输赢:’ * ‘表示玩家赢,’ # '表示电脑赢,'p’表示平局,‘c’表示玩家和电脑都没赢,继续下棋。
6、电脑下棋,根据随机函数生成的随机值来落棋,’ # '表示电脑下棋。
7、判断输赢
8、根据函数返回值继续往下执行
void menu() {
printf("******************\n");
printf("*****1.play*******\n");
printf("*****0.exit*******\n");
printf("******************\n");
}
之所以使用宏定义棋盘的行和列是因为将来如果更改棋盘的尺寸的话,代码修改起来可以方便点。
#define row 3
#define col 3
char board[row][col];//棋盘数组
void initboard(char board[row][col], int ROW, int COL){
int i = 0, j = 0;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
board[i][j] =' ';
}
}
}
void printboard(char board[row][col], int ROW, int COL) {
int i = 0, j = 0;
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 playermove(char board[row][col], int ROW, int COL) {
printf("玩家下棋:>");
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 computermove(char board[row][col], int ROW, int COL) {
printf("电脑走:>\n");
int x=0, y=0;
while (1) {
x = rand() % ROW;
y = rand() % COL;
if (board[x][y] == ' ') {
board[x][y] = '#';
break;
}
}
}
int isfull(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 0;//棋盘没满
}
}
return 1;//棋盘满了
}
char iswin(char board[row][col], int ROW, int COL) {
//判断行
for (int i = 0; i < ROW; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
return board[i][0];
}
//判断列
for (int i = 0; i < COL; i++) {
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
return board[0][i];
}
//判断对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][1];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
//判断平局
if (isfull(board, row, col))
return 'p';
return 'c';
}
***game.h***
#pragma once
#include
#include
#include
#define row 3
#define col 3
void initboard(char board[row][col], int ROW, int COL);//初始化棋盘
void printboard(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 iswin(char board[row][col], int ROW, int COL);//判断输赢
***game.c***
#include "game.h"
//初始化棋盘
void initboard(char board[row][col], int ROW, int COL){
int i = 0, j = 0;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
board[i][j] =' ';
}
}
}
//打印棋盘
void printboard(char board[row][col], int ROW, int COL) {
int i = 0, j = 0;
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 playermove(char board[row][col], int ROW, int COL) {
printf("玩家下棋:>");
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 computermove(char board[row][col], int ROW, int COL) {
printf("电脑走:>\n");
int x=0, y=0;
while (1) {
x = rand() % ROW;
y = rand() % COL;
if (board[x][y] == ' ') {
board[x][y] = '#';
break;
}
}
}
//判断输赢
//玩家赢 '*'
//电脑赢 '#'
//平局 'p'
//都没赢,继续走 'c'
int isfull(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 0;//棋盘没满
}
}
return 1;//棋盘满了
}
char iswin(char board[row][col], int ROW, int COL) {
//判断行
for (int i = 0; i < ROW; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
return board[i][0];
}
//判断列
for (int i = 0; i < COL; i++) {
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
return board[0][i];
}
//判断对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][1];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
//判断平局
if (isfull(board, row, col))
return 'p';
return 'c';
}
***源.c***
#include "game.h"
void menu() {
printf("******************\n");
printf("*****1.play*******\n");
printf("*****0.exit*******\n");
printf("******************\n");
}
void game(){
char board[row][col];//棋盘数组
initboard(board, row, col);//初始化棋盘
printboard(board, row, col);//打印棋盘
char ret=0;
while (1) {
playermove(board, row, col);//玩家下棋
printboard(board, row, col);
ret =iswin(board, row, col);//判断输赢
if (ret != 'c') {
break;
}
computermove(board, row, col);//电脑下棋
printboard(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() {
int input;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input) {
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误\n");
break;
}
} while (input);
return 0;
}