文章目录
-
- 三子棋的描述
- 思路
- 关键代码
- 运行代码
三子棋的描述
三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉棋、一条龙、井字棋等。游戏分为双方对战,双方依次在9宫格棋盘上摆放棋子,率先将自己的三个棋子走成一条线就视为胜利,而对方就算输了,但是三子棋在很多时候会出现和棋的局面。
思路
用C语言实现三子棋并不是很好看,如果要实现图片上就需要前端代码才可以。
1、我们必须要使用do{}while()实现游戏是否开始。
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("游戏开始\n");
game();
break;case 0:
printf("退出游戏\n");
break;default:
printf("输入错误,请重新输入");
break;}
} while (input);
return 0;
2、实现棋盘初始化
int i = 0;
int j = 0;
int k = 2;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (k>j)
printf("|");
}
printf("\n");
if (i < row - 1)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("---");
if (k>j)
printf("|");
}
printf("\n");}
}
3、实现3*3的空格
int i = 0;
int j = 0;for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}}
4、你下棋的坐标,然后检验你下的是否连线。
int x = 0;
int y = 0;
printf("玩家走:>\n");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");
}
}ret = InWin(board, ROW, COL);
if (ret != 'C')if (ret == '*')
{
printf("玩家赢\n");
}
5、电脑下棋的坐标,然后检验你下的是否连线。
printf("电脑走:>\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
//判断坐标是否被占用
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}}
if (ret != 'C')
else if (ret == '#')
{
printf("电脑赢\n");
}
6、剩余的则是平局
7、实现检验连线的是'*'、'#'和'Q'(关键代码)
//判断三行
int i = 0;
for (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];
break;
}
}
//判断三列
int j = 0;
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ')
{
return board[0][j];
break;
}
}
//判断对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
{
return board[0][2];
}
//如果棋盘满了返回1,没满返回0
int ret = IsFull(board, row, col);
if (ret == 1)
{
return 'Q';
}
//继续
return 'C';
运行代码
一、游戏函数声明(game.h)
#pragma once#include<stdio.h>
#include<stdlib.h>
#include<time.h>#define ROW 3//竖
#define COL 3//横//函数声明
//初始化棋盘的函数
void IsitBoard(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 InWin(char board[ROW][COL], int row, int col);
二、游戏实现
#define _CRT_SECURE_NO_WARNINGS 1#include"game.h"void IsitBoard(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++){board[i][j] = ' ';}}
}void DisplayBoard(char board[ROW][COL], int row, int col)
{int i = 0;int j = 0;int k = 2;for (i = 0; i < row; i++){for (j = 0; j < col; j++){printf(" %c ", board[i][j]);if (k>j)printf("|");}printf("\n");if (i < row - 1){int j = 0;for (j = 0; j < col; j++){printf("---");if (k>j)printf("|");}printf("\n");}}}
void PlayerMove(char board[ROW][COL], int row, int col)
{int x = 0;int y = 0;printf("玩家走:>\n");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");while (1){int x = rand() % row;int y = rand() % col;//判断坐标是否被占用if (board[x][y] == ' '){board[x][y] = '#';break;}}}
int IsFull(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 InWin(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][0] != ' '){return board[i][0];break;}}//判断三列int j = 0;for (j = 0; j < col; j++){if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' '){return board[0][j];break;}}//判断对角线if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' '){return board[0][0];}if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' '){return board[0][2];}//如果棋盘满了返回1,没满返回0int ret = IsFull(board, row, col);if (ret == 1){return 'Q';}//继续return 'C';
}
三、主代码
#define _CRT_SECURE_NO_WARNINGS 1#include "game.h"void menu()
{printf("**************************\n");printf("******* 三子棋游戏 *******\n");printf("******* 1.play *******\n");printf("******* 0.exit *******\n");printf("**************************\n");}
void game()
{//存储数据char board[ROW][COL];//初始化棋盘IsitBoard(board, ROW, COL);//打印棋盘——本质是打印数组内容DisplayBoard(board, ROW, COL);//*代表玩家赢//#代表电脑赢//'Q'代表平局//'C'代表游戏继续char ret = 0;while (1){//玩家下棋PlayerMove(board, ROW, COL);DisplayBoard(board, ROW, COL);//判断玩家是否赢得游戏ret = InWin(board, ROW, COL);if (ret != 'C')break;//电脑下棋ComputerMove(board, ROW, COL);DisplayBoard(board, ROW, COL);//判断电脑是否赢得游戏if (ret != 'C')break;}if (ret == '*'){printf("玩家赢\n");}else if (ret == '#'){printf("电脑赢\n");}else{printf("平局\n");}
}int main()
{int input = 0;srand((unsigned int)time(NULL));do{menu();printf("请选择:>");scanf("%d", &input);switch (input){case 1:printf("游戏开始\n");game();break;case 0:printf("退出游戏\n");break;default:printf("输入错误,请重新输入");break;}} while (input);return 0;}