简体   繁体   English

简单游戏的逻辑问题,我碰壁了,不知道该如何克服……SOS!

[英]Problem with a simple game's logic, I have hit a wall, and don't know how to get over it…SOS!

Ok, so I picked up this iOS app dev project for something to work on on the side, and it is materializing into something that is uglier than I was anticipating. 好的,所以我选择了这个iOS应用程序开发项目,以便可以在一边工作,并且正在变成比我预期的更丑陋的东西。 I have a simple simple game that I have built where I present a bit of text, and the user identifies what it is, by selecting one of four buttons. 我有一个简单的简单游戏,可以在其中显示一些文字,并且用户可以通过选择四个按钮之一来识别它是什么。

Now, I reckon a pickerview here would be a lot easier, but for design purposes, I am going to try and get my way through it doing it programmatically with buttons. 现在,我认为这里的pickerview会容易得多,但是出于设计目的,我将尝试通过按钮来以编程方式完成它。 So let me take a second to walk you through where I am. 所以,让我花点时间向您介绍我的位置。

When the user gets to the game screen, I have passed in two different arrays, and a number. 当用户进入游戏屏幕时,我传递了两个不同的数组和一个数字。 The first array contains 25 items of text, the second array contains 25 items of answers (that correspond with the first array as far as index goes), and then the number is the number of rounds the user wishes to play, with 25 being the max. 第一个数组包含25个文本项,第二个数组包含25个答案项(就索引而言,与第一个数组相对应),然后数字是用户希望玩的回合数,其中25个是最大。

Once the view loads, I take the number of rounds they have selected, and send it off to a method that returns an array of that number size, shuffled using the arc4random() %roundNumber method that I use as a means to access the index of the array I pass in. 视图加载后,我将选择的回合数发送给一个返回该数字大小的数组的方法,并使用arc4random() %roundNumber方法将其改组,该方法用作访问索引的方法我传入的数组。

This works really well. 这真的很好。 I can effectively grab at random from the source array, and after I present it, I remove the index from the index array so as to prevent duplication, and life is good. 我可以有效地从源数组中随机获取,并且在展示它之后,我从索引数组中删除了索引,以防止重复,并且生活很顺利。

THE PROBLEM: The buttons. 问题:按钮。 I don't know why, but I am having a real problem getting the buttons to populate randomly, and contain the correct answer as well. 我不知道为什么,但是让按钮随机填充并包含正确答案也确实存在问题。 The main problem that I am experiencing is the fact that I can't figure out how to randomly assign a value to the button. 我遇到的主要问题是我无法弄清楚如何为按钮随机分配一个值。 Meaning, the buttons have names, button1, button2, button3, and button4. 意思是,按钮的名称分别为button1,button2,button3和button4。 And because I am programmatically setting their titles, and I need to "hardcode" the value of the correct answer into a button, it turns out that the right answer button is the same every time. 而且由于我是以编程方式设置其标题的,并且需要将正确答案的值“硬编码”到按钮中,因此,每次正确的答案按钮都是相同的。

This really just boils down to pure thinking and logic, and quite frankly at this point I am running low. 这实际上可以归结为纯粹的思维和逻辑,坦率地说,在这一点上,我的工作量很低。 I am using a counter variable to access and keep track of which spot in the index is the correct answer, but I just can't figure out the rest of the puzzle. 我正在使用一个计数器变量来访问并跟踪索引中哪个位置是正确的答案,但我只是无法弄清楚其余的难题。

Below is what I have come up with...but I'm not too proud of it. 下面是我想出的...但是我对此并不感到骄傲。 I have just been retrying different things and now all I have is a mess: 我一直在尝试不同的事情,现在我所遇到的只是一团糟:

    for(int i = 0; i < 4; i++){

            //digits is an array of size 4 that is populated and shuffled in a similar manner to above

            switch ([[digits objectAtIndex:i] intValue]) {
                case 0:
                    [button1 setTitle:[NSString stringWithFormat:@"%@",[self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]]forState: UIControlStateNormal];

                    NSLog(@"1 %@", [self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]);
                    break;
                case 1:
                    [button2 setTitle:[NSString stringWithFormat:@"%@",[self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]]forState: UIControlStateNormal];

                    NSLog(@"2 %@", [self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]);
                    break;
                case 2:
                    [button3 setTitle:[NSString stringWithFormat:@"%@",[self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]]forState: UIControlStateNormal];

                    NSLog(@" %@", [self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]);
                    break;
                case 3:
                                           //note this is where I hard code the correct value
                    [button4 setTitle:[self.refList objectAtIndex:counter] forState: UIControlStateNormal];

                    NSLog(@"4 %@", [self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]);
                    break;
                default:
                    break;
            }

    }

I really appreciate any help on this, and I look forward to having this mystery revealed. 我对此表示感谢,并希望揭开这个谜底。

Thanks! 谢谢!

Step 1 - create four buttons, set frames and background colors appropriately. 步骤1-创建四个按钮,适当设置框架和背景颜色。 Set tags on the four buttons 0, 1, 2, 3. Set all buttons to one action for touch up inside. 在四个按钮0、1、2、3上设置标签。将所有按钮设置为一个动作以进行内部修饰。

Step 2 - generate a random number from 0 to 3. Use that value as an index into the array to select a button to hold the correct answer. 步骤2-生成一个介于0到3之间的随机数。使用该值作为数组的索引,以选择一个按钮来保存正确的答案。 Set the correct answer as title. 设置正确的答案作为标题。 Set incorrect answers on the remaining buttons. 在其余按钮上设置不正确的答案。 (you might use NSSet's setWithArray to get all the buttons as a set - after removing the chosen one from the set, use allObjects to get an array of the remaining buttons) (您可以使用NSSet的setWithArray来获取所有按钮作为一个集合-从集合中删除选定的按钮后,使用allObjects获取其余按钮的数组)

Step 3 - A button is pressed - what is its tag? 步骤3-按下按钮-它的标签是什么? If it matches the random number, you have a winner. 如果它与随机数匹配,那么您就有赢家。

Every UIView and subclass has a tag member. 每个UIView和子类都有一个标记成员。 Setting the tag is easy: 设置标签很容易:

myButton.tag = 0 ; myButton.tag = 0 ;

checking it is just as easy: 检查它很容易:

if(unknownButton.tag == randomNumber)
  // winner

You get all the buttons to trigger one action method so that's where the tag is checked. 您将获得所有按钮来触发一种操作方法,因此可以在此处检查标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM