游戏设计:
game.h
#pragma once
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
void InitBoard(char Board[ROW][COL], int row, int col);
void PrintBoard(char Board[ROW][COL], int row, int col);
void PlayerMove(char Board[ROW][COL], int row, int col);
char IsWin(char Board[ROW][COL], int row, int col);
void ComputerMove(char Board[ROW][COL],int row,int col);
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char Board[ROW][COL], int row, int col)
{
memset(&Board[0][0], ' ', row * col * sizeof(Board[0][0]));
}
void PrintBoard(char Board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf(" %c ", Board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
for (int 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, y = 0;
printf("请玩家下棋:>");
while (1)
{
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] = '*';
break;
}
else
{
printf("该位置已被占用,请重新输入:>");
}
}
else
{
printf("输入非法,请重新输入:>");
}
}
}
void ComputerMove(char Board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
printf("电脑下棋:>\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (Board[x][y] == ' ')
{
Board[x][y] = '#';
break;
}
}
}
int IsFull(char Board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
if (Board[i][j] == ' ')
return 0;
return 1;
}
char IsWin(char Board[ROW][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] != ' ')
return Board[i][0];
}
for (int 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))
return 'Q';
return 'C';
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("**********************\n");
printf("******* 1.play *******\n");
printf("******* 0.exit *******\n");
printf("**********************\n");
}
void game()
{
char Board[ROW][COL] = { 0 };
char ret = 0;
InitBoard(Board, ROW, COL);
PrintBoard(Board, ROW, COL);
srand((unsigned int)time(NULL));
while (1)
{
//玩家下棋
PlayerMove(Board, ROW, COL);
//打印棋盘
PrintBoard(Board, ROW, COL);
//判断输赢
ret = IsWin(Board, ROW, COL);
if (ret != 'C')
break;
//电脑下棋
ComputerMove(Board, ROW, COL);
PrintBoard(Board, ROW, COL);
//判断输赢
if (ret != 'C')
break;
}
if ('*' == ret)
printf("玩家赢\n");
else if ('#' == ret)
printf("电脑赢\n");
else if ('Q' == ret)
printf("平局\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请输入:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("游戏开始:\n");
game();
break;
case 0:
printf("游戏退出\n");
break;
default:
printf("输入错误,请重新输入;\n");
break;
}
} while (input);
return 0;
}