· 文章目录
一 : 总体代码结构
二 : 分步实现代码功能(详解)
总体代码结构思想
1.进入游戏时菜单以及选择选项
2.选择选项之后游戏进行开始
3.玩家下棋,并且判定胜负
4.电脑下棋,并且判定胜负
5.游戏结果返回
分步实现代码功能
? 1.进入游戏时菜单以及选择选项
void menu()//菜单
{
printf("******************************\n");
printf("**********1.开始游戏**********\n");
printf("**********0.退出游戏**********\n");
printf("******************************\n");
}
void test()
{
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("选择错误,请重新选择!\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
2.选择选项之后游戏程序开始
1.先创建好整体棋盘框架再去把功能一步步实现 由棋盘生成-->棋盘初始-->打印棋盘-->玩家开始下棋-->判断胜负-->电脑下棋-->判断胜负
void game()
{
char board [ROW][COL] = { 0 };
//初始化棋盘
InitBoard(board, ROW, COL);
//打印棋盘
DisplayBoard(board, ROW, COL);
char ret = 0;
while (1)
{
//玩家下
player_move(board, ROW, COL);//玩家下棋
DisplayBoard(board, ROW, COL);//玩家下棋之后打印棋盘
ret = Iswin(board, ROW, COL);//Iswin为判断胜负,ret用于接收函数调用返回的值进行判断
if (ret != 'C')
{
break;
}
//电脑下
computer_move(board, ROW, COL);//电脑下棋
DisplayBoard(board, ROW, COL);//电脑下棋之后打印棋盘
//判定输赢
ret = Iswin(board, ROW, COL);//Iswin为判断胜负,ret用于接收函数调用返回的值进行判断
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else
printf("平局\n");
}
?2.功能函数的声明与头文件创建
#pragma once
#include <stdio.h>//在头文件里包含,在功能实现以及框架工程里包含此头文件即可
#include <time.h>
#include <stdlib.h>
#define ROW 3//行数常量
#define COL 3//列数常量
void InitBoard(char board[ROW][COL],int row, int col);//初始化棋盘
void DisplayBoard(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 Iswin(char board[ROW][COL], int row, int col);//判定输赢
3.功能函数具体实现
·初始化棋盘 ?
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 DisplayBoard(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)//玩家下棋
{
printf("玩家下棋 ");
int i = 0;
int j = 0;
while (1)
{
scanf("%d %d", &i, &j);
if (i >= 1 && i <= row && j >= 1 && j <= col)
{
if (board[i - 1][j - 1] == ' ')
{
board[i - 1][j - 1] = '*';
break;
}
else
printf("此坐标已被占用,请重新输入");
}
}
}
·电脑下棋
void computer_move(char board[ROW][COL], int row, int col) //电脑下棋
{
int a = 0;
int b = 0;
printf("电脑下:>\n");
while (1)
{
a = rand() % ROW;
b = rand() % COL;
if (board[a][b] == ' ')
{
board[a][b] = '#';
break;
}
}
}
·判断输赢与是否平局
int IsFull(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 Iswin(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][0] != ' ')
return board[i][0];
}
for (i = 0; i < col; i++)//判断列
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
return board[i][0];
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
return board[0][0];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
return board[0][2];
//判断平局
if (1 == IsFull(board, row, col))
{
return 'Q';
}
return 'C';
}
·总代码!
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()//菜单
{
printf("******************************\n");
printf("**********1.开始游戏**********\n");
printf("**********0.退出游戏**********\n");
printf("******************************\n");
}
void game()
{
char board [ROW][COL] = { 0 };
//初始化棋盘
InitBoard(board, ROW, COL);
//打印棋盘
DisplayBoard(board, ROW, COL);
char ret = 0;
while (1)
{
//玩家下
player_move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = Iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
//电脑下
computer_move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
//判定输赢
ret = Iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else
printf("平局\n");
}
void test()
{
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("选择错误,请重新选择!\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
#pragma once
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 3
#define COL 3
void InitBoard(char board[ROW][COL],int row, int col);//初始化棋盘
void DisplayBoard(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 Iswin(char board[ROW][COL], int row, int col);//判定输赢
#define _CRT_SECURE_NO_WARNINGS 1
#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 DisplayBoard(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)//玩家下棋
{
printf("玩家下棋 ");
int i = 0;
int j = 0;
while (1)
{
scanf("%d %d", &i, &j);
if (i >= 1 && i <= row && j >= 1 && j <= col)
{
if (board[i - 1][j - 1] == ' ')
{
board[i - 1][j - 1] = '*';
break;
}
else
printf("此坐标已被占用,请重新输入");
}
}
}
void computer_move(char board[ROW][COL], int row, int col) //电脑下棋
{
int a = 0;
int b = 0;
printf("电脑下:>\n");
while (1)
{
a = rand() % ROW;
b = rand() % COL;
if (board[a][b] == ' ')
{
board[a][b] = '#';
break;
}
}
}
int IsFull(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 Iswin(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][0] != ' ')
return board[i][0];
}
for (i = 0; i < col; i++)//判断列
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
return board[i][0];
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
return board[0][0];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
return board[0][2];
//判断平局
if (1 == IsFull(board, row, col))
{
return 'Q';
}
return 'C';
}
|