Skip to content

Commit a4b08c3

Browse files
feat: main
1 parent c00b906 commit a4b08c3

File tree

3 files changed

+61
-17
lines changed

3 files changed

+61
-17
lines changed

cleared_lines.log

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Lines Cleared: 1
2+
Lines Cleared: 2
3+
Lines Cleared: 1
4+
Lines Cleared: 1
5+
Lines Cleared: 2
6+
Lines Cleared: 1
7+
Lines Cleared: 1
8+
Lines Cleared: 1

pkg/tetris/action.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ func (a action) String() string {
7676
return actionToStrMap[a]
7777
}
7878

79+
func (a action) GetLinesCleared() int {
80+
switch a {
81+
case actionSingle:
82+
return 1
83+
case actionDouble:
84+
return 2
85+
case actionTriple:
86+
return 3
87+
case actionTetris:
88+
return 4
89+
default:
90+
return 0
91+
}
92+
}
93+
7994
// ParseAction attempts to parse the given value into Action.
8095
// It supports string, fmt.Stringer, int, int64, and int32.
8196
// If the value is not a valid Action or the value is not a supported type, it will

pkg/tetris/scoring.go

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tetris
22

33
import (
44
"fmt"
5+
"os"
56
)
67

78
// Scoring is a scoring system for Tetris.
@@ -20,7 +21,8 @@ type Scoring struct {
2021
total int
2122
backToBack bool
2223

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
2426
}
2527

2628
// LastClearedLines returns the number of lines cleared in the most recent action.
@@ -90,6 +92,9 @@ func (s *Scoring) AddHardDrop(lines int) {
9092
// ProcessAction processes an action and updates the score, lines cleared, level, etc.
9193
// The returned boolean indicates if the game should end.
9294
// 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+
9398
func (s *Scoring) ProcessAction(a Action) (bool, error) {
9499
if a == Actions.None {
95100
return false, nil
@@ -113,36 +118,52 @@ func (s *Scoring) ProcessAction(a Action) (bool, error) {
113118
}
114119

115120
s.total += int(points+backToBack) * s.level
116-
linesCleared := int((points + backToBack) / 100)
117121

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
119135
s.lines += linesCleared
120136

121-
// if max lines enabled, and max lines reached
137+
// Check for max lines
122138
if s.maxLines > 0 && s.lines >= s.maxLines {
123139
s.lines = s.maxLines
124-
125140
if s.endOnMaxLines {
126141
return true, nil
127142
}
128143
}
129144

130-
// while increase level enabled, and the next level was reached
145+
// Level up logic
131146
for s.increaseLevel && s.lines >= s.level*5 {
132147
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
137154
}
138-
139-
// if max level reached
140-
s.level = s.maxLevel
141-
if s.endOnMaxLevel {
142-
return true, nil
143-
}
144-
break
145155
}
146156

147157
return false, nil
148158
}
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

Comments
 (0)