前言
提示:这里可以添加本文要记录的大概内容:
这里我们为了方便管理代码,我们创建3个文件分别是game.h,test.c,game.c。
game.h中是函数声明,test.c是函数的实现,game.c是运行测试,这样我们就可以每写一个函数对它进行测试。
提示:以下是本篇文章正文内容,下面案例可供参考
一、主要函数
void initArry(char Board[ROW][COL], int row, int col);
//棋盘的初始化
void printArry(char Board[ROW][COL],int row,int col);
//棋盘的打印
void displayArry(char Board[ROW][COL], int row, int col);
//下棋
void computerArry(char Board[ROW][COL], int row, int col);
//电脑下棋
char IsWin(char Board[ROW][COL], int row, int col);
//判断输赢
二、游戏的实现
1.game.h
代码如下(示例):
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 3//多子棋定义(这里我们是三子棋)
#define COL 3
//void game();
void initArry(char Board[ROW][COL], int row, int col);
void printArry(char Board[ROW][COL],int row,int col);
void displayArry(char Board[ROW][COL], int row, int col);
void computerArry(char Board[ROW][COL], int row, int col);
char IsWin(char Board[ROW][COL], int row, int col);
2.test.c和game.c
代码如下(示例):
#include"game.h"
//棋盘初始化
void initArry(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] = ' ';
}
}
}
//版本一,缺点代码写死,实现不了多子棋,只能实现3子棋
//void printArry(char arry[ROW][COL],int row,int col)
//{
//
// for (int i = 0; i < row; i++)
// {
// printf(" %c | %c | %c \n", arry[i][0],arry[i][1],arry[i][2]);
//
// if (i < row - 1)
// {
// printf("___|___|___\n");
// }
//
// }
//
//
//}
//版本二
void printArry(char Board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
int j = 0;
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 displayArry(char Board[ROW][COL], int row, int col)
{
//玩家下棋
int x=0, y=0;
while (1)
{
printf("输入坐标,中间用空格");
printf("玩家下棋-->\n");
scanf_s("%d %d", &x, &y);
//坐标合法
if (x>=1&&x<=row&&y>=1&&y<=col)
{
if (Board[x-1][y-1] == ' ')
{
Board[x-1][y-1] = '*';
break;
}
else
{
printf("位置被占用,不可以落棋子,请重新输入坐标");
}
}
else
{
printf("坐标非法越界数值,请重新输入");
}
}
}
void computerArry(char Board[ROW][COL], int row, int col)
{
int x=0, y=0;
printf("电脑下棋-->\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (Board[x][y] == ' ')
{
Board[x][y] = '#';
break;
}
}
}
int IsFull(char Board[ROW][COL], int row, int col)
{
int j = 0;
int i = 0;
for (j = 0; j < row; j++)
{
for (i = 0; i < col; i++)
{
if (Board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
//char IsWin(char Board[ROW][COL], int row, int col)
//{
// int i = 0;
// for (i = 0; i < row; i++)
// {
// if (Board[i][0] == Board[i][1] && Board[i][1] == Board[i][2]&&Board[i][0]!=' ')
// return Board[i][0];
//
// }
// int j = 0;
// for (j = 0; j < row; 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[1][1]!=' ')
// return Board[1][1];
// if (Board[0][2] == Board[1][1] && Board[1][1] == Board[2][2]&&Board[1][1]!=' ')
// return Board[1][1];
//
// if (IsFull(Board, row, col)==1)
// {
//
// return 'Q';
//
//
// }
// return 'C';
//
//}
//版本二
char IsWin(char Board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)//行
{
//if (Board[i][i] == Board[i][i+1] && Board[i][i+1] == Board[i][i+2] && Board[i][i] != ' ')
//return Board[i][i];
int j = 0;
for ( j = 0; j < col-1; j++)
{
if (Board[i][j] != Board[i][j + 1])
{
break;
}
}
if (j== col - 1 && Board[i][j] != ' ')
{
return Board[i][j];
}
}
i = 0;
for (i = 0; i < row; i++)//列
{
//if (Board[j][j] == Board[j][j+1] && Board[j][j+1] == Board[j][j+2] && Board[j][j] != ' ')
//return Board[j][j];
int j = 0;
for ( j = 0; j< col - 1; j++)
{
if (Board[j][i] != Board[j+ 1][i])
{
break;
}
}
if (j == row - 1 && Board[j][i] != ' ')
{
return Board[j][i];
}
}
/*if (Board[0][0] == Board[1][1] && Board[1][1] == Board[2][2] && Board[1][1] != ' ')
return Board[1][1];*/
int count = 0;
for (int i = 0; i < row-1 ; i++)
{
if (Board[i][i] == Board[i + 1][i + 1] && Board[i][i] != ' ')
{
count++;
}
if (row-1==count)
{
return Board[i][i];
}
}
int k = 0;
for ( int i = 0; i <row-1; i++)
{
int j = 0;
for (j = col - 1; j >= 1; j--)
{
if (i + j == row - 1)
{
if (Board[i][j] == Board[i + 1][j - 1] && Board[i][j] != ' ')
{
k++;
}
if (k == row - 1)
{
return Board[i][j];
}
}
}
}
/*if (Board[0][2] == Board[1][1] && Board[1][1] == Board[2][2] && Board[1][1] != ' ')
return Board[1][1];*/
if (IsFull(Board, row, col) == 1)
{
return 'Q';
}
return 'C';
}
---
#include"game.h"
//void menu()
//void game()
// void printArry()
// void initArry()
void menu()
{
printf("*******************\n");
printf("1.play********0.Exit\n");
printf("*******************\n");
}
void game()
{
char Board[ROW][COL] = {0};
initArry(Board, ROW,COL );
printArry(Board, ROW, COL);
//displayArry(Board, ROW, COL);
//printArry(Board, ROW, COL);
//computerArry(Board, ROW, COL);
char ret = 0;
while (1)
{
//玩家下棋
displayArry(Board, ROW, COL);
//打印棋盘
printArry(Board, ROW, COL);
ret=IsWin(Board, ROW, COL);
if (ret != 'C')
{
break;
}
computerArry(Board, ROW, COL);
printArry(Board, ROW, COL);
ret = IsWin(Board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
printf("玩家赢\n");
else if (ret == '#')
printf("电脑赢\n");
else
{
printf("平局\n");
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
char ret = 0;
do {
menu();
printf("请选择游戏-->:");
scanf_s("%d", &input);
switch (input)
{
case 1:
//printf("1.玩游戏\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
`