Skip to content

Commit eeaa2c0

Browse files
committed
WIP added info view with moves for current game pieces
1 parent 068bdd0 commit eeaa2c0

File tree

12 files changed

+250
-71
lines changed

12 files changed

+250
-71
lines changed

MoreChess.xcodeproj/project.pbxproj

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@
3030
B96004B02D4451AE006E9FB9 /* PieceBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = B96004AF2D4451AE006E9FB9 /* PieceBase.swift */; };
3131
B96004B62D458B49006E9FB9 /* BoardFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = B96004B52D458B49006E9FB9 /* BoardFactory.swift */; };
3232
B96EBB382D45944D00F42C70 /* HistoryState.swift in Sources */ = {isa = PBXBuildFile; fileRef = B96EBB372D45943800F42C70 /* HistoryState.swift */; };
33+
B96EBB3A2D459C4B00F42C70 /* InfoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B96EBB392D459C4B00F42C70 /* InfoView.swift */; };
34+
B96EBB3C2D45A10900F42C70 /* SimpleBoardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B96EBB3B2D45A10900F42C70 /* SimpleBoardView.swift */; };
35+
B96EBB3E2D45A2C300F42C70 /* PresentationConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = B96EBB3D2D45A2BE00F42C70 /* PresentationConstants.swift */; };
36+
B96EBB3F2D45A2C300F42C70 /* PresentationConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = B96EBB3D2D45A2BE00F42C70 /* PresentationConstants.swift */; };
37+
B96EBB412D45A41000F42C70 /* BoardViewProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = B96EBB402D45A41000F42C70 /* BoardViewProtocol.swift */; };
3338
B9ABF7A72BAEF11E00907C61 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9ABF7A62BAEF11E00907C61 /* AppState.swift */; };
3439
B9ABF7A92BAEF18A00907C61 /* GameState.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9ABF7A82BAEF18A00907C61 /* GameState.swift */; };
3540
B9ABF7AB2BAEF1EB00907C61 /* BoardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9ABF7AA2BAEF1EB00907C61 /* BoardView.swift */; };
3641
B9FAC9112BAEF492002A20F7 /* PositioningState.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FAC9102BAEF492002A20F7 /* PositioningState.swift */; };
3742
B9FAC9152BAEF6C3002A20F7 /* PositioningInteractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FAC9142BAEF6C3002A20F7 /* PositioningInteractor.swift */; };
38-
B9FAC9172BAEFFAC002A20F7 /* LayoutState.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FAC9162BAEFFAC002A20F7 /* LayoutState.swift */; };
3943
B9FAC9192BAF070C002A20F7 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B9FAC9182BAF070C002A20F7 /* Launch Screen.storyboard */; };
4044
B9FAC91B2BAF0CA7002A20F7 /* BuiltInPieces.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FAC91A2BAF0CA7002A20F7 /* BuiltInPieces.swift */; };
4145
B9FAC91D2BAF0D15002A20F7 /* BuiltInPieces.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9FAC91A2BAF0CA7002A20F7 /* BuiltInPieces.swift */; };
@@ -115,12 +119,15 @@
115119
B96004AF2D4451AE006E9FB9 /* PieceBase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PieceBase.swift; sourceTree = "<group>"; };
116120
B96004B52D458B49006E9FB9 /* BoardFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoardFactory.swift; sourceTree = "<group>"; };
117121
B96EBB372D45943800F42C70 /* HistoryState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryState.swift; sourceTree = "<group>"; };
122+
B96EBB392D459C4B00F42C70 /* InfoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoView.swift; sourceTree = "<group>"; };
123+
B96EBB3B2D45A10900F42C70 /* SimpleBoardView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleBoardView.swift; sourceTree = "<group>"; };
124+
B96EBB3D2D45A2BE00F42C70 /* PresentationConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PresentationConstants.swift; sourceTree = "<group>"; };
125+
B96EBB402D45A41000F42C70 /* BoardViewProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoardViewProtocol.swift; sourceTree = "<group>"; };
118126
B9ABF7A62BAEF11E00907C61 /* AppState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppState.swift; sourceTree = "<group>"; };
119127
B9ABF7A82BAEF18A00907C61 /* GameState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameState.swift; sourceTree = "<group>"; };
120128
B9ABF7AA2BAEF1EB00907C61 /* BoardView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoardView.swift; sourceTree = "<group>"; };
121129
B9FAC9102BAEF492002A20F7 /* PositioningState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PositioningState.swift; sourceTree = "<group>"; };
122130
B9FAC9142BAEF6C3002A20F7 /* PositioningInteractor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PositioningInteractor.swift; sourceTree = "<group>"; };
123-
B9FAC9162BAEFFAC002A20F7 /* LayoutState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutState.swift; sourceTree = "<group>"; };
124131
B9FAC9182BAF070C002A20F7 /* Launch Screen.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = "Launch Screen.storyboard"; sourceTree = "<group>"; };
125132
B9FAC91A2BAF0CA7002A20F7 /* BuiltInPieces.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BuiltInPieces.swift; sourceTree = "<group>"; };
126133
B9FAC91E2BAF0D6A002A20F7 /* Player.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Player.swift; sourceTree = "<group>"; };
@@ -258,6 +265,7 @@
258265
isa = PBXGroup;
259266
children = (
260267
B95CE5102AD4CB5B00978591 /* ContentView.swift */,
268+
B96EBB3D2D45A2BE00F42C70 /* PresentationConstants.swift */,
261269
B9FAC9312BB053AD002A20F7 /* Lobby */,
262270
B9FAC9322BB053B2002A20F7 /* Game */,
263271
B90F55A82BA61F6E00874F00 /* MovesView.swift */,
@@ -309,7 +317,6 @@
309317
B9FAC9382BB0586C002A20F7 /* LobbyState.swift */,
310318
B9ABF7A82BAEF18A00907C61 /* GameState.swift */,
311319
B9FAC9102BAEF492002A20F7 /* PositioningState.swift */,
312-
B9FAC9162BAEFFAC002A20F7 /* LayoutState.swift */,
313320
B96EBB372D45943800F42C70 /* HistoryState.swift */,
314321
);
315322
path = State;
@@ -342,16 +349,19 @@
342349
B9FAC92F2BB05325002A20F7 /* GameView.swift */,
343350
B9ABF7AA2BAEF1EB00907C61 /* BoardView.swift */,
344351
B90F55AB2BA6289800874F00 /* PieceView.swift */,
352+
B96EBB392D459C4B00F42C70 /* InfoView.swift */,
345353
);
346354
path = Game;
347355
sourceTree = "<group>";
348356
};
349357
B9FAC9352BB054A2002A20F7 /* Elements */ = {
350358
isa = PBXGroup;
351359
children = (
360+
B96EBB402D45A41000F42C70 /* BoardViewProtocol.swift */,
352361
B9FAC9362BB054C2002A20F7 /* ButtonStyles.swift */,
353362
B9FAC9482BB06C91002A20F7 /* SimpleNavigationBackButton.swift */,
354363
B9FAC94A2BB06FB2002A20F7 /* WaitingView.swift */,
364+
B96EBB3B2D45A10900F42C70 /* SimpleBoardView.swift */,
355365
);
356366
path = Elements;
357367
sourceTree = "<group>";
@@ -576,8 +586,8 @@
576586
B9FAC94F2BB2E833002A20F7 /* Color.swift in Sources */,
577587
B9FAC93F2BB059CD002A20F7 /* GameService.swift in Sources */,
578588
B9ABF7AB2BAEF1EB00907C61 /* BoardView.swift in Sources */,
579-
B9FAC9172BAEFFAC002A20F7 /* LayoutState.swift in Sources */,
580589
B9FAC92E2BB04A01002A20F7 /* PlayCondition.swift in Sources */,
590+
B96EBB3C2D45A10900F42C70 /* SimpleBoardView.swift in Sources */,
581591
B9FAC9392BB0586C002A20F7 /* LobbyState.swift in Sources */,
582592
B9FAC9112BAEF492002A20F7 /* PositioningState.swift in Sources */,
583593
B9FAC9372BB054C2002A20F7 /* ButtonStyles.swift in Sources */,
@@ -605,13 +615,16 @@
605615
B9FAC9222BAF1DCD002A20F7 /* GameRepository.swift in Sources */,
606616
B90F55A92BA61F6E00874F00 /* MovesView.swift in Sources */,
607617
B9ABF7A92BAEF18A00907C61 /* GameState.swift in Sources */,
618+
B96EBB3A2D459C4B00F42C70 /* InfoView.swift in Sources */,
608619
B9FAC94B2BB06FB2002A20F7 /* WaitingView.swift in Sources */,
609620
B94B61F52B68F8D700C998F3 /* PieceGenerator.swift in Sources */,
610621
B9FAC9432BB06315002A20F7 /* LobbyRepository.swift in Sources */,
611622
B9FAC91B2BAF0CA7002A20F7 /* BuiltInPieces.swift in Sources */,
612623
B9FAC9512BB2F46D002A20F7 /* Style.swift in Sources */,
613624
B9FAC91F2BAF0D6A002A20F7 /* Player.swift in Sources */,
614625
B9FAC9152BAEF6C3002A20F7 /* PositioningInteractor.swift in Sources */,
626+
B96EBB3F2D45A2C300F42C70 /* PresentationConstants.swift in Sources */,
627+
B96EBB412D45A41000F42C70 /* BoardViewProtocol.swift in Sources */,
615628
);
616629
runOnlyForDeploymentPostprocessing = 0;
617630
};
@@ -628,6 +641,7 @@
628641
B94B61B12B461D3200C998F3 /* GamePiece.swift in Sources */,
629642
B90F55AA2BA61F6E00874F00 /* MovesView.swift in Sources */,
630643
B9FAC9612BB30D8F002A20F7 /* StyleConstants.swift in Sources */,
644+
B96EBB3E2D45A2C300F42C70 /* PresentationConstants.swift in Sources */,
631645
B9FAC95C2BB2FD0C002A20F7 /* Typography.swift in Sources */,
632646
B94B61F62B68F8D700C998F3 /* PieceGenerator.swift in Sources */,
633647
B9FAC9672BB3129F002A20F7 /* LayoutDirection.swift in Sources */,

Shared/Business/Interactors/PositioningInteractor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ import Foundation
104104
// MARK: Private functions
105105

106106
private func gridOffsetFrom(offset: CGSize) -> GridCoordinate {
107-
let x = Int(floor((offset.width + appState.layoutState.elementDiameter/2) / appState.layoutState.elementDiameter)) * (isRTL ? -1 : 1)
108-
let y = Int(floor((offset.height + appState.layoutState.elementDiameter/2) / appState.layoutState.elementDiameter))
107+
let x = Int(floor((offset.width + PresentationConstants.Layout.elementDiameter/2) / PresentationConstants.Layout.elementDiameter)) * (isRTL ? -1 : 1)
108+
let y = Int(floor((offset.height + PresentationConstants.Layout.elementDiameter/2) / PresentationConstants.Layout.elementDiameter))
109109
return GridCoordinate(column: x, row: y)
110110
}
111111

Shared/Business/State/AppState.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ import Foundation
1111
var lobbyState = LobbyState()
1212
var gameState = GameState()
1313
var positioningState = PositioningState()
14-
var layoutState = LayoutState()
1514
var historyState = HistoryState()
1615
}

Shared/Business/State/GameState.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ import Foundation
5252
}
5353
}
5454

55+
var uniquePieces: [Character: GamePiece] {
56+
let allBoard = initialBoard.joined().compactMap { $0 }
57+
var uniquePieces: [Character: GamePiece] = [:]
58+
for piece in allBoard {
59+
uniquePieces[piece.pieceBase.icon] = piece
60+
}
61+
return uniquePieces
62+
}
63+
5564
init() {
5665
let players = [
5766
Players.one(local: true),
@@ -60,12 +69,6 @@ import Foundation
6069
self.players = players
6170
initialBoard = BoardFactory.fiveByFive.makeBoard(players: players)
6271

63-
let allBoard = initialBoard.joined().compactMap { $0 }
64-
var uniquePieces: [Character: GamePiece] = [:]
65-
for piece in allBoard {
66-
uniquePieces[piece.pieceBase.icon] = piece
67-
}
68-
6972
for piece in uniquePieces {
7073
print("\(piece.key) \(piece.value.description)")
7174
}

Shared/Business/State/LayoutState.swift

Lines changed: 0 additions & 13 deletions
This file was deleted.

Shared/Localizable.xcstrings

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
}
1515
}
1616
},
17+
"%lld x %lld -> %lld" : {
18+
"localizations" : {
19+
"en" : {
20+
"stringUnit" : {
21+
"state" : "new",
22+
"value" : "%1$lld x %2$lld -> %3$lld"
23+
}
24+
}
25+
}
26+
},
1727
"⌽" : {
1828

1929
},
@@ -22,6 +32,16 @@
2232
},
2333
"Back" : {
2434

35+
},
36+
"can make board with %lld x %lld" : {
37+
"localizations" : {
38+
"en" : {
39+
"stringUnit" : {
40+
"state" : "new",
41+
"value" : "can make board with %1$lld x %2$lld"
42+
}
43+
}
44+
}
2545
},
2646
"Client" : {
2747

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// BoardViewProtocol.swift
3+
// MoreChess
4+
//
5+
// Created by Richard Adem on 25/01/2025.
6+
//
7+
8+
import SwiftUI
9+
10+
protocol BoardViewProtocol {}
11+
extension BoardViewProtocol {
12+
func background(isTarget: Bool, rowIndex: Int, columnIndex: Int, rowCount: Int) -> some View {
13+
let index = rowIndex * rowCount + columnIndex
14+
let color: Color = switch (isTarget, (index % 2 == 0)) {
15+
case (true, _):
16+
PresentationConstants.GameColor.targetColor
17+
default:
18+
.clear
19+
}
20+
return ZStack {
21+
22+
// Figure out the background color by checking if the row index is even, and flip that is the
23+
// column index is even.
24+
let rowIndexEven = rowIndex % 2 == 0
25+
let showOddBoardColor = columnIndex % 2 == 0 ? !rowIndexEven : rowIndexEven
26+
Rectangle().fill(showOddBoardColor
27+
? PresentationConstants.GameColor.oddBoardColor
28+
: PresentationConstants.GameColor.evenBoardColor)
29+
Rectangle().strokeBorder(color, lineWidth: 2)
30+
}
31+
.zIndex(Double(isTarget
32+
? PresentationConstants.ZIndex.target.rawValue
33+
: PresentationConstants.ZIndex.normal.rawValue))
34+
}
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// SimpleBoardView.swift
3+
// MoreChess (iOS)
4+
//
5+
// Created by Richard Adem on 25/01/2025.
6+
//
7+
8+
import SwiftUI
9+
10+
// TODO: Add starting position and valid moves.
11+
// TODO: Can this be combined with `BoardView` to avoid repeated code?
12+
13+
/// Like `BoardView` but with no interaction, just to display something.
14+
struct SimpleBoardView: View, BoardViewProtocol {
15+
let board: Board
16+
var body: some View {
17+
Grid(horizontalSpacing: 0, verticalSpacing: 0) {
18+
ForEach(Array(board.enumerated()), id: \.offset) { rowIndex, row in
19+
GridRow {
20+
ForEach(Array(row.enumerated()), id:\.offset) { columnIndex, piece in
21+
Color.clear
22+
.frame(width: PresentationConstants.Layout.elementDiameter,
23+
height: PresentationConstants.Layout.elementDiameter)
24+
.background(
25+
background(isTarget: false,
26+
rowIndex: rowIndex,
27+
columnIndex: columnIndex,
28+
rowCount: board.count)
29+
)
30+
}
31+
}
32+
}
33+
}
34+
}
35+
}
36+
37+
#Preview {
38+
SimpleBoardView(board: BoardFactory.fiveByFive.makeBoard(players: [
39+
Players.one(local: true),
40+
Players.two(local: false)
41+
]))
42+
}

Shared/Presentation/Game/BoardView.swift

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,10 @@
77

88
import SwiftUI
99

10-
struct BoardView: View {
10+
struct BoardView: View, BoardViewProtocol {
1111
@Environment(AppState.self) private var appState
1212
@Environment(PositioningInteractor.self) private var positioningInteractor
1313

14-
private enum Constants {
15-
static let targetColor = Color.red
16-
static let oddBoardColor = Color.gray
17-
static let evenBoardColor = Color.white
18-
static let boardOutline = Color.gray
19-
}
20-
21-
private enum ZIndex: Int {
22-
case normal = 0
23-
case target = 1
24-
case selected = 2
25-
}
26-
2714
var body: some View {
2815
Grid(horizontalSpacing: 0, verticalSpacing: 0) {
2916
ForEach(Array(appState.gameState.board.enumerated()), id: \.offset) { rowIndex, row in
@@ -39,9 +26,9 @@ struct BoardView: View {
3926
let offset = appState.positioningState.pieceOffset(gridPosition)
4027
PieceView(piece: piece)
4128
.accessibility(label: Text("\(piece.description) piece"))
42-
.frame(width: appState.layoutState.elementDiameter,
43-
height: appState.layoutState.elementDiameter)
44-
.background(isTarget ? Constants.targetColor : piece.player.color)
29+
.frame(width: PresentationConstants.Layout.elementDiameter,
30+
height: PresentationConstants.Layout.elementDiameter)
31+
.background(isTarget ? PresentationConstants.GameColor.targetColor : piece.player.color)
4532
.offset(x: offset.width * (isRTL ? -1 : 1), y: offset.height)
4633
.gesture(
4734
DragGesture()
@@ -54,11 +41,13 @@ struct BoardView: View {
5441
positioningInteractor.endDrag()
5542
}
5643
)
57-
.zIndex(Double(isSelected ? ZIndex.selected.rawValue : ZIndex.normal.rawValue))
44+
.zIndex(Double(isSelected
45+
? PresentationConstants.ZIndex.selected.rawValue
46+
: PresentationConstants.ZIndex.normal.rawValue))
5847
} else {
5948
Color.clear
60-
.frame(width: appState.layoutState.elementDiameter,
61-
height: appState.layoutState.elementDiameter)
49+
.frame(width: PresentationConstants.Layout.elementDiameter,
50+
height: PresentationConstants.Layout.elementDiameter)
6251
}
6352
} // Group
6453
.background(background(isTarget: isTarget,
@@ -69,28 +58,8 @@ struct BoardView: View {
6958
}
7059
}
7160
} // Grid
72-
.border(Constants.boardOutline, width: 1)
61+
.border(PresentationConstants.GameColor.boardOutline, width: 1)
7362
} // body
74-
75-
func background(isTarget: Bool, rowIndex: Int, columnIndex: Int, rowCount: Int) -> some View {
76-
let index = rowIndex * rowCount + columnIndex
77-
let color: Color = switch (isTarget, (index % 2 == 0)) {
78-
case (true, _):
79-
Constants.targetColor
80-
default:
81-
.clear
82-
}
83-
return ZStack {
84-
85-
// Figure out the background color by checking if the row index is even, and flip that is the
86-
// column index is even.
87-
let rowIndexEven = rowIndex % 2 == 0
88-
let showOddBoardColor = columnIndex % 2 == 0 ? !rowIndexEven : rowIndexEven
89-
Rectangle().fill(showOddBoardColor ? Constants.oddBoardColor : Constants.evenBoardColor)
90-
Rectangle().strokeBorder(color, lineWidth: 2)
91-
}
92-
.zIndex(Double(isTarget ? ZIndex.target.rawValue : ZIndex.normal.rawValue))
93-
}
9463
}
9564

9665
#Preview {

0 commit comments

Comments
 (0)