三子棋/井字棋(C语言)

这个游戏需要用到三个文件

game.h头文件用来申明函数和导包

game.h如下:

#pragma once
#define ROW 3
#define COL 3
#include <stdlib.h>
#include <time.h>
#include <stdio.h>

//初始化棋盘的函数
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);
//玩家赢返回"w"
//电脑赢返回"c"
//平局返回"q"
//继续返回"a"
char Iswin(char board[ROW][COL], int row, int col);
//判断棋盘是否已经满了
int IsFull(char board[ROW][COL], int row, int col);


game.c如下:

#define _CRT_SECURE_NO_WARNINGS

#include "game.h"

#include <stdio.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] = ' ';

}

}

}

void DisplayBoard(char board[ROW][COL], int row, int col)

{

int i = 0;

for (i = 0; i < row; i++)

{

//打印数据

int j = 0;

for (j = 0; j < col; j++)

{

printf(" %c ", board[i][j]);

if (j < col - 1)

printf("|");

}

printf("\n");

//打印分割行

if (i != row - 1)

{

int j = 0;

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;

int y;

while (1)

{

printf("玩家下棋>\n");

printf("请输入坐标,坐标格式为 x y \n");

scanf("%d%d", &x, &y);

if ((x > 0 && x <= row)&& (y > 0 && y <= col)) {

if (board[x - 1][y - 1] != ' ') {

printf("此坐标已经被占用,请重新输入\n");

continue;

}

board[x - 1][y - 1] = 'w';

break;

}

else

{

printf("输入的坐标不合法,请重新输入\n");

continue;

}

}

}

void ComputerMove(char board[ROW][COL], int row, int col) {

printf("电脑下棋>\n");

int x = 0;

int y = 0;

//抢占天元

if (board[1][1] == ' ') {

board[1][1] = 'c';

return;

}
//抢先赢

int i = 0;

for (i = 0; i < row; i++) {

if (board[i][0] == board[i][1] && board[i][0] == 'c' && board[i][2] == ' ') {

board[i][2] = 'c';

return;

}

else if (board[i][1] == board[i][2] && board[i][1] == 'c' && board[i][0] == ' ') {

board[i][0] = 'c';

return;

}

else if (board[i][0] == board[i][2] && board[i][0] == 'c' && board[i][1] == ' ') {

board[i][1] = 'c';

return;

}

}

for (i = 0; i < col; i++) {

if (board[0][i] == board[1][i] && board[0][i] == 'c' && board[2][i] == ' ') {

board[2][i] = 'c';

return;

}

else if (board[1][i] == board[2][i] && board[1][i] == 'c' && board[0][i] == ' ') {

board[0][i] = 'c';

return;

}

else if (board[0][i] == board[2][i] && board[0][i] == 'c' && board[1][i] == ' ')

{

board[1][i] = 'c';

return;

}

}

if (board[0][0] == board[1][1] && board[1][1] == 'c' && board[2][2] == ' ') {

board[2][2] = 'c';

return;

}

if (board[2][2] == board[1][1] && board[1][1] == 'c' && board[0][0] == ' ') {

board[0][0] = 'c';

return;

}

if (board[2][2] == board[0][0] && board[0][0] == 'c' && board[1][1] == ' ') {

board[1][1] = 'c';

return;

}

if (board[1][1] == board[0][2] && board[1][1] == 'c' && board[2][0] == ' ') {

board[2][0] = 'c';

return;

}

if (board[1][1] == board[2][0] && board[1][1] == 'c' && board[0][2] == ' ') {

board[0][2] = 'c';

return;

}

if (board[0][2] == board[2][0] && board[0][2] == 'c' && board[1][1] == ' ') {

board[1][1] = 'c';

return;

}

//堵玩家,就是玩家有2个相连时要堵

//当玩家 行 有两个相连时堵

for (i = 0; i < row; i++) {

if (board[i][0] == board[i][1] && board[i][0] == 'w' && board[i][2] == ' ') {

board[i][2] = 'c';

return;

}

else if(board[i][1] == board[i][2] && board[i][1] == 'w' && board[i][0] == ' '){

board[i][0] = 'c';

return;

}

else if (board[i][0] == board[i][2] && board[i][0] == 'w' && board[i][1] == ' ') {

board[i][1] = 'c';

return;

}

}

//当玩家 列 有两个相连时堵

for (i = 0; i < col; i++) {

if (board[0][i] == board[1][i] && board[0][i] == 'w' && board[2][i] == ' ') {

board[2][i] = 'c';

return;

}else if (board[1][i] == board[2][i] && board[1][i] == 'w' && board[0][i] == ' ') {

board[0][i] = 'c';

return;

}

else if(board[0][i] == board[2][i] && board[0][i] == 'w' && board[1][i] == ' ')

{

board[1][i] = 'c';

return;

}

}

//当玩家 斜着的 有两个相连时堵

if (board[0][0] == board[1][1] && board[1][1] == 'w' && board[2][2] == ' ') {

board[2][2] = 'c';

return;

}

if (board[2][2] == board[1][1] && board[1][1] == 'w' && board[0][0] == ' ') {

board[0][0] = 'c';

return;

}

if (board[2][2] == board[0][0] && board[0][0] == 'w' && board[1][1] == ' ') {

board[1][1] = 'c';

return;

}

if (board[1][1] == board[0][2] && board[1][1] == 'w' && board[2][0] == ' ') {

board[2][0] = 'c';

return;

}

if (board[1][1] == board[2][0] && board[1][1] == 'w' && board[0][2] == ' ') {

board[0][2] = 'c';

return;

}

if (board[0][2] == board[2][0] && board[0][2] == 'w' && board[1][1] == ' ') {

board[1][1] = 'c';

return;

}

//随机乱下

while (1)

{

x = rand() % row;//0-2

y = rand() % col;//0-2

if (board[x][y] == ' ') {

board[x][y] = 'c';

break;

}

}

}

char Iswin(char board[ROW][COL], int row, int col) {

//行

int i = 0;

for (i = 0; i < row; i++) {

int i1 = 0;

char temp = board[i][i1];//每一行第一个元素记录到临时变量里

for (i1 = 1; i1 < col; i1++) {

if (temp != board[i][i1]) {

break;//一旦这一行有不同的就跳出这一行的判断

}

temp = board[i][i1];

if (temp == board[i][col - 1]&&temp!=' ') {

return board[i][col - 1];//如果temp已经记录到这一行最后一个元素,那就说明这一行全部相等,返回结果

}

}

}

//列

int j = 0;

for (j = 0; j < col; j++) {

if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ') {

return board[0][j];

}

}

//对角线

if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') {

return board[0][0];

}

if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ') {

return board[0][2];

}

//平局

if (IsFull(board,row,col)) {

return 'q';

}

//继续

return 'a';

}
//判断棋盘是否已满

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;

}


text.c如下:

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include "game.h"

void menu() {

printf("*******************\n");

printf("*****1.play********\n");

printf("*****0.exit********\n");

printf("*******************\n");

}

void game() {

char flag;

char board[ROW][COL] = { 0 };

//初始化棋盘的功能

InitBoard(board, ROW, COL);
//打印棋盘

DisplayBoard(board, ROW, COL);

while (1)

{

//玩家下棋

PlayerMove(board, ROW, COL);
//打印棋盘

DisplayBoard(board, ROW, COL);
//判断玩家胜利

flag = Iswin(board, ROW, COL);

if (flag != 'a') {

break;

}
//电脑下棋

ComputerMove(board, ROW, COL);

//判断电脑胜利

flag = Iswin(board, ROW, COL);

if (flag != 'a') {

break;

}

//打印棋盘

DisplayBoard(board, ROW, COL);

}

if (flag == 'w') {

printf("玩家赢\n");

}

else if(flag == 'c')

{

printf("电脑赢\n");

}

else

{

printf("平局\n");

}

DisplayBoard(board, ROW, COL);

}

int main() {

srand((unsigned int)time(NULL));//设置随机数生成起点

int input = 0;

do

{

menu();

printf("请选择:>");

scanf("%d", &input);

switch (input)

{

case 1:

game();

break;

case 0:

printf("退出游戏\n");

break;

default:

printf("选择错误\n");

break;

}

} while (input);

return 0;

}

相关推荐
手握风云-13 分钟前
零基础Java第十六期:抽象类接口(二)
数据结构·算法
lb363636363640 分钟前
整数储存形式(c基础)
c语言·开发语言
浪里个浪的10241 小时前
【C语言】从3x5矩阵计算前三行平均值并扩展到4x5矩阵
c语言·开发语言·矩阵
笨小古1 小时前
路径规划——RRT-Connect算法
算法·路径规划·导航
<但凡.1 小时前
编程之路,从0开始:知识补充篇
c语言·数据结构·算法
f狐0狸x2 小时前
【数据结构副本篇】顺序表 链表OJ
c语言·数据结构·算法·链表
paopaokaka_luck2 小时前
基于Spring Boot+Vue的多媒体素材管理系统的设计与实现
java·数据库·vue.js·spring boot·后端·算法
CoderBob2 小时前
【EmbeddedGUI】脏矩阵设计说明
c语言·单片机
浪里个浪的10242 小时前
【C语言】计算3x3矩阵每行的最大值并存入第四列
c语言·开发语言·矩阵
视觉小萌新2 小时前
VScode+opencv——关于opencv多张图片拼接成一张图片的算法
vscode·opencv·算法