我已经多次了,我在移动时得到一个额外的检查器(-1,-1)。在左边的白色检查器上。
不要介意移动另一个检查器,将其用于下一步。
我无法找到我在主板上设置额外检查器的位置[] []
所以,如果你能在任何人的帮助下,我们将非常感激。
/**
* \file CheckerWindow.cpp
* \brief Checker window
*/
#include "checkerwindow.h"
#include "ui_checkerwindow.h"
#include <QtGui>
#include <iostream>
/*!
*Game2
*\brief Checker
*/
CheckerWindow::CheckerWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::CheckerWindow)
{
ui->setupUi(this);
this->setFixedSize(500,650);
this->setWindowTitle("4x4 Checker");
// board slots
// row1
slot[0] = QRect(50,100,100,100);
slot[1] = QRect(150,100,100,100);
slot[2] = QRect(250,100,100,100);
slot[3] = QRect(350,100,100,100);
// row2
slot[4] = QRect(50,200,100,100);
slot[5] = QRect(150,200,100,100);
slot[6] = QRect(250,200,100,100);
slot[7] = QRect(350,200,100,100);
// row3
slot[8] = QRect(50,300,100,100);
slot[9] = QRect(150,300,100,100);
slot[10] = QRect(250,300,100,100);
slot[11] = QRect(350,300,100,100);
// row4
slot[12] = QRect(50,400,100,100);
slot[13] = QRect(150,400,100,100);
slot[14] = QRect(250,400,100,100);
slot[15] = QRect(350,400,100,100);
indexMoving=0;
isMoving=false;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = 0;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
color[i][j] = 0;
}
}
board[0][0]=1;
board[0][2]=1;
board[3][1]=2;
board[3][3]=2;
}
CheckerWindow::~CheckerWindow()
{
delete ui;
}
void CheckerWindow::paintEvent(QPaintEvent* e) {
QPainter* painter = new QPainter(this);
painter->fillRect(this->rect(),QColor("white"));
painter->setPen(QColor("black"));
int s=0;
painter->drawRects(slot, 16);
//to draw the background
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
int slotN = i*4 + j;
if (s == 1) {
painter->drawPixmap(slot[slotN].x(), slot[slotN].y(), QPixmap("../Images/wood1.png") );
s=0;
} else{
painter->drawPixmap(slot[slotN].x(), slot[slotN].y(), QPixmap("../Images/wood2.png") );
s=1;
}
}
s=1;
if(i==1) {
s=0;
}
}
s=0;
//add the chekers on the board
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
int slotN = i*4 + j;
if(color[i][j] == 1) {
qDebug("coloring");
painter->drawPixmap(slot[slotN].x(), slot[slotN].y(), QPixmap("../Images/green.jpeg") );
color[i][j]=0;
isMoving=true;
}
if (board[i][j] == 1) {
painter->drawPixmap(slot[slotN].x(), slot[slotN].y(), QPixmap("../Images/checker1.png") );
}
else if (board[i][j] == 2){
painter->drawPixmap(slot[slotN].x(), slot[slotN].y(), QPixmap("../Images/checker2.png") );
}
}
}
}
/**
* \brief This mouseEvent() function
* will move the ball downwards by 1, at the same time it should check
* \param The MouseEvent is used to know where the mouse is pressed
*/
void CheckerWindow::mousePressEvent(QMouseEvent *e) {
for (int i = 0; i < 16; i++) {
if (slot[i].contains(e->pos())) {
qDebug("Inside e->pos");
int row = i/4;
int col = i%4;
if(board[row][col]==1 || board[row][col]==2) {
indexMoving=i;
color[row][col]=1;
isMoving=true;
repaint();
qDebug("color");
}
else if(isMoving) {
qDebug("move");
int rowOld = indexMoving/4;
int colOld = indexMoving%4;
if(board[rowOld][colOld]==1) {
if((row == rowOld+1) && (col == colOld+1) ) {
qDebug("IN +1 +1");
board[row][col]=1;
board[rowOld][colOld]=0;
color[rowOld][colOld]=0;
repaint();
}
else if((row == rowOld+1) && (col == colOld-1) ) {
qDebug("IN +1 -1");
board[row][col]=1;
board[rowOld][colOld]=0;
color[rowOld][colOld]=0;
repaint();
}
else if(board[rowOld+1][colOld+1]==2) {
qDebug("IN +1 +1 moving over another");
board[row][col]=1;
board[rowOld][colOld]=3;
repaint();
board[rowOld][colOld]=0;
board[rowOld+1][colOld+1]=3;
repaint();
board[rowOld+1][colOld+1]=0;
}
else if(board[rowOld+1][colOld-1]==2) {
qDebug("IN +1 -1 moving over another");
board[row][col]=1;
board[rowOld][colOld]=3;
repaint();
board[rowOld][colOld]=0;
board[rowOld+1][colOld-1]=3;
repaint();
board[rowOld+1][colOld-1]=0;
}
}
else if(board[rowOld][colOld]==2) {
if((row == rowOld-1) && (col == colOld-1) ) {
qDebug("IN -1 -1");
board[row][col]=2;
board[rowOld][colOld]=0;
color[rowOld][colOld]=0;
repaint();
}
else if((row == rowOld-1) && (col == colOld+1) ) {
qDebug("IN -1 +1");
board[row][col]=2;
board[rowOld][colOld]=0;
color[rowOld][colOld]=0;
repaint();
}
else if(board[rowOld-1][colOld-1]==1) {
qDebug("IN +1 +1 moving over another");
board[row][col]=2;
board[rowOld][colOld]=3;
repaint();
board[rowOld][colOld]=0;
board[rowOld-1][colOld-1]=3;
repaint();
board[rowOld-1][colOld-1]=0;
}
else if(board[rowOld-1][colOld+1]==1) {
qDebug("IN +1 -1 moving over another");
board[row][col]=2;
board[rowOld][colOld]=3;
repaint();
board[rowOld][colOld]=0;
board[rowOld-1][colOld+1]=3;
repaint();
board[rowOld-1][colOld+1]=0;
}
}
//qDebug("Set is Moving False");
isMoving=false;
indexMoving=0;
}
}
else {
//qDebug("In the else");
//repaint();
}
}
}