实现一个3*3的棋盘游戏,玩家与电脑之间的操作。
声明部分
//game.h
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __GAME_H__
#define __GAME_H__
#define ROWS 3
#define CLOS 3
enum OP
{
EXIT,
PLAY
};
void init_board(char board[ROWS][CLOS]);
//初始化棋盘
void print_board(char board[ROWS][CLOS]);
//打印棋盘
int is_full(char board[ROWS][CLOS]);
//判断棋盘是否已满
void player_board(char board[ROWS][CLOS]);
//玩家玩
void computer_board(char board[ROWS][CLOS]);
//电脑玩
char check_win(char board[ROWS][CLOS]);
//判断输赢
void play_game(char board[ROWS][CLOS]);
//玩游戏
endif//__GAME_H__
实现部分
//game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
#include <stdio.h>
#include <stdlib.h>
void init_board(char board[ROWS][CLOS])//初始化棋盘
{
int i = 0;
int j = 0;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < CLOS; j++)
{
board[i][j] = ' ';
}
}
}
void print_board(char board[ROWS][CLOS])//打印棋盘
{
int i = 0;
for (i = 0; i < ROWS; i++)
{
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
if (i != 2)
{
printf("---|---|---\n");
}
}
}
int is_full(char board[ROWS][CLOS])//判断棋盘是否满
{
int i = 0;
int j = 0;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < CLOS; j++)
{
if (board[i][j] == ' ')
{
return 1;//棋盘未满
}
}
}
return 0;//棋盘满了
}
void player_board(char board[ROWS][CLOS])//玩家玩
{
int x = 0;
int y = 0;
printf("玩家玩>:");
while (1)
{
scanf("%d%d", &x, &y);
if (((x - 1) >= 0) && ((x - 1)<ROWS) && ((y - 1) >= 0) && ((y - 1)<CLOS) && board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'X';
break;
}
printf("请重新输入>\n");
}
}
void computer_board(char board[ROWS][CLOS])//电脑玩
{
int x = 0;
int y = 0;
printf("电脑玩>:\n");
while (1)
{
x = rand() % 3;
y = rand() % 3;
if (board[x][y] == ' ')
{
board[x][y] = '0';
break;
}
}
}
char check_win(char board[ROWS][CLOS])//判断输赢
{
int i = 0;
for (i = 0; i < ROWS; i++)
{
if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]))
return board[i][0];
}
for (i = 0; i < CLOS; i++)
{
if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]))
return board[0][i];
}
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))
return board[0][0];
if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]))
return board[1][1];
if (is_full(board) == 0)
return 'q';
else
return ' ';
}
void play_game(char board[ROWS][CLOS])//玩游戏
{
char ret = 0;
init_board(board);
print_board(board);
while (1)
{
player_board(board);
print_board(board);
if (check_win(board) == 'X')
{
ret = 'X';
break;
}
if (check_win(board) == 'q')
{
ret = 'q';
break;
}
computer_board(board);
print_board(board);
if (check_win(board) == '0')
{
ret = '0';
break;
}
if (check_win(board) == 'q')
{
ret = 'q';
break;
}
}
if (ret == 'X')
{
printf("恭喜你赢了\n");
}
else if (ret == '0')
{
printf("电脑赢\n");
}
else
{
printf("平局\n");
}
}
测试部分
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <windows.h>
#include "game.h"
#include <stdlib.h>
#include <time.h>
void meun()//菜单
{
printf("**********************\n");
printf("*********1.play*******\n");
printf("*********0.exit*******\n");
printf("**********************\n");
}
void game()
{
char board[ROWS][CLOS];
int input = 1;
srand((unsigned)(time(NULL)));
do
{
meun();
printf("请输入你的选择>");
scanf("%d", &input);
switch (input)
{
case PLAY:
play_game(board);
break;
case EXIT:
break;
}
} while (input);
}
int main()
{
game();
system("pause");
return 0;
}