-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import QtQuick 2.15 | ||
|
||
Item { | ||
property int row | ||
property int column | ||
property bool isMine | ||
property bool isOpened | ||
property bool isFlagged | ||
property int neighborMines | ||
signal leftClick | ||
signal rightClick | ||
|
||
Rectangle { | ||
width: parent.width | ||
height: parent.height | ||
border.color: "black" | ||
color: isOpened ? "lightgrey" : (isFlagged ? "white" : "grey") | ||
|
||
MouseArea { | ||
acceptedButtons: Qt.LeftButton | Qt.RightButton | ||
anchors.fill: parent | ||
onClicked: { | ||
if (mouse.button === Qt.LeftButton && !isFlagged) { | ||
leftClick() | ||
} | ||
if (mouse.button === Qt.RightButton) { | ||
rightClick() | ||
} | ||
} | ||
|
||
} | ||
|
||
Text { | ||
anchors.centerIn: parent | ||
text: isOpened ? (isMine ? "💣" : (neighborMines > 0 ? neighborMines : "")) : (isFlagged ? "🚩" : "") | ||
font.pointSize: 20 | ||
color: isMine ? "red" : "black" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import QtQuick 2.15 | ||
|
||
ListModel { | ||
id: minesModel | ||
|
||
property int rows: 15 | ||
property int columns: 20 | ||
signal gameOver() | ||
|
||
function initialize() { | ||
clearModel() | ||
generateMines() | ||
calculateNeighborMines() | ||
} | ||
|
||
function clearModel() { | ||
clear() | ||
for (var row = 0; row < rows; ++row) { | ||
for (var col = 0; col < columns; ++col) { | ||
append({ | ||
row: row, | ||
column: col, | ||
isMine: false, | ||
isOpened: false, | ||
isFlagged: false, | ||
neighborMines: 0 | ||
}) | ||
} | ||
} | ||
} | ||
|
||
function generateMines() { | ||
var mineCount = 40 // Número de minas | ||
while (mineCount > 0) { | ||
var row = Math.floor(Math.random() * rows) | ||
var col = Math.floor(Math.random() * columns) | ||
var index = row * columns + col | ||
if (!get(index).isMine) { | ||
get(index).isMine = true | ||
--mineCount | ||
} | ||
} | ||
} | ||
|
||
function calculateNeighborMines() { | ||
for (var row = 0; row < rows; ++row) { | ||
for (var col = 0; col < columns; ++col) { | ||
var index = row * columns + col | ||
if (!get(index).isMine) { | ||
var count = 0 | ||
for (var i = -1; i <= 1; ++i) { | ||
for (var j = -1; j <= 1; ++j) { | ||
var r = row + i | ||
var c = col + j | ||
if (r >= 0 && r < rows && c >= 0 && c < columns) { | ||
var neighborIndex = r * columns + c | ||
if (get(neighborIndex).isMine) { | ||
++count | ||
} | ||
} | ||
} | ||
} | ||
get(index).neighborMines = count | ||
} | ||
} | ||
} | ||
} | ||
|
||
function openCell(row, column) { | ||
var index = row * columns + column | ||
var cell = get(index) | ||
if (cell.isOpened || cell.isFlagged) { | ||
return | ||
} | ||
|
||
var stack = [] | ||
stack.push(cell) | ||
|
||
while (stack.length > 0) { | ||
var currentCell = stack.pop() | ||
currentCell.isOpened = true | ||
|
||
if (currentCell.isMine) { | ||
gameOver() | ||
return | ||
} | ||
|
||
if (currentCell.neighborMines === 0) { | ||
for (var i = -1; i <= 1; ++i) { | ||
for (var j = -1; j <= 1; ++j) { | ||
var r = currentCell.row + i | ||
var c = currentCell.column + j | ||
if (r >= 0 && r < rows && c >= 0 && c < columns) { | ||
var neighborIndex = r * columns + c | ||
var neighborCell = get(neighborIndex) | ||
if (!neighborCell.isOpened && !neighborCell.isMine) { | ||
stack.push(neighborCell) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function toggleFlag(row, column) { | ||
var index = row * columns + column | ||
var cell = get(index) | ||
if (!cell.isOpened) { | ||
cell.isFlagged = !cell.isFlagged | ||
// Emitir uma mudança no modelo para atualizar a exibição | ||
set(index, cell) | ||
} | ||
} | ||
|
||
Component.onCompleted: initialize() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import QtQuick 2.15 | ||
import QtQuick.Window 2.15 | ||
import QtQuick.Controls 2.15 | ||
|
||
Window { | ||
visible: true | ||
width: 800 | ||
height: 600 | ||
title: "Mines" | ||
|
||
MinesModel { | ||
id: minesModel | ||
rows: 15 | ||
columns: 20 | ||
onGameOver: { | ||
overlay.visible = true | ||
restartButton.visible = true | ||
} | ||
} | ||
|
||
GridView { | ||
id: gridView | ||
anchors.fill: parent | ||
model: minesModel | ||
cellWidth: width / minesModel.columns | ||
cellHeight: height / minesModel.rows | ||
anchors.margins: 5 | ||
|
||
delegate: Cell { | ||
width: gridView.cellWidth | ||
height: gridView.cellHeight | ||
row: model.row | ||
column: model.column | ||
isMine: model.isMine | ||
isOpened: model.isOpened | ||
isFlagged: model.isFlagged | ||
neighborMines: model.neighborMines | ||
onLeftClick: minesModel.openCell(row, column) | ||
onRightClick: minesModel.toggleFlag(row, column) | ||
} | ||
} | ||
|
||
Rectangle { | ||
id: overlay | ||
anchors.fill: parent | ||
color: "black" | ||
opacity: 0.7 | ||
visible: false | ||
} | ||
|
||
Button { | ||
id: restartButton | ||
text: "Restart" | ||
anchors.horizontalCenter: parent.horizontalCenter | ||
anchors.verticalCenter: parent.verticalCenter | ||
visible: false | ||
onClicked: { | ||
minesModel.initialize() | ||
overlay.visible = false | ||
restartButton.visible = false | ||
} | ||
} | ||
} |
File renamed without changes.