头文件部分
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 3
#define COL 3
void game(char map[ROW][COL],int row ,int col);
game.c-实现具体游戏逻辑部分
#include "game.h"
void peomove(char map[ROW][COL], int row, int col, char sign) {
int over = 1;//下棋结束标识 1-继续 0-结束
int hang = 0, lie = 0;
printf("玩家落子:行 列->\n");
do {
scanf("%d %d", &hang, &lie);
if (hang <= 0 || lie <= 0 || hang > row || lie > col) {
printf("输入数据不合法,请重新输入!\n");
continue;
}
if (map[hang-1][lie-1] != ' ') {
printf("该位置以下过,请重新选择落点\n");
}
else {
map[hang - 1][lie - 1] = sign;
over = 0;
}
} while (over);
}
void robotmove(char map[ROW][COL], int row, int col, char sign) {
int hang = 0, lie = 0;
int over = 1;//下棋结束标识 1-继续 0-结束
do {
hang = rand() % row;
lie = rand() % col;
if (map[hang][lie] == ' ') {
over = 0;
}
} while (over);
map[hang][lie] = sign;
printf("电脑落子:行:%d 列: %d \n",hang+1,lie+1);
}
char win(char map[ROW][COL], int row, int col) {
/*int m = row-1, n = col-1;*/
char empty = ' ';
char compare;
int i = 0, j = 0;
int yn = 1;//标识是否满足获胜条件,0不满足,1满足
//行检验
for (i = 0; i < row; i++) {
compare = map[i][0];
yn = 1;
for (j = 0; j < col; j++) {
if (map[i][j] != compare) {
yn = 0;
break;
}
}
if (yn == 1)
return compare;
}
//列检验
for (j = 0; j < col; j++) {
compare = map[0][j];
yn = 1;
for (i = 0; i < row; i++) {
if (map[i][j] != compare) {
yn = 0;
break;
}
}
if (yn == 1)
return compare;
}
//'/'型检验
/*compare = map[m][n];*/
compare = map[0][COL-1];
yn = 1;
for (i=0,j=col-1; i < row && j >= 0; i++, j--) {
if (map[i][j] != compare) {
yn = 0;
break;
}
}
if (yn == 1)
return compare;
//'\'型检验
compare = map[0][0];
yn = 1;
for (i = 0, j = 0; i < row&&j < col; i++, j++){
if (map[i][j] != compare) {
yn = 0;
break;
}
}
if (yn == 1)
return compare;
//格子若满则平局,反之游戏继续
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
if (map[i][j] == ' ')
return empty;
}
}
return 'C';
}
void display(char map[ROW][COL], int row, int col) {
int i = 0, j = 0;
for (i = 0; i < row; i++) {
//落棋行
for (j = 0; j < col; j++) {
printf(" %c ", map[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 initialmap(char map[ROW][COL], int row, int col, char sign) {
int i = 0, j = 0;
//初始化地图
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
map[i][j] = sign;
}
}
}
void game(char map[ROW][COL], int row, int col) {
char wintag;
char empty = ' ';
char peo = '*';
char robot = '@';
int i = 0, j = 0;
//初始化地图
initialmap(map, row, col, empty);
//打印空白地图开始游戏
//地图样式:
// | |
//---|---|---
printf("开始 游戏\n");
display(map,row,col);
while (1) {
//人下棋
peomove(map, row, col, peo);
//显示棋盘
display(map, row, col);
//判输赢
wintag=win(map, row, col);
//' '-继续;'玩家/电脑相应字符’-一方获胜;'C'-平局
if(wintag==' '){}
else if (wintag == peo ) {
printf("玩家获胜!\n");
break;
}
else {
printf("平局!\n");
break;
}
//电脑下棋
robotmove(map, row, col, robot);
//显示棋盘
display(map, row, col);
//判输赢
wintag=win(map, row, col);
if (wintag == ' ') {}
else if (wintag == robot) {
printf("电脑获胜!\n");
break;
}
else {
printf("平局!\n");
break;
}
}
}
测试main:
#include "game.h"
void menu() {
printf("$$$$$$$$$$$$$$$$$$$$$$\n");
printf("$$$ 欢迎来到三子棋 $$$\n");
printf("$$$ 1.游戏 2.退出 $$$\n");
printf("$$$$$$$$$$$$$$$$$$$$$$\n");
}
int main() {
srand((unsigned int)time(NULL));
int select = 0;
int tag = 1;//1表示游戏继续进行,0表示结束游戏,2表示输入不合法
char map[ROW][COL] = { 0 };
while (tag != 0) {
if (tag != 2) {
menu();
}
printf("输入选择(1或2)->");
scanf("%d", &select);
//判断如何进行
switch (select)
{
case 1:
{
tag = 1;
game(map,ROW,COL);
//进行游戏
break;
}
case 2:
{
tag = 0;
printf("游戏即将退出\n");
break;
}
default:
{
tag = 2;
printf("输入选择不合法,请重新输入\n");
break;
}
}
}
return 0;
}