首页 > 编程语言 > 用C语言实现三子棋小游戏
2021
09-04

用C语言实现三子棋小游戏

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

1. 规则介绍

三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙、井字棋等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。但是,有很多时候会出现和棋的情况。

2. 编写思路

2.1 选择界面的编写

提供选择界面(设计者可根据自己的喜好设定棋盘的大小;输入何种数字或者符号开始或者退出游戏也可由设计者自己确定数字)

static void Menu()
{
 printf("---------------------\n");
 printf("|  1: Play  2: Exit |\n");
 printf("---------------------\n");
 printf("Please Select# ");
}

2.2 棋盘的编写

首先我们应该实现三行三列的棋盘,为后续使用方便,所以我们在前面宏定义行(ROW 3)列(COL 3)都为3;设计者也可以根据自己的喜欢设置不同样式的棋盘;

static void Showboard(char board[][COL], int row, int col)
{
 printf("   |  1  |  2  |  3  |\n");
 printf("---------------------\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");
 }
}

2.3 玩家落子

设定让玩家以(x,y)坐标的方式决定落子方位,在玩家输入后,我们要设定判断程序,判断玩家输入的坐标是否合法(在我们设定的坐标范围内)、输入的位置是否已经落子;如果合法,且位置没被占用,则在输入的位置用符号代替玩家的棋子,这里用 ‘ X ' 表示玩家的棋子,为后续容易修改,在前面宏定义。

static void PlayerMove(char board[][COL], int row, int col)
{
 int x = 0;
 int y = 0;
 int quit = 0;
 while (!quit){
  printf("请输入你的位置# ");
  scanf("%d %d", &x, &y);
  if (x<1 || x>3 || y<1 || y>3){
   printf("你输入的坐标有误!\n");
   continue;
  }
  else if (board[x - 1][y - 1] != ' '){
   printf("该坐标已被占用!\n");
   continue;
  }
  else {
   board[x - 1][y - 1] = P_COLOR;
   break;
  }
 }
}

2.4 三子连珠判定

设定三子连珠所有可能的情况,然后判定是否可以继续进行游戏(返回NEXT),或者和棋(返回DRAW);

static char Judge(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][0] != ' '){
   return board[i][0];
  }
 }
 for (int 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];
  }
 }
 if (board[0][0] == board[1][1] && \
  board[1][1] == board[2][2] && \
  board[0][0] != ' '){
  return board[1][1];
 }
 if (board[0][2] == board[1][1] && \
  board[1][1] == board[2][0] && \
  board[1][1] != ' '){
  return board[1][1];
 }
 for (int i = 0; i < row; i++){
  for (int j = 0; j < col; j++){
   if (board[i][j] == ' '){
    return NEXT;
   }
  }
 }
 return DRAW;
}

2.5 电脑落子方式

电脑落子方式我们采用生成随机数

static void ComputerMove(char board[][COL], int row, int col)
{
 while (1){
  int x = rand() % row;
  int y = rand() % col;
  if (board[x][y] == ' '){
   board[x][y] = C_COLOR;
   break;
  }
 }
}

2.6 游戏逻辑

生成随机数,以便电脑落子使用;
生成二维数组作为棋盘,传入行和列,然后用memset初始化;
展示棋盘;
玩家落子;
判定落子是否合法;如果合法则轮到计算机落子;
判断计算机落子是否合法;如果合法则继续循环;
设定判断循环出口;

static void Game()
{
 srand((unsigned long)time(NULL));
 char board[ROW][COL];
 memset(board, ' ', sizeof(board));
 char result = 'x';
 do{
  Showboard(board, ROW, COL);
  PlayerMove(board, ROW, COL);
  result = Judge(board, ROW, COL);
  if (result != NEXT){
   break;
  }
  ComputerMove(board, ROW, COL);
  result = Judge(board, ROW, COL);
  if (result != NEXT){
   break;
  }
 } while (1);
    Showboard(board, ROW, COL);
 if (P_COLOR == result){
  printf("you win!\n");
 }
 else if (C_COLOR == result){
  printf("you loss!\n");
 }
 else{
  printf("draw!\n");
 }
}

2.7 设定游戏开始or退出

首先展示菜单;输入1开始游戏;输入2则退出;将上述代码整合后如下。

3. 整体代码

#include<stdivo.h>
#include<time.h>
#include<Windows.h>

#pragma warning(disable:4996)

#define ROW 3
#define COL 3

#define P_COLOR 'X'
#define C_COLOR 'O'
#define NEXT 'N'
#define DRAW 'D'

static void Menu()
{
 printf("---------------------\n");
 printf("|  1: Play  2: Exit |\n");
 printf("---------------------\n");
 printf("Please Select# ");
}
static void Showboard(char board[][COL], int row, int col)
{
 printf("   |  1  |  2  |  3  |\n");
 printf("---------------------\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");
 }
}
static void PlayerMove(char board[][COL], int row, int col)
{
 int x = 0;
 int y = 0;
 int quit = 0;
 while (!quit){
  printf("请输入你的位置# ");
  scanf("%d %d", &x, &y);
  if (x<1 || x>3 || y<1 || y>3){
   printf("你输入的坐标有误!\n");
   continue;
  }
  else if (board[x - 1][y - 1] != ' '){
   printf("该坐标已被占用!\n");
   continue;
  }
  else {
   board[x - 1][y - 1] = P_COLOR;
   break;
  }
 }
}
static char Judge(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][0] != ' '){
   return board[i][0];
  }
 }
 for (int 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];
  }
 }
 if (board[0][0] == board[1][1] && \
  board[1][1] == board[2][2] && \
  board[0][0] != ' '){
  return board[1][1];
 }
 if (board[0][2] == board[1][1] && \
  board[1][1] == board[2][0] && \
  board[1][1] != ' '){
  return board[1][1];
 }
 for (int i = 0; i < row; i++){
  for (int j = 0; j < col; j++){
   if (board[i][j] == ' '){
    return NEXT;
   }
  }
 }
 return DRAW;
}
static void ComputerMove(char board[][COL], int row, int col)
{
 while (1){
  int x = rand() % row;
  int y = rand() % col;
  if (board[x][y] == ' '){
   board[x][y] = C_COLOR;
   break;
  }
 }
}
static void Game()
{
 srand((unsigned long)time(NULL));
 char board[ROW][COL];
 memset(board, ' ', sizeof(board));
 char result = 'x';
 do{
  Showboard(board, ROW, COL);
  PlayerMove(board, ROW, COL);
  result = Judge(board, ROW, COL);
  if (result != NEXT){
   break;
  }
  ComputerMove(board, ROW, COL);
  result = Judge(board, ROW, COL);
  if (result != NEXT){
   break;
  }
 } while (1);
    Showboard(board, ROW, COL);
 if (P_COLOR == result){
  printf("you win!\n");
 }
 else if (C_COLOR == result){
  printf("you loss!\n");
 }
 else{
  printf("draw!\n");
 }
}
int main()
{
 int quit = 0;
 int select = 0;
 while (!quit){
  Menu();
  scanf("%d", &select);
  switch (select){
  case 1:
   Game();
   printf("Play Again?\n");
   break;
  case 2:
   quit = 1;
   break;
  default:
   printf("\n");
   break;
  }
 }
 printf("ByeBye!\n");
 system("pause");
 return 0;
}

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

编程技巧