Skip to content

Instantly share code, notes, and snippets.

@lfaoro
Created September 10, 2014 08:44
Show Gist options
  • Save lfaoro/7eea3ecffe63eb2f783d to your computer and use it in GitHub Desktop.
Save lfaoro/7eea3ecffe63eb2f783d to your computer and use it in GitHub Desktop.
import Cocoa
import Swift
enum Shape {
case K, Q, B, R, N, P
static let allValues = [K, Q, B, R, N, P]
}
enum Color {
case w, b
static let allValues = [w, b]
}
struct Piece {
var color: Color
var shape: Shape
func getRawPiece(color: Color, shape: Shape) {
}
}
var piece = Piece(color: .w, shape: .Q)
piece.getRawPiece(.w, shape: .Q)
//Global vars
var frameSize:CGFloat = 600
let cellSize:CGFloat = 8
var cellLength:CGFloat = frameSize / cellSize
var cellX = 0
var cellY = 0
class Chessboard {
// let rawPiece:String = Piece.getRawPiece(msg: "ciao")
var piece = Piece(color: .w, shape: .Q)
piece.getRawPiece(.w, shape: .Q)
let letters = ["a","b","c","d","e","f","g","h"]
let numbers = ["1","2","3","4","5","6","7","8"]
//_ removes the forced declaration of row: when using the function
func piecePosition(column: Int, _ row: Int) -> String {
if (column < letters.startIndex || column > letters.count) {
return "The board accepts onlynumbers from 0 to 7"
}else if (row < numbers.startIndex || row > numbers.count) {
return "The board accepts onlynumbers from 0 to 7"
}
var pieceAtColumn = letters[column]
var pieceAtRow = numbers[row]
let piecePosition = pieceAtColumn + pieceAtRow
return piecePosition
}
//Define a Mutable Dictionary containing the state of every Cell
var cellState: [String: Piece?] = [:]
//Set the cellState to the initial Chess setting
func newGame (boardView: NSView) {
//Add the board image to the View Background
//To create an array of Boards and let the user choose between them
var img = NSImage(named: "blue2")
var boardImage = NSImageView(frame: CGRect(x: 0, y:0, width: frameSize, height: frameSize))
boardImage.image = img
boardView.addSubview(boardImage, positioned: .Below, relativeTo: nil)
boardView.setFrameSize(CGSize(width: frameSize, height: frameSize))
for column in 0...7 {
for row in 0...7 {
let squareX = CGFloat(column) * cellLength
let squareY = CGFloat(row) * cellLength
var notation = piecePosition(column, row)
var value: Piece? = nil
//Draw the initial board pieces using cases
//Seems cumbersome but I can't think of a better way to do it...
switch (column, row) {
case (_, 1):
var value = Piece(color: .w, shape: .P)
cellState[notation] = value
var pieceImg = NSImage(named: "wP")
var pieceView = NSImageView(frame: CGRect(x: squareX, y: squareY, width: cellLength, height: cellLength))
pieceView.image = pieceImg
boardView.addSubview(pieceView, positioned: .Above, relativeTo: nil)
case (_, 6):
var value = Piece(color: .b, shape: .P)
cellState[notation] = value
var pieceImg = NSImage(named: "bP")
var pieceView = NSImageView(frame: CGRect(x: squareX, y: squareY, width: cellLength, height: cellLength))
pieceView.image = pieceImg
boardView.addSubview(pieceView, positioned: .Above, relativeTo: nil)
case (0, 0), (0, 7):
var value = Piece(color: .w, shape: .R)
var pieceImg = NSImage(named: "wR")
var pieceView = NSImageView(frame: CGRect(x: squareX, y: squareY, width: cellLength, height: cellLength))
pieceView.image = pieceImg
boardView.addSubview(pieceView, positioned: .Above, relativeTo: nil)
case (8, 1), (8, 8):
var value = Piece(color: .b, shape: .R)
var pieceImg = NSImage(named: "bR")
var pieceView = NSImageView(frame: CGRect(x: squareX, y: squareY, width: cellLength, height: cellLength))
pieceView.image = pieceImg
boardView.addSubview(pieceView, positioned: .Above, relativeTo: nil)
case (1, 2), (1, 7):
var value = Piece(color: .w, shape: .N)
var pieceImg = NSImage(named: "wN")
var pieceView = NSImageView(frame: CGRect(x: squareX, y: squareY, width: cellLength, height: cellLength))
pieceView.image = pieceImg
boardView.addSubview(pieceView, positioned: .Above, relativeTo: nil)
case (8, 2), (8, 7):
var value = Piece(color: .b, shape: .N)
var pieceImg = NSImage(named: "bN")
var pieceView = NSImageView(frame: CGRect(x: squareX, y: squareY, width: cellLength, height: cellLength))
pieceView.image = pieceImg
boardView.addSubview(pieceView, positioned: .Above, relativeTo: nil)
default:
value = nil
cellState[notation] = value
}
}
}
// println(cellState)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment