思维导图
头文件代码实现:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define ROW 3
#define COL 3
void InitBroad(char board[ROW][COL], int row, int col);//声明初始化棋盘为空格;
void DispalyBoard(char board[ROW][COL], int row, int col);//声明打印棋盘
void play_move(char board, 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 InitBroad(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 DispalyBoard(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 play_move(char board[ROW][COL], int row, int col)//玩家移动
{
int x = 0;
int y = 0;
printf("玩家下棋\n");
while (1)
{
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] = '*';
break;
}
else
{
printf("该坐标已被占用,请重新输入\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
}
int if_full(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++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 0;
}
void computer_move(char board[ROW][COL], int row, int col)//电脑移动
{
int x = 0;
int y = 0;
printf("电脑下棋:>\n");
while (1)
{
x = rand() % row;//0-2
y = rand() % col;//0-2
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
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];
}
}
//判断列
for (i = 0;i < col;i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
{
return board[1][i];
}
}
//对角线
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';
}
主体测试代码实现:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "game.h"
#include<time.h>
#include<stdlib.h>
void menu()
{
printf("***********************\n");
printf("********1.play*********\n");
printf("********2.exit*********\n");
printf("***********************\n");
}
void game()
{
char ret = 0;
//存放下棋的数据
char board[ROW][COL] = { 0 };
//初始化棋盘为空格;
InitBroad(board, ROW, COL);
//打印棋盘
DispalyBoard(board, ROW, COL);
while(1)
{
//玩家下棋
play_move(board, ROW, COL);
DispalyBoard(board, ROW, COL);
ret = is_win(board,ROW,COL);
if (ret != 'C')
{
break;
}
//电脑下棋
computer_move(board, ROW, COL);
DispalyBoard(board, ROW, COL);
ret=is_win(board,ROW,COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("恭喜你赢啦!\n");
}
else if (ret == '#')
{
printf("你输了哦,再接再厉!\n");
}
else
{
printf("平局!\n");
}
DispalyBoard(board, ROW, COL);
}
//玩家赢返回'*'
//电脑赢返回“#”
//平局返回“Q"
//继续返回”C“
void test()
{
int input = 0;
do
{
menu();
printf("请输入1或2进行游戏:> \n");
scanf("%d", &input);
switch(input)
{
case 1:
game();
break;
case 2:
printf("退出游戏成功!");
break;
default:
printf("输入错误,请重新输入:>");
break;
}
} while (input);
}
int main()
{
srand((unsigned)time(NULL));
test();
return 0;
}
基本游戏实现玩法:
?
|