服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 用C语言实现三子棋

用C语言实现三子棋

2021-11-12 15:55SSnnX C/C++

这篇文章主要为大家详细介绍了用C语言实现三子棋,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

三子棋含义:

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

用C语言实现三子棋

基本思路:

1.创建用户交互菜单界面

2.初始化棋盘

3.显示棋盘面板(为了不重复显示棋盘,使用清屏操作)

4.用户落子

5.判断胜负

6.电脑随机正确落子

7.判断胜负

(在每次写程序之前,可以向如下图所示,写出思路或伪代码

用C语言实现三子棋

创建多文件项目:

写代码之前,首先建立三个文件,以方便后序代码更加完整清晰地呈现。

用C语言实现三子棋

1.创建用户交互菜单界面

?
1
2
3
4
5
6
void Meau(){
 printf("+------ meau ----------+\n");
 printf("|----  1.play  --------|\n");
 printf("|----  0.quit  --------|\n");
 printf("+----------------------+\n");
}

2.初始化棋盘

使用宏定义,将棋盘中的内容初始化为空。

?
1
2
3
4
5
6
7
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;
  }
 }
}

3.显示棋盘面板

为了不重复显示棋盘,使用清屏操作,使得每次现实的棋盘只有一张。

通过不断调试,使得最终界面,与预期所需界面一致。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
static void ShowBoard(char board[][COL],int row,int col){
 system("cls");//清屏操作
 printf("  ");
 for (int i = 0; i < col; i++){
  printf(" %3d", i + 1);
 }
 printf("\n----------------\n");
 for (int i = 0; i < row; i++){
  printf("%-2d", i + 1);
  for (int j = 0; j < col; j++){
   printf("| %c ", board[i][j]);
  }
  printf("\n----------------\n");
 }
}

4.用户落子

落子只能落在为空的位置上,所以在落子前需要判空,若为空,则落子,否则提示重新落子。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
static void PlayerMove(char board[][COL], int row, int col){
 int x = 0;
 int y = 0;
 while (1){
  printf("please enter your postion<x,y>: ");
  scanf("%d %d", &x, &y);
  if (x<1 || x>3 || y<1 || y>3){
   printf("Postion Error!");
   continue;
  }
  if (board[x - 1][y - 1] == INIT){
   board[x - 1][y - 1] = WHITE;
   break;
  }
  else{
   printf("Postion Is Not Empty!\n");
  }
 }
}

5.电脑随机正确落子

使用随机数,在正确位置落子。

?
1
2
3
4
5
6
7
8
9
10
static void ComputerMove(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] = BLACK;
   break;
  }
 }
}

6.判断胜负

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
static char IsEnd(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] != INIT){
   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] != INIT){
   return board[0][j];
  }
 }
 if (board[0][0] == board[1][1] && \
  board[1][1] == board[2][2] && \
  board[0][0] != INIT){
  return board[0][0];
 }
 if(board[0][2] == board[1][1] && \
  board[1][1] == board[2][0] && \
  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;
}

7.创建Game界面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void Game()
{
 char board[ROW][COL];
 InitBoard(board, ROW, COL);
 srand((unsigned long)time(NULL));
 char result = 0;
 while (1){
  ShowBoard(board, ROW, COL);
  PlayerMove(board, ROW, COL);
  result = IsEnd(board, ROW, COL);
  if (result != NEXT){
   break;
  }
 
 ShowBoard(board, ROW, COL);
 ComputerMove(board, ROW, COL);
 result = IsEnd(board, ROW, COL);
 
 if (result != NEXT){
  break;
     }
 }
 ShowBoard(board, ROW, COL);
 switch (result){
 case WHITE:
  printf("You win!\n");
  break;
 case BLACK:
  printf("you lose!\n");
  break;
 case DRAW:
  printf("it ends in a draw\n");
  break;
 defult:
  printf("bug\n");
  break;
 }
}

完整代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//main.c文件
#include"game.h"
 
void Meau(){
 printf("+------ meau ----------+\n");
 printf("|----  1.play  --------|\n");
 printf("|----  0.quit  --------|\n");
 printf("+----------------------+\n");
}
int main(){
 int select = 0;
 int quit = 0;
     while (!quit)
  {
   Meau();
   printf("please enter your choose: ");
   scanf("%d", &select);
   switch (select)
   {
   case 1:
    Game();
    break;
   case 0:
    quit = 1;
    break;
   defult:
    printf("Select error!Try again!\n");
    break;
      }
  }
 printf("byebye!\n");
 system("pause");
 return 0;
}
 
//game.c 文件
#include"game.h"
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 ShowBoard(char board[][COL],int row,int col){
 system("cls");
 printf("  ");
 for (int i = 0; i < col; i++){
  printf(" %3d", i + 1);
 }
 printf("\n----------------\n");
 for (int i = 0; i < row; i++){
  printf("%-2d", i + 1);
  for (int j = 0; j < col; j++){
   printf("| %c ", board[i][j]);
  }
  printf("\n----------------\n");
 }
}
static char IsEnd(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] != INIT){
   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] != INIT){
   return board[0][j];
  }
 }
 if (board[0][0] == board[1][1] && \
  board[1][1] == board[2][2] && \
  board[0][0] != INIT){
  return board[0][0];
 }
 if(board[0][2] == board[1][1] && \
  board[1][1] == board[2][0] && \
  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 PlayerMove(char board[][COL], int row, int col){
 int x = 0;
 int y = 0;
 while (1){
  printf("please enter your postion<x,y>: ");
  scanf("%d %d", &x, &y);
  if (x<1 || x>3 || y<1 || y>3){
   printf("Postion Error!");
   continue;
  }
  if (board[x - 1][y - 1] == INIT){
   board[x - 1][y - 1] = WHITE;
   break;
  }
  else{
   printf("Postion Is Not Empty!\n");
  }
 }
}
static void ComputerMove(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] = BLACK;
   break;
   
  }
 }
}
 
void Game()
{
 char board[ROW][COL];
 InitBoard(board, ROW, COL);
 srand((unsigned long)time(NULL));
 char result = 0;
 while (1){
  ShowBoard(board, ROW, COL);
  PlayerMove(board, ROW, COL);
  result = IsEnd(board, ROW, COL);
  if (result != NEXT){
   break;
  }
 
 ShowBoard(board, ROW, COL);
 ComputerMove(board, ROW, COL);
 result = IsEnd(board, ROW, COL);
 
 if (result != NEXT){
  break;
     }
 }
 ShowBoard(board, ROW, COL);
 switch (result){
 case WHITE:
  printf("You win!\n");
  break;
 case BLACK:
  printf("you lose!\n");
  break;
 case DRAW:
  printf("it ends in a draw\n");
  break;
 defult:
  printf("bug\n");
  break;
 }
}
 
//game.h文件
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include <time.h>
#include <stdlib.h>
#include<windows.h>
#pragma warning(disable:4996)
#define ROW 3
#define COL 3
 
#define INIT ' '
#define WHITE 'X'//player
#define BLACK 'O'//computer
 
#define DRAW 'D'
#define NEXT 'N'
extern void Game();
#endif

代码结果显示

用C语言实现三子棋

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/SSnnX/article/details/117520170

延伸 · 阅读

精彩推荐
  • C/C++使用C++制作简单的web服务器(续)

    使用C++制作简单的web服务器(续)

    本文承接上文《使用C++制作简单的web服务器》,把web服务器做的功能稍微强大些,主要增加的功能是从文件中读取网页并返回给客户端,而不是把网页代码...

    C++教程网5492021-02-22
  • C/C++c/c++内存分配大小实例讲解

    c/c++内存分配大小实例讲解

    在本篇文章里小编给大家整理了一篇关于c/c++内存分配大小实例讲解内容,有需要的朋友们可以跟着学习参考下。...

    jihite5172022-02-22
  • C/C++C语言实现双人五子棋游戏

    C语言实现双人五子棋游戏

    这篇文章主要为大家详细介绍了C语言实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    两片空白7312021-11-12
  • C/C++关于C语言中E-R图的详解

    关于C语言中E-R图的详解

    今天小编就为大家分享一篇关于关于C语言中E-R图的详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    Struggler095962021-07-12
  • C/C++OpenCV实现拼接图像的简单方法

    OpenCV实现拼接图像的简单方法

    这篇文章主要为大家详细介绍了OpenCV实现拼接图像的简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    iteye_183805102021-07-29
  • C/C++深入C++拷贝构造函数的总结详解

    深入C++拷贝构造函数的总结详解

    本篇文章是对C++中拷贝构造函数进行了总结与介绍。需要的朋友参考下...

    C++教程网5182020-11-30
  • C/C++c/c++实现获取域名的IP地址

    c/c++实现获取域名的IP地址

    本文给大家汇总介绍了使用c/c++实现获取域名的IP地址的几种方法以及这些方法的核心函数gethostbyname的详细用法,非常的实用,有需要的小伙伴可以参考下...

    C++教程网10262021-03-16
  • C/C++C语言main函数的三种形式实例详解

    C语言main函数的三种形式实例详解

    这篇文章主要介绍了 C语言main函数的三种形式实例详解的相关资料,需要的朋友可以参考下...

    ieearth6912021-05-16