C语言实现三子棋(井字棋)算法

时间:2022-03-27 03:06:01

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

游戏文件主干(test.c):

?
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
#include"game.h"
 
void menu()//游戏菜单
{
 printf("************************************************\n");
 printf("**********          1.play             *********\n");
 printf("**********          0.exit             *********\n");
 printf("************************************************\n");
}
void game()//游戏玩法实现主体(粗略)
{
 printf("三子棋:>\n");
 char board[LINE][ROW];
 Init_checkerboard(board, LINE, ROW);//初始化棋盘
 Print_checkerboard(board, LINE, ROW);//打印棋盘
 char ret = 0;
 while (1)//条件为1可以一直循环
 {
  Player_game(board, LINE, ROW);//玩家行动
  Print_checkerboard(board, LINE, ROW);//玩家行动后的棋盘
  ret = Iswin(board, LINE, ROW);//判断是否出现胜负
  if (ret != 'c')//出现胜负就跳出循环
  {
   break;
  }
  Commputer_game(board, LINE, ROW);//电脑回合
  Print_checkerboard(board, LINE, ROW);//电脑行动后的棋盘
  ret = Iswin(board, LINE, ROW);//判断是否出现胜负
  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:
   game();//游戏函数主题(粗略)
   break;
  case 0:
   printf("您已退出游戏!\n");
   break;
  default:
   printf("输入数字无效,请重新输入");
   break;
  }
 } while (input);
 return 0;
}

游戏主干(game.c):

?
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
#include"game.h"//游戏主干(详细)
void Init_checkerboard(char board[LINE] [ROW], int line, int row)
{
 int i = 0;
 int j = 0;
 for ( i = 0; i <line; i++)
 {
  for ( j = 0; j <row; j++)
  {
   board[i][j]=' ';
  }
 }
}//棋盘的初始化
 
void Print_checkerboard(char board[LINE][ROW], int line, int row)
{
 int i = 0;
 for (i = 0; i < line; i++)
 {
  int j = 0;
  for (j = 0; j < line; j++)
  {
   printf(" %c ",board[i][j]);
   if (j < row - 1)
   {
    printf("|");
   }
  }
  printf("\n");
  if (i < row - 1)
  {
   for (j = 0; j < row; j++)
   
   {
    printf("---");
    if (j < row - 1)
    {
     printf("|");
    }
   }
  }
  printf("\n");
 }
}//生成3-3的棋盘
 
void Player_game(char board[LINE][ROW], int line, int row)
{
 printf("玩家下棋:>");
 int x = 0;
 int y = 0;
 while (1)
 {
  printf("请输入想要下棋的位置坐标:>");
  scanf("%d%d",&x,&y);
  if (x >= 1 && x <= line && y >= 1 && y <= row)
  {
   if (board[x - 1][y - 1] == ' ')
   {
    board[x - 1][y - 1] = '*';
    break;
   }
   else
   {
    printf("坐标被占用,请重新输入\n");
   }
  }
  else
  {
   printf("非法坐标,超出范围\n");
  }
 }
}//玩家行动的限制与实现
 
 
void Commputer_game(char board[LINE][ROW], int line, int row)
{
 int x = 0;
 int y = 0;
 while (1)
 {
  x = rand() % line;
  y = rand() % row;
  if (board[x][y]==' ')
  {
   board[x][y] = '#';
   break;
  }
 }
}//电脑行动的限制与实现
 
 
char con(char board[LINE][ROW], int line, int row)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < line; i++)
 {
  for (j = 0; j < row; j++)
  {
   if (board[i][j] == ' ')
   {
    return 'a';
   }
  }
 }
}//判断游戏是否继续的实现
 
char rank(char board[LINE][ROW], int line, int row)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < row; i++)
 {
  for (j = 0; j < line; j++)
  {
   if (board[j][i] == '*' && board[j][i] == board[j+1][i] && board[j+2][i] == board[j][i])
   {
    return 'q';
   }
   else if (board[j][i] == '#' && board[j][i] == board[j + 1][i] && board[j + 2][i] == board[j][i])
   {
    return 'w';
   }
  }
 }
}//判断一列的胜负条件
 
char Iswin(char board[LINE][ROW], int line, int row)//判断胜负的条件
{
 int i = 0;
 int j = 0;
 for (i = 0; i < line; i++)
 {
  for (j = 0; j < row; j++)
  {
 
   //判断行的胜负条件
   if (board[i][j] == board[i][j + 1] && board[i][j] == '*' && board[i][j] == board[i][j + 2])
   {
     return '*';
   }
   else if (board[i][j] == '#' && board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2])
   {
    return '#';
   }
   //判断列
   else if (rank(board,LINE,ROW)=='q')
   {
     return '*';
   }
   else if (rank(board, LINE, ROW) == 'w')
   {
     return '#';
   }
   //对角线判断
   else if ((board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2] && board[i][j] == '*') || (board[i][j + 2] == board[i + 1][j + 1] && board[i + 2][j] == board[i][j + 2] && board[i][j + 2] == '*'))
   {
     return '*';
   }
   else if ((board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2] && board[i][j] == '#') || (board[i][j + 2] == board[i + 1][j + 1] && board[i + 2][j] == board[i][j + 2] && board[i][j + 2] == '#'))
   {
     return '#';
   }
   else if (con(board, LINE, ROW) == 'a')
   {
    return 'c';
   }
   else
   {
    return 'p';
   }
  }
 }
 return 0;
}

游戏的头文件(game.h):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once//保证被重复调用后依然只执行一次
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
 
#define LINE 3
#define ROW  3
 
//初始化棋盘:
extern void Init_checkerboard(char board[LINE][ROW], int line, int row);
//打印棋盘:
extern void Print_checkerboard(char board[LINE][ROW], int line, int row);
//玩家走:
extern void Player_game(char board[LINE][ROW], int line, int row);
//电脑走:
extern void Commputer_game(char board[LINE][ROW], int line, int row);
//判断胜负:
extern char Iswin(char board[LINE][ROW], int line, int row);

代码运行图:

C语言实现三子棋(井字棋)算法

C语言实现三子棋(井字棋)算法

C语言实现三子棋(井字棋)算法

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

原文链接:https://blog.csdn.net/m0_60117108/article/details/119151615