@@ -2,6 +2,7 @@ package tetris
2
2
3
3
import (
4
4
"fmt"
5
+ "os"
5
6
)
6
7
7
8
// Scoring is a scoring system for Tetris.
@@ -20,7 +21,8 @@ type Scoring struct {
20
21
total int
21
22
backToBack bool
22
23
23
- lastCleared int // Stores the number of lines cleared in the last action
24
+ lastCleared int // Stores the number of lines cleared in the last action
25
+ playerName string // ✅ New: Store player's name
24
26
}
25
27
26
28
// LastClearedLines returns the number of lines cleared in the most recent action.
@@ -90,6 +92,9 @@ func (s *Scoring) AddHardDrop(lines int) {
90
92
// ProcessAction processes an action and updates the score, lines cleared, level, etc.
91
93
// The returned boolean indicates if the game should end.
92
94
// ProcessAction processes an action and updates the score, lines cleared, level, etc.
95
+ // Define the file path where we store cleared lines
96
+ const clearedLinesFile = "cleared_lines.log"
97
+
93
98
func (s * Scoring ) ProcessAction (a Action ) (bool , error ) {
94
99
if a == Actions .None {
95
100
return false , nil
@@ -113,36 +118,52 @@ func (s *Scoring) ProcessAction(a Action) (bool, error) {
113
118
}
114
119
115
120
s .total += int (points + backToBack ) * s .level
116
- linesCleared := int ((points + backToBack ) / 100 )
117
121
118
- s .lastCleared = linesCleared // Store last cleared lines
122
+ // Get cleared lines
123
+ linesCleared := a .GetLinesCleared ()
124
+
125
+ // ✅ Write to file if cleared lines change and it's not 0
126
+ if linesCleared > 0 && linesCleared != s .lastCleared {
127
+ err := appendToFile (clearedLinesFile , fmt .Sprintf ("Lines Cleared: %d\n " , linesCleared ))
128
+ if err != nil {
129
+ fmt .Println ("⚠️ Error writing to file:" , err )
130
+ }
131
+ }
132
+
133
+ // Update last cleared lines
134
+ s .lastCleared = linesCleared
119
135
s .lines += linesCleared
120
136
121
- // if max lines enabled, and max lines reached
137
+ // Check for max lines
122
138
if s .maxLines > 0 && s .lines >= s .maxLines {
123
139
s .lines = s .maxLines
124
-
125
140
if s .endOnMaxLines {
126
141
return true , nil
127
142
}
128
143
}
129
144
130
- // while increase level enabled, and the next level was reached
145
+ // Level up logic
131
146
for s .increaseLevel && s .lines >= s .level * 5 {
132
147
s .level ++
133
-
134
- // if no max level, or max level not reached
135
- if s .maxLevel <= 0 || s .level < s .maxLevel {
136
- continue
148
+ if s .maxLevel > 0 && s .level >= s .maxLevel {
149
+ s .level = s .maxLevel
150
+ if s .endOnMaxLevel {
151
+ return true , nil
152
+ }
153
+ break
137
154
}
138
-
139
- // if max level reached
140
- s .level = s .maxLevel
141
- if s .endOnMaxLevel {
142
- return true , nil
143
- }
144
- break
145
155
}
146
156
147
157
return false , nil
148
158
}
159
+
160
+ func appendToFile (filename , content string ) error {
161
+ file , err := os .OpenFile (filename , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
162
+ if err != nil {
163
+ return err
164
+ }
165
+ defer file .Close ()
166
+
167
+ _ , err = file .WriteString (content )
168
+ return err
169
+ }
0 commit comments