作为刚学习c语言的萌新,代码可能存在bug
刚开始创建两个源文件,一个头文件,(头文件存放的是函数的声明,一个源文件存放游戏的逻辑运行函数【如三子棋的胜利规则,玩家怎么下棋等等】,另一个存放main函数和主要函数,例如我game函数调用代表游戏开始,然后再game函数里调用游戏的实现逻辑的函数)
接着分析这个代码如何实现
1.首先需要一个棋盘,然后考虑的是玩家走和电脑走,然后判断谁赢了或者平局或者继续走
2.三子棋中显示的数据我们可以用数组存起来,最后打印数组,数组还应改包括分割线
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3 //头文件,代码的行列尽量宏定义,这样修改起来方便
#pragma once //存放主要函数的实现
#include"game.h"
int is_full(char board[ROW][COL], int row, int col) //判断棋盘是否满格,满格还没分出胜负
//就平局
{
int temp = 0;
for (int i = 0; i < row; i++)
for (int j = 0;j < col; j++)
if (board[i][j] == ' ')
{
temp = 1;
break;
}
if (temp == 1)
return 0;
else
return 1;
}
void initboard(char board[ROW][COL], int row, int col) //初始化棋盘让数组内部为空格,
{
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
board[i][j] = ' ';
}
void printboard(char board[ROW][COL], int row, int col)//打印棋盘的函数,注意最后一行没有
{ //下划线,最后一列没有竖线
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf(" %c ", board[i][j]); //先打印数据再打印分割线
if (j != col - 1)
printf("|");
}
printf("\n"); //注意换行
if (i != row - 1)
{
for (int j = 0; j < col; j++)
{
printf("---");
if (j != col - 1)
printf("|");
}
}
printf("\n");
}
}
void player_move(char board[ROW][COL], int row, int col) //玩家下棋的逻辑是让玩家输入行列
{ //然后让对应的数组地方赋值为*,
printf("玩家下棋"); //再打印一次棋盘,(注意玩家的行
int h = 0, k = 0; //列是从1开始,数组是从0开始
while (1)
{
printf("\n请输入行:\n");
scanf("%d", &h);
printf("请输入列:\n"); //要判断玩家输入的坐标的合法性,那个位
scanf("%d", &k); //置不能有别的数(即要为空格)
//以及输入的坐标要比定义的棋盘小
if (h >= 1 && h <= row && k >= 1 && k <= col)
{
if (board[h - 1][k - 1] == ' ')
{
board[h - 1][k - 1] = '*'; //玩家下棋用*,电脑下棋用#
break;
}
else
printf("坐标被占用,请重新输入!\n");
}
else
{
printf("坐标非法,请重新输入!\n");
}
}
}
void computer_move(char board[ROW][COL], int row, int col) //电脑下棋,不会算法,只能弄一
//个随机值
{
printf("电脑下棋\n");
while (1)
{
int x = rand() % ROW; //主函数用srand设置时间帧,然后%row是为了让值在0到row-1的范围
int y = rand() % COL;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
char panduan(char board[ROW][COL], int row, int col) //判断胜利条件,三个连一起
{
for (int i = 0; i < row; i++)
{ //电脑更人一共两个,所以横竖判断4次
for (int j = 0; j < col - 2; j++) //现在判断横竖
{
for (int k = 0; k < 3; k++)
if (board[i][j] == '#' && board[i][j + 1] == '#' && board[i][j + 2] == '#')
return board[i][j];
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col - 2; j++)
{
for (int k = 0; k < 3; k++)
if (board[i][j] == '*' && board[i][j + 1] == '*' && board[i][j + 2] == '*')
return board[i][j];
}
}
for (int j = 0; j < col; j++)
{
for (int i = 0; i < row - 2; i++)
{
for (int k = 0; k < 3; k++)
if (board[i][j] == '*' && board[i + 1][j] == '*' && board[i + 2][j] == '*')
return board[i][j];
}
}
for (int j = 0; j < col; j++)
{
for (int i = 0; i < row - 2; i++)
{
for (int k = 0; k < 3; k++)
if (board[i][j] == '#' && board[i + 1][j] == '#' && board[i + 2][j] == '#')
return board[i][j];
}
}
//接下来判断对角线
for (int i = 0; i < row - 2; i++)
{
for (int j = 0; j < col - 2; j++)
{
for (int k = 0; k < 3; k++)
if (board[i][j] == '#' && board[i + 1][j + 1] == '#' && board[i + 2][j + 2] == '#')
return board[i][j];
}
}
for (int i = 0; i < row - 2; i++)
{
for (int j = 0; j < col - 2; j++)
{
for (int k = 0; k < 3; k++)
if (board[i][j] == '*' && board[i + 1][j + 1] == '*' && board[i + 2][j + 2] == '*')
return board[i][j];
}
}
//判断斜角线
for (int i = row - 1; i >= 2; i--)
for (int j = 0; j < col - 3; j++)
if (board[i][j] == '*' && board[i - 1][j + 1] == '*' && board[i - 2][j + 2] == '*')
return board[i][j];
for (int i = row - 1; i >= 2; i--)
for (int j = 0; j < col - 3; j++)
if (board[i][j] == '#' && board[i - 1][j + 1] == '#' && board[i - 2][j + 2] == '#')
return board[i][j];
if (is_full(board, row, col) != 1) //最后考虑棋盘满了没,没满就继续输入
return 'c'; //满了就平局(因为前面都是for循环遍历
else //一旦符合条件就return走不会执行下面
return 'q'; //的代码,因此一旦执行下面的代码,
} //证明还没分出胜负)