1.前言
今天我们来做一个三子棋小游戏。
主要用到知识:数组。
2.思路分析
我们之前写代码都是直接创建一个test.c文件然后把全部代码写在里面,在某些情况下可能比较方便,但在真实业务下这样是非常不利于维护、调试和更新的。
所以这次我们要使用一种写项目常用的手段:模块化文件管理,通俗的说就是,用不同的文件实现不同的功能
本次主要用到:
game.h----函数声明、头文件包含、全局变量声明等
game.c----函数实现
test.c----功能测试
基本思路:
game.h和gamec中:
初始化棋盘
打印棋盘
玩家下棋
电脑下棋
判断胜负
判断棋盘是否已满
这些是三子棋主要的函数。
test.c主函数:
选择1/0(代表是否玩)
0退出,1初始化
循环下棋:打印+玩家下子+电脑下子+判断胜负
直到输赢有返回值,此时判断是谁赢或者平局(棋盘满)
请读者自行分析下面的完整代码(doge):
//game.h
#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void Initboard(char board[3][3], int row, int vol);
void prtboard(char board[3][3], int row, int vol);
void playermove(char board[3][3], int row, int vol);
void computermove(char board[3][3]);
int judge(char board[3][3], int row, int vol);
int boardfull(char board[3][3], int row, int vol);
//game.c
#include"game.h"
void Initboard(char board[3][3], int row, int vol)
{
int i = 0, j = 0;
for (; i < 3; i++)
{
for (; j < 3; j++)
{
board[i][j] = ' ';
}
j = 0;
}
}
/*
* * | * | *
* ---|---|---
* * | * | *
* ---|---|---
* * | | *
*
*
*/
void prtboard(char board[3][3], int row, int vol)
{
printf(" %c | %c | %c \n", board[0][0], board[0][1], board[0][2]);
printf("---|---|---\n");
printf(" %c | %c | %c \n", board[1][0], board[1][1], board[1][2]);
printf("---|---|---\n");
printf(" %c | %c | %c \n", board[2][0], board[2][1], board[2][2]);
}
//玩家使用*,电脑使用/
void playermove(char board[3][3], int row, int vol)
{
board[row - 1][vol - 1] = '*';
}
void computermove(char board[3][3])
{
while (1)
{
int x = rand() % 3;
int y = rand() % 3;
if (board[x][y] == ' ')
{
board[x][y] = '/';
break;
}
}
}
int judge(char board[3][3], int row, int vol)
{
int x = 0, y = 0;
for (; x < 3; x++)
{
if (board[x][0] == board[x][1] && board[x][0] == board[x][2])
{
if (board[x][0] == '*') return 1;//1代表玩家赢
if (board[x][0] == '/') return 2;//2代表电脑赢
}
}
for (; y < 3; y++)
{
if (board[0][y] == board[1][y] && board[0][y] == board[2][y])
{
if (board[0][y] == '*') return 1;//1代表玩家赢
if (board[0][y] == '/') return 2;//2代表电脑赢
}
}
if (board[0][0] == board[1][1] && board[0][0] == board[2][2])
{
if (board[0][y] == '*') return 1;//1代表玩家赢
if (board[0][y] == '/') return 2;//2代表电脑赢
}
if (board[0][2] == board[1][1] && board[0][2] == board[2][0])
{
if (board[0][y] == '*') return 1;//1代表玩家赢
if (board[0][y] == '/') return 2;//2代表电脑赢
}
return 0;//都没有则返回0
}
int boardfull(char board[3][3], int row, int vol)
{
int count = 0;
int x = count / 3, y = count % 3;
while (count < 9)
{
if (board[x][y] == ' ') return 0;
count++;
x = count / 3;
y = count % 3;
}
return 1;
}
//test.c
#define _CRT_SECURE_NO_WARNINGS 0
#include "game.h"
int main()
{
int start = 0;
char board[3][3] = { 0 };
int x = 0, y = 0;
int win = 0;
int full = 0;
printf("******************\n");
printf("*** 请选择.. ***\n");
printf("***1.开始/0.退出***\n");
printf("******************\n");
scanf("%d", &start);
if (!start) return 0;
Initboard(board, 3, 3);
prtboard(board, 3, 3);
while (1)
{
printf("请选择落子坐标..");
scanf("%d %d", &x, &y);
playermove(board, x, y);
prtboard(board, 3, 3);
win = judge(board, 3, 3);
full = boardfull(board, 3, 3);
if (win == 1)
{
printf("玩家胜");
break;
}
else if (win == 2)
{
printf("电脑胜");
break;
}
if (full == 1)
{
printf("棋盘已满");
break;
}
computermove(board);
prtboard(board, 3, 3);
win = judge(board, 3, 3);
full = boardfull(board, 3, 3);
if (win == 1)
{
printf("玩家胜");
break;
}
else if (win == 2)
{
printf("电脑胜");
break;
}
if (full == 1)
{
printf("棋盘已满");
break;
}
}
return 0;
}