#pragma once
#define ROW 3
#define COL 3
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int isWin(char board[ROW][COL], int row, int col);
void playMove(char board[ROW][COL], int row, int col);
void init(char board[ROW][COL], int row, int col);
void print(char board[ROW][COL], int row, int col);
void computerMove(char board[ROW][COL], int row, int col);
接下来分别写初始化函数等(写代码时差点气到强迫症的某人哈)注意空格!!
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
// 棋盘初始化
void init(char board[ROW][COL], int row, int col)
{
// memset 用来清空一个清空一个结构类型的变量或数组
memset(board, ' ', row*col);
}
// 打印棋盘(精致凯的启迪)
void print(char board[ROW][COL], int row, int col)
{
int i = 0;
printf(" ________ ________ ________\n");
for (; i < row; ++i)
{
printf("| | | |\n");
printf("| %c | %c | %c |\n", board[i][0], board[i][1], board[i][2]);
printf("| | | |\n");
int j = 0;
for (; j < col; ++j)
{
printf("|________");
}
printf("|\n");
}
}
// 玩家落子,判断玩家输入坐标的正确性
// 玩家落子在棋盘用'o'表示
void playMove(char board[ROW][COL], int row, int col)
{
int m = 0;
int n = 0;
do
{
printf("请输入坐标:\n");
scanf("%d %d", &m, &n);
if (board[m - 1][n - 1] == ' ')
{
board[m - 1][n - 1] = 'o';
break;
}
else
{
printf("输入错误,请重新输入\n");
}
} while (board[m - 1][n - 1] != ' ');
}
// 电脑落子,保证其落子随机性
void computerMove(char board[ROW][COL], int row, int col)
{
int a = 0;
int b = 0;
do
{
//限制电脑落子坐标范围,产生随机数
a = rand() % row;
b = rand() % col;
if (board[a][b] == ' ')
{
board[a][b] = '*';
break;
}
} while (board[a][b] != ' ');
}
//判断游戏结果,玩家赢返回1,电脑赢返回-1,平局返回0
int isWin(char board[ROW][COL], int row, int col)
{
int i = 0;
for (; i < row; ++i)
{
if (board[i][0] == 'o'&& board[i][1] == 'o'&& board[i][2] == 'o')
return 1;
else if (board[0][i] == 'o' && board[1][i] == 'o'&& board[2][i] == 'o')
return 1;
}
if (board[0][0] == 'o'&& board[1][1] == 'o'&& board[2][2] == 'o' || (board[0][2] == 'o'&& board[1][1] == 'o'&& board[2][0] == 'o'))
return 1;
int j = 0;
for (;j < row; ++j)
{
if (board[j][0] == '*'&& board[j][1] == '*'&& board[j][2] == '*')
return -1;
else if (board[0][j] == '*' && board[1][j] == '*'&& board[2][j] == '*')
return -1;
}
if (board[0][0] == '*'&& board[1][1] == '*'&& board[2][2] == '*' || (board[0][2] == '*'&& board[1][1] == '*'&& board[2][0] == '*'))
return -1;
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j)
{
if (board[i][j] == ' ')
return 0;
}
}
}
游戏的主体部分
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
//设置菜单函数,游戏进入界面
printf("*****************************************\n");
printf("************欢迎来到三子棋游戏***********\n");
printf("******************请选择*****************\n");
printf("********* 1.进入游戏 0.退出 ************\n");
printf("*****************************************\n");
}
// 游戏开始进行,玩家和电脑没走一步都需要判断输赢
void game()
{
int flag = 2;
char board[ROW][COL];
init(board, ROW, COL);
print(board, ROW, COL);
while (1)
{
playMove(board, ROW, COL);
system("cls");
print(board, ROW, COL);
flag = isWin(board, ROW, COL);
if (flag == 1)
{
printf("\n恭喜你赢了\n\n");
break;
}
if (flag != 0)
{
printf("\n平局\n\n");
break;
}
computerMove(board, ROW, COL);
system("cls");
print(board, ROW, COL);
flag = isWin(board, ROW, COL);
if (flag == -1)
{
printf("\n很遗憾,电脑获胜\n\n");
break;
}
if (flag != 0)
{
printf("\n平局\n\n");
break;
}
}
}
// 主函数
int main()
{
int input = 0;
srand(time(NULL));
do
{
menu();
scanf("%d", &input);
system("cls");
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("输入错误,请重新选择\n");
break;
}
} while (input);
return 0;
}