Scissors Stone cloth (man-machine battle)

Source: Internet
Author: User

#import <Foundation/Foundation.h>

@interface Computer:nsobject

@property (assign,nonatomic) int comcount;

-(int) punches;//Punch

@end

#import "Computer.h"

@implementation Computer

-(int) punches

{

int num=arc4random ()%3+1;//randomly generates a value of 1-3

Switch (NUM)

{

Case 1:

NSLog (@ "The computer is out of stone");

Break

Case 2:

NSLog (@ "Computer out of Scissors");

Break

Case 3:

NSLog (@ "Computer is out of cloth");

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

Types of Punches

-(int) punches

{

int num1;

NSLog (@ "Please punch:1--> stone,2--> scissors,3--> cloth");

scanf ("%d", &num1);

Switch (NUM1)

{

Case 1:

NSLog (@ "You're out of stone! ");

Break

Case 2:

NSLog (@ "You're out of scissors! ");

Break

Case 3:

NSLog (@ "You're out of cloth! ");

Break

Default

Break

}

return NUM1;

}

A comparison between the method of punching and the method of computer random punch

-(int) Playcompare: (int) computer and: (int) person

{

if ((computer==1&&person==2) | | | (computer==2&&person==3) | | (computer==3&&person==1))

{

NSLog (@ "Computer wins! ");

return-1;

}

else if (Computer==person)

{

NSLog (@ "Draw");

return 0;

}

Else

{

NSLog (@ "Congratulations: you won!" ");

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--> continues 0--> end");

scanf ("%d", &num2);

count++;

NSLog (@ "A total of%d innings", count);

NSLog (@ "man wins%d innings, computer wins%d innings", person.percount,computer.comcount);

}

}

return 0;

}

Scissors Stone cloth (man-machine battle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.