Skip to content

Commit

Permalink
Add mines game
Browse files Browse the repository at this point in the history
  • Loading branch information
bigbruno committed May 24, 2024
1 parent f4732ce commit c04e2da
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 1 deletion.
40 changes: 40 additions & 0 deletions usr/share/calamares/branding/biglinux/Cell.qml
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"
}
}
}
117 changes: 117 additions & 0 deletions usr/share/calamares/branding/biglinux/MinesModel.qml
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()
}
2 changes: 1 addition & 1 deletion usr/share/calamares/branding/biglinux/branding.desc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ images:
productIcon: "/usr/share/icons/hicolor/scalable/apps/icon-big-hd-install.svg"
productWelcome: "big-welcome.svg"

slideshow: "show.qml"
slideshow: "mines.qml"
slideshowAPI: 1

style:
Expand Down
63 changes: 63 additions & 0 deletions usr/share/calamares/branding/biglinux/mines.qml
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
}
}
}

0 comments on commit c04e2da

Please sign in to comment.