1,五子棋游戏的规则简介:
双方依次下子,谁先连成五子一条线则胜利。
2,
/*****************************************/
FIR.pro:
#-------------------------------------------------
#
# Project created by QtCreator 2012-09-01T15:09:11
#
#-------------------------------------------------
QT += core gui
TARGET = FIR
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
/******************************/
wight.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui>
class Widget : public QWidget
{
Q_OBJECT
private:
int a[15][15];
int player;
bool isWin(int, int);
bool f1(int, int);
bool f2(int, int);
bool f3(int, int);
bool f4(int, int);
public:
Widget(QWidget *parent = 0);
~Widget();
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent * e);
};
#endif // WIDGET_H
/*****************************************/
main:
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
/*****************************************/
widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
resize(640, 640);
player = 0;
memset(a, 0, 15 * 15 * sizeof(int));
}
Widget::~Widget()
{
}
画方格:
void Widget::paintEvent(QPaintEvent *)
{
QPainter p(this);
int i , j ;
for(i = 0; i < 16; i++)
{
p.drawLine(20, 20 + i * 40, 620, 20 + i * 40);
p.drawLine(20 + i * 40, 20, 20 + i * 40, 620);
}
int m = 5;
int n = 0;
while(1)
{
QBrush brush(Qt::SolidPattern);
brush.setColor(Qt::blue);
p.setBrush(brush);
// p.drawRect((i+1)*20,(j+1)*20,80,80);//zheng fang xing
// p.drawRect((i+1)*20,(j+1)*20,40,160);//chang fang xing
p.drawRect((m+1)*20,(n+1)*20,80,40);
p.drawRect((m+3)*20,(n+3)*20,80,40);
//p.drawRect();
//p.drawEllipse(QPoint((i + 1) * 40, (j + 1) * 40), 15, 15);
}
}
鼠标响应:
void Widget::mousePressEvent(QMouseEvent * e)
{
/*
setWindowTitle(QString::number(e->x()) +
" " +
QString::number(e->y()));
*/
int x, y;
if(e->x() >= 20 && e->x() < 620 && e->y() >= 20 && e->y() < 620)
{
x = (e->x() - 20) / 40;
y = (e->y() - 20) / 40;
if (!a[x][y])
{
a[x][y] = player++ % 2 + 1;
}
if(isWin(x, y))
{
update();
setEnabled(false);
}
}
update();
}
判断输赢:
bool Widget::isWin(int x, int y)
{
return f1(x, y) || f2(x, y) || f3(x, y) || f4(x ,y);
}
判断四个边界:
bool Widget::f1(int x, int y)
{
int i;
for (i = 0; i < 5; i++)
{
if(y - i >= 0 &&
y + 4 - i <= 0xF &&
a[x][y - i] == a[x][y + 1 - i] &&
a[x][y - i] == a[x][y + 2 - i] &&
a[x][y - i] == a[x][y + 3 - i] &&
a[x][y - i] == a[x][y + 4 - i])
return true;
}
return false;
}
bool Widget::f2(int x, int y)
{
int i;
for (i = 0; i < 5; i++)
{
if(x - i >= 0 &&
x + 4 - i <= 0xF &&
a[x - i][y] == a[x + 1 - i][y] &&
a[x - i][y] == a[x + 2 - i][y] &&
a[x - i][y] == a[x + 3 - i][y] &&
a[x - i][y] == a[x + 4 - i][y])
return true;
}
return false;
}
bool Widget::f3(int x, int y)
{
int i;
for (i = 0; i < 5; i++)
{
if(x - i >= 0 &&
y - i >= 0 &&
x + 4 - i <= 0xF &&
y + 4 - i <= 0xF &&
a[x - i][y - i] == a[x + 1 - i][y + 1 - i] &&
a[x - i][y - i] == a[x + 2 - i][y + 2 - i] &&
a[x - i][y - i] == a[x + 3 - i][y + 3 - i] &&
a[x - i][y - i] == a[x + 4 - i][y + 4 - i])
return true;
}
return false;
}
bool Widget::f4(int x, int y)
{
int i;
for (i = 0; i < 5; i++)
{
if(x + i <= 0xF &&
y - i >= 0 &&
x - 4 + i >= 0 &&
y + 4 - i <= 0xF &&
a[x + i][y - i] == a[x - 1 + i][y + 1 - i] &&
a[x + i][y - i] == a[x - 2 + i][y + 2 - i] &&
a[x + i][y - i] == a[x - 3 + i][y + 3 - i] &&
a[x + i][y - i] == a[x - 4 + i][y + 4 - i])
return true;
}
return false;
}