-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.swift
213 lines (187 loc) · 6.15 KB
/
main.swift
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import Darwin
import Foundation
// Function to enable raw mode
func enableRawMode() {
var term = termios()
tcgetattr(STDIN_FILENO, &term)
term.c_lflag &= ~(UInt(ICANON | ECHO))
tcsetattr(STDIN_FILENO, TCSANOW, &term)
}
// Function to disable raw mode
func disableRawMode() {
var term = termios()
tcgetattr(STDIN_FILENO, &term)
term.c_lflag |= UInt(ICANON | ECHO)
tcsetattr(STDIN_FILENO, TCSANOW, &term)
}
// Enable raw mode at the start
enableRawMode()
defer { disableRawMode() } // Ensure raw mode is disabled when the program exits
let rows = 20
let columns = 10
var grid = Array(repeating: Array(repeating: 0, count: columns), count: rows)
let shapes = [
[[1, 1, 1, 1]], // I
[[1, 1], [1, 1]], // O
[[1, 1, 1], [0, 1, 0]], // T
[[1, 1, 0], [0, 1, 1]], // S
[[0, 1, 1], [1, 1, 0]], // Z
]
let shapeQueueLength = 3
var shapeQueue: [[[Int]]] = []
fillShapeQueue()
func fillShapeQueue() {
while shapeQueue.count < shapeQueueLength {
shapeQueue.append(shapes.randomElement()!)
}
}
struct Piece {
var shape: [[Int]]
var x: Int
var y: Int
mutating func rotate() {
shape = shape[0].indices.map { i in
shape.reversed().map { $0[i] }
}
}
}
var currentPiece = Piece(shape: shapes.randomElement()!, x: columns / 2 - 1, y: 0)
// Function to render the shape queue
func renderShapeQueue() {
print("Next:")
for shape in shapeQueue {
for row in shape {
for cell in row {
print(cell == 1 ? "■" : "·", terminator: "")
}
print("")
}
print("")
}
}
func renderGrid() {
print("\u{001B}[2J") // Clear screen
// Display the grid with the label for the shape queue
for row in -1..<rows { // Start from -1 to add the label before the grid
if row == -1 {
// Print the label above the queue
let queueLabel = (0..<columns).map { _ in " " }.joined()
print(queueLabel + " Next shapes:", terminator: "")
} else {
// Render each row of the grid
for col in 0..<columns {
if grid[row][col] == 1 {
print("█", terminator: "")
} else if isPieceAt(row: row, col: col) {
print("■", terminator: "")
} else {
print("·", terminator: "")
}
}
// Render a gap, then the queue
print(" ", terminator: "")
// Display the upcoming shape for this row
if row < shapeQueueLength * 4 {
let shapeIndex = row / 4
let shapeRow = row % 4
if shapeRow < shapeQueue[shapeIndex].count {
for cell in shapeQueue[shapeIndex][shapeRow] {
print(cell == 1 ? "■" : " ", terminator: "")
}
} else {
print(" ", terminator: "")
}
}
}
print("") // Move to the next line
}
}
func isPieceAt(row: Int, col: Int) -> Bool {
let pieceHeight = currentPiece.shape.count
let pieceWidth = currentPiece.shape[0].count
let relativeRow = row - currentPiece.y
let relativeCol = col - currentPiece.x
return relativeRow >= 0 && relativeRow < pieceHeight && relativeCol >= 0
&& relativeCol < pieceWidth && currentPiece.shape[relativeRow][relativeCol] == 1
}
func canMove(_ piece: Piece, dx: Int, dy: Int) -> Bool {
for row in 0..<piece.shape.count {
for col in 0..<piece.shape[row].count {
if piece.shape[row][col] == 1 {
let newX = piece.x + col + dx
let newY = piece.y + row + dy
if newX < 0 || newX >= columns || newY >= rows
|| (newY >= 0 && grid[newY][newX] == 1)
{
return false
}
}
}
}
return true
}
func placePiece(_ piece: Piece) {
for row in 0..<piece.shape.count {
for col in 0..<piece.shape[row].count {
if piece.shape[row][col] == 1 {
grid[piece.y + row][piece.x + col] = 1
}
}
}
}
func clearLines() {
grid = grid.filter { row in row.contains(0) }
let cleared = rows - grid.count
grid = Array(repeating: Array(repeating: 0, count: columns), count: cleared) + grid
}
var tickCount = 0
let dropInterval = 1
// Game timer for automatic piece drop
let gameTimer = DispatchSource.makeTimerSource()
gameTimer.schedule(deadline: .now(), repeating: .milliseconds(500))
gameTimer.setEventHandler {
tickCount += 1
if tickCount >= dropInterval {
tickCount = 0
if canMove(currentPiece, dx: 0, dy: 1) {
currentPiece.y += 1
} else {
placePiece(currentPiece)
clearLines()
currentPiece = Piece(shape: shapeQueue.removeFirst(), x: columns / 2 - 1, y: 0)
fillShapeQueue()
if !canMove(currentPiece, dx: 0, dy: 0) {
print("Game Over")
disableRawMode()
exit(0)
}
}
}
renderGrid()
}
//input handler shenanigans, can't use readLine() because it blocks the main thread
//we need to read input asynchronously to keep the game timer running
//so we use FileHandle.standardInput.readabilityHandler to read input from the terminal
//and update the game state accordingly
//weeeeeeird stuff
FileHandle.standardInput.readabilityHandler = { handle in
let input = handle.availableData
guard let key = String(data: input, encoding: .utf8)?.first else { return }
switch key {
case "a":
if canMove(currentPiece, dx: -1, dy: 0) { currentPiece.x -= 1 }
case "d":
if canMove(currentPiece, dx: 1, dy: 0) { currentPiece.x += 1 }
case "s":
if canMove(currentPiece, dx: 0, dy: 1) { currentPiece.y += 1 }
case "w":
var rotatedPiece = currentPiece
rotatedPiece.rotate()
if canMove(rotatedPiece, dx: 0, dy: 0) { currentPiece.rotate() }
default:
break
}
}
// Start the game timer and keep the main thread alive
gameTimer.resume()
RunLoop.main.run()