当前位置:网站首页>【猜拳游戏 基于Objective-C语言】
【猜拳游戏 基于Objective-C语言】
2022-08-08 04:50:00 【清风清晨】
前言
这是一个猜拳游戏,玩家选择想要出的拳头:1 剪刀 2 石头 3 布,
机器人随机出拳头,
裁判宣布比赛结果,并为胜利者加1分。
一、玩家类Player.h和Player.m两个文件
Player.h文件:
#import <Foundation/Foundation.h>
#import “FistType.h”
NS_ASSUME_NONNULL_BEGIN
@interface Player : NSObject
@property NSString *name;
@property FistType fistType;
@property int score;
- (void)showFist;
- (NSString *)fistTypeWithNumber:(int)number;
@end
NS_ASSUME_NONNULL_END
Player.m文件:
#import “Player.h”
@implementation Player
(void)showFist
{
NSLog(@“亲爱的玩家【%@】,请输入你要出的拳头,1 剪刀 2 石头 3 布:”,self.name);
int userSelected = 0;
rewind(stdin);
scanf(“%d”,&userSelected);
NSString *fistType = [self fistTypeWithNumber:userSelected];
NSLog(@“亲爱的玩家【%@】,您选择的拳头是:%@”,self.name,fistType);
self.fistType = userSelected;
}(NSString *)fistTypeWithNumber:(int)number
{
switch(number)
{
case 1:
return @“剪刀”;
break;
case 2:
return @“石头”;
break;
case 3:
return @“布”;
break;
default:
return @“请输入正确的数值”;
break;
}
}
@end
二、机器人类Robot.h文件和Robot.m文件
1.Robot.h文件
#import <Foundation/Foundation.h>
#import “FistType.h”
NS_ASSUME_NONNULL_BEGIN
@interface Robot : NSObject
@property NSString *name;
@property FistType fistType;
@property int score;
- (void)showFist;
- (NSString *)fistTypeWithNumber:(int)number;
@end
NS_ASSUME_NONNULL_END
2.Robot.m文件:
#import “Robot.h”
#import <stdlib.h>
@implementation Robot
- (void)showFist
{
int robotSelected = arc4random_uniform(3) + 1;
NSString *robotFistType = [self fistTypeWithNumber:robotSelected];
NSLog(@“机器人【%@】,你出的拳头是:%@”,self.name,robotFistType);
self.fistType = robotSelected;
} - (NSString *)fistTypeWithNumber:(int)number
{
switch(number)
{
case 1:
return @“剪刀”;
break;
case 2:
return @“石头”;
break;
case 3:
return @“布”;
break;
default:
return @“请输入正确的数值”;
break;
}
}
@end
3.裁判类Judge.h文件和Judge.m文件:
Judge.h文件:
#import <Foundation/Foundation.h>
#import “Player.h”
#import “Robot.h”
NS_ASSUME_NONNULL_BEGIN
@interface Judge : NSObject
@property NSString *name;
- (void)judgeWithPlayer:(Player *)player andWith:(Robot *)robot;
@end
NS_ASSUME_NONNULL_END
4.Judge.m文件:
#import “Judge.h”
@implementation Judge
- (void)judgeWithPlayer:(Player *)player andWith:(Robot *)robot
{
int playerFist = player.fistType;
int robotFist = robot.fistType;
//player robot
// 1 3 -2 剪刀 1 石头 2 布 3
// 2 1 1
// 3 2 1
if((playerFist - robotFist) == -2 || (playerFist - robotFist) == 1)
{
//玩家赢
NSLog(@“亲爱的玩家【%@】,恭喜你获得了胜利!”,player.name);
int playerScore = player.score;
playerScore++;
player.score = playerScore;
}
else if(playerFist == robotFist )
{
//平局
NSLog(@“你们真是心有灵犀啊!”);
}
else
{
//机器人赢
NSLog(@“机器人【%@】,获得了胜利”,robot.name);
int robotScore = robot.score;
robotScore++;
robot.score = robotScore;
}
NSLog(@“玩家【%@】 -------- 机器人【%@】 ---------”,player.name,robot.name);
NSLog(@“%d --------------- %d”,player.score,robot.score);
}
@end
5.main.m文件:
#import <Foundation/Foundation.h>
#import “Player.h”
#import “Robot.h”
#import “Judge.h”
int main(int argc, const char * argv[])
{
Class classPlayer = [Player class];
Class classRobot = [Robot class];
Class classJudge = [Judge class];
Player *p1 = [classPlayer new];
Robot *r1 = [classRobot new];
Judge *j1 = [classJudge new];
p1.name = @“小明”;
r1.name = @“啊法狗”;
j1.name = @“黑哨”;
SEL playerShowFist = @selector(showFist);
SEL robotShowFist = @selector(showFist);
SEL judgeJudge = @selector(judgeWithPlayer:andWith:);
while(1)
{
[p1 performSelector:playerShowFist];
[r1 performSelector:robotShowFist];
[j1 performSelector:judgeJudge withObject:p1 withObject:r1];
char ans = ‘a’;
rewind(stdin);
NSLog(@“亲爱的玩家【%@】,您还想再玩儿1把吗:y/n”,p1.name);
scanf(“%c”,&ans);
if(ans != ‘y’)
{
return 0;
}
}
}
5.FistType.h文件:
#ifndef FistType_h
#define FistType_h
typedef enum
{
FistTypeJianDao = 1,
FistTypeShiTou = 2,
FistTypeBu = 3
} FistType;
#endif /* FistType_h */
总结
把上面几个文件建立好,把内容拷贝进去,然后运行,就可以了,需要Xcode和Mac电脑。
边栏推荐
- Open3D ICP精配准(使用鲁棒性核函数)
- The difference between classification, object detection, semantic segmentation, and instance segmentation
- 国内最主流的5大项目工时管理系统
- L3-006 Slash in the wind
- XDR technology
- The 5 most mainstream project time management systems in China
- Mini Program Optimization Practice
- MySQL从入门到入土【20W字收藏篇】
- Open3D 基于颜色的ICP配准
- leetcode 70. Stair Climbing Dynamic Programming
猜你喜欢

leetcode: 874. Simulate a walking robot

Filter 过滤器的使用

风控策略必学|这种用决策树来挖掘规则的方法

C language - function

A line of code counts the number of occurrences of the specified string in the text

The research project of the Institute of Metal Research of the Chinese Academy of Sciences has been certified by Huawei, helping to develop a new paradigm in materials science!
![Error: [Intervention] Unable to preventDefault inside passive event listener due to target ...](/img/57/44e6bc7a330f075f79eb428475d89c.png)
Error: [Intervention] Unable to preventDefault inside passive event listener due to target ...
![[opencv] Introduction to opencv development kit](/img/1a/7b60426d109c9f7231c67e4a4dad46.png)
[opencv] Introduction to opencv development kit

《动机与人格》笔记(二)——认识和理解的欲望

KDD‘22推荐系统论文梳理(24篇研究&36篇应用论文)
随机推荐
10款自媒体人必备的免费工具,快速高效运营
leetcode 112.路经总和 递归
MYSQL导出数据字典
KDD‘22推荐系统论文梳理(24篇研究&36篇应用论文)
Mini Program Optimization Practice
Unity-CharacterController(角色控制器)
Research on Blind Recognition of Digital Modulated Signal Based on MindSpore Framework
Risk control strategy must be learned | This method of mining rules with decision trees
The fledgling Xiao Li's 115th blog project notes on the creation of the domestic GD32F103RCT6 basic project
bpftrace:简便输出调试信息
11-golang流程控制
【Win10】若干睡眠问题及对策
【js基础】js的垃圾回收机制/内存回收机制
Let your text be seen by more people: Come and contribute, the payment is reliable!
14.Unity2D 横版 粒子系统特效 飙血粒子+高处落地粒子+对象池管理所有粒子
Shell 脚本 — 多行注释、开启子/不开启子进程执行、转义带颜色输出、读取键盘输入、输入输出重定向、单双引号、命令替换、读取变量、系统变量、正则过滤、算术运算、一行多条命令、字符串比较
leetcode 112. Path sum recursion
【多任务模型】《Multi-Faceted Hierarchical Multi-Task Learning for a Large Number of Tasks with Multi-dimens
MySQL - Indexes and Transactions
MYSQL export data dictionary