如何用多文件编写三字棋
程序员文章站
2022-06-28 15:21:31
菜鸟如何写三子棋1 三子棋设计思路菜单设计(输入开始,退出游戏)。游戏模块设计。1)初始化棋盘2)显示棋盘3)开始玩(循环执行:用户走,判断;电脑走,判断)①下面给出game.h头文件部分#ifndef __GAME_H__#include #include #include #include #include#def...
菜鸟如何写三子棋
1 三子棋设计思路
- 菜单设计(输入开始,退出游戏)。
- 游戏模块设计。
1)初始化棋盘
2)显示棋盘
3)开始玩(循环执行:用户走,判断;电脑走,判断)
①下面给出game.h头文件部分
#ifndef __GAME_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include<Windows.h>
#define ROWS 3
#define COLS 3
void Meun();
void Game();
void InitBoard(char board[ROWS][COLS], int x, int y);
void DisplayBboard(char board[ROWS][COLS], int x, int y);
void PlayerMove(char board[ROWS][COLS], int x, int y);
void ComputerMove(char board[ROWS][COLS], int x, int y);
char Judge(char board[ROWS][COLS], int x, int y);
#pragma warning(disable:4996)
//#define _CRT_SECURE_NO_WARNINGS 1
#endif __GAME_H__
②下面给出game.c部分代码
#include "game.h"
void Meun()//打印菜单
{
printf("**************************\n");
printf("********* Meun *********\n");
printf(" 1.Start game. \n");
printf(" 0.Exit game. \n");
printf("**************************\n");
}
void DisplayBoard(char board[ROWS][COLS], int x, int y)//打印棋盘
{
int i = 0;
int j = 0;
for (i = 0; i<x; i++)
{
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
//Sleep(1000);
if (2 != i)printf("---|---|---\n");
}
}
void Game()//开始游戏
{
char board[ROWS][COLS];
char ret = 0;
InitBoard(board, ROWS, COLS);
DisplayBoard(board, ROWS, COLS);
do
{
PlayerMove(board, ROWS, COLS);
ret = Judge(board, ROWS, COLS);
if ('X' == ret){ printf("Player win!\n"); break; }
if ('O' == ret){ printf("Computer win!\n"); break; }
if (' ' == ret){ printf("Dogfall!\n"); break; }
ComputerMove(board, ROWS, COLS);
ret = Judge(board, ROWS, COLS);
if ('X' == ret){ printf("Player win!\n"); break; }
if ('O' == ret){ printf("Computer win!\n"); break; }
if (' ' == ret){ printf("Dogfall!\n"); break; }
} while (1);
}
void InitBoard(char board[ROWS][COLS], int x, int y)//初始化游戏
{
memset(board, ' ', ROWS*COLS*sizeof(char));
}
void PlayerMove(char board[ROWS][COLS], int x, int y)//玩家玩
{
int i = 0;
int j = 0;
while (1){
printf("Player input>: ");
scanf("%d %d", &i, &j); //输入位置
if (' ' == board[i - 1][j - 1] \
&& (i - 1) >= 0 && (i - 1)<x \
&& (j - 1) >= 0 && (j - 1)<y) {
board[i - 1][j - 1] = 'X';
DisplayBoard(board, ROWS, COLS);
break;
}
else printf("Error input!\n");
}
}
void ComputerMove(char board[ROWS][COLS], int x, int y)//电脑玩
{
int i = 0;
int j = 0;
printf("Computer input>:");
while (1)
{
i = rand() % 3;
j = rand() % 3;
if (' ' == board[i][j])//位置合法性检测
{
printf("%d %d\n", i + 1, j + 1);
board[i][j] = 'O';
DisplayBoard(board, ROWS, COLS);
break;
}
}
}
char Judge(char board[ROWS][COLS], int x, int y)//检测输赢
{
int i = 0;
int j = 0;
int count = 0;
for (i = 0; i<x; i++){
if (board[0][i] == board[1][i] && board[0][i] == board[2][i]){
if ('X' == board[0][i]) return 'X';
if ('O' == board[0][i]) return 'O';
}
if (board[i][0] == board[i][1] && board[i][0] == board[i][2]){
if ('X' == board[i][0]) return 'X';
if ('O' == board[i][0]) return 'O';
}
}
if (board[0][0] == board[1][1] && board[0][0] == board[2][2]){
if ('X' == board[0][0]) return 'X';
if ('O' == board[0][0]) return 'O';
}
if (board[0][2] == board[1][1] && board[0][2] == board[2][0]){
if ('X' == board[1][1]) return 'X';
if ('O' == board[1][1]) return 'O';
}
for (i = 0; i<x; i++){//判断棋盘是否有子
for (j = 0; j < y; j++){
if (' ' != board[i][j]){
count++;
}
}
}
if (9 == count)return ' ';
}
③下面给出main.c部分代码
#include "game.h"
int main()
{
int input = 0;
do
{
Meun();
printf("Please select >: ");
scanf("%d", &input);
switch (input)
{
case 1:Game(); break;
case 0:printf(" Exit \n"); break;
default:printf("Error! \n"); break;
}
} while (input);
return 0;
}
本文地址:https://blog.csdn.net/qq_37857219/article/details/109276387
上一篇: 电脑配置怎么查看?电脑系统属性查看方法
下一篇: 什么是聚合路由器?主要运用在哪些方面?
推荐阅读