I'm trying to make a game where you press the back of a card, it displays a random card and then removes that card from the card deck array. When I simulate the program it runs for a random amount of times and then gives me the Index out of range error.
Does anyone see where I have done something wrong?
var cardDeck = [card1, card2, card3 etc....]
var randomCard: Int = 0
#IBAction func row1card5tap(_ sender: UITapGestureRecognizer) {
randomCard = Int.random(in: 0...51)
if cardDeck.contains(cardDeck[randomCard]) {
row1card5.image = cardDeck[randomCard+1]
cardDeck.remove(at: randomCard)
print("removed:" + "\(cardDeck[randomCard])")
} else {
print("card already removed")
}
}
You should make sure the array count is bigger than the requestedIndex + 1 before subscripting
var cardDeck = [card1, card2, card3 etc....]
var randomCard: Int = 0
#IBAction func row1card5tap(_ sender: UITapGestureRecognizer) {
randomCard = Int.random(in: 0...51)
if cardDeck.count > randomCard + 1 {
row1card5.image = cardDeck[randomCard+1]
cardDeck.remove(at: randomCard)
print("removed:" + "\(cardDeck[randomCard])")
} else {
print("card already removed")
}
}
If you want
it displays a random card and then removes that card from the card deck array
then the upper bound of the random array should be always the current number of cards in the deck
var cardDeck = [card1, card2, card3 etc....]
#IBAction func row1card5tap(_ sender: UITapGestureRecognizer) {
if cardDeck.isEmpty { return }
let randomIndex = Int.random(in: 0..<cardDeck.count)
row1card5.image = cardDeck[randomIndex]
cardDeck.remove(at: randomIndex)
}
Related
var players : [Player]?
var currentplayerIndex = 0
var currentQuestion : QuestionObject!
var questions = Questions()
var score = 0
var currentQuestionPos = 0
func updateUi() {
if let score = players?[currentplayerIndex].score {
playerPoints.text = "Points: \(score)"
}
}
func loadnextQuestion () {
if(currentQuestionPos < questions.questions.count) {
currentQuestionPos += 1
if currentplayerIndex < players!.count - 1 {
currentplayerIndex += 1
} else {
currentplayerIndex = 0
}
playerTurn.text = (players?[currentplayerIndex].name)
currentQuestion = questions.questions[currentQuestionPos]
questionText.text = currentQuestion.question
}
}
#IBAction func submitButtonPressed(_ sender: UIButton) {
let i = pickerView.selectedRow(inComponent: 0)
if(currentQuestion.answer == i) {
players?[currentplayerIndex].score += 1
loadnextQuestion()
updateUi()
} else {
updateUi()
loadnextQuestion()
}
}
}
My score displays only 0 all the time.
Does not increase when the answer is right.
All the added players get 1 question each but the sore is still 0 for all the players.
Your code doesn't work because you are doing this series of things when the answer is correct:
players?[currentplayerIndex].score += 1
loadnextQuestion()
updateUi()
The player's score is indeed incremented. But notice that the next thing you do is loadnextQuestion(), which increments currentplayerIndex. This causes updateUi to update the label's text to the score of the next player, not of the player that just answered the question correctly.
One way to fix this is to swap the last two lines:
players?[currentplayerIndex].score += 1
updateUi()
loadnextQuestion()
Notice that this is the same order as in the else branch, which you wrote correctly.
This causes the label to always display the score of the last player, which might not be desirable. Assuming there's not like 100 players or something crazy like that, I think it would be a much better UX if you display all the player's scores:
func updateUi() {
if let scores = players?.map({ "\($0.score)" }) {
playerPoints.text = "Points: \(scores.joined(separator: ", "))"
}
}
You can replace the button action work
#IBAction func submitButtonPressed(_ sender: UIButton) {
let i = pickerView.selectedRow(inComponent: 0)
if(currentQuestion.answer == i) {
players?[currentplayerIndex].score += 1
updateUi()
loadnextQuestion()
} else {
updateUi()
loadnextQuestion()
}
}
Here is the error that I am seeing.
The "cardButton" is responsible for showing the next question. This is a small card app game that I am trying to make and as you can see from the image this is where I am having the issue with the code.
Here is the code:
import UIKit
class MultipleViewController: UIViewController {
#IBOutlet weak var questionLabel2: UILabel!
#IBOutlet var answerButtons: [UIButton]!
#IBOutlet weak var cardButton: UIButton!
#IBAction func cardButtonHandle(_ sender: Any) {
cardButton.isEnabled = true
if questionIdx < count(mcArray) - 1 { // There are still more questions
questionIdx += 1 //
} else {
questionIdx = 0
}
nextQuestion()
}
#IBAction func answerButtonHandle(_ sender: UIButton) {
if sender.titleLabel?.text == correctAnswer{
sender.backgroundColor = UIColor.green
print("Correct!")
} else {
sender.backgroundColor = UIColor.red
print("Wrong Answer")
}
for button in answerButtons{
button.isEnabled = false
if button.titleLabel?.text == correctAnswer {
button.backgroundColor = UIColor.green
}
}
cardButton.isEnabled = true // next question
}
var correctAnswer: String? // correct answers
var answers = [String]() // answers
var question : String? // Questions
var questionIdx = 0 // not sure what this is ?
override func viewDidLoad() {
super.viewDidLoad()
// titleForButtons() // call buttons as soon its loaded..
cardButton.isEnabled = false
nextQuestion()
}
func nextQuestion (){
let currentQuestion = mcArray![questionIdx]
answers = currentQuestion["Answers"] as! [String]
correctAnswer = currentQuestion["CorrectAnswer"] as? String
question = currentQuestion["Question"] as? String
titleForButtons ()
}
func titleForButtons (){
for (idx,button) in answerButtons .enumerated() {
button.titleLabel?.lineBreakMode = .byWordWrapping
button.setTitle(answers[idx],for:.normal)
button.isEnabled = true
}
questionLabel2.text = question
}
}
The following should work, you did not have the correct syntax for the length of the array. Note that if you have not initialized your questions array, this would cause a crash. Therefore you might want to add a guard into your code. Perhaps use the following
#IBAction func cardButtonHandle(_ sender: Any) {
cardButton.isEnabled = true
if questionIdx < (mcArray!.count) - 1 { // There are still more questions
questionIdx += 1 //
} else {
questionIdx = 0
}
nextQuestion()
}
I want to show the first item on viewdidload() and then have the user go to the next item in the array and have the option to go back and disable the back button if it’s the first index and disable the button if its the last index. Here is my code
#IBAction func back(_ sender: Any) {
c -= 1
if c == thkrArray.count{
nextButton.isEnabled = false
}
if c == 0{
backButton.isEnabled = false
}
let thkr: String = thkrArray[c]
Text1.text = thkr
}
#IBAction func A2(_ sender: Any) {
c += 1
if c == thkrArray.count{
nextButton.isEnabled = false
}
if c == 0 {
backButton.isEnabled = false
}
if c > 0{
backButton.isEnabled = false
}
let thkr: String = thkrArray[c]
Text1.text = thkr
}
The easiest way to achieve this is to add an observer to c:
var c: Int = 0 {
didSet {
Text1.text = thkrArray[c]
backButton.isEnabled = c > 0
nextButton.isEnabled = c < thkrArray.count - 1
}
}
#IBAction func back(_ sender: Any) {
c -= 1
}
#IBAction func A2(_ sender: Any) { // isn't this `next`?
c += 1
}
override func viewDidLoad() {
super.viewDidLoad()
c = 0
}
In my quiz app, if I wanted to keep track of the question number the user was on and display it, how would I do so with my following code? So if the user has answered two questions when the third appears I want it to display "question number 3". Basically I want the user to know what number question they are on and it should be equivalent to the number of questions they've answered plus one.
Here's my code:
import UIKit
class ViewController: UIViewController {
var questionList = [String]()
func updateCounter() {
counter -= 1
questionTimer.text = String(counter)
if counter == 0 {
timer.invalidate()
wrongSeg()
}
}
func randomQuestion() {
//random question
if questionList.isEmpty {
questionList = Array(QADictionary.keys)
}
let rand = Int(arc4random_uniform(UInt32(questionList.count)))
questionLabel.text = questionList[rand]
//matching answer values to go with question keys
var choices = QADictionary[questionList[rand]]!
questionList.remove(at: rand)
//create button
var button:UIButton = UIButton()
//variables
var x = 1
rightAnswerBox = arc4random_uniform(4)+1
for index in 1...4
{
button = view.viewWithTag(index) as! UIButton
if (index == Int(rightAnswerBox))
{
button.setTitle(choices[0], for: .normal)
}
else {
button.setTitle(choices[x], for: .normal)
x += 1
}
}
randomImage()
}
let QADictionary = ["Who is Thor's brother?" : ["Atum", "Loki", "Red Norvell", "Kevin Masterson"], "What is the name of Thor's hammer?" : ["Mjolinr", "Uru", "Stormbreaker", "Thundara"], "Who is the father of Thor?" : ["Odin", "Sif", "Heimdall", "Balder"]]
//wrong view segue
func wrongSeg() {
performSegue(withIdentifier: "incorrectSeg", sender: self)
}
//proceed screen
func rightSeg() {
performSegue(withIdentifier: "correctSeg", sender: self)
}
//variables
var rightAnswerBox:UInt32 = 0
var index = 0
//Question Label
#IBOutlet weak var questionLabel: UILabel!
//Answer Button
#IBAction func buttonAction(_ sender: AnyObject) {
if (sender.tag == Int(rightAnswerBox))
{
rightSeg()
timer.invalidate()
print ("Correct!")
}
if counter != 0 {
counter = 15
}
else if (sender.tag != Int(rightAnswerBox)) {
wrongSeg()
print ("Wrong!")
timer.invalidate()
questionList = []
}
}
override func viewDidAppear(_ animated: Bool)
{
randomQuestion()
questionTimer.text = String(counter)
timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector: #selector(ViewController.updateCounter), userInfo: nil, repeats: true)
}
//variables
var counter = 15
var timer = Timer()
#IBOutlet weak var questionTimer: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
Take counter for total number of question globally and update it in random question like
var answerdQuestion = 1;
func randomQuestion() {
//random question
if questionList.isEmpty {
questionList = Array(QADictionary.keys)
}
lblQuestionNumber.text = Strint(answerdQuestion)
answerdQuestion += 1
:
:
}
In my code I am attempting to just set the text of a UILabel. The function doing this is being called, and everything inside it executes as expected, except for the UILabel.text assignment. Below is the gist of the code being run:
override func viewDidAppear(animated: Bool) {
var count2 = count
ref.child(String(count2-3)).observeSingleEventOfType(.Value, withBlock: { (snapshot2) in
var hitterRef = snapshot2.childSnapshotForPath("/Hitter")
var pitcherRef = snapshot2.childSnapshotForPath("/Pitcher")
var playRef = snapshot2.childSnapshotForPath("/Play")
var play = String(playRef.childSnapshotForPath("/Name").value!)
var outs = playRef.childSnapshotForPath("/Outs").value! as! Int
var rbi = playRef.childSnapshotForPath("/RBI").value! as! Int
assignScore(play, outs: outs, rbis: rbi, pitcherSelected: pitcherSelected, ResultsLabel: ResultsLabel, playName: playName, pointDiff: pointDiff)
})
print("successful delay")
count2++
}
func assignScore(play:String, outs:Int, rbis:Int, pitcherSelected:Bool, ResultsLabel: UILabel, playName: UILabel, pointDiff: UILabel){
var playScore = 0.0
print("assign score is running")
if(pitcherSelected == true) {
/*
*Below section covers the outs of the batter selected
*/
//below means that the user has selected the batter
if(outs == 0){
if(play == "Walk") {
//this means that the pitcher walked the batter
playScore -= 0.5
}else {
//means just a regular out
//nothing happens
}
}
}
ResultsLabel.text = "Test Text"
print("Below text test")
}