Coding with xcode 9.2 version command line.
An index out of range error occurred in the makeWall in the makeLadder function, so we are not receiving the results properly. Where should I modify to get the right result?ㅠ<
Example)
How many people will participate?
3
What is the maximum ladder height?
4
//Current Status
| | |
| |-|
|-| |
|
//Results to come out
| | |
| |-|
|-| |
|-| |
Code
import Foundation
// Get Participant Input
func getPlayer() -> Int {
print("How many people will participate?")
let player = readLine()
return Int(player ?? "") ?? 0
}
// Get ladder height input
func getLadderHeight() -> Int {
print("What is the maximum ladder height?"")
let ladderHeight = readLine()
return Int(ladderHeight ?? "") ?? 0
}
// Ladder scaffolding preparation - serve
// An array with a false value is randomly changed from false to true according to a random value
func randomChange(_ arrLadder:[Bool]) -> [Bool]{
var ladderArr:[Bool] = arrLadder
for val in 0 ..< ladderArr.count {
let random = arc4random_uniform(2)
ladderArr[val] = (random == 0)
}
return ladderArr
}
// Ladder scaffolding preparation - main
func makeLadderFrame(_ player:Int, _ ladderHeight:Int) -> [[Bool]] {
var arrLadder: [[Bool]] = Array(repeating: Array(repeating: false, count: player - 1), count: ladderHeight)
return arrLadder
}
func preparadeToMakeFootBoard(_ ladderHeight: Int, _ player: Int) -> [[Bool]] {
var arrLadder = makeLadderFrame(player,ladderHeight)
for row in 0 ..< ladderHeight {
arrLadder[row] = randomChange(arrLadder[row])
}
return arrLadder
}
// Making a ladder scaffold
// scaffolding element
enum LadderFootBoardElements: String {
case hasFootBoard = "-"
case noFootBoard = " "
}
// Replace the non-value two-dimensional array created by readyToMakeFootBoard with scaffolding elements
func transferBoolToHorizon(booleanElement: Bool) -> String {
return booleanElement ? LadderFootBoardElements.hasFootBoard.rawValue : LadderFootBoardElements.noFootBoard.rawValue
}
// Completing the Ladder Shape - Serve
func makeWall(_ readyFootBoard:[Bool],_ player: Int) {
for val in 0 ..< player - 1 {
let footboard = transferBoolToHorizon(booleanElement: readyFootBoard[val])
print ("\(footboard)", terminator : "|")
}
}
// Completing the Ladder Shape - Main
func makeLadder(_ player: Int, _ ladderHeight:Int) {
let readyFootBoard = preparadeToMakeFootBoard(player, ladderHeight)
for row in 0 ..< ladderHeight {
print ("|", terminator: "")
makeWall(readyFootBoard[row], player)
print()
}
}
// main (executive)
while true {
let player = getPlayer()
let ladderHeight = getLadderHeight()
makeLadder(player, ladderHeight)
break
}
// The factor that this function receives is the value of ladderHeight with the player value, and the value of ladderHeight with the index out of range error.
func preparadeToMakeFootBoard(_ ladderHeight: Int, _ player: Int) -> [[Bool]] {
var arrLadder = makeLadderFrame(player,ladderHeight)
for row in 0 ..< ladderHeight {
arrLadder[row] = randomChange(arrLadder[row])
}
return arrLadder
}
//
let readyFootBoard = preparadeToMakeFootBoard(player, ladderHeight)
This is the source that changed the factor received in the preparadeToMakeFootBoard function and removed the -1 from the player value. (The player value is set to the height value, so I changed it to the minus source.)
// Corrected source
//
// // main.swift
// // af
//
// // Created by maccli1 on 2018. 1. 22..
// Copyright © 2018 my young. All rights reserved.
//
import Foundation
// Get Participant Input
func getPlayer() -> Int {
print("How many people will participate?")
let player = readLine()
return Int(player ?? "") ?? 0
}
// Get ladder height input
func getLadderHeight() -> Int {
print("What is the maximum ladder height?"")
let ladderHeight = readLine()
return Int(ladderHeight ?? "") ?? 0
}
// Ladder scaffolding preparation - serve
// An array with a false value is randomly changed from false to true according to a random value
func randomChange(_ arrLadder:[Bool]) -> [Bool]{
var ladderArr:[Bool] = arrLadder
for val in 0 ..< ladderArr.count {
let random = arc4random_uniform(2)
ladderArr[val] = (random == 0)
}
return ladderArr
}
// Ladder scaffolding preparation - main
func makeLadderFrame(_ player:Int, _ ladderHeight:Int) -> [[Bool]] {
let arrLadder: [[Bool]] = Array(repeating: Array(repeating: false, count: player), count: ladderHeight)
return arrLadder
}
func preparadeToMakeFootBoard(_ player: Int, _ ladderHeight: Int) -> [[Bool]] {
var arrLadder = makeLadderFrame(player,ladderHeight)
for row in 0 ..< ladderHeight {
arrLadder[row] = randomChange(arrLadder[row])
}
return arrLadder
}
// Making a ladder scaffold
// scaffolding element
enum LadderFootBoardElements: String {
case hasFootBoard = "-"
case noFootBoard = " "
}
// Replace the non-value two-dimensional array created by readyToMakeFootBoard with scaffolding elements
func transferBoolToHorizon(booleanElement: Bool) -> String {
return booleanElement ? LadderFootBoardElements.hasFootBoard.rawValue : LadderFootBoardElements.noFootBoard.rawValue
}
// Completing the Ladder Shape - Serve
func makeWall(_ readyFootBoard:[Bool],_ player: Int) {
for val in 0 ..< player {
let footboard = transferBoolToHorizon(booleanElement: readyFootBoard[val])
print ("\(footboard)", terminator : "|")
}
}
// Completing the Ladder Shape - Main
func makeLadder(_ player: Int, _ ladderHeight:Int) {
let readyFootBoard = preparadeToMakeFootBoard(player, ladderHeight)
for row in 0 ..< ladderHeight {
print ("|", terminator: "")
makeWall(readyFootBoard[row], player)
print()
}
}
// main (executive)
while true {
let player = getPlayer()
let ladderHeight = getLadderHeight()
makeLadder(player, ladderHeight)
break
}
// result value
How many people will participate?
3
What is the maximum ladder height?
4
| | |-|
| |-| |
| |-| |
| | | |
The intentions may be different, and if there is anything wrong, please leave a comment and we will additionally help you.
© 2024 OneMinuteCode. All rights reserved.