大一下作的五子棋(老师要求),说要基于qt,但我还没学就用easyx做了,具体我已经打包,链接在最下面。
源.cpp
#define __CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include"play.h"
#include"za.h"
using namespace std;
int main() {
play p;
p.bgm();
p.player();
}
play.cpp
#include"za.h"
#include<graphics.h>
#include <Windows.h>
#include<mmsystem.h>
#pragma comment(lib,"WINMM.LIB")
using namespace std;
int x_axis;
int y_axis;
int step = 1;
int num_step;
char s[5];
MOUSEMSG m;
chessboard cb;
void play::bgm() {
mciSendString("open bgm.mp3", NULL, 0, NULL);
mciSendString("play bgm.mp3", NULL, 0, NULL);
}
void luozi() {
/*mciSendString("open 20220423_154258805.wav", NULL, 0, NULL);*/
mciSendString("play 123.mp3", NULL, 0, NULL);
}
int play::player()
{
cb.initchessboard();
cb.printchessboard();
while (step <= 255)
{
if (step % 2 == 1)
{
setfillcolor(BLACK);
fillcircle(637, 434, RADIUS);
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
x_axis = (int)((double)m.x / CELLSIZE + 0.5);
y_axis = (int)((double)m.y / CELLSIZE + 0.5);
if (cb.board[y_axis][x_axis] == ' ' && y_axis >= 0 && y_axis < 15 && x_axis >= 0 && x_axis < 15)
{
cb.board[y_axis][x_axis] = 'b';
luozi();
fillcircle(x_axis * CELLSIZE, y_axis * CELLSIZE, RADIUS);
/*cout << cb.board[y_axis][x_axis] << endl;*/
if (judge(y_axis, x_axis) == 1) {
MessageBoxA(0, "黑棋胜利!", "游戏结束", MB_OK);
return 1;
}
step++;
}
break;
}
}
else
{
setfillcolor(WHITE);
fillcircle(637, 434, RADIUS);
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
x_axis = (int)((double)m.x / CELLSIZE + 0.5);
y_axis = (int)((double)m.y / CELLSIZE + 0.5);
if (cb.board[y_axis][x_axis] == ' ' && y_axis >= 0 && y_axis < 15 && x_axis >= 0 && x_axis < 15)
{
cb.board[y_axis][x_axis] = 'w';
fillcircle(x_axis * CELLSIZE, y_axis * CELLSIZE, RADIUS);
luozi();
if (judge(y_axis, x_axis) == 1) {
MessageBoxA(0, "白棋胜利!", "游戏结束", MB_OK | MB_TOPMOST);
return 1;
}
step++;
}
break;
}
}
}
MessageBoxA(0, "平局!", "游戏结束", 3);
return 0;
}
int play::judge(int x, int y)
{
char c = 'c';
int num = 0;
for (int i = 0; i < MSIZE; i++)
{
/*cout << cb.board[x][y] << endl;*/
if (cb.board[i][y] == cb.board[x][y])
{
for (int j = 1; j < 5; j++)
{
if (cb.board[i + j][y] == cb.board[x][y]) {
num++;
}
else {
num = 0;
}
}
}
if (num == 4) {
return 1;
}
}
for (int i = 0; i < MSIZE; i++)
{
if (cb.board[x][i] == cb.board[x][y])
{
for (int j = 1; j < 5; j++)
{
if (cb.board[x][i + j] == cb.board[x][y])
{
num++;
}
else
{
num = 0;
}
}
}
if (num == 4) {
return 1;
}
}
for (int i = 0; i < MSIZE; i++) {
for (int j = 0; j < MSIZE; j++) {
if (i + 4 < 15 && j + 4 < 15 && cb.board[i + 1][j + 1] == cb.board[x][y] && cb.board[i + 2][j + 2] == cb.board[x][y]
&& cb.board[i + 3][j + 3] == cb.board[x][y] && cb.board[i + 4][j + 4] == cb.board[x][y] && cb.board[i + 5][j + 5] == cb.board[x][y])
{
return 1;
}
//斜左下判断
if (i + 4 < 15 && j - 4 < 15 && cb.board[i + 1][j - 1] == cb.board[x][y] && cb.board[i + 2][j - 2] == cb.board[x][y]
&& cb.board[i + 3][j - 3] == cb.board[x][y] && cb.board[i + 4][j - 4] == cb.board[x][y] && cb.board[i + 5][j - 5] == cb.board[x][y])
{
return 1;
}
}
}
return 0;
}
chessboard.cpp
#include"chessboard.h"
//#include<easyx.h>
#include<graphics.h>
void chessboard::initchessboard()
{
for (int i = 0; i < MSIZE; i++) {
for (int j = 0; j < MSIZE; j++) {
board[i][j] = ' ';
}
}
}
void chessboard::printchessboard()
{
initgraph(720, 600);
IMAGE img;
loadimage(&img, "beijing.png", 720, 600);
putimage(0, 0, &img);
}
chessboard.h
#pragma once
#define MSIZE 15
using namespace std;
class chessboard {
public:
char board[MSIZE][MSIZE];
void initchessboard();
void printchessboard();
};
play.h
#pragma once
#include <iostream>
using namespace std;
class play {
public:
int player();
int judge(int x, int y);
void bgm();
};
za.h
#pragma once
#include "chessboard.h"
#include"play.h"
#include<easyx.h>
#include<iostream>
#include<conio.h>
#define board_hight 560
#define board_width 560
#define CELLSIZE (board_width/(MSIZE-1))
#define RADIUS CELLSIZE/2
//#define MSIZE 15