码迷,mamicode.com
首页 > 其他好文 > 详细

MRC实现猜拳游戏(附:@property理解偏差纠正)

时间:2015-09-01 01:29:16      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

  1 /*  猜拳游戏
  2     对象1:人,  属性:输入要出的拳并显示,分数,姓名
  3     对象2:Bot,属性同上,继承。自动生成出拳,枚举法。显示出拳,与传过来的人的出拳作比较,判定结果并输出分数。
  4  其他:每比较一次提示是否继续。
  5  
  6  注意:MRC!
  7  */
  8 
  9 #import <Foundation/Foundation.h>
 10 #import "Bot.h"
 11 int main(int argc, const char * argv[])
 12 {
 13     Person* p1 = [[Person alloc]init];
 14     p1.name = @"jack";
 15     p1.score = 0;
 16     
 17     Bot* b1 = [[Bot alloc]init];
 18     b1.score = 0;
 19     char flag = 0;
 20     
 21     do
 22     {
 23         [p1 fistSltAndShow];
 24         [b1 fistSltAndShow:p1];
 25         NSLog(@"继续请按y,结束请按n。");
 26         rewind(stdin);
 27         scanf("%c",&flag);
 28     }while (flag != n && flag != N);
 29     
 30     [b1 release];
 31     [p1 release];
 32     return 0;
 33 }
 34 //Person.h
 35 #import <Foundation/Foundation.h>
 36 typedef enum
 37 {
 38     kJianDao = 1,
 39     kShiTou,
 40     kBu
 41 }Fist;
 42 
 43 @interface Person : NSObject
 44 @property(nonatomic, retain) NSString* name;
 45 @property(nonatomic, assign) int score;
 46 @property(nonatomic, assign) Fist fist;
 47 - (void)fistSltAndShow;
 48 @end
 49 //
 50 //  Person.m
 51 
 52 #import "Person.h"
 53 
 54 @implementation Person
 55 - (void)dealloc
 56 {
 57     NSLog(@"人挂了");
 58     [_name release];
 59     [super dealloc];
 60 }
 61 - (void)fistSltAndShow
 62 {
 63     do
 64     {
 65     NSLog(@"请选择出拳:1-剪刀,2-石头,3-布");
 66     _fist = -1;
 67     rewind(stdin);
 68     scanf("%d",&_fist);
 69     }while (_fist < 1 || _fist > 3);
 70     NSLog(@"%@出拳为:%@", _name, (_fist == 1 )?@"剪刀":(_fist == 2 ? @"石头" : @""));
 71 }
 72 @end
 73 //Bot.h
 74 #import "Person.h"
 75 
 76 @interface Bot : Person
 77 - (void)fistSltAndShow:(Person*)person;
 78 @end
 79 //
 80 //  Bot.m
 81 #import "Bot.h"
 82 #import <stdlib.h>
 83 @implementation Bot
 84 - (void)fistSltAndShow:(Person*)person
 85 {
 86     self.fist = arc4random_uniform(3) + 1;
 87     NSLog(@"bot出拳为:%@", (self.fist == 1 )?@"剪刀":(self.fist == 2 ? @"石头" : @""));
 88     if ((self.fist - person.fist == 1)|| (-2 == self.fist - person.fist) )//1 - 3   2 - 1  3 - 2
 89     {
 90         self.score++;
 91         NSLog(@"bot赢了");
 92     }else if (0 == self.fist - person.fist)
 93     {
 94         NSLog(@"平局");
 95     }else
 96     {
 97         person.score++;
 98         NSLog(@"%@赢了",person.name);
 99     }
100     NSLog(@"%@:%d-----bot:%d",person.name , person.score, self.score);
101 }
102 - (void)dealloc
103 {
104     NSLog(@"Bot挂了");
105     [super dealloc];
106 }
107 @end

技术分享

老乡华威ppt图片分享:

技术分享

MRC实现猜拳游戏(附:@property理解偏差纠正)

标签:

原文地址:http://www.cnblogs.com/Dast1/p/4774659.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!