基于Ubuntu下的QT制作黑白棋游戏
本文以begin为第一跳转界面
第二跳转界面(heibai)为People to People(人人对战)
第三跳转界面(heibai2)为People to Computer(人机对战)
三张效果图如下:
各个页面源代码如下:
头文件:
begin.h
#ifndef BEGIN_H
#define BEGIN_H
#include <heibai.h>
#include <heibai2.h>
#include <QMainWindow>
#include <QPaintEvent>
#include <QWidget>
#include <QMouseEvent>
#include <phonon/MediaObject>
namespace Ui {
class begin;
}
//enum GridState{Black,White,Empty};
class begin : public QMainWindow
{
Q_OBJECT
public:
explicit begin(QWidget *parent = 0);
~begin();
void paintEvent(QPaintEvent *e);
enum GridState{Black,White,Empty};
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void put();
void put2();
void do_timer();
private:
Ui::begin *ui;
heibai b;
heibai2 b2;
int sec;
};
#endif // BEGIN_H
heibai.h
#ifndef HEIBAI_H
#define HEIBAI_H
#include <QWidget>
#include <QPaintEvent>
#include <QMainWindow>
#include <QMouseEvent>
namespace Ui {
class heibai;
}
class heibai : public QWidget
{
Q_OBJECT
public:
explicit heibai(QWidget *parent = 0);
~heibai();
void paintEvent(QPaintEvent *e);
enum GridState{Black,White,Empty};
void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
int judgeRule(int x, int y, int gridNum, void *chess, GridState currentRole);
int judgeRule2(int x, int y, int gridNum, void *chess, GridState currentRole);
private:
Ui::heibai *ui;
int sec;
QTimer *timer;
int chessNumber[8][8];
GridState currentRole;
int xPoint,yPoint;
int BlackNumber;
int WhiteNumber;
signals:
void give();
private slots:
void do_timer();
void do_timer2();
void on_pushButton_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void on_pushButton_5_clicked();
void on_pushButton_7_clicked();
void on_pushButton_8_clicked();
};
#endif // HEIBAI_H
heibai2.h
#ifndef HEIBAI2_H
#define HEIBAI2_H
#include <QWidget>
#include <QPaintEvent>
#include <QMainWindow>
#include <QMouseEvent>
namespace Ui {
class heibai2;
}
class heibai2 : public QWidget
{
Q_OBJECT
public:
explicit heibai2(QWidget *parent = 0);
void paintEvent(QPaintEvent *e);
enum GridState{Water,Fire,Air};
void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
int judgeRule(int x, int y, int gridNum, void *chess, GridState currentRole);
int judgeRule2(int x, int y, int gridNum, void *chess, GridState currentRole);
~heibai2();
signals:
void give2();
private slots:
void do_timer();
void do_timer2();
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void on_pushButton_5_clicked();
void on_pushButton_6_clicked();
void on_pushButton_7_clicked();
private:
Ui::heibai2 *ui;
int sec;
QTimer *timer;
int chessNumber[8][8];
GridState currentRole;
int xPoint,yPoint;
int BlackNumber;
int WhiteNumber;
};
#endif // HEIBAI2_H
begin.cpp
#include "begin.h"
#include "ui_begin.h"
#include "QPainter"
#include "QTimer"
begin::begin(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::begin)
{
ui->setupUi(this);
connect(&this->b,SIGNAL(give()),this,SLOT(put()));
connect(&this->b2,SIGNAL(give2()),this,SLOT(put2()));
}
begin::~begin()
{
delete ui;
}
void begin::put()
{
this->show();
}
void begin::put2()
{
this->show();
}
void begin::paintEvent(QPaintEvent *e) //background picture
{
QPainter p(this);
p.drawPixmap(0,0,1366,768,QPixmap(":/new/prefix1/images /11.png"));
}
void begin::on_pushButton_clicked()
{
b.setFixedSize(1366,768);
b.show();
this->close();
}
void begin::on_pushButton_2_clicked()
{
b2.setFixedSize(1366,768);
b2.show();
this->close();
}
heibai.cpp
#include "heibai.h"
#include "ui_heibai.h"
#include <QTimer>
#include <QPainter>
#include <QPen>
#include "QMovie"
#include "QMessageBox"
heibai::heibai(QWidget *parent) :
QWidget(parent),
ui(new Ui::heibai)
{
ui->setupUi(this);
sec=11;
this->setMouseTracking(true);
QTimer *timer;
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(do_timer2()));
timer->start(1000);
QMovie *movie=new QMovie(":/new/prefix1/images /3.gif");
ui->label->setPixmap(QPixmap(":/new/prefix1/images /black.png"));
ui->label_2->setPixmap(QPixmap(":/new/prefix1/images /white.png"));
ui->label_3->setMovie(movie);
movie->start();
movie->setSpeed(100);
BlackNumber=2;
WhiteNumber=2;
ui->lcdNumber->display(BlackNumber);
ui->lcdNumber_2->display(WhiteNumber);
int i,j;
currentRole=Empty;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
chessNumber[i][j]=Empty;
}
}
chessNumber[3][3]=Black;
chessNumber[4][4]=Black;
chessNumber[3][4]=White;
chessNumber[4][3]=White;
}
void heibai::mouseMoveEvent(QMouseEvent *e)
{
xPoint=e->x();
yPoint=e->y();
this->update();
}
heibai::~heibai()
{
delete ui;
}
void heibai::do_timer()
{
if(sec>0)
{
sec--;
ui->lcdNumber_3->display(sec);
}
else
{
sec=11;
}
}
void heibai::on_pushButton_clicked()
{
emit give();
this->close();
}
void heibai::paintEvent(QPaintEvent *e)
{
QPainter painer(this);
painer.drawPixmap(0,0,1366,768,QPixmap(":/new/prefix1/images /22.png"));
QPen pen;
pen.setBrush(QBrush(Qt::Dense1Pattern));
pen.setColor(QColor(Qt::blue));
painer.setPen(pen);
painer.drawPixmap(110,110,480,480,QPixmap(":/new/prefix1/images /pvm.png"));
int i,j;
for(i=0;i<=8;i++)
{
painer.drawLine(110,110+(60*i),590,110+(60*i));
painer.drawLine(110+(60*i),110,110+(60*i),590);
}
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
if(chessNumber[i][j]==Black)
{
painer.drawPixmap(110+60*i,110+60*j,60,60,QPixmap(":/new/prefix1/images /black.png"));
}
else if(chessNumber[i][j]==White)
{
painer.drawPixmap(110+60*i,110+60*j,60,60,QPixmap(":/new/prefix1/images /white.png"));
}
}
if(currentRole==Black)
{
painer.drawPixmap(xPoint-30,yPoint-30,60,60,QPixmap(":/new/prefix1/images /black.png"));
}
else
{
painer.drawPixmap(xPoint-30,yPoint-30,60,60,QPixmap(":/new/prefix1/images /white.png"));
}
}
}
void heibai::mousePressEvent(QMouseEvent *e)
{
int x=e->x();
x=(x-110)/60;
int y=e->y();
y=(y-110)/60;
int a;
if(currentRole==Black)
{
a=judgeRule(x,y,8,chessNumber,currentRole);
if(a>0)
{
if(currentRole==Black)
{
sec=11;
BlackNumber=BlackNumber+a+1;
WhiteNumber=WhiteNumber-a;
ui->lcdNumber->display(BlackNumber);
ui->lcdNumber_2->display(WhiteNumber);
currentRole=White;
}
else
{
sec=11;
BlackNumber=BlackNumber-a;
WhiteNumber=WhiteNumber+a+1;
ui->lcdNumber->display(BlackNumber);
ui->lcdNumber_2->display(WhiteNumber);
currentRole=Black;
}
}
}
if(currentRole==White)
{
a=judgeRule(x,y,8,chessNumber,currentRole);
if(a>0)
{
if(currentRole==White)
{
sec=11;
BlackNumber=BlackNumber-a;
WhiteNumber=WhiteNumber+a+1;
ui->lcdNumber->display(BlackNumber);
ui->lcdNumber_2->display(WhiteNumber);
currentRole=Black;
}
else
{
sec=11;
BlackNumber=BlackNumber+a+1;
WhiteNumber=WhiteNumber-a;
ui->lcdNumber->display(BlackNumber);
ui->lcdNumber_2->display(WhiteNumber);
currentRole=White;
}
}
}
ui->lineEdit->clear();
int add=0;
int i,j;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
if(chessNumber[i][j]!=Empty)
add++;
if(add==64)
{
ui->pushButton->setEnabled(true);
if(BlackNumber>WhiteNumber)
QMessageBox::information(NULL,QString("Tip"),QString("The black is win"));
else if(BlackNumber==WhiteNumber)
QMessageBox::information(NULL,QString("Tip"),QString("Draw"));
else
QMessageBox::information(NULL,QString("Tip"),QString("The white is win"));
sec=0;
}
int flag=0;
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
if(judgeRule2(i,j,8,chessNumber,currentRole))
flag=1;
if(add!=64 && flag==0)
{
ui->pushButton->setEnabled(true);
if(BlackNumber>WhiteNumber)
QMessageBox::information(NULL,QString("Tip"),QString("The black is win"));
else if(BlackNumber==WhiteNumber && BlackNumber != 2 && WhiteNumber != 2)
QMessageBox::information(NULL,QString("Tip"),QString("Draw"));
else if(WhiteNumber>BlackNumber)
QMessageBox::information(NULL,QString("Tip"),QString("The white is win"));
sec=0;
}
this->update();
}
int heibai::judgeRule(int x, int y, int gridNum, void *chess, GridState currentRole)
{
int dir[8][2]={{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
if( x < 0 || x>= gridNum || y < 0 || y >= gridNum)
// 判断坐标是否合法
return 0;
int temp_x = x, temp_y = y;
int i = 0, eatNum = 0;
typedef int (*p)[gridNum]; //自定义类型
p chessBoard = p(chess); //类型转换 并赋值给新变量 chessBoard
//如果此方格内已有棋子,返回;
if(chessBoard[temp_x][temp_y]!=Empty)
return 0;
for(i = 0 ; i <8; i++) //循环判断8个方向
{
temp_x += dir[i][0]; temp_y += dir[i][1];
//准备判断相邻棋子
//如果没有出界,且相邻棋子是对方棋子,才有吃子的可能.
if((temp_x < gridNum && temp_x >=0 && temp_y < gridNum && temp_y >= 0)
&& (chessBoard[temp_x][temp_y] != currentRole)
&& chessBoard[temp_x][temp_y]!=Empty)
{
temp_x += dir[i][0]; temp_y += dir[i][1];
//继续判断下一个
//同样判断是否出界
while(temp_x < gridNum && temp_x >=0 && temp_y < gridNum && temp_y >= 0)
{
//遇到空位跳出
if(chessBoard[temp_x][temp_y] == Empty)
break;
//找到了自己的棋子,可以吃子
if(chessBoard[temp_x][temp_y] == currentRole)
{
chessBoard[x][y] = currentRole;
//改变(x,y)坐标内的信息
temp_x -= dir[i][0]; temp_y -= dir[i][1];
while((temp_x != x )||(temp_y != y))
//吃子的过程
{
chessBoard[temp_x][temp_y] = currentRole;
temp_x -= dir[i][0]; temp_y -= dir[i][1];
eatNum ++;
}
break;
}
//如果仍然是对方棋子,继续沿此方向查找
temp_x += dir[i][0]; temp_y += dir[i][1];
}
}
temp_x = x; temp_y = y; //一个方向判断结束,准备下一个方向
}
return eatNum;
}
void heibai::do_timer2()
{
if(sec>0)
{
sec--;
ui->lcdNumber_3->display(sec);
}
else
{
ui->lineEdit->setText("time over");
if(currentRole==White)
{
currentRole=Black;
sec=11;
}
else if(currentRole==Black)
{
sec=11;
currentRole=White;
}
}
this->update();
}
void heibai::on_pushButton_3_clicked()
{
ui->lcdNumber->display(0);
timer->stop();
sec=11;
BlackNumber=2;
WhiteNumber=2;
ui->lcdNumber->display(BlackNumber);
ui->lcdNumber_3->display(WhiteNumber);
int i,j;
currentRole=Empty;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
chessNumber[i][j]=Empty;
}
}
chessNumber[3][3]=Black;
chessNumber[4][4]=Black;
chessNumber[3][4]=White;
chessNumber[4][3]=White;
}
void heibai::on_pushButton_4_clicked()
{
currentRole=Black;
BlackNumber=2;
WhiteNumber=2;
}
int heibai::judgeRule2(int x, int y, int gridNum, void *chess, GridState currentRole)
{
int dir[8][2]={{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
if( x < 0 || x>= gridNum || y < 0 || y >= gridNum)
// 判断坐标是否合法
return 0;
int temp_x = x, temp_y = y;
int i = 0, eatNum = 0;
typedef int (*p)[gridNum]; //自定义类型
p chessBoard = p(chess); //类型转换 并赋值给新变量 chessBoard
//如果此方格内已有棋子,返回;
if(chessBoard[temp_x][temp_y]!=Empty)
return 0;
for(i = 0 ; i <8; i++) //循环判断8个方向
{
temp_x += dir[i][0]; temp_y += dir[i][1];
//准备判断相邻棋子
//如果没有出界,且相邻棋子是对方棋子,才有吃子的可能.
if((temp_x < gridNum && temp_x >=0 && temp_y < gridNum && temp_y >= 0)
&& (chessBoard[temp_x][temp_y] != currentRole)
&& chessBoard[temp_x][temp_y]!=Empty)
{
temp_x += dir[i][0]; temp_y += dir[i][1];
//继续判断下一个
//同样判断是否出界
while(temp_x < gridNum && temp_x >=0 && temp_y < gridNum && temp_y >= 0)
{
//遇到空位跳出
if(chessBoard[temp_x][temp_y] == Empty)
break;
//找到了自己的棋子,可以吃子
if(chessBoard[temp_x][temp_y] == currentRole)
{
//改变(x,y)坐标内的信息
temp_x -= dir[i][0]; temp_y -= dir[i][1];
while((temp_x != x )||(temp_y != y))
//吃子的过程
{
temp_x -= dir[i][0]; temp_y -= dir[i][1];
eatNum ++;
}
break;
}
//如果仍然是对方棋子,继续沿此方向查找
temp_x += dir[i][0]; temp_y += dir[i][1];
}
}
temp_x = x; temp_y = y; //一个方向判断结束,准备下一个方向
}
return eatNum;
}
void heibai::on_pushButton_5_clicked()
{
BlackNumber=2;
WhiteNumber=2;
ui->lcdNumber->display(BlackNumber);
ui->lcdNumber_2->display(WhiteNumber);
int i,j;
currentRole=Empty;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
chessNumber[i][j]=Empty;
}
}
chessNumber[3][3]=Black;
chessNumber[4][4]=Black;
chessNumber[3][4]=White;
chessNumber[4][3]=White;
}
void heibai::on_pushButton_8_clicked()
{
QMessageBox::information(NULL,QString("Tip"),QString("The Computer is win!"));
}
void heibai::on_pushButton_7_clicked()
{
QMessageBox::information(NULL,QString("Regulation"),QString("此游戏默认黑棋先下!点击begin开始,如果开始时是白旗,请再次点击一次begin切换到黑棋。棋子:黑白棋棋子每颗由黑白两色组成,一面白,一面黑,共64个(包括棋盘中央的4个)。棋子呈圆饼形。两个玩家各下一面棋子。棋盘:黑白棋棋盘由64(8*8)格的正方格组成,游戏进行时棋子要下在格内。棋盘为8×8的方格布局,开局时在棋盘正中有摆好的四枚棋子,黑白各2枚,交叉放置,由执黑棋的一方先落子,双方交替下子,棋子落在方格内,一局游戏结束后双方更换执子颜色。步合法的棋步包括:在一个空格新落下一个棋子,并且翻转对手一个或多个棋子。下子方式:把自己颜色的棋子放在棋盘的空格上,而当自己放下的棋子在横、竖、斜八个方向内有一个自己的棋子,则被夹在中间的对方棋子全部翻转会成为自己的棋子。夹住的位置上必须全部是对手的棋子,不能有空格。并且,只有在可以翻转棋子的地方才可以下子。一步棋可以在数个方向上翻棋,任何被夹住的棋子都必须被翻转过来,棋手无权选择不去翻某个棋子必须是刚下的子夹对方才能够翻对方的子,因翻转对方的棋子而夹住的子是不能被翻的。翻转棋子时,有一个棋子的翻转动画大概显示1秒左右每次下子最少必须翻转对方一个棋子,若棋局中下子都不能翻转对方棋子,则自动pass轮空,客户端界面气泡提示:您无子可下由对方继续下子,而对方气泡提示:由于对方无子可下,您可继续下子。若二个玩家都不能下子翻转对方棋子,游戏结束。
"));
}
heibai2.cpp
#include "heibai2.h"
#include "ui_heibai2.h"
#include <QTimer>
#include <QPainter>
#include <QPen>
#include "QMovie"
#include "QMessageBox"
heibai2::heibai2(QWidget *parent) :
QWidget(parent),
ui(new Ui::heibai2)
{
ui->setupUi(this);
sec=11;
this->setMouseTracking(true);
QTimer *timer;
timer= new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(do_timer2()));
timer->start(1000);
QMovie *movie=new QMovie(":/new/prefix1/images /1.gif");
ui->label->setPixmap(QPixmap(":/new/prefix1/images /black.png"));
ui->label_2->setPixmap(QPixmap(":/new/prefix1/images /white.png"));
ui->label_3->setMovie(movie);
movie->start();
movie->setSpeed(100);
BlackNumber=2;
WhiteNumber=2;
ui->lcdNumber->display(BlackNumber);
ui->lcdNumber_2->display(WhiteNumber);
int i,j;
currentRole=Air;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
chessNumber[i][j]=Air;
}
}
chessNumber[3][3]=Water;
chessNumber[4][4]=Water;
chessNumber[3][4]=Fire;
chessNumber[4][3]=Fire;
}
void heibai2::mouseMoveEvent(QMouseEvent *e)
{
xPoint=e->x();
yPoint=e->y();
this->update();
}
heibai2::~heibai2()
{
delete ui;
}
void heibai2::do_timer()
{
if(sec>0)
{
sec--;
ui->lcdNumber_3->display(sec);
}
else
{
sec=11;
}
}
void heibai2::on_pushButton_clicked()
{
emit give2();
this->close();
}
void heibai2::paintEvent(QPaintEvent *e)
{
QPainter painer(this);
painer.drawPixmap(0,0,1366,768,QPixmap(":/new/prefix1/images /33.png"));
QPen pen;
pen.setBrush(QBrush(Qt::Dense1Pattern));
pen.setColor(QColor(Qt::white));
painer.setPen(pen);
painer.drawPixmap(110,110,480,480,QPixmap(":/new/prefix1/images /over.png"));
int i,j;
for(i=0;i<=8;i++)
{
painer.drawLine(110,110+(60*i),590,110+(60*i));
painer.drawLine(110+(60*i),110,110+(60*i),590);
}
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
if(chessNumber[i][j]==Water)
{
painer.drawPixmap(110+60*i,110+60*j,60,60,QPixmap(":/new/prefix1/images /black.png"));
}
else if(chessNumber[i][j]==Fire)
{
painer.drawPixmap(110+60*i,110+60*j,60,60,QPixmap(":/new/prefix1/images /white.png"));
}
}
if(currentRole==Water)
{
painer.drawPixmap(xPoint-30,yPoint-30,60,60,QPixmap(":/new/prefix1/images /black.png"));
}
else
{
painer.drawPixmap(xPoint-30,yPoint-30,60,60,QPixmap(":/new/prefix1/images /white.png"));
}
}
}
void heibai2::mousePressEvent(QMouseEvent *e)
{
int x=e->x();
x=(x-110)/60;
int y=e->y();
y=(y-110)/60;
int a;
if(currentRole==Water)
{
a=judgeRule(x,y,8,chessNumber,currentRole);
if(a>0)
{
if(currentRole==Water)
{
sec=11;
BlackNumber=BlackNumber+a+1;
WhiteNumber=WhiteNumber-a;
ui->lcdNumber->display(BlackNumber);
ui->lcdNumber_2->display(WhiteNumber);
currentRole=Fire;
}
int m,n;
for(m=0;m<8;m++)
{
int eatNUmber;
for(n=0;n<8;n++)
{
eatNUmber=judgeRule(m,n,8,chessNumber,currentRole);
if(eatNUmber>0)
break;
}
if(eatNUmber>0)
{
sec=11;
BlackNumber=BlackNumber-eatNUmber;
WhiteNumber=WhiteNumber+eatNUmber+1;
ui->lcdNumber->display(BlackNumber);
ui->lcdNumber_2->display(WhiteNumber);
currentRole=Water;
sec=11;
break;
}
}
}
}
ui->lineEdit->clear();
int add=0;
int i,j;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
if(chessNumber[i][j]!=Air)
add++;
if(add==64)
{
ui->pushButton->setEnabled(true);
if(BlackNumber>WhiteNumber)
QMessageBox::information(NULL,QString("Tip"),QString("The black is win"));
else if(BlackNumber==WhiteNumber)
QMessageBox::information(NULL,QString("Tip"),QString("Draw"));
else
QMessageBox::information(NULL,QString("Tip"),QString("The white is win"));
sec=0;
}
int flag=0;
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
if(judgeRule2(i,j,8,chessNumber,currentRole))
flag=1;
if(add!=64 && flag==0)
{
ui->pushButton->setEnabled(true);
if(BlackNumber>WhiteNumber)
QMessageBox::information(NULL,QString("Tip"),QString("The black is win"));
else if(BlackNumber==WhiteNumber && BlackNumber != 2 && WhiteNumber != 2 )
QMessageBox::information(NULL,QString("Tip"),QString("Draw"));
else if(WhiteNumber>BlackNumber)
QMessageBox::information(NULL,QString("Tip"),QString("The white is win"));
sec=0;
}
this->update();
}
int heibai2::judgeRule(int x, int y, int gridNum, void *chess, GridState currentRole)
{
int dir[8][2]={{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
if( x < 0 || x>= gridNum || y < 0 || y >= gridNum)
// 判断坐标是否合法
return 0;
int temp_x = x, temp_y = y;
int i = 0, eatNum = 0;
typedef int (*p)[gridNum]; //自定义类型
p chessBoard = p(chess); //类型转换 并赋值给新变量 chessBoard
//如果此方格内已有棋子,返回;
if(chessBoard[temp_x][temp_y]!=Air)
return 0;
for(i = 0 ; i <8; i++) //循环判断8个方向
{
temp_x += dir[i][0]; temp_y += dir[i][1];
//准备判断相邻棋子
//如果没有出界,且相邻棋子是对方棋子,才有吃子的可能.
if((temp_x < gridNum && temp_x >=0 && temp_y < gridNum && temp_y >= 0)
&& (chessBoard[temp_x][temp_y] != currentRole)
&& chessBoard[temp_x][temp_y]!=Air)
{
temp_x += dir[i][0]; temp_y += dir[i][1];
//继续判断下一个
//同样判断是否出界
while(temp_x < gridNum && temp_x >=0 && temp_y < gridNum && temp_y >= 0)
{
//遇到空位跳出
if(chessBoard[temp_x][temp_y] == Air)
break;
//找到了自己的棋子,可以吃子
if(chessBoard[temp_x][temp_y] == currentRole)
{
chessBoard[x][y] = currentRole;
//改变(x,y)坐标内的信息
temp_x -= dir[i][0]; temp_y -= dir[i][1];
while((temp_x != x )||(temp_y != y))
//吃子的过程
{
chessBoard[temp_x][temp_y] = currentRole;
temp_x -= dir[i][0]; temp_y -= dir[i][1];
eatNum ++;
}
break;
}
//如果仍然是对方棋子,继续沿此方向查找
temp_x += dir[i][0]; temp_y += dir[i][1];
}
}
temp_x = x; temp_y = y; //一个方向判断结束,准备下一个方向
}
return eatNum;
}
void heibai2::do_timer2()
{
if(sec>0)
{
sec--;
ui->lcdNumber_3->display(sec);
}
else
{
ui->lineEdit->setText("time over");
if(currentRole==Fire)
{
currentRole=Water;
sec=11;
}
else if(currentRole==Water)
{
sec=11;
currentRole=Fire;
}
}
this->update();
}
void heibai2::on_pushButton_3_clicked()
{
ui->lcdNumber->display(0);
timer->stop();
sec=11;
BlackNumber=2;
WhiteNumber=2;
ui->lcdNumber->display(BlackNumber);
ui->lcdNumber_3->display(WhiteNumber);
int i,j;
currentRole=Air;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
chessNumber[i][j]=Air;
}
}
chessNumber[3][3]=Water;
chessNumber[4][4]=Water;
chessNumber[3][4]=Fire;
chessNumber[4][3]=Fire;
}
void heibai2::on_pushButton_4_clicked()
{
currentRole=Water;
BlackNumber=2;
WhiteNumber=2;
}
int heibai2::judgeRule2(int x, int y, int gridNum, void *chess, GridState currentRole)
{
int dir[8][2]={{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
if( x < 0 || x>= gridNum || y < 0 || y >= gridNum)
// 判断坐标是否合法
return 0;
int temp_x = x, temp_y = y;
int i = 0, eatNum = 0;
typedef int (*p)[gridNum]; //自定义类型
p chessBoard = p(chess); //类型转换 并赋值给新变量 chessBoard
//如果此方格内已有棋子,返回;
if(chessBoard[temp_x][temp_y]!=Air)
return 0;
for(i = 0 ; i <8; i++) //循环判断8个方向
{
temp_x += dir[i][0]; temp_y += dir[i][1];
//准备判断相邻棋子
//如果没有出界,且相邻棋子是对方棋子,才有吃子的可能.
if((temp_x < gridNum && temp_x >=0 && temp_y < gridNum && temp_y >= 0)
&& (chessBoard[temp_x][temp_y] != currentRole)
&& chessBoard[temp_x][temp_y]!=Air)
{
temp_x += dir[i][0]; temp_y += dir[i][1];
//继续判断下一个
//同样判断是否出界
while(temp_x < gridNum && temp_x >=0 && temp_y < gridNum && temp_y >= 0)
{
//遇到空位跳出
if(chessBoard[temp_x][temp_y] == Air)
break;
//找到了自己的棋子,可以吃子
if(chessBoard[temp_x][temp_y] == currentRole)
{
//改变(x,y)坐标内的信息
temp_x -= dir[i][0]; temp_y -= dir[i][1];
while((temp_x != x )||(temp_y != y))
//吃子的过程
{
temp_x -= dir[i][0]; temp_y -= dir[i][1];
eatNum ++;
}
break;
}
//如果仍然是对方棋子,继续沿此方向查找
temp_x += dir[i][0]; temp_y += dir[i][1];
}
}
temp_x = x; temp_y = y; //一个方向判断结束,准备下一个方向
}
return eatNum;
}
void heibai2::on_pushButton_5_clicked()
{
QMessageBox::information(NULL,QString("Tip"),QString("Computer is win!"));
}
void heibai2::on_pushButton_6_clicked()
{
BlackNumber=2;
WhiteNumber=2;
ui->lcdNumber->display(BlackNumber);
ui->lcdNumber_2->display(WhiteNumber);
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
chessNumber[i][j]=Air;
}
}
chessNumber[3][3]=Water;
chessNumber[4][4]=Water;
chessNumber[3][4]=Fire;
chessNumber[4][3]=Fire;
}
void heibai2::on_pushButton_7_clicked()
{
QMessageBox::information(NULL,QString("Regulation"),QString("此游戏默认黑棋先下!点击begin开始,如果开始时是白旗,请再次点击一次begin切换到黑棋。棋子:黑白棋棋子每颗由黑白两色组成,一面白,一面黑,共64个(包括棋盘中央的4个)。棋子呈圆饼形。两个玩家各下一面棋子。棋盘:黑白棋棋盘由64(8*8)格的正方格组成,游戏进行时棋子要下在格内。棋盘为8×8的方格布局,开局时在棋盘正中有摆好的四枚棋子,黑白各2枚,交叉放置,由执黑棋的一方先落子,双方交替下子,棋子落在方格内,一局游戏结束后双方更换执子颜色。步合法的棋步包括:在一个空格新落下一个棋子,并且翻转对手一个或多个棋子。下子方式:把自己颜色的棋子放在棋盘的空格上,而当自己放下的棋子在横、竖、斜八个方向内有一个自己的棋子,则被夹在中间的对方棋子全部翻转会成为自己的棋子。夹住的位置上必须全部是对手的棋子,不能有空格。并且,只有在可以翻转棋子的地方才可以下子。一步棋可以在数个方向上翻棋,任何被夹住的棋子都必须被翻转过来,棋手无权选择不去翻某个棋子必须是刚下的子夹对方才能够翻对方的子,因翻转对方的棋子而夹住的子是不能被翻的。翻转棋子时,有一个棋子的翻转动画大概显示1秒左右每次下子最少必须翻转对方一个棋子,若棋局中下子都不能翻转对方棋子,则自动pass轮空,客户端界面气泡提示:您无子可下由对方继续下子,而对方气泡提示:由于对方无子可下,您可继续下子。若二个玩家都不能下子翻转对方棋子,游戏结束。
"));
}
main.cpp
#include "begin.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
begin w;
w.show();
w.setFixedSize(1366,768);
return a.exec();
}