zoukankan      html  css  js  c++  java
  • 剪刀石头布 (人机对战)

    #import <Foundation/Foundation.h>

    @interface Computer : NSObject

    @property(assign,nonatomic) int ComCount;

    -(int)Punches;//出拳

    @end

    #import "Computer.h"

    @implementation Computer

    -(int)Punches

    {

        int num=arc4random()%3+1;//随机产生1-3的值

        

        switch (num)

        {

            case 1:

                NSLog(@"计算机出的是石头");

                break;

            case 2:

                NSLog(@"计算机出的是剪刀");

                break;

            case 3:

                NSLog(@"计算机出的是布");

                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

    //出拳的种类

    -(int)Punches

    {

        int num1;

        NSLog(@"请出拳:1-->石头,2-->剪刀,3-->布");

        scanf("%d",&num1);

        

        switch (num1)

        {

            case 1:

                NSLog(@"您出的是石头!");

                break;

            case 2:

                NSLog(@"您出的是剪刀!");

                break;

            case 3:

                NSLog(@"您出的是布!");

                break;

            default:

                break;

        }

        return num1;

    }

    //人出拳的方法与计算机随机出拳的方法进行比较

    -(int)PlayCompare:(int)Computer and:(int)Person

    {

        if ((Computer==1&&Person==2)||(Computer==2&&Person==3)||(Computer==3&&Person==1))

        {

            NSLog(@"计算机赢了!");

            return -1;

        }

        

        else if (Computer==Person)

        {

            NSLog(@"平局");

             return 0;

        }

       else

       {

           NSLog(@"恭喜:您赢了!");

           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-->继续 0-->结束");

                scanf("%d",&num2);

                count++;

                

                NSLog(@"总共进行了%d局",count);

                NSLog(@"人胜了%d局,计算机胜了%d局",person.PerCount,computer.ComCount);

            }

            

        }

        return 0;

    }

  • 相关阅读:
    函数指针作为函数參数,实现冒泡排序的升序排序和降序排序
    为什么通过空指针(NULL)能够正确调用类的部分成员函数
    vc6.0 点编译时提示Cannot complile the file &#39;D:souce-codevc-workspace对话框MainFrm.h&#39;; no compile tool is
    struts2中Action訪问servlet的两种方式
    删除LINUX更新后多余的内核
    cocos2d-x 3.0rc2版公布了
    The user specified as a definer (&#39;root&#39;@&#39;%&#39;) does not exist
    HDU 4287 Intelligent IME(map运用)
    HDU 4925 Apple Tree(推理)
    Linux下使用Fastboot给手机刷ROM
  • 原文地址:https://www.cnblogs.com/tmf-4838/p/5256297.html
Copyright © 2011-2022 走看看