取奇數遊戲(c)

來源:互聯網
上載者:User

/*
該遊戲規則如下: 操作者先輸入一個奇數 N(<200)表示N個石
子. 設電腦為 A 方,操作者為 B 方, 雙方輪流取石子,每次取1-3個. 最後取
到石子總數為奇數的一方獲勝. 編製程式使電腦有較多的獲勝機會,
*/

#include "stdio.h"
#include "conio.h"
main()
{

    int total;
    int p=0,c=0,pAdd,cAdd;

    printf("total=");
    scanf("%d",&total);

    printf("\n****begin****\n\n");

    do
    {
        if(c%2==0)
        {
           if(total>=3)
           {
              cAdd=3;
           }
           else if(total>=1)
           {
               cAdd=1;
           }
        }
        else
        {
            if(total>=2)
            {
                cAdd=2;
            }
            else
            {
               cAdd=1;
            }
        }
        total-=cAdd;
        c+=cAdd;
        printf("computer:%d\n",cAdd);
        if(total==0) break;
        printf("    %d leave.\n",total);

        printf("    person:");
        scanf("%d",&pAdd);
        while(pAdd>3 || pAdd<1 || pAdd>total)
        {
            printf("    error! %d leave.again:",total);
            scanf("%d",&pAdd);
        }

        total-=pAdd;
        p+=pAdd;
        printf("person:%d\n",pAdd);

    }while(total!=0);
    
    printf("\n****end****\n\n");

    outputResult(p,c);
    
    getch();
}

outputResult(_pNum,_cNum)
{
   int pTag,cTag;

   printf("----result----\n");
   printf("person:%d\n",_pNum);
   printf("computer:%d\n",_cNum);

   pTag=(_pNum%2!=0?1:0);
   cTag=(_cNum%2!=0?1:0);

   printf("--------\n");
   if((pTag||cTag) && !(pTag&&cTag))
   {
      if(pTag)
      {
          printf("person win!");
      }
      else
      {
          printf("computer win!");
      }

   }
   else
   {
      printf("no one win!");
   }

    
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.