0
点赞
收藏
分享

微信扫一扫

C语言实现三子棋游戏

Test.c

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include"game.h"
//测试三子棋
void menu()
{
printf("***********************");
printf("*1.play 2.exit *");

printf("***********************");
}
//游戏整个实现
void game()
{
char ret = 0;
//创建数组,存放一组数组的信息
char board[Row][Col] = {0};
//初始化棋盘
InitBoard(board, Row, Col);
//打印棋盘;
DisplayBoard(board, Row, Col);
//下棋
while (1)
{
//玩家下棋
Playermove(board, Row, Col);
DisplayBoard(board, Row, Col);
//判断玩家是否赢了
ret=Iswin(board,Row,Col);
{
if (ret != 'C')
break;
}
//电脑下棋
computer(board, Row, Col);
DisplayBoard(board, Row, Col);
//判断电脑是否赢了
ret = Iswin(board,Row,Col);
{
if (ret != 'C')
break;
}
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if ('#')
{
printf("电脑赢\n");
}
else
printf("平局\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("\n请输入:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break ;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}

Game.h

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#define Row 3
#define Col 3
//函数的声明
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 computer(char board[Row][Col],int row,int col);
char Iswin(char board[Row][Col],int row,int col);
//游戏的四种状态
//玩家赢 *
//电脑赢 #
//平局 Q
//继续 C

Game.c

#include "game.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++)
// {
// //打印一行的数据
// printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
// //打印分割行
// if(i<row-1)
// printf("---|---|---\n");
// }
//
//}
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)
{
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 = 0;
int y = 0;
printf("玩家走:\n");
while (1)
{
printf("请输入要下的坐标:");
scanf("%d%d", &x, &y);
//判断下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("该坐标被占用\n");
}
}
else
{
printf("坐标非法,请重新输入:\n");
break;
}
}
}
void computer(char board[Row][Col], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑走:\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//返回1表示满了
//返回0表示没满
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;//满了
}
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][1] != ' ')

{
return board[i][1];
}
}
for (i = 0; i < col; i++)
{
//竖三列
if (board[0][i] board[1][i] && board[1][i] board[2][i] && board[1][i] != ' ')

return board[1][i];
}
//对角线
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[0][2] && board[1][1] != ' ')

return board[1][1];
//判断是否平局
if (1 == IsFull(board,Row,Col))
return 'Q';
else
return 'C';
}

C语言实现三子棋游戏_i++

举报

相关推荐

0 条评论