How do I save the background color in NSUserDefaults and write code for game clear conditions?

Asked 2 years ago, Updated 2 years ago, 139 views

I'm a beginner in programming.
There are some things I don't understand while making games as a hobby.

The four colors below are buttons.(I plan to increase it to 5 colors next time.)
Each color button you press can reflect the color of the button you press on the mass you touch.

1
If you turn off the application task, it will start again from the beginning.
I would like to save the color of the squares on the smartphone itself with NSUserDefaults so that I can continue even if I turn off the application task.
In that case, is it okay to save every time I touch the mass?
Also, what kind of code should I write when I save the color?

2
I want to clear the game only if the colors of the adjacent squares are different when all squares are filled.
Nothing happens if the colors of adjacent squares match, and only if all adjacent squares are different.
I want to move the screen to clear the game.
In this case, should I wave the tag for each square?
Also, how should I write the code for this condition?

These are the two points.
I'm sorry that I did two things at once.
I don't understand these two points by myself now, so please let me know.

It would be very helpful if you could write the code for both of them.
Just in case, I'll put the code and the image that I'm writing now.

I'm sorry that the question is difficult to understand.

I look forward to hearing from you.

Enter a description of the image here

myImageA=UIColor.redColor();
         myImageB=UIColor.greenColor();
         myImageC=UIColor.blueColor();
         myImageE=UIColor.yellowColor();


        myImageView.backgroundColor=myImageD
        myImageView 2.backgroundColor=myImageD
        myImageView 3.backgroundColor=myImageD
        myImageView 4.backgroundColor=myImageD
        myImageView 5.backgroundColor=myImageD
        myImageView 6.backgroundColor=myImageD

        myImageView 8.backgroundColor=myImageD
        myImageView 9.backgroundColor=myImageD
        myImageView 10.backgroundColor=myImageD
        myImageView 11.backgroundColor=myImageD
        myImageView 12.backgroundColor=myImageD
        myImageView 13.backgroundColor=myImageD
         myImageView 14.backgroundColor=myImageD
        myImageView 15.backgroundColor=myImageD
        myImageView 16.backgroundColor=myImageD

        myImageViewa.backgroundColor=myImageD
        myImageView2a.backgroundColor=myImageD
        myImageView 3a.backgroundColor=myImageD
        myImageView4a.backgroundColor=myImageD
        myImageView 5a.backgroundColor=myImageD
        myImageView 6a.backgroundColor=myImageD
        myImageView 7a.backgroundColor=myImageD
        myImageView 8a.backgroundColor=myImageD
        myImageView 9a.backgroundColor=myImageD
        myImageView 10a.backgroundColor=myImageD
        myImageView 12a.backgroundColor=myImageD
        myImageView 14a.backgroundColor=myImageD
        myImageView 13a.backgroundColor=myImageD
        myImageView 15a.backgroundColor=myImageD
        myImageView 16a.backgroundColor=myImageD

        myImageViewb.backgroundColor=myImageD
        myImageView 2b.backgroundColor=myImageD
        myImageView 3b.backgroundColor=myImageD
        myImageView 4b.backgroundColor=myImageD
        myImageView 5b.backgroundColor=myImageD
        myImageView 6b.backgroundColor=myImageD
        myImageView 7b.backgroundColor=myImageD
        myImageView 8b.backgroundColor=myImageD
        myImageView 9b.backgroundColor=myImageD
        myImageView 10b.backgroundColor=myImageD
        myImageView 11b.backgroundColor=myImageD
        myImageView 13b.backgroundColor=myImageD
        myImageView 14b.backgroundColor=myImageD
        myImageView 15b.backgroundColor=myImageD
        myImageView 16b.backgroundColor=myImageD


        myImageView 2c.backgroundColor=myImageD
        myImageView 3c.backgroundColor=myImageD
        myImageView 4c.backgroundColor=myImageD
        myImageView 5c.backgroundColor=myImageD
        myImageView 6c.backgroundColor=myImageD
        myImageView 7c.backgroundColor=myImageD
        myImageView 8c.backgroundColor=myImageD
        myImageView 9c.backgroundColor=myImageD
        myImageView 10c.backgroundColor=myImageD
        myImageView 11c.backgroundColor=myImageD
        myImageView 12c.backgroundColor=myImageD
        myImageView 13c.backgroundColor=myImageD
        myImageView 14c.backgroundColor=myImageD
        myImageView 15c.backgroundColor=myImageD
        myImageView 16c.backgroundColor=myImageD






    }



    funconClickMyButton (sender:UIButton) {


        myImageD=myImageA


        }


    funconClickMyButton2(sender:UIButton){

        myImageD=myImageB

        }


    funconClickMyButton3(sender:UIButton){

        myImageD=myImageC

    }

    funconClickMyButton4(sender:UIButton){

        myImageD=myImageE

    }


    override functouchesBegan(touches:Set<UITouch>, withEvent:UIEvent?) {
        let touch=touches.first as UITouch?

        iflet touchedImageView=touch!.view as?UIImageView{

            touchedImageView.backgroundColor=myImageD


        }




    }






    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

There are four colors to save, so should I prepare four forkies for that?
If there is only one forky, will all the colors in the saved location be the same?

ios swift iphone swift2

2022-09-30 21:12

1 Answers

As the amount of explanation is going to be large, I plan to reply over a few days.

I would like to save the color of the squares in the smartphone itself with NSUserDefaults so that I can continue to do so even if I turn off the application task.

Describes how to use the NSUserDefaults class.
First, in NSUserDefaults.standardUserDefaults(), call the instance as singleton (Singleton).To store data in NSUserDefaults,

defaults.setObject("a text value", forKey:"value")

I will send you a method like this.Check the class reference for more information, as the methods vary by type, such as integer values, real values, and NSDate instances.
NSUserDefaults saves data written at the appropriate time to a file, but uses the method synchronize() when you need to force the file to be saved.
To read data from NSUserDefaults,

let theText=defaults.stringForKey("value")

The method is used.This also depends on the type, so look it up in the class reference.
Note that instances of UIColor cannot be stored in NSUserDefaults. The types (classes) that can be stored in NSuserDefaults are limited, and not only UIColor cannot be saved.Color information must be replaced one-to-one with integer values and strings, and then stored in NSUserDefaults.
One-to-one correspondence is useful using an enumerator (enum).

 enum Color: Int {
    case White = 0
    case Red
    case Blue
    case Green
    case Yellow

    func color()->UIColor {
        switchself{
        case.White:
            return UIColor.whiteColor()
        case.Red:
            return UIColor.redColor()
        case.Blue:
            return UIColor.blueColor()
        case.Green:
            return UIColor.greenColor()
        case.Yellow:
            return UIColor.yellowColor()
        }
    }
}

White = 0, Red = 1, Blue = 2, Green = 3, Yellow = 4 and UIColor.

// Obtained an integer value for the color.
Let colorNumber: Int=Color.Blue.rawValue
// Obtained the color UIColor.
let colorValue: UIColor=Color.Red.color()

<January 28, 2016

In this case, we have to save color information for 8×8=64 squares, so let's consider using an array to save the array.
First, the instance variable is an 8x8 two-dimensional array of matrix that replaces the color information with the Int value.

 var matrix: [[Int]]

To save this to NSUserDefaults,

let defaults=NSUserDefaults.standardUserDefaults()
defaults.setObject(matrix, forKey: "Matrix")

To invoke a saved array, use the

let defaults=NSUserDefaults.standardUserDefaults()
if let theArray=defaults.arrayForKey("Matrix")as?[[Int]]{
    matrix=theArray
}

The return value for arrayForKey() is optional.Returns nil if it is not stored in NSUserDefaults at the first launch of the app and so on. nil check and downcast to the appropriate array type

<Additional Notes So far

When to store data in NSUserDefaults depends on the programmer's design (design), I don't think there's a single correct answer."Save every time you touch the mass" is the least worrisome, but there is a risk that code becomes complicated and bugs are likely to occur due to omission.

I want to clear the game only if the colors of the adjacent squares are different when all squares are filled.

To do this, you must first design a structure of program data that can capture adjacent squares.Currently, the design has not been completed, so I don't know what to do at all.
Generally speaking, I can think of a design that expresses the 8x8 squares in a two-dimensional array.Use the previous enumeration Color to create an 8x8 array with the Int type as an element.

 var matrix = [[Int]] (count: 8, repeatedValue: [Int] (count: 8, repeatedValue:0))

Elements in the n-row m column can be retrieved using matrix[m][n], but the next element is

Enter a description of the image here

You can retrieve it like this.(Note that there may be no adjacent squares in the case of the grid at the end.If you try to browse an element in an out-of-range array, the program crashes.)


2022-09-30 21:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.