问题描述
C语言实现三子棋问题
- 构造棋盘,进行三子棋游戏
- 玩家下棋
- 电脑随机下棋
- 判断输赢
步骤概述
- 打印菜单 是否进入游戏
- 初始化棋盘
- 玩家下棋
- 电脑下棋
- 判断输赢
- 继续下棋?赢?输?平局?
具体代码
代码组成
- test.c 测试三子棋
- game.h 游戏的函数声明
- game.c 游戏函数的实现
代码内容
菜单打印
void menu()
{
printf("*************************\n");
printf("******* 1.play *******\n");
printf("******* 0.exit *******\n");
printf("*************************\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新选择!\n");
break;
}
} while (input);
return 0;
}
初始化棋盘
利用宏定义 #define ROW 3 #define COL 3 推高代码可读性,用ROW代替行数,COL代替列数 方便后期修改棋盘布局(若想扩大棋盘 比如改为10*10的棋盘 直接将宏定义中的数字更改掉即可)
#include <stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
void init_Board(char board, int row, int col);
#include"game.h"
void game()
{
char ret = 0;
char board[ROW][COL] = { 0 };
init_board(board,ROW,COL);
}
void init_board(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for ( i = 0; i < row; i++)
{
for ( j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
打印棋盘
void display_Board(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for ( i = 0; i < row; i++)
{
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, y = 0;
do
{
printf("请输入你要下棋的位置:>");
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] = '*';
display_Board(board, ROW, COL);
break;
}
else
{
printf("该坐标被占用");
}
}
printf("输入坐标不合法,请重新输入:>\n");
} while (1);
}
void game()
{
char ret = 0;
char board[ROW][COL] = { 0 };
init_board(board,ROW,COL);
display_board(board, ROW, COL);
player_move(board, ROW, COL);
display_board(board, ROW, COL);
}
电脑下棋
srand((unsigned int)time(NULL));
#include<time.h>
#include<stdlib.h>
void computer_move(char board[ROW][COL], int row, int col)
{
printf("电脑下棋:>\n");
do
{
int x = rand() % row + 1;
int y = rand() % row + 1;
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '#';
display_Board(board, ROW, COL);
break;
}
} while (1);
}
判断输赢平局或者棋盘未满继续下棋
int is_full(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 1;
}
char is_win(char board[ROW][COL], int row, int col)
{
int ret = is_full(board, ROW, COL);
for (int i = 0; i < row; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i]&&board[0][i]!=' ')
{
return board[1][i];
}
else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}
else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
else if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
{
return board[0][0];
}
else
{
if (ret == 1)
{
return 'Q';
}
else
{
return 'C';
}
}
}
}
void game()
{
char ret = 0;
char board[ROW][COL] = { 0 };
init_board(board,ROW,COL);
display_board(board, ROW, COL);
while (1)
{
player_move(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
display_board(board, ROW, COL);
computer_move(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
display_board(board, ROW, COL);
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else if (ret == 'Q')
{
printf("平局\n");
}
display_board(board, ROW, COL);
}
代码总结
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
void init_Board(char board, int row, int col);
void display_Board(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);
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void init_Board(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for ( i = 0; i < row; i++)
{
for ( j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void display_Board(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for ( i = 0; i < row; i++)
{
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, y = 0;
do
{
printf("请输入你要下棋的位置:>");
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] = '*';
display_Board(board, ROW, COL);
break;
}
else
{
printf("该坐标被占用");
}
}
printf("输入坐标不合法,请重新输入:>\n");
} while (1);
}
void computer_move(char board[ROW][COL], int row, int col)
{
printf("电脑下棋:>\n");
do
{
int x = rand() % row + 1;
int y = rand() % row + 1;
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '#';
display_Board(board, ROW, COL);
break;
}
} while (1);
}
int is_full(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 1;
}
char is_win(char board[ROW][COL], int row, int col)
{
int ret = is_full(board, ROW, COL);
for (int i = 0; i < row; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i]&&board[0][i]!=' ')
{
return board[1][i];
}
else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}
else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
else if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
{
return board[0][0];
}
else
{
if (ret == 1)
{
return 'Q';
}
else
{
return 'C';
}
}
}
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("**************************\n");
printf("******* 1.play *******\n");
printf("******* 0.exit *******\n");
printf("**************************\n");
}
void game()
{
char board[ROW][COL] = { 0 };
char ret= 0;
init_Board(board,ROW,COL);
display_Board(board,ROW,COL);
while(1)
{
player_move(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
display_Board(board, ROW, COL);
computer_move(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
display_Board(board, ROW, COL);
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else if (ret == 'Q')
{
printf("平局\n");
}
display_Board(board, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
printf("进入游戏\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入有误,请重新输入\n");
break;
}
} while (input);
}
|