首页 > 编程语言 > C语言实现简易的三子棋游戏
2020
12-30

C语言实现简易的三子棋游戏

本文实例为大家分享了C语言实现三子棋游戏的具体代码,供大家参考,具体内容如下

1.游戏介绍

三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。

2.游戏效果

3.源代码

(1)头文件命名为为game.h

主要是整个程序中函数的声明

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3 //行
#define COL 3 //列
void BoardInit(char board[ROW][COL], int row, int col);
//棋盘展示
void BoardShow(char board[ROW][COL], int row, int col);
//人下一个位置用M表示,电脑下一个位置用C表示
void PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
//如果人赢返回M 电脑赢返回C 平局返回Q
char IsWin(char board[ROW][COL], int row, int col);
char IsFull(char board[ROW][COL], int row, int col);

(2)游戏文件命名为game.c

主要是三子棋游戏的实现

#include"game.h"
#include<stdlib.h>
//棋盘初始化
void BoardInit(char board[ROW][COL], int row, int col)
{
 for (int i = 0; i < row; i++)
 {
 for (int j = 0; j < col; j++)
 {
 board[i][j] = ' ';
 }
 }
}
//棋盘展示
void BoardShow(char board[ROW][COL], int row, int col)
{
 for (int i = 0; i < row; i++)
 {
 printf("-------------------\n");
 printf("| | | |\n");
 printf("| | | |\n");
 for (int j = 0; j < col; j++)
 {
 printf(" %c ",board[i][j]);
 
 }
 printf("\n");
 }
 printf("-------------------\n");
}
//玩家下子
void PlayerMove(char board[ROW][COL], int row, int col)
{
 int x, y;
 while (1)
 {
 printf("请输入你要下的坐标:>");
 scanf_s("%d %d", &x, &y);
 //对玩家落子坐标判断
 if (x > row || y > col || x < 0 || y < 0)
 {
 printf("非法输入请重新输入\n");
 continue;
 }
 else if (board[x][y] != ' ')
 {
 printf("此位置已落子,请重新输入!\n");
 continue;
 }
 //满足落子要求返回M
 else
 {
 board[x][y] = 'M';
 return 'M';
 break;
 }
 }
 }
//电脑下子
void ComputerMove(char board[ROW][COL], int row, int col)
{
 srand(time(NULL));
 while (1)
 {
 int x = rand() % 3;
 int y = rand() % 3;
 //满足落子要求返回C
 if (board[x][y] != " ")
 {
 board[x][y] = 'C';
 return 'C';
 break;
 }
 }
}
//判断棋盘是否被下满
char 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 'Q'; //满了
 
}
//输赢判断
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])
 {
 if (board[i][0] == 'M')
 {
 return 'M';
 }
 else if (board[i][0] == 'C')
 {
 return 'C';
 }
 }
 }
 //竖行三个个连子赢
 for (int i = 0; i < col; i++)
 {
 if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
 {
 if (board[0][i] == 'M')
 {
 return 'M';
 }
 else if (board[0][i] == 'C')
 {
 return 'C';
 }
 }
 }
 // 斜向"\"这样三个连子赢
 if (board[0][0] == board[1][1] && board[1][1] == board[2][2])
 {
 if (board[1][1] == 'M')
 {
 return 'M';
 }
 else if (board[1][1] == 'C')
 {
 return 'C';
 }
 }
 // 斜向"/"这样三个连子赢
 if (board[0][0] == board[1][1] && board[1][1] == board[2][2])
 {
 if (board[1][1] == 'M')
 {
 return 'M';
 }
 else if (board[1][1] == 'C')
 {
 return 'C';
 }
 }
 //判断平局,即棋盘下满为平局
 if ((IsFull(board, ROW, COL)) == 1)
 {
 return 'Q';
 }
 return 1;
}

(3)测试文件命名为test.c

主要实现程序的测试

#include<stdio.h>
#include "game.h"
#include<stdlib.h>
// 目录
void meun()
{
 printf("********************************\n");
 printf("************1.三子棋************\n");
 printf("************2.五子棋************\n");
 printf("************0.退出**************\n");
 printf("********************************\n");
}
//三子棋
void ThressChess()
{
 char board[ROW][COL];
 BoardInit(board, ROW, COL);
 BoardShow(board, ROW, COL);
 char ret = IsWin(board, ROW, COL);
 while (1)
 {
 //对玩家下的做判断
 printf("玩家出手\n");
 PlayerMove(board, ROW, COL);
 BoardShow(board, ROW, COL);
 ret = IsWin(board, ROW, COL);
 if (ret == 'C')
 {
 printf("电脑赢了\n");
 break;
 }
 else if (ret == 'M')
 {
 printf("恭喜你赢了!\n");
 break;
 }
 else if (ret == 'Q')
 {
 printf("平局\n");
 break;
 }
 //对电脑下的做判断
 ComputerMove(board, ROW, COL);
 BoardShow(board, ROW, COL);
 ret = IsWin(board, ROW, COL);
 if (ret == 'M')
 {
 printf("恭喜你赢了!\n");
 break;
 }
 else if (ret == 'C')
 {
 printf("电脑赢了\n");
 break;
 }
 else if (ret == '1')
 {
 printf("平局\n");
 break;
 }
 else
 {
 printf("继续\n");
 }
 }
}
int main()
{
 int input;
 srand(time(NULL));
 do
 {
 meun();
 printf("请选择:");
 scanf_s("%d", &input);
 switch (input)
 {
 case 1:
 ThressChess();
 break;
 case 2:
 printf("正在研发,敬请期待!\n");
 break;
 case 0:
 printf("按任意键退出\n");
 break;
 default:
 printf("非法输入,请重新输入\n");
 break;
 }
 } while (input);
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧