一.创建用户选择菜单
二.创建主函数
三.编写游戏函数
我们对常用的字符和变量进行定义,以便我们后期的维护和修改,构建游戏的大体思路,创建游戏函数
四.用多文件形式展示代码与运行成果
1.头文件 game.h
2.源文件 main.c
3.源文件 game.c
一.game.h
#ifndef __GAME_H__
#define __GAME_H__
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#define ROW 3
#define COL 3
#define INIT ' '
#define WHITE 'X' //Player
#define BLACK 'O' //Computer
#define DRAW 'D'
#define NEXT 'N'
#pragma warning(disable:4996)
extern void Game();
#endif
二.main.c
#include "game.h"
void Menu()
{
printf("+-------------------------------+\n");
printf("| 1. Play 0. Exit |\n");
printf("+-------------------------------+\n");
}
int main()
{
int select = 0;
int quit = 0;
while (!quit){
Menu();
printf("Please Select# ");
scanf("%d", &select);
switch (select){
case 1:
Game();
break;
case 0:
quit = 1;
break;
default:
printf("Enter Error, Try Again!\n");
break;
}
}
printf("ByeBye!\n");
system("pause");
return 0;
}
三.game.c
#include "game.h"
static void InitBoard(char board[][COL], int row, int col)
{
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
board[i][j] = INIT;
}
}
}
static void ShowBoard(char board[][COL], int row, int col)
{
system("cls");
printf(" ");
for (int i = 0; i < col; i++){
printf("%4d", i+1);
}
printf("\n--------------\n");
for (int i = 0; i < row; i++){
printf("%-2d", i+1); //2
for (int j = 0; j < col; j++){
printf("| %c ", board[i][j]); //12
}
printf("\n--------------\n");
}
}
//#define WHITE 'X' //Player
//#define BLACK 'O' //Computer
//#define DRAW 'D'
//#define NEXT 'N'
static char IsEnd(char board[][COL], int row, int col)
{
for (int i = 0; i < row; i++){
if (board[i][0] == board[i][1] && \
board[i][1] == board[i][2] && \
board[i][0] != INIT){
return board[i][0];
}
}
for (int j = 0; j < COL; j++){
if (board[0][j] == board[1][j] && \
board[1][j] == board[2][j] && \
board[0][j] != INIT){
return board[0][j];
}
}
if (board[0][0] == board[1][1] && \
board[1][1] == board[2][2] && \
board[1][1] != INIT){
return board[1][1];
}
if (board[0][2] == board[1][1] && \
board[1][1] == board[2][0] && \
board[1][1] != INIT){
return board[1][1];
}
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
if (board[i][j] == INIT){
return NEXT;
}
}
}
return DRAW;
}
static void PlayerMove(char board[][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1){
printf("Please Enter Postion<x,y># ");
scanf("%d %d", &x, &y);
if (x < 1 || y < 1 || x > 3 || y > 3){
printf("Enter Postion Error!\n");
continue;
}
if (board[x - 1][y - 1] == INIT){
board[x - 1][y - 1] = WHITE;
break;
}
else{
printf("Postion Is Not Empty!\n");
}
}
}
static void ComputerMove(char board[][COL], int row, int col)
{
while (1){
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == INIT){
board[x][y] = BLACK;
break;
}
}
}
void Game()
{
char board[ROW][COL];
InitBoard(board, ROW, COL);
srand((unsigned long)time(NULL));
char result = 0;
while (1){
ShowBoard(board, ROW, COL);
PlayerMove(board, ROW, COL);
result = IsEnd(board, ROW, COL);
if (result != NEXT){
break;
}
ShowBoard(board, ROW, COL);
ComputerMove(board, ROW, COL);
result = IsEnd(board, ROW, COL);
if (result != NEXT){
break;
}
}
ShowBoard(board, ROW, COL);
switch (result){
case WHITE:
printf("You Win!\n");
break;
case BLACK:
printf("You Lose!\n");
break;
case DRAW:
printf("You == Computer!\n");
break;
default:
printf("BUG!\n"); //Do Nothing!
break;
}
}
至此.一个简单的三子棋游戏就完成了.有兴趣的可以运行下试试..