当前位置:网站首页>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. .
边栏推荐
猜你喜欢

离开页面的提示

【用C语言绘制谢尔宾斯基三角形】
![[C language] relevant distinction between strlen and sizeof](/img/c0/c026818692a01c1867771434e90da8.png)
[C language] relevant distinction between strlen and sizeof

C language - assignment operator, compound assignment operator, self increasing and self decreasing operator, comma operator, conditional operator, goto statement, comment

多线程中 synchronized 锁升级的原理是什么?

【洋哥带你玩转线性表(三)——双向链表】

HCIP-第三天-广域网拓扑实验

excel整行删除,图片一起删除

C language - array, string handler, strlen, strcpy and strncpy, strcat and strncat, StrCmp and strncmp

(CF1691D) Max GEQ Sum
随机推荐
【C语言】strlen与sizeof相关区分
On the first day of staying in the blog [for 80000]!
STM32 introductory tutorial lesson 2
Solve every bit of an integer
Sort the three integers from large to small (introduce various methods in detail)
C language - assignment operator, compound assignment operator, self increasing and self decreasing operator, comma operator, conditional operator, goto statement, comment
Lora通信应用开发
NB-IOT接入云平台
TreeSet集合存储元素的问题
(题意+详细思路+加注释代码) Codeforces Round #805 (Div. 3)F. Equate Multisets
HCIP-第二天
Hcip OSPF comprehensive experiment
(super detailed version, don't know to comment at any time) codeforces round 804 (Div. 2) C the third problem
Golang implements TCP chat room
C语言的常数知识讲解
NAT network address conversion experiment
ESP8266Wi-Fi接入云平台
HCIP oSPF综合实验
Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay
Educational Codeforces Round 132 (Rated for Div. 2), problem: (D) Rorororobot