-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouseEvent.cpp
More file actions
117 lines (100 loc) · 3.11 KB
/
mouseEvent.cpp
File metadata and controls
117 lines (100 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "mainwindow.h"
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
qDebug() << event->pos() << endl;
if(gameover == false && isInRange(event->x(), event->y())){
convertPos(event->x(), event->y());
// start timer
timer->start(1000);
time.start();
showTime();
connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
if(editMode == false){
switch(player){
case 1:{ // red: player 1
if(board[xPos][yPos] >= 16){
hidePath();
this->clickedChess = board[xPos][yPos];
showPath();
}
else if(path[xPos][yPos]->isVisible()){
moveChess(xPos, yPos);
hidePath();
player = 2;
checkWin();
showPlayer();
player2_label->setFrameStyle(QFrame::WinPanel | QFrame::Raised);
player1_label->setFrameStyle(QFrame::NoFrame);
}
else{
qDebug() << "Wrong Place" << endl;
}
break;
}
case 2:{ // black: player 2
if(board[xPos][yPos] < 16 && board[xPos][yPos] != NAN){
hidePath();
this->clickedChess = board[xPos][yPos];
showPath();
}
else if(path[xPos][yPos]->isVisible()){
moveChess(xPos, yPos);
hidePath();
player = 1;
checkWin();
showPlayer();
player1_label->setFrameStyle(QFrame::WinPanel | QFrame::Raised);
player2_label->setFrameStyle(QFrame::NoFrame);
}
else{
qDebug() << "Wrong Place" << endl;
}
break;
}
}
}
else if(editMode == true){
if(board[xPos][yPos] != NAN){
this->clickedChess = board[xPos][yPos];
showPath();
}
else if(path[xPos][yPos]->isVisible()){
moveChess(xPos, yPos);
hidePath();
}
}
}
else if(gameover == true){
qDebug() << "GAME ENDS" << endl;
}
else{
qDebug() << "Out of Range" << endl;
}
}
}
void MainWindow::convertPos(int x, int y){
for(int i = 0; i < 9; i++){
for(int j = 0; j < 10; j++){
if(x >= X[i] - chessSize/2 && x <= X[i] + chessSize/2 && y >= Y[j] - chessSize/2 && y <= Y[j] + chessSize/2){
xPos = i;
yPos = j;
return;
}
}
}
}
bool MainWindow::isInRange(int x, int y){
for(int i = 0; i < 9; i++){
for(int j = 0; j < 10; j++){
// in range
if(x >= X[i] - chessSize/2 && x <= X[i] + chessSize/2 && y >= Y[j] - chessSize/2 && y <= Y[j] + chessSize/2){
xPos = i;
yPos = j;
return true;
}
}
}
// not in range
return false;
}