Skip to content

Commit

Permalink
Throws when document cannot be properly read instead of crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-bovet committed Aug 1, 2021
1 parent 9052424 commit 984cf71
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 22 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Enjoy!

## What remains to be done

- Filter saving PGN to 80 characters per line
- Finish Zobrist hashing unit test with all the scenarios: castling, attack, etc.
- Ensure that when a Game is copied, the history is also copied, not just referenced because it will get messed up (see FEngineInfo)
- Better handling of openings when a FEN is passed (and the board is not in the expected state for the opening)
Expand All @@ -31,3 +32,10 @@ Enjoy!
- Add more openings
- Transposition table [here](http://www.chessbin.com/post/Transposition-Table-and-Zobrist-Hashing) and [here](https://chessprogramming.wikispaces.com/Transposition+Table)
- [Optimization](https://people.cs.clemson.edu/~dhouse/courses/405/papers/optimize.pdf)

## Limitations

- unable to have read-only document (it always want to write it back)
- background of List is not transparent in Light Mode (it is white)
- unable to create rich text with support of tapping in it (like to select precisely the white or black move)

2 changes: 1 addition & 1 deletion Shared/BChessUIApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
@main
struct BChessUIApp: App {
var body: some Scene {
DocumentGroup(newDocument: ChessDocument()) { file in
DocumentGroup(newDocument: try! ChessDocument()) { file in
ContentView(document: file.$document)
}
#if os(macOS)
Expand Down
10 changes: 6 additions & 4 deletions Shared/ChessDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ struct ChessDocument: FileDocument {
return PiecesFactory().pieces(forState: engine.state)
}

init(pgn: String = startPosPGN, white: GamePlayer? = nil, black: GamePlayer? = nil, rotated: Bool = false, mode: GameMode = GameMode(value: .play)) {
init(pgn: String = startPosPGN, white: GamePlayer? = nil, black: GamePlayer? = nil, rotated: Bool = false, mode: GameMode = GameMode(value: .play)) throws {
self.pgn = pgn
self.engine.loadAllGames(pgn)
guard self.engine.loadAllGames(pgn) else {
throw CocoaError(.fileReadCorruptFile)
}
self.whitePlayer = white ?? GamePlayer(name: "", computer: false, level: 0)
self.blackPlayer = black ?? GamePlayer(name: "", computer: true, level: 0)
self.rotated = rotated
Expand Down Expand Up @@ -146,9 +148,9 @@ struct ChessDocument: FileDocument {
let decoder = JSONDecoder()
let state = try decoder.decode(GameState.self, from: data)

self.init(pgn: state.pgn, white: state.white, black: state.black, rotated: state.rotated)
try self.init(pgn: state.pgn, white: state.white, black: state.black, rotated: state.rotated)
} else if configuration.contentType == .pgn {
self.init(pgn: String(decoding: data, as: UTF8.self),
try self.init(pgn: String(decoding: data, as: UTF8.self),
white: GamePlayer(name: "", computer: false, level: 0),
black: GamePlayer(name: "", computer: false, level: 0))
} else {
Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/BoardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct BoardView: View {

struct BoardView_Previews: PreviewProvider {
static var previews: some View {
let doc = ChessDocument()
let doc = try! ChessDocument()
BoardView(document: .constant(doc))
}
}
6 changes: 3 additions & 3 deletions Shared/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ struct ContentView_Previews: PreviewProvider {

static var previews: some View {
Group {
let doc = ChessDocument(mode: GameMode(value: .analyze))
let doc = try! ChessDocument(mode: GameMode(value: .analyze))
ContentView(document: .constant(doc))
}
Group {
let doc = ChessDocument(mode: GameMode(value: .train))
let doc = try! ChessDocument(mode: GameMode(value: .train))
ContentView(document: .constant(doc))
}
Group {
let doc = ChessDocument(rotated: true)
let doc = try! ChessDocument(rotated: true)
ContentView(document: .constant(doc))
}
}
Expand Down
4 changes: 2 additions & 2 deletions Shared/Views/InformationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ struct InformationView: View {
struct BottomInformationView_Previews: PreviewProvider {
static var previews: some View {
Group {
let doc = ChessDocument(pgn: "1. e4 e5 *")
let doc = try! ChessDocument(pgn: "1. e4 e5 *")
InformationView(document: .constant(doc))
}
Group {
let doc = ChessDocument(pgn: "1. e4 e5 2. Nf3 Nf6 3. Nxe5 d6 4. Nc3 dxe5 *")
let doc = try! ChessDocument(pgn: "1. e4 e5 2. Nf3 Nf6 3. Nxe5 d6 4. Nc3 dxe5 *")
InformationView(document: .constant(doc))
}
}
Expand Down
4 changes: 2 additions & 2 deletions Shared/Views/LabelsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ struct LabelsView: View {
struct LabelsView_Previews: PreviewProvider {
static var previews: some View {
Group {
let doc = ChessDocument()
let doc = try! ChessDocument()
ZStack {
BoardView(document: .constant(doc))
LabelsView(document: .constant(doc))
}
}
Group {
let doc = ChessDocument(rotated: true)
let doc = try! ChessDocument(rotated: true)
ZStack {
BoardView(document: .constant(doc))
LabelsView(document: .constant(doc))
Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/NavigationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct NavigationView: View {

struct NavigationView_Previews: PreviewProvider {
static var previews: some View {
let doc = ChessDocument()
let doc = try! ChessDocument()
NavigationView(document: .constant(doc))
}
}
4 changes: 2 additions & 2 deletions Shared/Views/NewGameView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ struct NewGameView: View {
struct NewGameView_Previews: PreviewProvider {
static var previews: some View {
Group {
let doc = ChessDocument()
let doc = try! ChessDocument()
NewGameView(document: .constant(doc), editMode: false)
}
Group {
let doc = ChessDocument()
let doc = try! ChessDocument()
NewGameView(document: .constant(doc), editMode: true)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Shared/Views/PiecesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ struct PiecesView: View {
struct PiecesView_Previews: PreviewProvider {
static var previews: some View {
Group {
let doc = ChessDocument()
let doc = try! ChessDocument()
ZStack {
BoardView(document: .constant(doc))
PiecesView(document: .constant(doc))
}
}
Group {
let doc = ChessDocument(rotated: true)
let doc = try! ChessDocument(rotated: true)
ZStack {
BoardView(document: .constant(doc))
PiecesView(document: .constant(doc))
Expand Down
6 changes: 3 additions & 3 deletions Shared/Views/TopInformationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ struct TopInformationView: View {
struct TopInformationView_Previews: PreviewProvider {
static var previews: some View {
Group {
let doc = ChessDocument(pgn: "1. e4 e5")
let doc = try! ChessDocument(pgn: "1. e4 e5")
TopInformationView(document: .constant(doc))
}
Group {
let doc = ChessDocument(pgn: "1. e4 e5 2. Nf3 Nf6 3. Nxe5 d6 4. Nc3 dxe5 ")
let doc = try! ChessDocument(pgn: "1. e4 e5 2. Nf3 Nf6 3. Nxe5 d6 4. Nc3 dxe5 ")
TopInformationView(document: .constant(doc))
}
Group {
let doc = ChessDocument(pgn: "1. e4 e5 2. Nf3 Nf6 3. Nxe5 d6 4. Nc3 dxe5 5. d4 exd4 6. Qxd4 Qxd4 7. Nd5 Nxd5 8. Bb5+ c6 9. Bxc6+ Nxc6 *")
let doc = try! ChessDocument(pgn: "1. e4 e5 2. Nf3 Nf6 3. Nxe5 d6 4. Nc3 dxe5 5. d4 exd4 6. Qxd4 Qxd4 7. Nd5 Nxd5 8. Bb5+ c6 9. Bxc6+ Nxc6 *")
TopInformationView(document: .constant(doc))
}
}
Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/VariationSelectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct VariationSelectionView: View {
struct VariationSelectionView_Previews: PreviewProvider {
static var previews: some View {
Group {
let doc = ChessDocument()
let doc = try! ChessDocument()
ZStack {
BoardView(document: .constant(doc))
PiecesView(document: .constant(doc))
Expand Down

0 comments on commit 984cf71

Please sign in to comment.