和电脑玩石头剪刀布
Posted 无梦为安
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了和电脑玩石头剪刀布相关的知识,希望对你有一定的参考价值。
#import <Foundation/Foundation.h>
#import "Judge.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Judge * ju=[Judge new];
[ju caijue];
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property(assign,nonatomic) int ren;
/**
* 带返回值的声明
*
* @return 字符串(剪刀,石头,布)
*/
-(NSString*)personuse;
@end
@implementation Person
/**
* 实现
*
* @return NSstring类型
*/
-(NSString*)personuse
{
NSString *st=[[NSString alloc]init];
switch (self.ren) {
case 1:
st=[NSString stringWithFormat:@"石头"];
break;
case 2:
st=[NSString stringWithFormat:@"剪刀"];
break;
case 3:
st=[NSString stringWithFormat:@"布"];
break;
default:
break;
}
return st;
}
@end
#import <Foundation/Foundation.h>
@interface Comepute : NSObject
@property(assign,nonatomic) int ji; //同人
-(NSString*)jisuse;//同人
@end
#import "Comepute.h"
@implementation Comepute
//同人
-(NSString *)jisuse
{
NSString *st=[[NSString alloc]init];
switch (self.ji) {
case 1:
st=[NSString stringWithFormat:@"石头"];
break;
case 2:
st=[NSString stringWithFormat:@"剪刀"];
break;
case 3:
st=[NSString stringWithFormat:@"布"];
break;
default:
break;
}
return st;
}
@end
#import <Foundation/Foundation.h>
#import "Comepute.h"
#import "Person.h"
#import "Methods.h"
@interface Judge : Methods
-(void)caijue;
@end
#import "Judge.h"
@implementation Judge
-(void)caijue
{
int a=1 ,i;
while (a==1||a==2||a==3) {
NSLog(@"请输入猜拳的方式:1--石头 2--剪刀 3--布");
scanf("%d",&a);
if (a==1||a==2||a==3) {
Person *per=[[Person alloc]init];
per.ren=a;
NSLog(@"人出的是:%@",[per personuse]);
Comepute *com=[Comepute new];
com.ji=arc4random()%3+1;
NSLog(@"计算机出的是:%@",[com jisuse]);
[super personwithperson:per.ren andcomeputerwithcomputer:com.ji];
i++;
}
else{
NSLog(@"输入错误,游戏结束");
}
}
NSLog(@"猜拳的次数是:%d 人胜利的次数是:%d 计算机胜利的次数是:%d 平局的次数:%d",i,super.rensheng,super.jisuansheng,i-(super.rensheng+super.jisuansheng));
}
@end
#import <Foundation/Foundation.h>
#import "Person.h"
#import "Comepute.h"
@interface Methods : NSObject
@property(assign,nonatomic) int rensheng;
@property(assign,nonatomic) int jisuansheng;
-(void)personwithperson:(int) a andcomeputerwithcomputer:(int) b;
@end
#import "Methods.h"
@implementation Methods
-(void)personwithperson:(int)a andcomeputerwithcomputer:(int)b
{
if ((a==1&&b==3)||(a==2&&b==1)||(a==3&&b==2)) {
NSLog(@"计算机胜利");
self.jisuansheng++;
} else if ((a==1&&b==2)||(a==2&&b==3)||(a==3&&b==1)) {
NSLog(@"人胜利");
self.rensheng++;
} else{
NSLog(@"平局");
}
}
@end
以上是关于和电脑玩石头剪刀布的主要内容,如果未能解决你的问题,请参考以下文章