I am attempting to make a TicTacToe game. I'm trying to use this checkForWinner
function but keep getting errors. Any help is greatly appreciated.
Here's my code
class ViewController: UIViewController {
@IBOutlet weak var CurrentLetterLabel: UILabel!
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var label3: UILabel!
@IBOutlet weak var label4: UILabel!
@IBOutlet weak var label5: UILabel!
@IBOutlet weak var label6: UILabel!
@IBOutlet weak var label7: UILabel!
@IBOutlet weak var label8: UILabel!
@IBOutlet weak var label9: UILabel!
@IBOutlet weak var label10: UILabel!
@IBOutlet weak var label11: UILabel!
let letter = "XOXOXOXOXO"
var moveCounter = 0
var currentLetter:Character!
var labelArray = [UILabel]()
override func viewDidLoad() {
super.viewDidLoad()
labelArray.append(label1)
labelArray.append(label2)
labelArray.append(label3)
labelArray.append(label4)
labelArray.append(label5)
labelArray.append(label6)
labelArray.append(label7)
labelArray.append(label8)
labelArray.append(label9)
labelArray.append(label10)
labelArray.append(label11)
getCharacter()
}
func getCharacter(){
let myRange = Range<String.Index>(start: letter.startIndex.advancedBy(moveCounter), end: letter.startIndex.advancedBy(moveCounter + 1))
CurrentLetterLabel.text = letter.substringWithRange(myRange)
currentLetter = Character(letter.substringWithRange(myRange))
}
func clearLabels(Action:UIAlertAction){
label1.text = ""
label2.text = ""
label3.text = ""
label4.text = ""
label5.text = ""
label6.text = ""
label7.text = ""
label9.text = ""
label10.text = ""
label11.text = ""
}
func presentWinningAlert(){
moveCounter = 0
let alert = UIAlertController(title: "", message: "byr", preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: clearLabels)
alert.addAction(okAction)
presentViewController(alert, animated: true, completion: nil)
}
@IBAction func onTapped(sender: UITapGestureRecognizer) {
print("Here")
let selectedPoint = sender.locationInView(self.view)
print(selectedPoint)
for label in labelArray{
if CGRectContainsPoint(label1.frame, selectedPoint){
label1.text = String(currentLetter)
}
if CGRectContainsPoint(label2.frame, selectedPoint){
label2.text = String(currentLetter)
}
if CGRectContainsPoint(label3.frame, selectedPoint){
label3.text = String(currentLetter)
}
if CGRectContainsPoint(label4.frame, selectedPoint){
label4.text = String(currentLetter)
}
if CGRectContainsPoint(label5.frame, selectedPoint){
label5.text = String(currentLetter)
}
if CGRectContainsPoint(label6.frame, selectedPoint){
label6.text = String(currentLetter)
}
if CGRectContainsPoint(label7.frame, selectedPoint){
label7.text = String(currentLetter)
}
if CGRectContainsPoint(label8.frame, selectedPoint){
label8.text = String(currentLetter)
}
if CGRectContainsPoint(label9.frame, selectedPoint){
label9.text = String(currentLetter)
}
if CGRectContainsPoint(label10.frame, selectedPoint){
label10.text = String(currentLetter)
}
}
moveCounter++
if moveCounter == 9{
presentWinningAlert()
}
getCharacter()
}
func checkForWinner(){
if label1.text == label2.text && label2.text == label3.text && label3.text != "" {
presentWinningAlert("\(label3.text!)")
}
else if label4.text == label5.text && label5.text == label6.text && label6.text != "" {
presentWinningAlert("\(label6.text!)")
}
else if label7.text == label8.text && label8.text == label9.text && label9.text != "" {
presentWinningAlert("\(label9.text!)")
}
else if label1.text == label4.text && label4.text == label7.text && label7.text != "" {
presentWinningAlert("\(label7.text!)")
}
else if label2.text == label5.text && label5.text == label8.text && label8.text != "" {
presentWinningAlert("\(label8.text!)")
}
else if label3.text == label6.text && label6.text == label9.text && label9.text != "" {
presentWinningAlert("\(label9.text!)")
}
else if label1.text == label5.text && label5.text == label9.text && label9.text != "" {
presentWinningAlert("\(label9.text!)")
}
else if label3.text == label5.text && label5.text == label7.text && label7.text != "" {
presentWinningAlert("\(label7.text!)")
}
if moveCounter == 9
{
presentWinningAlert("No One")
}
}
}
You are calling the function with a string parameter, but the function does not take the string parameter. Either the call should be
presentWinningAlert()
without passing any arguments or the function should be modified to
func presentWinningAlert(title: String){
moveCounter = 0
let alert = UIAlertController(title: title, message: "byr", preferredStyle: UIAlertControllerStyle.Alert)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With