目录
game.h如下:
game.c如下:
text.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;
}文章来源:https://www.toymoban.com/news/detail-808293.html
} while (input);
return 0;
}文章来源地址https://www.toymoban.com/news/detail-808293.html
到了这里,关于三子棋/井字棋(C语言)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!