Last active
March 18, 2022 19:24
-
-
Save tornikegomareli/b789e9bd00e2da595df847c95c6e502a to your computer and use it in GitHub Desktop.
Swift structured based chess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum CubeColorType { | |
case white | |
case black | |
case none | |
} | |
class Cube { | |
var color: CubeColorType | |
var perviousCube: Cube? | |
var numeration: Int | |
var id: Int = 0 | |
init(color: CubeColorType, previousCube: Cube?, numeration: Int, id: Int) { | |
self.color = color | |
self.perviousCube = previousCube | |
self.numeration = numeration | |
self.id = id | |
} | |
} | |
struct ChessBoard { | |
var cubes: [Cube] = [] | |
mutating func fillCubes() -> [Cube] { | |
let firstCube = Cube(color: .white, previousCube: nil, numeration: 1, id: 1) | |
cubes.append(firstCube) | |
for index in 0...69 { | |
let newCube = createNewCubeOnIndex(index: index) | |
cubes.append(newCube) | |
} | |
return cubes | |
} | |
mutating func fillCubesForIOS() -> [Cube] { | |
let firstCube = Cube(color: .white, previousCube: nil, numeration: 1, id: 1) | |
cubes.append(firstCube) | |
for index in 0...62 { | |
let newCube = createNewCubeForiOS(index: index) | |
cubes.append(newCube) | |
} | |
return cubes | |
} | |
func createNewCubeForiOS(index: Int) -> Cube { | |
if cubes[index].id % 8 == 0 { | |
let newCube = Cube(color: cubes[index].color, previousCube: cubes[index].perviousCube, numeration: cubes[index].numeration + 1, id: cubes[index].id + 1) | |
return newCube | |
} | |
let currentCube = cubes[index] | |
let nextCubeColor = getCorrectColor(color: currentCube.color) | |
let newCube = Cube(color: nextCubeColor, previousCube: currentCube, numeration: currentCube.numeration + 1, id: currentCube.id + 1) | |
return newCube | |
} | |
func createNewCubeOnIndex(index: Int) -> Cube { | |
if index != 0 && cubes[index].numeration % 8 == 0 { | |
let currentCube = cubes[index] | |
let emptyCube = Cube(color: .none, previousCube: currentCube, numeration: currentCube.numeration + 1, id: currentCube.id + 1) | |
return emptyCube | |
} | |
let currentCube = cubes[index] | |
if currentCube.color == .none { | |
let newLineCube = Cube(color: currentCube.perviousCube!.color, previousCube: currentCube, numeration: 1, id: currentCube.id + 1) | |
return newLineCube | |
} | |
let nextCubeColor = getCorrectColor(color: currentCube.color) | |
let newCube = Cube(color: nextCubeColor, previousCube: currentCube, numeration: currentCube.numeration + 1, id: currentCube.id + 1) | |
return newCube | |
} | |
func getCorrectColor(color: CubeColorType) -> CubeColorType { | |
switch color { | |
case .white: | |
return CubeColorType.black | |
case .black: | |
return CubeColorType.white | |
case .none: | |
return CubeColorType.none | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment