大数据最新C语言练手小项目(巩固加深知识点理解)_好玩的c语言项目(3)

		board[i][j] = ' ';
	}
}

}

void displayBoard(char board[][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
//打印行
for (j = 0; j < col; j++)
{
//打印列
cout <<" “<<board[i][j]<<” ";
//被限制的打印条件
if (j < col - 1)
{
cout << “|”;
}
}
cout << “\n”;
//打印分割线
//如果我该行的个数是小于总行数减一的话
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
cout << “—”;
//被限制的打印条件
if (j < col - 1)
{
cout << “|”;
}
}
}
cout << “\n”;
}
}

void PlayerMove(char board[][COL], int row, int col)
{
int x = 0, y = 0;
cout << “玩家下棋\n”;
while (1)
{
cout << “请输入坐标\n”;
cin >> x >> y;
if (x >= 1 && x <= row && y >= 1 && y <= col)
{

		if (board[x - 1][y - 1] == ' ')
		{
			board[x - 1][y - 1] = '*';
			break;
		}
		else
		{
			cout << "坐标被占用,请重新下棋\n";
		}
	}
	else
	{
		cout << "输入有误,请重新输入\n";
	}
}

}

void ComeputerMove(char board[][COL], int row, int col)
{
cout << “电脑下棋\n”;
int x = 0, y = 0;
x = rand() % row;//范围0—2
y = rand() % col;//范围0—2
while (1)
{
if (board[x][y] == ’ ')
{
board[x][y] = ‘#’;
break;
}
else
{
x = rand() % row;//范围0—2
y = rand() % col;//范围0—2
}
}
}

char IsWin(char board[][COL], int row, int col)
{
//行
int i = 0, j = 0, count = 1, x = 0;
char a = 0;
for (i = 0; i < row; i++)
{
x = 0, count = 1;
for (j = 0; j < col; j++)
{
if (board[i][x] == board[i][x + 1] && x < col&&board[i][x]!=’ ')
{
a = board[i][x];
count++;
x++;
}
if (count == col)
{
return a;
}
}
}

//列
i = 0, j = 0, count = 1, x = 0;
for (i = 0; i < col; i++)
{
	count = 1, x = 0;
	for (j = 0; j < row; j++)
	{
		if (board[x][i] == board[x + 1][i] && x < row&&board[x][i] != ' ')
		{
			a = board[x][i];
			count++;
			x++;
		}
		if (count == row)
		{
			return a;
		}
	}
}

//对角线
i = 0, j = 0, count = 0, x = 0;
for (i = 0; i < row; i++)
{
	for (j = 0; j < col; j++)
	{
		x = board[0][0];
		if (i == j && board[i][j] == x && x != ' ')
		{
			a = board[i][j];
			count++;
		}
		
	}
}
if (count == row)
{
	return a;
}

i = 0, j = 0, count = 1;
char y = 0, z = 0;
for (i = 0; i < row; i++)
{
	for (j = 0; j < col; j++)
	{
		y = board[0][row-1];
		if (i == j && board[i][j] == y && y != ' '&& board[row - 1][0] == board[i][j] && board[row-1][0]==y)
		{
			z = board[i][j];
			count++;
		}
	}
}
if (board[row - 1][0] == board[0][row - 1])
{
	count++;
}
if (count == row)
{
	return z;
}
//到这里的话就是前面的情况都不符合就是说没有人赢
//此时判断平局或者继续
i = 0, j = 0, count = 0, x = 0;
for (i = 0; i < row; i++)
{
	for (j = 0; j < col; j++)
	{
		if (board[i][j] == ' ')
		{
			return 0;//未满
		}
	}
}
return 1;//已满

}

void game()
{
//将二维数组中的元素先初始化为0
char board[ROW][COL] = { 0 };
char ret = 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 != 0)
{
break;
}
//电脑下棋
ComeputerMove(board, ROW, COL);
displayBoard(board, ROW, COL);
//每次电脑下完棋之后也都要进行判断输赢
ret = IsWin(board, ROW, COL);
if (ret != 0)
{
break;
}
}
if (ret == ‘*’)
{
cout << “恭喜您,您赢了!\n”;
}
else if (ret == ‘#’)
{
cout << “很遗憾,您输给了电脑\n”;
}
else
{
cout << “平局!\n”;
}
}


## 头文件以及函数的声名部分



#pragma once
#include
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#define ROW 3
#define COL 3
using namespace std;
//菜单的声名
void menu();

//初始化棋盘
void InitBoard(char board[][COL], int row, int col);

//陈列棋盘
void displayBoard(char board[][COL], int row, int col);

//玩家下棋
void PlayerMove(char board[][COL], int row, int col);

//电脑下棋
//找没有下棋的随机下棋
void ComeputerMove(char board[][COL], int row, int col);

//判断输赢
char IsWin(char board[][COL], int row, int col);

//游戏界面
void game();


## 根据需要进行扩展


![img](https://img-blog.csdnimg.cn/img_convert/c755a4c04d7d25bbc076c8ea616adab5.png)
![img](https://img-blog.csdnimg.cn/img_convert/14187b281393c394ee690e7b65273f8c.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

]
[外链图片转存中...(img-4oUE5Whd-1714781031039)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值