目录
三子棋相信大家都不陌生,是一款非常好玩的益智游戏。
游戏规则是:谁先将三颗棋子连成一条线,谁就获胜
1.需要有一个棋盘下棋
2.玩家能下棋,电脑能自动下棋
3.能判断输赢
这次的代码使用game.h(函数声明)game.c(函数体实现)main.c(游戏逻辑实现)
三子棋其实,用的是一个3*3的格子,我们可以用二维数组来实现
其中的行(row)和列(col)使用宏定义,这样我们修改棋盘大小时会很方便,只需要改这两个值即可。
用printf实现
将数组,行和列传过去
使用for循环实现
游戏无非就是这4种情况
判断是否为平局的函数
主函数逻辑
游戏逻辑
- #pragma once
- #include
- #include
- #include
- #define ROW 3
- #define COL 3
- //初始化棋盘
- void Initboard(char board[ROW][COL],int row, int col);
- //打印棋盘
- void Display(char board[ROW][COL],int row, int col);
- //玩家下棋
- void Player_move(char board[ROW][COL],int row,int col);
- //电脑下棋
- void Computer_move(char board[ROW][COL], int row, int col);
- //判断输赢
- char is_win(char board[ROW][COL], int row, int col);
- #include"game.h"
-
- //初始化
- void Initboard(char board[ROW][COL], int row, int col)
- {
- int i = 0;
- for (i = 0; i < row; i++)
- {
- int j = 0;
- for (j = 0; j < col; j++)
- {
- board[i][j] = ' ';
- }
- }
- }
-
- //打印棋盘
- void Display(char board[ROW][COL], int row, int col)
- {
- int i = 0;
- for (i = 0; i < row; i++)
- {
- int j = 0;
- for (j = 0; j < col; j++)
- {
- printf(" %c ", board[i][j]);
- if (j < col - 1)
- {
- printf("|");
- }
-
- }
- printf("\n");
-
-
- if (i < row - 1)
- {
-
- for (j = 0; j < col; j++)
- {
- printf("---");
-
- if (j < col - 1)
- {
- printf("|");
-
- }
- }
- printf("\n");
- }
-
- }
- }
-
- //玩家下棋
- void Player_move(char board[ROW][COL], int row, int col)
- {
- int x = 0;
- int y = 0;
- while (1)
- {
- printf("请输入坐标:\n");
- scanf("%d %d", &x, &y);
- if (x >= 1 && x <= row && y >= 1 && y <= col)
- {
- if (board[x - 1][y - 1] == ' ')
- {
- board[x - 1][y - 1] = '*';
- break;
- }
- else
- {
- printf("已经输入,重新选位置\n");
- }
- }
- else
- {
- printf("输入非法,重新输入\n");
- }
- }
- }
-
-
- //电脑下棋
- void Computer_move(char board[ROW][COL], int row, int col)
- {
- printf("电脑下棋\n");
- while (1)
- {
- int x = rand() % row;
- int y = rand() % col;
- if (board[x][y] == ' ')
- {
- board[x][y] = '#';
- break;
- }
- }
- }
-
- //判断是否为平局
- static int If_full(char board[ROW][COL], int row, int col)
- {
- int i = 0;
- int j = 0;
- for (i = 0; i < row; i++)
- {
- for (j = 0; j < col; j++)
- {
- if (board[i][j] == ' ')
- {
- return 0;
- }
- }
- }
- return 1;
- }
-
- //判断输赢,平局还是继续的状态
- char is_win(char board[ROW][COL], int row, int col)
- {
- //判断行
- int i = 0;
- for (i = 0; i < row; i++)
- {
- if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
- {
- return board[i][1];
- }
-
- }
- //判断列
- int j = 0;
- for (j = 0; j < col; j++)
- {
- if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][i] != ' ')
- {
- return board[1][j];
- }
- }
- //对角线
- if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
- {
- return board[1][1];
- }
- if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
- {
- return board[1][1];
- }
-
- if (If_full(board, ROW, COL) == 1)
- {
- return 'Q';
- }
- return 'C';
- }
- #include "game.h"
-
- menu()
- {
- printf("***************************\n");
- printf("***********1.play**********\n");
- printf("***********0.exit**********\n");
- printf("***************************\n");
- }
- void game()
- {
- char ret = 0;
- char board[ROW][COL] = {0};
- //初始化棋盘
- Initboard( board, ROW, COL);
- //打印棋盘
- Display(board, ROW, COL);
- while (1)
- {
- //玩家下棋
- Player_move(board, ROW, COL);
- Display(board, ROW, COL);
- ret = is_win(board,ROW,COL);
- if (ret != 'C')
- {
- break;
- }
- //电脑下棋
- Computer_move(board, ROW, COL);
- Display(board, ROW, COL);
- ret = is_win(board,ROW,COL);
- if (ret != 'C')
- {
- break;
- }
- }
- /*
- ret返回
- 'C'继续
- '*'玩家赢
- '#'电脑赢
- 'Q'平局
- */
- if (ret == '*')
- {
- printf("玩家赢\n");
- }
- else if ( ret=='#')
- {
- printf("电脑赢\n");
- }
- else
- {
- printf("平局\n");
- }
-
-
- }
-
- int main()
- {
- srand((unsigned int)time(NULL));
- int input = 0;
- do
- {
- menu();
- printf("请输入模式\n");
- scanf("%d", &input);
- switch (input)
- {
- case 1:
- game();
- break;
- case 0:
- break;
- default:
- printf("输入错误,请重新输入\n");
- }
- } while (input);
- return 0;
- }
希望我的内容能给你带来帮助。
A huge tree that fills one’s arms grows from a tiny seedling; a nine-storied tower rises from a heap of earth; a thousand li journey starts with the first step.
合抱之木,生于毫末,九层之台,起于累土,千里之行,始于足下。——老子