// // main.swift // tetriscli // // Created by Shadowfacts on 10/13/19. // Copyright © 2019 Shadowfacts. All rights reserved. // import Foundation class TwoDimString: CustomStringConvertible { var width: Int var height: Int var rows: [String] var description: String { return rows.joined(separator: "\n") } init(width: Int, height: Int) { self.width = width self.height = height self.rows = [] let empty = String(repeating: " ", count: width) for _ in 1...height { rows.append(empty) } } subscript(column: Int, row: Int) -> Character { get { guard column >= 0 && column < width, row >= 0 && row < height else { fatalError("Out of bounds position \(column), \(row) for TwoDimString(width: \(width), height: \(height))") } let rowStr = rows[row] return rowStr[rowStr.index(rowStr.startIndex, offsetBy: column)] } set { guard column >= 0 && column < width, row >= 0 && row < height else { fatalError("Out of bounds position \(column), \(row) for TwoDimString(width: \(width), height: \(height))") } let rowStr = rows[row] rows[row] = String(rowStr.prefix(column)) + String(newValue) + String(rowStr.dropFirst(column + 1)) } } func setRow(_ row: Int, to str: String) { guard row >= 0 && row < height else { fatalError("Can't set row \(row) for TwoDimString(width: \(width), height: \(height))") } guard str.count == width else { fatalError("Can't set row with count \(str.count) for TwoDimString(width: \(width), height: \(height))") } rows[row] = str } func setColumn(_ col: Int, to str: String) { guard col >= 0 && col < width else { fatalError("Can't set column \(col) for TwoDimString(width: \(width), height: \(height))") } guard str.count == height else { fatalError("Can't set row with count \(str.count) for TwoDimString(width: \(width), height: \(height))") } for row in 0..= 0 && column + string.width <= width, row >= 0 && row + string.height <= height else { fatalError("Can't set TwoDimString(width: \(string.width), height: \(string.height)) at (\(column), \(row))") } for subCol in 0..= 0 && y + top + 1 < height { for x in 0..= 0 && x + left + 1 < width { self[x + left + 1, y + top + 1] = currentPiece.tiles[y][x] ? "X" : "-" } } } } } let controller = GameController() controller.start() func readMove() { print(TwoDimString(controller: controller)) print("Move: ", terminator: "") let input = readLine()!.trimmingCharacters(in: .whitespacesAndNewlines) switch input { case "", "s", "step": controller.step() case "cw": controller.rotate(direction: .clockwise) case "ccw": controller.rotate(direction: .counterClockwise) case "l", "left": controller.left() case "r", "right": controller.right() case "drop": controller.drop() case "hold": break default: break } } while true { readMove() }