我正在为设计游戏的女儿制作一个简单的程序。我只是想让 Mac 提供一个从 1 到 6 的随机数,将其与用户的猜测进行比较,然后得到用于确定玩家在棋盘游戏中可以移动多少空间的差值。一切正常,除了程序生成的差异始终为 3。它正确生成随机数并正确读取用户的输入。这是代码。我真的很感激帮助。我对此很陌生,并意识到可能有一个非常简单的答案。我已经搜索和搜索并没有提出解决方案。在某一时刻,它正确地产生了差异,但现在不是。非常感谢!
//
// AstroGuessAppDelegate.h
//
// Created by Trent Evans on 3/7/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
int macPick;
int numberGuess;
int numberDiff;
@interface AstroGuessAppDelegate : NSObject {
IBOutlet NSWindow *window;
IBOutlet id moveResultLabel;
IBOutlet id thinkingLabel;
IBOutlet NSComboBox *numberGuessBox;
}
- (IBAction)compareNumbersAndSendResults:(id)sender;
- (IBAction)macThinkOfNumber:(id)sender;
@end
//
// AstroGuessAppDelegate.m
//
// Created by Trent Evans on 3/7/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "AstroGuessAppDelegate.h"
@implementation AstroGuessAppDelegate
- (IBAction)macThinkOfNumber:(id)sender {
macPick = (arc4random() % 6) + 1;
NSString *thinkingLabelText = [NSString stringWithFormat: @"Ok. I'm thinking of a number."];
[thinkingLabel setStringValue:thinkingLabelText];
}
- (IBAction)compareNumbersAndSendResults:(id)sender {
numberGuess = [numberGuessBox intValue];
numberDiff = macPick - numberGuess;
if (numberDiff<0) {
numberDiff = numberDiff * -1;
}
NSString *moveResultLabelText;
if (numberDiff=0) {
moveResultLabelText = [NSString stringWithFormat:@"My number was %i. Your number was %i. The difference is %i. \nBLAST OFF!\nMove forward 6 spaces", macPick, numberGuess, numberDiff];
}
if (numberDiff=1) {
moveResultLabelText = [NSString stringWithFormat:@"My number was %i. Your number was %i. The difference is %i. \nORBIT!\nMove forward 4 spaces", macPick, numberGuess, numberDiff];
}
if (numberDiff=2) {
moveResultLabelText = [NSString stringWithFormat:@"My number was %i. Your number was %i. The difference is %i. \nRE-ENTRY!\nMove forward 2 spaces", macPick, numberGuess, numberDiff];
}
if (numberDiff=3) {
moveResultLabelText = [NSString stringWithFormat:@"My number was %i. Your number was %i. The difference is %i. \nSPLASHDOWN!\nMove forward 1 space", macPick, numberGuess, numberDiff];
}
else {
moveResultLabelText = [NSString stringWithFormat:@"My number was %i. Your number was %i. The difference is %i. \nBLACK HOLE!\nSorry. You don't get to move.", macPick, numberGuess, numberDiff];
}
[moveResultLabel setStringValue:moveResultLabelText];
}
@end