C语言小游戏–三子棋
三子棋 (图片来自百度)
而C语言也可以实现简单三子棋游戏,并和电脑做对手进行人机对战。设计思路如下:
void Menu() //菜单函数
{
printf(" _________________________________\n");
printf(" | |\n");
printf(" | 1.开始游戏 |\n");
printf(" | 0. 退 出 |\n");
printf(" ---------------------------------\n");
}
int main()
{
int select = 0;
int quit = 0;
while (!quit){
printf("要来一把三子棋吗?\n");
Menu();
scanf("%d", &select);
switch (select){
case 1:
Game();
break;
case 0:
quit = 1;
break;
default:
printf("请输入正确的选择\n\n");
break;
}
}
printf("再见!\n");
#define INIT ' ' //棋盘初始化为空格
//棋盘初始化函数
static void InitBoard(char board[][COL],int row,int col)
{
for (int i = 0; i < ROW; i++){
for (int j = 0; j < COL; j++)
board[i][j] = INIT;
}
}
//显示棋盘
static void Show(char board[][COL], int row, int col)
{
system("cls");system("cls"); // system函数代表执行系统命令,system("cls")就是执行命令“清屏”的意思
for (int i = 0; i < COL; i++){
printf(" %d ", i+1);
}
printf("\n ____________\n");
for (int i = 0; i < ROW; i++){
printf("%d ", i+1);
for (int j = 0; j < COL; j++){
printf(" |%c ",board[i][j]);
}
printf("\n ------------\n");
}
}
#define ROW 3 //棋盘大小三行
#define COL 3 //棋盘大小三列
#define PLAYER 'O' //玩家棋子为O
#define COMPUTER 'X' //电脑棋子为X
#define DRAW 'D' //平局
#define NEXT 'N' //继续
void Game()
{
char board[ROW][COL];
InitBoard(board, ROW, COL); //初始化棋盘
srand((unsigned long)time (NULL)); //随机数种子
char result = 0;
while (1){
Show(board, ROW, COL);
// 玩家落子
Player(board, ROW, COL);
result = End(board, ROW, COL); //判断结果
if (result != NEXT){
break;
}
Show(board, ROW, COL);
// 电脑落子
Computer(board, ROW, COL);
result = End(board, ROW, COL);
if (result != NEXT){
break;
}
}
switch (result){ //result带来的三种最终结果
case PLAYER:
printf("你赢了!!!\n\n\n");
break;
case COMPUTER:
printf("Game Over,你输给了电脑。\n\n\n");
break;
case DRAW:
printf("你和电脑下了个平手。\n\n\n");
break;
default :
printf("BUG????\n\n"); //不应该运行到这
break;
}
}
static void Player(char board[][COL], int row, int col)
{
int x, y;
x = 0, y = 0;
while (1){
printf("请输入你想要下的棋子坐标(x,y)\n");
scanf("%d %d", &x, &y);
if (x<ROW - 2 || y<COL - 2 || x>ROW || y>COL){
printf("请输入正确的坐标\n");
continue;
}
if (board[x - 1][y - 1] == INIT){
board[x - 1][y - 1] = PLAYER;
break;
}
else{ printf("这个位置已经有棋子了\n");
}
}
}
void Computer(char board[][COL], int row, int col)
{
while (1){
int x = rand() % ROW;
int y = rand() % COL;
if (board[x][y] == INIT){
board[x][y] = COMPUTER;
break;
}
}
}
static char End(char board[][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][1] != INIT){
return board[i][1];
}
}
for (int j = 0; j < COL; j++){
if (board[0][j] == board[1][j] && \
board[1][j] == board[2][j] && \
board[1][j] != INIT){
return board[1][j];
}
}
if (board[0][2] == board[1][1] && \
board[1][1] == board[2][0] && \
board[1][1] != INIT){
return board[1][1];
}
if (board[0][0] == board[1][1] && \
board[1][1] == board[2][2] && \
board[1][1] != INIT){
return board[1][1];
}
for (int i = 0; i < ROW; i++){
for (int j = 0; j < COL; j++)
if (board[i][j]==INIT){
return NEXT;
}
}
return DRAW;
}
结果图:
#pragma once
#include
#include
#include //生成随机数
#include //时间生成随机数种子
#pragma warning (disable:4996)
#define ROW 3 //棋盘大小三行
#define COL 3 //棋盘大小三列
#define INIT ' ' //棋盘初始化为空格
#define DRAW 'D' //平局
#define NEXT 'N' //继续
#define PLAYER 'O' //玩家棋子为O
#define COMPUTER 'X' //电脑棋子为X
extern void Menu();
extern void Game();
extern void InitBoard(char board[][COL], int row, int col);
extern void Show(char board[][COL], int row, int col);
extern char End(char board[][COL], int row, int col);
extern void Player(char board[][COL], int row, int col);
extern void Computer(char board[][COL], int row, int col);
#include "three.h"
int main()
{
int select = 0;
int quit = 0;
while (!quit){
printf("要来一把三子棋吗?\n");
Menu();
scanf("%d", &select);
switch (select){
case 1:
Game();
break;
case 0:
quit = 1;
break;
default:
printf("请输入正确的选择\n\n");
break;
}
}
printf("再见!\n");
system("pause");
return 0;
}
#include "three.h"
void Menu()
{
printf(" _________________________________\n");
printf(" | |\n");
printf(" | 1.开始游戏 |\n");
printf(" | 0. 退 出 |\n");
printf(" ---------------------------------\n");
}
static void InitBoard(char board[][COL],int row,int col)
{
for (int i = 0; i < ROW; i++){
for (int j = 0; j < COL; j++)
board[i][j] = INIT;
}
}
static void Show(char board[][COL], int row, int col)
{
system("cls"); // system函数代表执行系统命令,system("cls")就是执行命令“清屏”的意思
for (int i = 0; i < COL; i++){
printf(" %d ", i+1);
}
printf("\n ____________\n");
for (int i = 0; i < ROW; i++){
printf("%d ", i+1);
for (int j = 0; j < COL; j++){
printf(" |%c ",board[i][j]);
}
printf("\n ------------\n");
}
}
void Game()
{
char board[ROW][COL];
InitBoard(board, ROW, COL); //初始化棋盘
srand((unsigned long)time (NULL)); //随机数种子
char result = 0;
while (1){
Show(board, ROW, COL);
Player(board, ROW, COL);
result = End(board, ROW, COL);
if (result != NEXT){
break;
}
Show(board, ROW, COL);
Computer(board, ROW, COL);
result = End(board, ROW, COL);
if (result != NEXT){
break;
}
}
Show(board, ROW, COL);
switch (result){
case PLAYER:
printf("你赢了!!!\n\n\n");
break;
case COMPUTER:
printf("Game Over,你输给了电脑。\n\n\n");
break;
case DRAW:
printf("你和电脑下了个平手。\n\n\n");
break;
default :
printf("BUG????\n\n"); //不应该运行到这
break;
}
}
static char End(char board[][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][1] != INIT){
return board[i][1];
}
}
for (int j = 0; j < COL; j++){
if (board[0][j] == board[1][j] && \
board[1][j] == board[2][j] && \
board[1][j] != INIT){
return board[1][j];
}
}
if (board[0][2] == board[1][1] && \
board[1][1] == board[2][0] && \
board[1][1] != INIT){
return board[1][1];
}
if (board[0][0] == board[1][1] && \
board[1][1] == board[2][2] && \
board[1][1] != INIT){
return board[1][1];
}
for (int i = 0; i < ROW; i++){
for (int j = 0; j < COL; j++)
if (board[i][j]==INIT){
return NEXT;
}
}
return DRAW;
}
static void Player(char board[][COL], int row, int col)
{
int x, y;
x = 0, y = 0;
while (1){
printf("请输入你想要下的棋子坐标(x,y)\n");
scanf("%d %d", &x, &y);
if (x<ROW - 2 || y<COL - 2 || x>ROW || y>COL){
printf("请输入正确的坐标\n");
continue;
}
if (board[x - 1][y - 1] == INIT){
board[x - 1][y - 1] = PLAYER;
break;
}
else{ printf("这个位置已经有棋子了\n");
}
}
}
void Computer(char board[][COL], int row, int col)
{
while (1){
int x = rand() % ROW;
int y = rand() % COL;
if (board[x][y] == INIT){
board[x][y] = COMPUTER;
break;
}
}
}
ps:随后我会补充更完整的解决思路。希望大家评论留言