假定x和y都是gamestate枚举类型。判断x和y是否是同一个常量的最优方法
时间: 2024-03-23 17:43:05 浏览: 35
判断x和y是否是同一个常量,可以使用以下代码:
```
if (x == y) {
// x和y是同一个常量
} else {
// x和y不是同一个常量
}
```
这是因为枚举类型在C++中被实现为整型常量,在比较枚举类型的值时,实际比较的是它们的整数值。因此,如果x和y的整数值相等,它们就是同一个常量。
相关问题
用VS 2017并且运用easy-x编写一个五子棋带有悔棋和认输功能
好的,以下是一个使用 easyX 编写的五子棋程序,包含了悔棋和认输功能:
```cpp
#include <graphics.h>
#include <conio.h>
#include <time.h>
// 定义棋盘格子大小、边框宽度、棋子半径
const int CELL_SIZE = 40;
const int BORDER_WIDTH = 2;
const int CHESS_RADIUS = CELL_SIZE / 2 - BORDER_WIDTH;
// 定义棋盘大小、起始坐标
const int BOARD_SIZE = 15;
const int BOARD_X = CELL_SIZE;
const int BOARD_Y = CELL_SIZE;
// 定义窗口大小
const int WINDOW_WIDTH = CELL_SIZE * (BOARD_SIZE + 2);
const int WINDOW_HEIGHT = CELL_SIZE * (BOARD_SIZE + 2);
// 定义棋子颜色,0表示无棋子,1表示黑子,2表示白子
const int CHESS_COLOR[3] = { 0, RGB(0, 0, 0), RGB(255, 255, 255) };
// 定义游戏状态,0表示未开始,1表示进行中,2表示黑方胜利,3表示白方胜利,4表示平局
const int GAME_STATE[5] = { 0, 1, 2, 3, 4 };
// 定义游戏数据结构
struct GameData
{
int board[BOARD_SIZE][BOARD_SIZE]; // 棋盘
int gameState; // 游戏状态
int turn; // 下棋方,0表示黑方,1表示白方
int winner; // 胜者,0表示黑方,1表示白方
void init()
{
memset(board, 0, sizeof(board));
gameState = GAME_STATE[0];
turn = 0;
winner = -1;
}
void drawBoard()
{
// 绘制棋盘边框
setlinecolor(RGB(0, 0, 0));
setlinestyle(PS_SOLID, BORDER_WIDTH);
rectangle(BOARD_X - BORDER_WIDTH, BOARD_Y - BORDER_WIDTH,
BOARD_X + CELL_SIZE * BOARD_SIZE + BORDER_WIDTH,
BOARD_Y + CELL_SIZE * BOARD_SIZE + BORDER_WIDTH);
// 绘制棋盘格子
for (int i = 0; i < BOARD_SIZE; i++)
{
for (int j = 0; j < BOARD_SIZE; j++)
{
setfillcolor(RGB(214, 175, 87));
solidrectangle(BOARD_X + i * CELL_SIZE, BOARD_Y + j * CELL_SIZE,
BOARD_X + (i + 1) * CELL_SIZE, BOARD_Y + (j + 1) * CELL_SIZE);
}
}
// 绘制星位
setfillcolor(RGB(0, 0, 0));
solidcircle(BOARD_X + 3 * CELL_SIZE, BOARD_Y + 3 * CELL_SIZE, 4);
solidcircle(BOARD_X + 11 * CELL_SIZE, BOARD_Y + 3 * CELL_SIZE, 4);
solidcircle(BOARD_X + 7 * CELL_SIZE, BOARD_Y + 7 * CELL_SIZE, 4);
solidcircle(BOARD_X + 3 * CELL_SIZE, BOARD_Y + 11 * CELL_SIZE, 4);
solidcircle(BOARD_X + 11 * CELL_SIZE, BOARD_Y + 11 * CELL_SIZE, 4);
}
void drawChess(int x, int y)
{
// 计算棋子中心坐标
int cx = BOARD_X + x * CELL_SIZE + CELL_SIZE / 2;
int cy = BOARD_Y + y * CELL_SIZE + CELL_SIZE / 2;
// 绘制棋子
setfillcolor(CHESS_COLOR[board[x][y]]);
solidcircle(cx, cy, CHESS_RADIUS);
}
void play(int x, int y)
{
if (board[x][y] != 0) return; // 如果该位置已有棋子,返回
board[x][y] = turn + 1;
drawChess(x, y);
winner = checkWinner(x, y);
if (winner != -1)
{
gameState = GAME_STATE[winner + 2];
}
else if (isTie())
{
gameState = GAME_STATE[4];
}
else
{
turn = !turn;
}
}
int checkWinner(int x, int y)
{
int i, j, k, count;
// 横向判断
count = 0;
for (i = x - 4; i <= x + 4; i++)
{
if (i < 0 || i > BOARD_SIZE - 1) continue;
if (board[i][y] == turn + 1) count++;
else count = 0;
if (count == 5) return turn;
}
// 纵向判断
count = 0;
for (j = y - 4; j <= y + 4; j++)
{
if (j < 0 || j > BOARD_SIZE - 1) continue;
if (board[x][j] == turn + 1) count++;
else count = 0;
if (count == 5) return turn;
}
// 斜向判断(左上-右下)
count = 0;
for (k = -4; k <= 4; k++)
{
i = x + k;
j = y + k;
if (i < 0 || i > BOARD_SIZE - 1 || j < 0 || j > BOARD_SIZE - 1) continue;
if (board[i][j] == turn + 1) count++;
else count = 0;
if (count == 5) return turn;
}
// 斜向判断(左下-右上)
count = 0;
for (k = -4; k <= 4; k++)
{
i = x + k;
j = y - k;
if (i < 0 || i > BOARD_SIZE - 1 || j < 0 || j > BOARD_SIZE - 1) continue;
if (board[i][j] == turn + 1) count++;
else count = 0;
if (count == 5) return turn;
}
return -1;
}
bool isTie()
{
for (int i = 0; i < BOARD_SIZE; i++)
{
for (int j = 0; j < BOARD_SIZE; j++)
{
if (board[i][j] == 0) return false;
}
}
return true;
}
void undo()
{
// 找到上一步棋子的坐标,并将其擦除
int x = -1, y = -1;
for (int i = BOARD_SIZE - 1; i >= 0; i--)
{
for (int j = BOARD_SIZE - 1; j >= 0; j--)
{
if (board[i][j] != 0)
{
x = i;
y = j;
board[i][j] = 0;
break;
}
}
if (x != -1 && y != -1) break;
}
if (x == -1 || y == -1) return;
// 重新绘制棋盘和棋子
clearrectangle(BOARD_X + x * CELL_SIZE, BOARD_Y + y * CELL_SIZE,
BOARD_X + (x + 1) * CELL_SIZE, BOARD_Y + (y + 1) * CELL_SIZE);
drawBoard();
for (int i = 0; i < BOARD_SIZE; i++)
{
for (int j = 0; j < BOARD_SIZE; j++)
{
if (board[i][j] != 0)
{
drawChess(i, j);
}
}
}
// 恢复游戏状态
gameState = GAME_STATE[1];
turn = !turn;
winner = -1;
}
};
// 定义全局变量
GameData game;
// 定义悔棋和认输按钮位置
const int BUTTON_X = BOARD_X + CELL_SIZE * (BOARD_SIZE + 1) + 20;
const int BUTTON_Y = BOARD_Y + CELL_SIZE * 3;
const int BUTTON_WIDTH = 100;
const int BUTTON_HEIGHT = 40;
// 绘制悔棋按钮
void drawUndoButton()
{
setfillcolor(RGB(255, 0, 0));
settextcolor(RGB(255, 255, 255));
settextstyle(16, 0, _T("宋体"));
rectangle(BUTTON_X, BUTTON_Y, BUTTON_X + BUTTON_WIDTH, BUTTON_Y + BUTTON_HEIGHT);
outtextxy(BUTTON_X + 20, BUTTON_Y + 10, _T("悔棋"));
}
// 绘制认输按钮
void drawGiveUpButton()
{
setfillcolor(RGB(255, 0, 0));
settextcolor(RGB(255, 255, 255));
settextstyle(16, 0, _T("宋体"));
rectangle(BUTTON_X, BUTTON_Y + BUTTON_HEIGHT + 20,
BUTTON_X + BUTTON_WIDTH, BUTTON_Y + BUTTON_HEIGHT * 2 + 20);
outtextxy(BUTTON_X + 20, BUTTON_Y + BUTTON_HEIGHT + 30, _T("认输"));
}
// 判断鼠标是否在悔棋按钮上
bool isMouseOnUndoButton(int mx, int my)
{
return mx >= BUTTON_X && mx <= BUTTON_X + BUTTON_WIDTH &&
my >= BUTTON_Y && my <= BUTTON_Y + BUTTON_HEIGHT;
}
// 判断鼠标是否在认输按钮上
bool isMouseOnGiveUpButton(int mx, int my)
{
return mx >= BUTTON_X && mx <= BUTTON_X + BUTTON_WIDTH &&
my >= BUTTON_Y + BUTTON_HEIGHT + 20 && my <= BUTTON_Y + BUTTON_HEIGHT * 2 + 20;
}
// 处理鼠标消息
void onMouseMsg(MOUSEMSG msg)
{
if (msg.uMsg == WM_LBUTTONDOWN)
{
if (game.gameState != GAME_STATE[1]) return;
int mx = msg.x, my = msg.y;
if (mx >= BOARD_X && mx <= BOARD_X + CELL_SIZE * BOARD_SIZE &&
my >= BOARD_Y && my <= BOARD_Y + CELL_SIZE * BOARD_SIZE)
{
int x = (mx - BOARD_X) / CELL_SIZE;
int y = (my - BOARD_Y) / CELL_SIZE;
game.play(x, y);
}
else if (isMouseOnUndoButton(mx, my))
{
game.undo();
}
else if (isMouseOnGiveUpButton(mx, my))
{
game.gameState = GAME_STATE[game.turn + 2];
}
}
}
int main()
{
// 初始化窗口和游戏数据
initgraph(WINDOW_WIDTH, WINDOW_HEIGHT);
game.init();
// 绘制棋盘和按钮
game.drawBoard();
drawUndoButton();
drawGiveUpButton();
// 循环处理消息
while (true)
{
// 绘制游戏状态
if (game.gameState == GAME_STATE[2])
{
outtextxy(BOARD_X + CELL_SIZE * BOARD_SIZE / 2 - 40, BOARD_Y - 30, _T("黑方胜利"));
}
else if (game.gameState == GAME_STATE[3])
{
outtextxy(BOARD_X + CELL_SIZE * BOARD_SIZE / 2 - 40, BOARD_Y - 30, _T("白方胜利"));
}
else if (game.gameState == GAME_STATE[4])
{
outtextxy(BOARD_X + CELL_SIZE * BOARD_SIZE / 2 - 40, BOARD_Y - 30, _T("平局"));
}
// 处理鼠标消息
MOUSEMSG msg = GetMouseMsg();
if (msg.uMsg == WM_MOUSEMOVE)
{
// 如果鼠标移动到按钮上,改变按钮颜色
if (isMouseOnUndoButton(msg.x, msg.y))
{
setfillcolor(RGB(255, 128, 128));
settextcolor(RGB(255, 255, 255));
rectangle(BUTTON_X, BUTTON_Y, BUTTON_X + BUTTON_WIDTH, BUTTON_Y + BUTTON_HEIGHT);
outtextxy(BUTTON_X + 20, BUTTON_Y + 10, _T("悔棋"));
}
else if (isMouseOnGiveUpButton(msg.x, msg.y))
{
setfillcolor(RGB(255, 128, 128));
settextcolor(RGB(255, 255, 255));
rectangle(BUTTON_X, BUTTON_Y + BUTTON_HEIGHT + 20,
BUTTON_X + BUTTON_WIDTH, BUTTON_Y + BUTTON_HEIGHT * 2 +
用VS 2017并且运用easy-x编写一个五子棋带有悔棋和认输功能的代码
好的,下面是一个简单的五子棋游戏的代码示例,主要实现了棋盘绘制、落子、悔棋和认输等功能。注意,以下代码仅供参考,你需要根据实际情况进行修改和完善。
```c++
#include <graphics.h>
#include <conio.h>
#include <stack>
using namespace std;
const int BOARD_SIZE = 15; // 棋盘大小
const int CELL_SIZE = 40; // 棋盘格子大小
const int BOARD_LEFT = 50; // 棋盘左上角坐标
const int BOARD_TOP = 50;
enum class ChessType { NONE, BLACK, WHITE }; // 棋子类型
enum class GameState { PLAYING, BLACK_WIN, WHITE_WIN, DRAW }; // 游戏状态
class ChessBoard {
public:
ChessBoard() {
init();
}
void init() {
memset(board_, 0, sizeof(board_));
current_player_ = ChessType::BLACK;
game_state_ = GameState::PLAYING;
undo_stack_.clear();
}
void draw() {
cleardevice();
setbkcolor(WHITE);
setfillcolor(RGB(232, 189, 109));
solidrectangle(BOARD_LEFT, BOARD_TOP, BOARD_LEFT + BOARD_SIZE * CELL_SIZE, BOARD_TOP + BOARD_SIZE * CELL_SIZE);
setlinecolor(BLACK);
for (int i = 0; i < BOARD_SIZE; ++i) {
line(BOARD_LEFT, BOARD_TOP + i * CELL_SIZE, BOARD_LEFT + (BOARD_SIZE - 1) * CELL_SIZE, BOARD_TOP + i * CELL_SIZE);
line(BOARD_LEFT + i * CELL_SIZE, BOARD_TOP, BOARD_LEFT + i * CELL_SIZE, BOARD_TOP + (BOARD_SIZE - 1) * CELL_SIZE);
}
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
int x = BOARD_LEFT + i * CELL_SIZE;
int y = BOARD_TOP + j * CELL_SIZE;
if (board_[i][j] == ChessType::BLACK) {
setfillcolor(BLACK);
solidcircle(x, y, CELL_SIZE / 2 - 2);
} else if (board_[i][j] == ChessType::WHITE) {
setfillcolor(WHITE);
solidcircle(x, y, CELL_SIZE / 2 - 2);
}
}
}
settextcolor(BLACK);
settextstyle(20, 0, _T("Consolas"));
outtextxy(BOARD_LEFT, BOARD_TOP + BOARD_SIZE * CELL_SIZE + 10, _T("按 R 键重新开始游戏,Q 键退出游戏"));
outtextxy(BOARD_LEFT, BOARD_TOP + BOARD_SIZE * CELL_SIZE + 40, _T("按 U 键悔棋,S 键认输"));
}
void putChess(int x, int y) {
if (game_state_ != GameState::PLAYING) {
return;
}
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
return;
}
if (board_[x][y] != ChessType::NONE) {
return;
}
board_[x][y] = current_player_;
undo_stack_.push({x, y, current_player_});
if (checkWin(x, y)) {
game_state_ = (current_player_ == ChessType::BLACK) ? GameState::BLACK_WIN : GameState::WHITE_WIN;
} else if (checkDraw()) {
game_state_ = GameState::DRAW;
} else {
current_player_ = (current_player_ == ChessType::BLACK) ? ChessType::WHITE : ChessType::BLACK;
}
}
bool checkWin(int x, int y) {
int dx[4] = {1, 0, 1, 1};
int dy[4] = {0, 1, 1, -1};
for (int i = 0; i < 4; ++i) {
int count = 1;
for (int j = 1; j <= 4; ++j) {
int nx = x + j * dx[i];
int ny = y + j * dy[i];
if (nx < 0 || nx >= BOARD_SIZE || ny < 0 || ny >= BOARD_SIZE || board_[nx][ny] != current_player_) {
break;
}
++count;
}
for (int j = 1; j <= 4; ++j) {
int nx = x - j * dx[i];
int ny = y - j * dy[i];
if (nx < 0 || nx >= BOARD_SIZE || ny < 0 || ny >= BOARD_SIZE || board_[nx][ny] != current_player_) {
break;
}
++count;
}
if (count >= 5) {
return true;
}
}
return false;
}
bool checkDraw() {
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
if (board_[i][j] == ChessType::NONE) {
return false;
}
}
}
return true;
}
void undo() {
if (!undo_stack_.empty()) {
auto last_move = undo_stack_.top();
undo_stack_.pop();
board_[last_move.x][last_move.y] = ChessType::NONE;
current_player_ = last_move.player;
game_state_ = GameState::PLAYING;
}
}
void resign() {
if (game_state_ == GameState::PLAYING) {
game_state_ = (current_player_ == ChessType::BLACK) ? GameState::WHITE_WIN : GameState::BLACK_WIN;
}
}
ChessType getCurrentPlayer() const {
return current_player_;
}
GameState getGameState() const {
return game_state_;
}
private:
struct Move {
int x;
int y;
ChessType player;
};
ChessType board_[BOARD_SIZE][BOARD_SIZE]; // 棋盘数组
ChessType current_player_; // 当前玩家
GameState game_state_; // 游戏状态
stack<Move> undo_stack_; // 悔棋记录
};
int main() {
initgraph(BOARD_LEFT + BOARD_SIZE * CELL_SIZE + 200, BOARD_TOP + BOARD_SIZE * CELL_SIZE, SHOWCONSOLE);
setbkcolor(WHITE);
ChessBoard board;
while (true) {
board.draw();
if (board.getGameState() != GameState::PLAYING) {
TCHAR msg[100];
if (board.getGameState() == GameState::BLACK_WIN) {
_stprintf_s(msg, _T("黑方胜利"));
} else if (board.getGameState() == GameState::WHITE_WIN) {
_stprintf_s(msg, _T("白方胜利"));
} else {
_stprintf_s(msg, _T("平局"));
}
MessageBox(NULL, msg, _T("游戏结束"), MB_OK);
board.init();
continue;
}
if (board.getCurrentPlayer() == ChessType::BLACK) {
outtextxy(BOARD_LEFT + BOARD_SIZE * CELL_SIZE + 10, BOARD_TOP, _T("黑方下棋"));
} else {
outtextxy(BOARD_LEFT + BOARD_SIZE * CELL_SIZE + 10, BOARD_TOP, _T("白方下棋"));
}
int x = -1;
int y = -1;
if (_kbhit()) {
int ch = _getch();
if (ch == 'q' || ch == 'Q') {
break;
} else if (ch == 'r' || ch == 'R') {
board.init();
continue;
} else if (ch == 'u' || ch == 'U') {
board.undo();
continue;
} else if (ch == 's' || ch == 'S') {
board.resign();
continue;
} else if (ch == ' ') {
POINT pt;
GetCursorPos(&pt);
ScreenToClient(GetForegroundWindow(), &pt);
x = (pt.x - BOARD_LEFT + CELL_SIZE / 2) / CELL_SIZE;
y = (pt.y - BOARD_TOP + CELL_SIZE / 2) / CELL_SIZE;
}
}
if (x >= 0 && y >= 0) {
board.putChess(x, y);
}
Sleep(10);
}
closegraph();
return 0;
}
```
运行以上代码,可以看到一个简单的五子棋游戏界面,支持悔棋、认输等功能。