当前位置:网站首页>Fist guessing applet based on Object-C novice on the road
Fist guessing applet based on Object-C novice on the road
2022-07-27 02:28:00 【Breezy morning】
Guessing applet

Preface
This is a small guessing program , You can choose :“ scissors , stone , cloth ”, The computer randomly selects “ scissors , stone , cloth ”, The computer referee decides to win or lose , Announce the result of the game , And give the winner extra points .
One 、 The code is divided into the following files
Two 、 The following are listed respectively 8 The contents of a file
1.main.m
#import <Foundation/Foundation.h>
#import “Judge.h”
int main()
{
Player *p1 = [Player new];
p1->_name = @“ Xiao Ming ”;
Robot *r1 = [Robot new];
r1->_name = @“ Afar dog ”;
Judge *j1 = [Judge new];
j1->_name = @“ Black whistle ”;
while(1)
{
//1. Players punch
[p1 showFist];
//2. Robot punches
[r1 showFist];
//3. The referee announced the result of the match , And give the winner extra points
[j1 judgeWithPlayer:p1 andRobot:r1];
char ans = ‘a’;
NSLog(@“ Dear player 【%@】, Would you like to play again ,y/n:”,p1->_name);
rewind(stdin);
scanf(“%c”,&ans);
if(ans != ‘y’)
{
NSLog(@“ Welcome to come again next time !【%@】”,p1->_name);
break;
}
}
return 0;
}
Player.h
**
The code is as follows :
#import <Foundation/Foundation.h>
#import “FistType.h”
/// Player class
@interface Player : NSObject
{
@public
/// Player's name
NSString *_name;
/// Player's score
int _score;
/// Players' fists
FistType _selectedFist ;
}
/// How players punch
- (void)showFist;
/// Receive input numbers , String converted to the selected fist - (NSString *)fistWithNumber:(int)number;
@end
3.Player.m
#import “Player.h”
@implementation Player
/// How players punch
- (void)showFist
{
//1. Prompt the user to choose fist
NSLog(@“ Dear player 【%@】, Please choose your fist :1. scissors 2. stone 3. cloth :”,_name);
int userSelected = 0 ;
//2. Receive user input , And save it to the class attribute
scanf(“%d”,&userSelected);
NSString *type = [self fistWithNumber:userSelected];
NSLog(@“ Dear player 【%@】, The fist you choose is %@”,_name,type);
_selectedFist = userSelected;
}
/// Receive input numbers , String converted to the selected fist - (NSString *)fistWithNumber:(int)number
{
switch(number)
{
case 1: return @“ scissors ”;
case 2: return @“ stone ”;
case 3: return @“ cloth ”;
default: return @“ Please enter 1-3 Integer between !”;
}
}
@end
FistType.h
typedef enum FistType
{
FistTypeJianDao = 1 ,
FistTypeShiTou = 2 ,
FistTypeBu = 3
} FistType;
Robot.h
#import <Foundation/Foundation.h>
#import “FistType.h”
/// Robot man
@interface Robot : NSObject
{
@public
/// The name of the robot
NSString *_name;
/// Robot score
int _score;
/// The fist chosen by the robot
FistType _selectedFist;
}
/// How robots punch
- (void)showFist;
/// Receive the fist of choice , String converted to fist - (NSString *)fistWithNumber:(int)number;
@end
Robot.m
#import “Robot.h”
@implementation Robot
/// How robots punch
- (void)showFist
{
//1. produce 1-3 Random number between , As a fist of robot
int selectedFist = arc4random_uniform(3) + 1 ;
NSString *type = [self fistWithNumber:selectedFist];
NSLog(@“ robot 【%@】 The fist is :【%@】”,_name,type);
//2. Save the fist of the robot in the class attribute
_selectedFist = selectedFist;
}
/// Receive the fist of choice , String converted to fist
- (NSString *)fistWithNumber:(int)number
{
switch(number)
{
case 1: return @“ scissors ”;
case 2: return @“ stone ”;
case 3: return @“ cloth ”;
default: return @“ Please enter 1-3 Integer between !”;
}
}
@end
Judge.h
#import <Foundation/Foundation.h>
#import “Player.h”
#import “Robot.h”
/// Referee category
@interface Judge : NSObject
{
@public
NSString *_name;
}
/// The referee's method of judging whether to win or lose , And give the winner extra points
- (void)judgeWithPlayer:(Player *)player andRobot:(Robot *)robot;
@end
Judge.m
#import “Judge.h”
@implementation Judge
/// The referee's method of judging whether to win or lose , And give the winner extra points
- (void)judgeWithPlayer:(Player *)player andRobot:(Robot *)robot
{
//1. Get the fist selected by the player , The fist chosen by the robot
int playerSelected = player->_selectedFist;
int robotSelected = robot->_selectedFist;
//2. Judgement of winning or losing
// scissors 1
// stone 2
// cloth 3
// Game player wins
// 1 3
// 2 1
// 3 2
if((playerSelected - robotSelected == -2) ||
(playerSelected - robotSelected == 1))
{
// Game player wins
NSLog(@“ Dear player 【%@】, You won !”,player->_name);
player->_score++;
}
else if(playerSelected == robotSelected)
{
// It ends in a draw
NSLog(@“ The player 【%@】---- robot 【%@】---- You are really smart !”,
player->_name,robot->_name);
}
else
{
// Robot wins
NSLog(@“ robot 【%@】, It's a victory !”,robot->_name);
robot->_score++;
}
NSLog(@“---- The player 【%@】----【%d】----- robot 【%@】-----【%d】----”,
player->_name,player->_score,robot->_name,robot->_score);
}
@end
summary
stay Xcode Create these files in , Copy and paste them separately , Click on the run , That's all right. .
边栏推荐
- Lvs+keepalived project practice
- (super detailed version, don't know to comment at any time) codeforces round 804 (Div. 2) C the third problem
- MGRE、PPP、HDLC综合实验
- Guangguangzai's CSDN journey
- 今天浅讲一下转义字符【萌新版】
- (前缀和/思维)Codeforces Round #806 (Div. 4)F. Yet Another Problem About Pairs Satisfying an Inequality
- Codeforces Round #796 (Div. 2), problem: (1688C) Manipulating History
- N methods of SQL optimization
- Codeforces Round #796 (Div. 2), problem: (1688C) Manipulating History
- Golang -- parse yaml file
猜你喜欢

全连MGRE与星型拓扑MGRE

Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay

最新京东短信登录+傻妞机器人保姆级部署教程(2022/7/24)

Find a specific number in an ordered array

lvs+keepalived项目实战

HandsomeForum学习论坛

求解100~200之间的素数

OSPF basic experimental configuration
![[C language programming] branch structure](/img/17/f9d5360d8802d29079dcbfcc302003.png)
[C language programming] branch structure

Is it useful to lie down with your eyes closed when you can't sleep?
随机推荐
【用C语言绘制谢尔宾斯基三角形】
Codeforces Round #810 (Div. 2), problem: (B) Party
Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
Detailed source code of golang bufio reader
多线程中 synchronized 锁升级的原理是什么?
识时务者常用网址大全
JUC concurrent programming
js中的数组方法和循环
Hcip OSPF comprehensive experiment
SQL优化的N种方法
聊聊自动化测试的度量指标
RS-485 bus communication application
光光光仔的CSDN之旅
C language - assignment operator, compound assignment operator, self increasing and self decreasing operator, comma operator, conditional operator, goto statement, comment
(史上最详细)Codeforces Round #805 (Div. 3)E. Split Into Two Sets
First acquaintance with C language (1)
MySQL course 1. simple command line -- simple record welcome to supplement and correct errors
[C language] relevant distinction between strlen and sizeof
数字芯片的面积优化:第三届“华为杯”研究生创芯大赛数字方向上机题1详解
求解整数的每一位