井字棋
用EasyX和C++实现井字棋小游戏
源码及注释
#include<graphics.h>
char board_data[3][3] =
{
{'-','-','-'},
{'-','-','-'},
{'-','-','-'},
};
char current_piece = 'O';
//检测指定棋子的玩家是否获胜
bool CheckWin(char c)
{
// 检查每一行
for (int i = 0; i < 3; i++)
{
if (board_data[i][0] == c && board_data[i][1] == c && board_data[i][2] == c)
return true;
}
// 检查每一列
for (int j = 0; j < 3; j++)
{
if (board_data[0][j] == c && board_data[1][j] == c && board_data[2][j] == c)
return true;
}
// 检查对角线
if (board_data[0][0] == c && board_data[1][1] == c && board_data[2][2] == c)
return true;
if (board_data[0][2] == c && board_data[1][1] == c && board_data[2][0] == c)
return true;
// 如果没有匹配,则返回 false
return