Задание с Шахматами от Алексея Скутаренко https://vk.com/topic-58860049_31709995
/*
//1
struct Student {
let name : String
let surname : String
let birthday : (day: Int8, month: Int8, year: Int)
let averageMark : Float
}
let stBohdan = Student(name: "Bohdan", surname: "Yevchenko", birthday: (day: 28, month: 01, year: 1998), averageMark: 10.9)
let stKirillo = Student(name: "Kirill", surname: "Kravtsov", birthday: (day: 03, month: 06, year: 1997), averageMark: 10.2)
let stYana = Student(name: "Yana", surname: "Ivanova", birthday: (day: 28, month: 02, year: 1987), averageMark: 8.5)
let stAnna = Student(name: "Anna", surname: "Ivanova", birthday: (day: 02, month: 02, year: 1989), averageMark: 8.5)
let stOlya = Student(name: "Olya", surname: "Ivanova", birthday: (day: 12, month: 09, year: 1989), averageMark: 9.7)
let stAnna2 = Student(name: "Anna", surname: "Ivanova", birthday: (day: 02, month: 09, year: 1987), averageMark: 9.6)
let journal = [stKirillo, stBohdan, stYana, stAnna, stOlya, stAnna2]
//2
func printStudents(students:[Student]) {
println("#\tSurname\tName\tBirthday\tAverage Mark")
for i in 0..<students.count {
print("\(i+1)\t\(students[i].surname)\t\(students[i].name)\t")
let birthday = students[i].birthday
print("\(birthday.day).\(birthday.month).\(birthday.year)\t")
println("\(students[i].averageMark)")
}
}
println("Task 2")
printStudents(journal)
//3
println("\n\nTask 3")
printStudents(sorted(journal) { $0.averageMark > $1.averageMark } )
*/
//4
/*
func sortByNames(first:Student, second:Student) -> Bool {
if first.surname != second.surname {
return first.surname < second.surname
}
if first.name != second.name {
return first.name < second.name
}
if first.averageMark != second.averageMark {
return first.averageMark > second.averageMark
}
if first.birthday.year != second.birthday.year {
return first.birthday.year < second.birthday.year
}
if first.birthday.month != second.birthday.month {
return first.birthday.month < second.birthday.month
}
return first.birthday.day < second.birthday.day
}
println("\n\nTask 4")
printStudents(sorted(journal, sortByNames))
*/
//5
/*
change let to var
struct Student {
var name : String
var surname : String
var birthday : (day: Int8, month: Int8, year: Int)
var averageMark : Float
}
*/
/*
var journal2 = journal
journal2.count
journal2[0].name = "Stas"
journal2[1].name = "Alex"
journal2[3].averageMark = 5.95
journal2[4].surname = "Yevchenko"
journal2[5].birthday = (day: 01, month: 12, year: 2000)
println("\n\nTask 5\njournal 2")
printStudents(journal2)
println("\nstandart journal")
printStudents(journal)
*/
//6
/*
class Student {
var name : String
var surname : String
var birthday : (day: Int8, month: Int8, year: Int)
var averageMark : Float
init(name:String, surname:String, birthdayDay day: Int8, month: Int8, year: Int, averageMark:Float) {
self.name = name
self.surname = surname
self.birthday = (day: day, month: month, year: year)
self.averageMark = averageMark
}
}
let stBohdan = Student(name: "Bohdan", surname: "Yevchenko", birthdayDay: 28, month: 01, year: 1998, averageMark: 10.9)
let stKirillo = Student(name: "Kirill", surname: "Kravtsov", birthdayDay: 03, month: 06, year: 1997, averageMark: 10.2)
let stYana = Student(name: "Yana", surname: "Ivanova", birthdayDay: 28, month: 02, year: 1987, averageMark: 8.5)
let stAnna = Student(name: "Anna", surname: "Ivanova", birthdayDay: 02, month: 02, year: 1989, averageMark: 8.5)
let stOlya = Student(name: "Olya", surname: "Ivanova", birthdayDay: 12, month: 09, year: 1989, averageMark: 9.7)
let stAnna2 = Student(name: "Anna", surname: "Ivanova", birthdayDay: 02, month: 09, year: 1987, averageMark: 9.6)
let journal = [stKirillo, stBohdan, stYana, stAnna, stOlya, stAnna2]
func printStudents(students:[Student]) {
println("#\tSurname\tName\tBirthday\tAverage Mark")
for i in 0..<students.count {
print("\(i+1)\t\(students[i].surname)\t\(students[i].name)\t")
let birthday = students[i].birthday
print("\(birthday.day).\(birthday.month).\(birthday.year)\t")
println("\(students[i].averageMark)")
}
}
var journal2 = journal
journal2[0].name = "Stas"
journal2[1].name = "Alex"
journal2[3].averageMark = 5.95
journal2[4].surname = "Yevchenko"
journal2[5].birthday = (day: 01, month: 12, year: 2000)
println("Task 6\njournal 2")
printStudents(journal2)
println("\n standart journal")
printStudents(journal)
*/
//007
enum ChessType {
case Pawn //пешка
case Knight //конь
case Bishop //офицер/слон
case Rook //ладья
case Queen //королева
case King //король
case Cell //клетка
}
class Chess {
var x : Character
var y : Int
var isWhite : Bool
var type : ChessType
var description : String {
switch type {
case .Pawn : return isWhite ? "\u{2659}" : "\u{265F}"
case .Knight : return isWhite ? "\u{2658}" : "\u{265E}"
case .Bishop : return isWhite ? "\u{2657}" : "\u{265D}"
case .Rook : return isWhite ? "\u{2656}" : "\u{265C}"
case .Queen : return isWhite ? "\u{2655}" : "\u{265B}"
case .King : return isWhite ? "\u{2654}" : "\u{265A}"
case .Cell : return isWhite ? "\u{25A1}" : "\u{25A0}"
}
}
init(x:Character, y:Int, isWhite:Bool, type:ChessType) {
self.x = x
self.y = y
self.isWhite = isWhite
self.type = type
}
}
func setChessBoard(figures:Chess...) -> [String:Chess] {
var chessBoard = [String:Chess]()
for figure in figures {
chessBoard["\(figure.x)\(figure.y)"] = figure
}
let letters = Array("abcdefgh")
let n = 8
var color = false
for i in 1...n {
for j in letters {
if chessBoard["\(j)\(i)"] == nil {
chessBoard["\(j)\(i)"] = Chess(x: j, y: i, isWhite: color, type: ChessType.Cell)
}
color = !color
}
color = !color
}
return chessBoard
}
func setChessBoard(figures:[Chess]) -> [String:Chess] {
var chessBoard = [String:Chess]()
for figure in figures {
chessBoard["\(figure.x)\(figure.y)"] = figure
}
let letters = Array("abcdefgh")
let n = 8
var color = false
for i in 1...n {
for j in letters {
if chessBoard["\(j)\(i)"] == nil {
chessBoard["\(j)\(i)"] = Chess(x: j, y: i, isWhite: color, type: ChessType.Cell)
}
color = !color
}
color = !color
}
return chessBoard
}
func printChessBoard(chessBoard:[String:Chess]) {
let letters = Array("abcdefgh")
let n = 8
print(" ")
for j in 0..<n {
print("\(letters[j]) ")
}
println()
var value = true
for i in 1...n {
print("\(n-i+1) ")
for j in 0..<n {
let figure = chessBoard["\(letters[j])\(n-i+1)"]!
print(figure.description + " ")
}
println("\(n-i+1)")
}
print(" ")
for j in 0..<n {
print("\(letters[j]) ")
}
println()
}
func getCellColor(x:Character, y:Int) -> Bool {
let letters = Array("abcdefghijklmnopqrstuvwxyz")
let xNumb = find(letters, x)!
return (xNumb + y) % 2 == 0 ? true : false
}
func checkPawnMove(isWhite:Bool, field:[String:Chess], fromX:Int, fromY:Int, toX:Int, toY:Int) -> Bool{
let letters = Array("abcdefghijklmnopqrstuvwxyz")
switch field["\(letters[toX-1])\(toY)"]!.type {
case .Cell : //just move
if fromX == toX {
if isWhite {
if fromY - toY == -1 {
return true
}
if fromY - toY == -2 && fromY == 2 {
return true
}
} else {
if fromY - toY == 1 {
return true
}
if fromY - toY == 2 && fromY == 7 {
return true
}
}
return false
}
default : //hit
if abs(fromX - toX) == 1 {
if isWhite {
if fromY - toY == -1 {
return true
}
} else {
if fromY - toY == 1 {
return true
}
}
return false
}
}
return false
}
func checkKnightMove(field:[String:Chess], fromX:Int, fromY:Int, toX:Int, toY:Int) -> Bool {
if abs(fromX - toX) == 1 && abs(fromY - toY) == 2 || abs(fromX - toX) == 2 && abs(fromY - toY) == 1 {
return true
}
return false
}
func checkBishopMove(field:[String:Chess], fromX:Int, fromY:Int, toX:Int, toY:Int) -> Bool {
if abs(fromX - toX) == abs(fromY - toY) {
return true
}
return false
}
func checkRookMove(field:[String:Chess], fromX:Int, fromY:Int, toX:Int, toY:Int) -> Bool {
if fromX == toX || fromY == toY {
return true
}
return false
}
func checkQueenMove(field:[String:Chess], fromX:Int, fromY:Int, toX:Int, toY:Int) -> Bool {
if checkBishopMove(field, fromX, fromY, toX, toY) || checkRookMove(field, fromX, fromY, toX, toY) {
return true
}
return false
}
func checkKingMove(field:[String:Chess], fromX:Int, fromY:Int, toX:Int, toY:Int) -> Bool {
if abs(fromX - toX) <= 1 && abs(fromY - toY) <= 1 {
return true
}
return false
}
func moveFigure(inout board:[String:Chess], var figure:Chess, toX toXCharacter:Character, #toY:Int) {
let letters = Array("abcdefgh")
if var toX = find(letters, toXCharacter) {
toX++
if toY >= 1 && toY <= 8 {
let fromX = find(letters, figure.x)! + 1
let fromY = figure.y
var canMove = false
let toFigure = board["\(toXCharacter)\(toY)"]!
if toFigure.type == .Cell || toFigure.isWhite != figure.isWhite {
canMove = checkMove(figure, fromX: fromX, fromY: fromY, toX: toX, toY: toY)
}
if canMove {
board["\(letters[fromX-1])\(fromY)"] = Chess(x: letters[fromX-1], y: fromY, isWhite: getCellColor(letters[fromX-1], fromY), type: .Cell)
figure.x = toXCharacter
figure.y = toY
board["\(toXCharacter)\(toY)"] = figure
println("move from \(letters[fromX-1])\(fromY) to \(toXCharacter)\(toY)")
} else {
println("failed to move from \(letters[fromX-1])\(fromY) to \(toXCharacter)\(toY)")
}
} else {
println("Error 404: \(toY) not found")
}
} else {
println("Error 404: \(toXCharacter) not found")
}
}
func checkMove(figure:Chess, #fromX:Int, #fromY:Int, #toX:Int, #toY:Int) -> Bool {
switch figure.type {
case .Pawn : return checkPawnMove(figure.isWhite, board, fromX, fromY, toX, toY)
case .Knight : return checkKnightMove(board, fromX, fromY, toX, toY)
case .Bishop : return checkBishopMove(board, fromX, fromY, toX, toY)
case .Rook : return checkRookMove(board, fromX, fromY, toX, toY)
case .Queen : return checkQueenMove(board, fromX, fromY, toX, toY)
case .King : return checkKingMove(board, fromX, fromY, toX, toY)
default : break
}
return false
}
func newGame() -> [String:Chess] {
var board = [String:Chess]()
var rookB1 = Chess(x: "a", y: 8, isWhite: false, type: .Rook)
var rookB2 = Chess(x: "h", y: 8, isWhite: false, type: .Rook)
var rookW1 = Chess(x: "a", y: 1, isWhite: true, type: .Rook)
var rookW2 = Chess(x: "h", y: 1, isWhite: true, type: .Rook)
var knightB1 = Chess(x: "b", y: 8, isWhite: false, type: .Knight)
var knightB2 = Chess(x: "g", y: 8, isWhite: false, type: .Knight)
var knightW1 = Chess(x: "b", y: 1, isWhite: true, type: .Knight)
var knightW2 = Chess(x: "g", y: 1, isWhite: true, type: .Knight)
var bishopB1 = Chess(x: "c", y: 8, isWhite: false, type: .Bishop)
var bishopB2 = Chess(x: "f", y: 8, isWhite: false, type: .Bishop)
var bishopW1 = Chess(x: "c", y: 1, isWhite: true, type: .Bishop)
var bishopW2 = Chess(x: "f", y: 1, isWhite: true, type: .Bishop)
var queenB = Chess(x: "d", y: 8, isWhite: false, type: .Queen)
var kingB = Chess(x: "e", y: 8, isWhite: false, type: .King)
var queenW = Chess(x: "d", y: 1, isWhite: true, type: .Queen)
var kingW = Chess(x: "e", y: 1, isWhite: true, type: .King)
var figures = [rookB1, rookB2, rookW1
, rookW2, knightB1, knightB2, knightW1
, knightW2, bishopB1, bishopB2, bishopW1
, bishopW2, bishopB1, bishopB2, bishopW1
, bishopW2, queenB, kingB, queenW, kingW]
var color = true
var y = 2
let letters = Array("abcdefghijklmnopqrstuvwxyz")
for _ in 1...2 {
for i in 0..<8 {
figures.append(Chess(x: letters[i], y: y, isWhite: color, type: .Pawn))
}
color = !color
y = 7
}
board = setChessBoard(figures)
return board
}
var board = newGame()
printChessBoard(board)
println()
moveFigure(&board, board["e2"]!, toX: "e", toY: 4)
moveFigure(&board, board["e7"]!, toX: "e", toY: 5)
printChessBoard(board)
moveFigure(&board, board["e2"]!, toX: "e", toY: 40)
println("вне диапазона")
moveFigure(&board, board["e2"]!, toX: "z", toY: 4)
println("вне диапазона")
moveFigure(&board, board["e2"]!, toX: "e", toY: 4)
println("двигать не фигуру")
moveFigure(&board, board["e4"]!, toX: "e", toY: 6)
println("двигать пешку через 2 кл")
moveFigure(&board, board["e4"]!, toX: "e", toY: 3)
println("двигать пешку назад")
moveFigure(&board, board["e4"]!, toX: "e", toY: 5)
println("пешка бьет меняя вертикаль")
moveFigure(&board, board["f1"]!, toX: "c", toY: 4)
moveFigure(&board, board["d7"]!, toX: "d", toY: 6)
println()
printChessBoard(board)
moveFigure(&board, board["d1"]!, toX: "h", toY: 5)
moveFigure(&board, board["b8"]!, toX: "c", toY: 7)
println("конь ходит только буквой г")
moveFigure(&board, board["b8"]!, toX: "c", toY: 6)
println()
printChessBoard(board)
moveFigure(&board, board["d2"]!, toX: "d", toY: 4)
moveFigure(&board, board["e5"]!, toX: "d", toY: 4)
println("убийство пешки")
println()
printChessBoard(board)
moveFigure(&board, board["h5"]!, toX: "f", toY: 7)
moveFigure(&board, board["e8"]!, toX: "e", toY: 6)
println("король через 2 кл\n")
println("White Wins!!!\n")
printChessBoard(board)