Easyx使用(对弈类小作品)
【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
写软件很容易有误区,一种是代码量太小,好像写完也没什么用。还有一种是代码量太大,似乎需要很多的人、花费很多的时间才能开发,依靠一个人根本没有办法做完。而easyx就是有这么个好处,要么是算法+仿真,要么是游戏,要么是工具类产品,一个人短时间也可以开发出来。如果是有ai的帮助,那就开发更快了。
1、五子棋
五子棋是一种简单的对弈小游戏。以前在学校的时候,甚至可以双方用圆珠笔在白纸上面玩。它的玩法很简单,就是黑、白两个颜色的棋子,谁先连成5个,谁就可以获胜。方向可以是水平的、垂直的、斜线的。
2、开发任务分解
如果是软件来做,那么可以黑白交替的方法来进行。比如先黑棋,再白棋。首先,需要绘制棋盘。然后,需要绘制棋子。接着就是书写规则。游戏里面最重要的就是规则。最后是对事件进行判断处理,每次下棋的时候,判断位置、绘制棋子、判断胜负,一气呵成。
3、绘制棋盘
棋盘其实比较好绘制,就是一个表格,
// draw basic boardstatic void drawBoard()
{for (int i = 0; i < BOARD_SIZE; i++) {// draw horizontal lineline(BOARD_OFFSET, BOARD_OFFSET + i * CELL_SIZE, BOARD_OFFSET + (BOARD_SIZE - 1) * CELL_SIZE, BOARD_OFFSET + i * CELL_SIZE);// draw vertical lineline(BOARD_OFFSET + i * CELL_SIZE, BOARD_OFFSET, BOARD_OFFSET + i * CELL_SIZE, BOARD_OFFSET + (BOARD_SIZE - 1) * CELL_SIZE);}
}
4、绘制棋子
所谓的棋子绘制,就是在棋盘的直线交汇处绘制一个实心圆。当然,有的时候是白色的,有的时候是黑色的,
// draw chessstatic void drawPiece(int x, int y, int color)
{// draw pieces hereif (color == 1) {setfillcolor(BLACK);}else if (color == 2) {setfillcolor(WHITE);}fillcircle(BOARD_OFFSET + x * CELL_SIZE, BOARD_OFFSET + y * CELL_SIZE, CELL_SIZE / 3);
}
5、胜负判断
胜负判断,也就是说白棋,或者是黑棋放下去的时候,在水平、垂直或者是斜线方向上,有没有出现五子棋的情况,
// about chess rulestatic bool isWin(int player)
{int i = 0;int j = 0;for (i = 0; i < BOARD_SIZE; i++) {for (j = 0; j < BOARD_SIZE; j++) {if (board[i][j] == player) {// horizontalif (j + 4 < BOARD_SIZE && board[i][j + 1] == player && board[i][j + 2] == player && board[i][j + 3] == player && board[i][j + 4] == player) return true;// verticalif (i + 4 < BOARD_SIZE && board[i + 1][j] == player && board[i + 2][j] == player && board[i + 3][j] == player && board[i + 4][j] == player) return true;// up left to low right if (i + 4 < BOARD_SIZE && j + 4 < BOARD_SIZE && board[i + 1][j + 1] == player && board[i + 2][j + 2] == player && board[i + 3][j + 3] == player && board[i + 4][j + 4] == player) return true;// low left to up rightif (i - 4 >= 0 && j + 4 < BOARD_SIZE && board[i - 1][j + 1] == player && board[i - 2][j + 2] == player && board[i - 3][j + 3] == player && board[i - 4][j + 4] == player) return true;}}}return false;
}
6、最后就是事件处理
所谓的事件处理,其实就是鼠标。当鼠标按下去的时候,我们要看下当前这个位置,和键盘上哪个格子最近。接着就是判断这个格子是否被占领。在更新棋盘之后,还要继续分析是否胜负已定,还是说要切换到另外一个选手,继续下棋。
// get mouse eventif (msg.message == WM_LBUTTONDOWN && msg.lbutton) {int mouseX = msg.x;int mouseY = msg.y;// find nearest positionint closestX = (mouseX - BOARD_OFFSET + CELL_SIZE / 2) / CELL_SIZE;int closestY = (mouseY - BOARD_OFFSET + CELL_SIZE / 2) / CELL_SIZE;// do restriction for position closestX & closestYif (closestX < 0) closestX = 0;if (closestX > (BOARD_SIZE-1)) closestX = BOARD_SIZE -1;if (closestY < 0) closestY = 0;if (closestY > (BOARD_SIZE - 1)) closestY = BOARD_SIZE - 1;if (board[closestY][closestX] == 0) {// check if board emptyboard[closestY][closestX] = currentPlayer;drawPiece(closestX, closestY, currentPlayer);if (isWin(currentPlayer)) {gameOver = true;}else{currentPlayer = (currentPlayer == 1) ? 2 : 1; // switch playerif (currentPlayer == 1)outtextxy(650, 300, "Current: BLACK");elseouttextxy(650, 300, "Current: WHITE");}}}
7、字符提示
程序中有两个地方需要字符提示,一个就是下棋的时候,判断当前是哪一位在下棋。还有一个就是结束的时候,需要判断谁获得了比赛。整个代码还是比较清晰的,有兴趣的同学可以参考下完整的代码,自己实际试一下。
#define _CRT_SECURE_NO_WARNINGS // remove compile warning here
#include <graphics.h>
#include <conio.h>
#include <stdio.h>// initial variable herestatic const int BOARD_SIZE = 15; // grid number
static const int CELL_SIZE = 40; // cell size
static const int BOARD_OFFSET = 20; // offset to left and top
static int board[BOARD_SIZE][BOARD_SIZE] = {0}; // 0: empty,1: black,2: white// draw basic boardstatic void drawBoard()
{for (int i = 0; i < BOARD_SIZE; i++) {// draw horizontal lineline(BOARD_OFFSET, BOARD_OFFSET + i * CELL_SIZE, BOARD_OFFSET + (BOARD_SIZE - 1) * CELL_SIZE, BOARD_OFFSET + i * CELL_SIZE);// draw vertical lineline(BOARD_OFFSET + i * CELL_SIZE, BOARD_OFFSET, BOARD_OFFSET + i * CELL_SIZE, BOARD_OFFSET + (BOARD_SIZE - 1) * CELL_SIZE);}
}// draw chessstatic void drawPiece(int x, int y, int color)
{// draw pieces hereif (color == 1) {setfillcolor(BLACK);}else if (color == 2) {setfillcolor(WHITE);}fillcircle(BOARD_OFFSET + x * CELL_SIZE, BOARD_OFFSET + y * CELL_SIZE, CELL_SIZE / 3);
}// about chess rulestatic bool isWin(int player)
{int i = 0;int j = 0;for (i = 0; i < BOARD_SIZE; i++) {for (j = 0; j < BOARD_SIZE; j++) {if (board[i][j] == player) {// horizontalif (j + 4 < BOARD_SIZE && board[i][j + 1] == player && board[i][j + 2] == player && board[i][j + 3] == player && board[i][j + 4] == player) return true;// verticalif (i + 4 < BOARD_SIZE && board[i + 1][j] == player && board[i + 2][j] == player && board[i + 3][j] == player && board[i + 4][j] == player) return true;// up left to low right if (i + 4 < BOARD_SIZE && j + 4 < BOARD_SIZE && board[i + 1][j + 1] == player && board[i + 2][j + 2] == player && board[i + 3][j + 3] == player && board[i + 4][j + 4] == player) return true;// low left to up rightif (i - 4 >= 0 && j + 4 < BOARD_SIZE && board[i - 1][j + 1] == player && board[i - 2][j + 2] == player && board[i - 3][j + 3] == player && board[i - 4][j + 4] == player) return true;}}}return false;
}int main(int argc ,char* argv[])
{int currentPlayer = 1; // 1 - black, 2 - whitebool gameOver = false;ExMessage msg;initgraph(800, 600);// set background colorsetbkcolor(BLUE);cleardevice();// initial board statefor (int i = 0; i < BOARD_SIZE; i++) {for (int j = 0; j < BOARD_SIZE; j++) {board[i][j] = 0;}}// draw boarddrawBoard(); // add information about current userif (currentPlayer == 1)outtextxy(650, 300, "Current: BLACK");elseouttextxy(650, 300, "Current: WHITE");// loop to check gamewhile (!gameOver) {if (peekmessage(&msg, EM_MOUSE)) {// get mouse eventif (msg.message == WM_LBUTTONDOWN && msg.lbutton) {int mouseX = msg.x;int mouseY = msg.y;// find nearest positionint closestX = (mouseX - BOARD_OFFSET + CELL_SIZE / 2) / CELL_SIZE;int closestY = (mouseY - BOARD_OFFSET + CELL_SIZE / 2) / CELL_SIZE;// do restriction for position closestX & closestYif (closestX < 0) closestX = 0;if (closestX > (BOARD_SIZE-1)) closestX = BOARD_SIZE -1;if (closestY < 0) closestY = 0;if (closestY > (BOARD_SIZE - 1)) closestY = BOARD_SIZE - 1;if (board[closestY][closestX] == 0) {// check if board emptyboard[closestY][closestX] = currentPlayer;drawPiece(closestX, closestY, currentPlayer);if (isWin(currentPlayer)) {gameOver = true;}else{currentPlayer = (currentPlayer == 1) ? 2 : 1; // switch playerif (currentPlayer == 1)outtextxy(650, 300, "Current: BLACK");elseouttextxy(650, 300, "Current: WHITE");}}}}// sleep for a whileSleep(20);}// clear device and judge winnercleardevice();if (currentPlayer == 1)outtextxy(320, 240, "Black Wins!!!");elseouttextxy(320, 240, "White Wins!!!");// game ends_getch();closegraph();return 0;
}