标签:
#import <Foundation/Foundation.h>
@interface Computer : NSObject
@property(assign,nonatomic) int ComCount;
-(int)Punches;//出拳
@end
#import "Computer.h"
@implementation Computer
-(int)Punches
{
int num=arc4random()%3+1;//随机产生1-3的值
switch (num)
{
case 1:
NSLog(@"计算机出的是石头");
break;
case 2:
NSLog(@"计算机出的是剪刀");
break;
case 3:
NSLog(@"计算机出的是布");
break;
default:
break;
}
return num;
}
@end
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property(assign,nonatomic) int PerCount;
-(int)Punches;
-(int)PlayCompare:(int)Computer and :(int)Person;
@end
#import "Person.h"
@implementation Person
//出拳的种类
-(int)Punches
{
int num1;
NSLog(@"请出拳:1-->石头,2-->剪刀,3-->布");
scanf("%d",&num1);
switch (num1)
{
case 1:
NSLog(@"您出的是石头!");
break;
case 2:
NSLog(@"您出的是剪刀!");
break;
case 3:
NSLog(@"您出的是布!");
break;
default:
break;
}
return num1;
}
//人出拳的方法与计算机随机出拳的方法进行比较
-(int)PlayCompare:(int)Computer and:(int)Person
{
if ((Computer==1&&Person==2)||(Computer==2&&Person==3)||(Computer==3&&Person==1))
{
NSLog(@"计算机赢了!");
return -1;
}
else if (Computer==Person)
{
NSLog(@"平局");
return 0;
}
else
{
NSLog(@"恭喜:您赢了!");
return 1;
}
}
@end
#import <Foundation/Foundation.h>
#import "Computer.h"
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
Computer *computer=[Computer new];
Person *person=[Person new];
int count=0;
int num2;
for (int i=0; i<=20; i++)
{
int first=[person Punches];
int second=[computer Punches];
int third=[person PlayCompare:first and:second];
if (third==1)
{
person.PerCount++;
}
if (third==2)
{
computer.ComCount++;
}
NSLog(@"1-->继续 0-->结束");
scanf("%d",&num2);
count++;
NSLog(@"总共进行了%d局",count);
NSLog(@"人胜了%d局,计算机胜了%d局",person.PerCount,computer.ComCount);
}
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/tmf-4838/p/5256297.html