首先创建一个game.h的头文件,用于定义游戏程序内的函数。
创建一个一个game.c和一个test.c的源文件,实现三子棋游戏。
游戏设计思路:
1.菜单打印函数(玩家选择是否玩游戏)
2.棋盘打印函数(构思三子棋的棋盘应该怎么输出)
3.输赢判断函数(判断玩家赢还是电脑赢)
4.玩家下棋函数(通过输入的x,y坐标来确定玩家落子的位置)
5.电脑下棋函数(电脑产生随机的棋子二维数组下标值,实现电脑随机落子)
6.主函数(通过调用子函数,实现游戏)
先给出game.h的内容:
#ifndef __GAME_H__
#define __GAME_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 PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
char IsWin(char board[ROW][COL], int row, int col);
# endif
test.c的内容:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <stdlib.h>
#include"game.h"
void menu()
{
printf("****************************\n");
printf("*******1.play 0.exit*******\n");
printf("****************************\n");
}
void game()
{
char board[ROW][COL] = { 0 };
char ret = 0;
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);
while (1)
{
PlayerMove(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != ' ')
break;
DisplayBoard(board, ROW, COL);
ComputerMove(board, ROW,COL);
ret = IsWin(board, ROW, COL);
if (ret != ' ')
break;
DisplayBoard(board, ROW, COL);
}
if (ret == 'X')
printf("玩家赢\n");
else if (ret == '*')
printf("电脑赢\n");
else if (ret == 'Q')
printf("平局\n");
DisplayBoard(board, ROW, COL);
}
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();
system("pause");
return 0;
}
game.c的内容:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include"game.h"
void InitBoard(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++)
{
board[i][j] = ' ';
}
}
//memset(&board[0][0], ' ', row * col * sizeof(board[0][0]));
}
void DisplayBoard(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++)
{
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 PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家走:\n");
while (1)
{
printf("请输入坐标(1-3):");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= 3 && y >= 1 && y <= 3)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'X';
break;
}
else
{
printf("坐标被占用,重新输入!\n");
}
}
else
{
printf("坐标有误,重新输入!\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑走:>\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '*';
break;
}
}
}
static 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[0][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 (IsFull(board, row, col) == 1)
{
return 'Q';
}
return ' ';
}