2018-02-14 14:57:02 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-06-01 10:16:36 -05:00
|
|
|
"log"
|
2018-02-14 14:57:02 -06:00
|
|
|
"time"
|
|
|
|
|
2019-12-01 15:03:27 -06:00
|
|
|
"github.com/gdamore/tcell"
|
2018-02-14 14:57:02 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-01-09 20:30:45 -06:00
|
|
|
boardXOffset = 4
|
|
|
|
boardYOffset = 2
|
|
|
|
aiTickDivider = 8
|
|
|
|
rankingFileName = "/go-tetris.db"
|
|
|
|
settingsFileName = "/go-tetris.json"
|
2018-02-14 14:57:02 -06:00
|
|
|
|
2018-10-02 20:21:08 -05:00
|
|
|
// MinoPreview is for the preview mino
|
2018-02-14 14:57:02 -06:00
|
|
|
MinoPreview MinoType = iota
|
2018-10-02 20:21:08 -05:00
|
|
|
// MinoCurrent is for the current mino
|
|
|
|
MinoCurrent = iota
|
|
|
|
// MinoDrop is for the drop mino
|
|
|
|
MinoDrop = iota
|
2019-12-01 15:03:27 -06:00
|
|
|
|
|
|
|
colorBlank = tcell.ColorBlack
|
|
|
|
colorCyan = tcell.ColorAqua // I
|
|
|
|
colorBlue = tcell.ColorBlue // J
|
|
|
|
colorWhite = tcell.ColorWhite // L
|
|
|
|
colorYellow = tcell.ColorYellow // O
|
|
|
|
colorGreen = tcell.ColorLime // S
|
|
|
|
colorMagenta = tcell.ColorFuchsia // T
|
|
|
|
colorRed = tcell.ColorRed // Z
|
|
|
|
|
|
|
|
engineModeRun engineMode = iota
|
|
|
|
engineModeRunWithAI
|
|
|
|
engineModeStopped
|
|
|
|
engineModeGameOver
|
|
|
|
engineModePaused
|
|
|
|
engineModePreview
|
|
|
|
engineModeEdit
|
2018-02-14 14:57:02 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2019-12-01 15:03:27 -06:00
|
|
|
engineMode int
|
|
|
|
|
2018-10-02 20:21:08 -05:00
|
|
|
// MinoType is the type of mino
|
|
|
|
MinoType int
|
|
|
|
// MinoBlocks is the blocks of the mino
|
2019-12-01 15:03:27 -06:00
|
|
|
MinoBlocks [][]tcell.Color
|
2018-10-02 20:21:08 -05:00
|
|
|
// MinoRotation is the rotation of the mino
|
2018-02-14 14:57:02 -06:00
|
|
|
MinoRotation [4]MinoBlocks
|
|
|
|
|
2018-10-02 20:21:08 -05:00
|
|
|
// Mino is a mino
|
2018-02-14 14:57:02 -06:00
|
|
|
Mino struct {
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
length int
|
|
|
|
rotation int
|
|
|
|
minoRotation MinoRotation
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:21:08 -05:00
|
|
|
// Minos is a bag of minos
|
2018-02-14 14:57:02 -06:00
|
|
|
Minos struct {
|
|
|
|
minoBag [7]MinoRotation
|
|
|
|
bagRand []int
|
|
|
|
bagIndex int
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:21:08 -05:00
|
|
|
// Board is the Tetris board
|
2018-02-14 14:57:02 -06:00
|
|
|
Board struct {
|
|
|
|
boardsIndex int
|
|
|
|
width int
|
|
|
|
height int
|
2019-12-01 15:03:27 -06:00
|
|
|
colors [][]tcell.Color
|
2018-02-14 14:57:02 -06:00
|
|
|
rotation [][]int
|
|
|
|
previewMino *Mino
|
|
|
|
currentMino *Mino
|
|
|
|
dropDistance int
|
2019-01-11 20:39:41 -06:00
|
|
|
fullLinesY []bool
|
2018-02-14 14:57:02 -06:00
|
|
|
}
|
|
|
|
|
2019-01-09 20:30:45 -06:00
|
|
|
// Boards holds all the boards
|
2018-02-14 14:57:02 -06:00
|
|
|
Boards struct {
|
2019-01-09 20:30:45 -06:00
|
|
|
name string
|
2019-12-01 15:03:27 -06:00
|
|
|
colors [][]tcell.Color
|
2018-02-14 14:57:02 -06:00
|
|
|
rotation [][]int
|
|
|
|
}
|
|
|
|
|
2019-01-09 20:30:45 -06:00
|
|
|
// BoardsJSON is for JSON format of boards
|
|
|
|
BoardsJSON struct {
|
|
|
|
Name string
|
|
|
|
Mino [][]string
|
|
|
|
Rotation [][]int
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:21:08 -05:00
|
|
|
// View is the display engine
|
2018-02-14 14:57:02 -06:00
|
|
|
View struct {
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:21:08 -05:00
|
|
|
// Ai is the AI engine
|
2018-02-14 14:57:02 -06:00
|
|
|
Ai struct {
|
|
|
|
queue *[]rune
|
|
|
|
newQueue *[]rune
|
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:21:08 -05:00
|
|
|
// Ranking holds the ranking scores
|
2018-02-14 14:57:02 -06:00
|
|
|
Ranking struct {
|
|
|
|
scores []uint64
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:21:08 -05:00
|
|
|
// Engine is the Tetirs game engine
|
2018-02-14 14:57:02 -06:00
|
|
|
Engine struct {
|
|
|
|
stopped bool
|
|
|
|
chanStop chan struct{}
|
2019-12-01 15:03:27 -06:00
|
|
|
chanEventKey chan *tcell.EventKey
|
2018-02-14 14:57:02 -06:00
|
|
|
ranking *Ranking
|
|
|
|
timer *time.Timer
|
|
|
|
tickTime time.Duration
|
2019-12-01 15:03:27 -06:00
|
|
|
mode engineMode
|
2018-02-14 14:57:02 -06:00
|
|
|
score int
|
|
|
|
level int
|
|
|
|
deleteLines int
|
|
|
|
ai *Ai
|
|
|
|
aiEnabled bool
|
|
|
|
aiTimer *time.Timer
|
2019-01-09 20:30:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Edit is the board edit mode
|
|
|
|
Edit struct {
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
moved bool
|
|
|
|
boardSize bool
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
saved bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settings is the JSON load/save file
|
|
|
|
Settings struct {
|
|
|
|
Boards []BoardsJSON
|
2018-02-14 14:57:02 -06:00
|
|
|
}
|
2019-12-01 15:03:27 -06:00
|
|
|
|
|
|
|
// EventGame is an game event
|
|
|
|
EventGame struct {
|
|
|
|
when time.Time
|
|
|
|
}
|
2018-02-14 14:57:02 -06:00
|
|
|
)
|
|
|
|
|
2019-12-01 15:03:27 -06:00
|
|
|
// When returns event when
|
|
|
|
func (EventGame *EventGame) When() time.Time {
|
|
|
|
return EventGame.when
|
|
|
|
}
|
|
|
|
|
2018-02-14 14:57:02 -06:00
|
|
|
var (
|
|
|
|
baseDir string
|
2018-06-01 10:16:36 -05:00
|
|
|
logger *log.Logger
|
2019-12-01 15:03:27 -06:00
|
|
|
screen tcell.Screen
|
2018-02-14 14:57:02 -06:00
|
|
|
minos *Minos
|
|
|
|
board *Board
|
|
|
|
view *View
|
|
|
|
engine *Engine
|
2019-01-09 20:30:45 -06:00
|
|
|
edit *Edit
|
|
|
|
|
|
|
|
boards []Boards
|
|
|
|
numInternalBoards int
|
2018-02-14 14:57:02 -06:00
|
|
|
)
|