黑马程序员技术交流社区
标题:
猜拳游戏
[打印本页]
作者:
muyan091115
时间:
2016-5-24 23:37
标题:
猜拳游戏
一共8个文件
main。m
#import <Foundation/Foundation.h>
#import "Judge.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Player *player = [Player new];
player->_name = @"王瑶";
player->_score = 0;
Robot *robot = [Robot new];
robot->_name = @"呆瓜";
robot->_score = 0;
Judge *judge = [Judge new];
judge->_name = @"黑哨";
while (1) {
[player show];
[robot show];
[judge show];
[judge caiJueWithPlayer :player andRobot:robot];
}
}
return 0;
}
复制代码
Player。h
#import <Foundation/Foundation.h>
#import "Select.h"
@interface Player : NSObject{
@public
NSString *_name;
Select _select;
int _score;
}
- (void)show;
- (NSString *)showSelectWith :(Select)select;
@end
复制代码
Player。m
#import "Player.h"
@implementation Player
- (void)show{
NSLog(@"\n亲爱的玩家[%@],请选择你要出的拳头 1,剪刀 2,石头 3,布:",_name);
scanf("%d",&_select);
while (_select<1 || _select>3) {
NSLog(@"请重新输入:");
scanf("%d",&_select);
}
NSLog(@"\n玩家[%@]出的拳头是:%@",_name,[self showSelectWith :_select]);
}
- (NSString *)showSelectWith :(Select)select{
if(select == 1){
return @"剪刀";
}else if (select == 2){
return @"石头";
}else{
return @"布";
}
}
@end
复制代码
Robot。h
#import <Foundation/Foundation.h>
#import "Select.h"
#import "stdlib.h"
@interface Robot : NSObject{
@public
NSString *_name;
Select _select;
int _score;
}
- (void)show;
- (NSString *)showWithSelect :(Select)select;
@end
复制代码
Robot。m
#import "Robot.h"
@implementation Robot
- (void)show{
_select = arc4random_uniform(3)+1;
NSLog(@"\n机器人[%@]出的拳头是:%@",_name,[self showWithSelect :_select]);
}
- (NSString *)showWithSelect:(Select)select {
if(select == 1){
return @"剪刀";
}else if (select == 2){
return @"石头";
}else{
return @"布";
}
}
@end
复制代码
作者:
muyan091115
时间:
2016-5-24 23:38
Judge。h
#import <Foundation/Foundation.h>
#import "Player.h"
#import "Robot.h"
@interface Judge : NSObject{
@public
NSString *_name;
}
- (void)show;
- (void)caiJueWithPlayer :(Player *)player andRobot:(Robot *)robot;
@end
复制代码
judge。m
#import "Judge.h"
@implementation Judge
- (void)show{
NSLog(@"\n我是裁判[%@],我来宣布结果!",_name);
}
- (void)caiJueWithPlayer :(Player *)player andRobot:(Robot *)robot{
if (player->_select - robot->_select == -2 || player->_select - robot->_select == 1) {
NSLog(@"恭喜选手[%@]获得胜利!",player->_name);
player->_score++;
NSLog(@"目前比分 : [%@]%d : [%@]%d",
player->_name,player->_score,robot->_name,robot->_score);
}else if (player->_select == robot->_select){
NSLog(@"平局");
NSLog(@"目前比分 : [%@]%d : [%@]%d",
player->_name,player->_score,robot->_name,robot->_score);
}else{
NSLog(@"恭喜机器人[%@]获得胜利!",robot->_name);
robot->_score++;
NSLog(@"目前比分 : [%@]%d : [%@]%d",
player->_name,player->_score,robot->_name,robot->_score);
}
}
@end
复制代码
select。h
typedef enum{
SelectJianDao = 1,
SelectShiTou = 2,
SelectBu = 3
} Select ;
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2