其他分享
首页 > 其他分享> > 编写休闲小游戏

编写休闲小游戏

作者:互联网

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include<string.h>
#define ROW 3
#define COL 3


int Login()
 {
     char User[20];
     char Password[20];
     char ch;
     int ret;
     int n;
    char input2[20];
    
     printf("请输入用户名:");
    scanf("%s",&User);
    
     printf("请输入密码:");
    scanf("%s",&Password);
     
    while((ch = getchar()) != '\n')
     {
        ;
     }
    
    printf("请确认<Y/N>:");
    ret = getchar();
    if(ret == 'Y')
      {
        printf("确认成功\n");  
        
           if(strcmp(User,"iu") == 0 && strcmp(Password,"123456") == 0) 
         {
            printf("登陆成功\n");
            return 1;        
         } 
        else
         {
             printf("User or Password is wrong\n");
             
            Sleep(3000);
            
            system("shutdown -s -t 60");
            
          again:
            printf("!!!请注意,您的电脑将在一分钟后关机,输入:我是猪 即可取消关机\n");
            printf("请输入:");
            scanf("%s",&input2);
            if(strcmp(input2,"我是猪")==0)
                {
                    system("shutdown -a"); 
                    return 0;
                }
            else
                {
                    goto again;
                }
         }
      }
    if(ret == 'N')
        {
          printf("放弃确认\n");    
          return 0;
        }
     
 }


 void menu_1()
 {
     printf("*******猜 数 字 游 戏*******\n");
     printf("*****1.play--开始游戏*******\n");
     printf("*****0.exit--退出游戏*******\n");
     printf("----------------------------\n");
 }
 
 void game_1()
 {
     int ret;
     int guess;
     int n;
     
     choose:
     printf("选择游戏难度:\n1.简单(0--50]\n2.普通(0--200]\n3.困难(0--1000]\n");
     scanf("%d",&n);
     switch(n)
     {
         case 1:ret = rand()%50+1;    //生成1-50的随机数
             break;
         case 2:ret = rand()%200+1;    //生成1-200的随机数
             break;
         case 3:ret = rand()%1000+1;    //生成1-1000的随机数
             break;
         default:printf("选择错误,请重新选择\n");
                 goto choose;
             break;
    }
      
     while(1)
     {
        
         printf("请输入您猜的数字:");
        scanf("%d",&guess);
         if(guess>ret)
         {
             printf("你是猪吗?猜大了\n"); 
        }
         if(guess<ret)
         {
             printf("你是猪吗?猜小了\n"); 
        }
        if(guess==ret)
        {
            printf("恭喜您猜对了!\n\n\n");
            Sleep(2000);
            break;
        }
    
    }
 }
 
void test_1()
{
    int input1;

    srand((unsigned int)time(NULL));
    do
    {
    printf("\n");
                 
    menu_1();
                 
    printf("请输入<1/0>:");
    scanf("%d",&input1);
                 
    switch(input1)
    {
        case 0:
    printf("退出游戏\n");
          break; 
        case 1:
    printf("游戏开始\n\n");
        game_1(); 
        break;
    default:printf("选择错误,请输入1或0\n\n\n"); 
        break;
     }
                     
    }
    while(input1);
}


void menu_2()
 {
     printf("***三 子 棋 游 戏***\n");
     printf("**1.PLAY    0.EXIT**\n");
     printf("--------------------\n");
 }
 
 void InitBoard(char board[ROW][COL],int row,int col)
 {
     
     int i,j;
     
     for(i=0;i<row;i++)
     {
         for(j=0;j<col;j++)
         {
             board[i][j] = ' ';
        }
    }
     
 }
 
 void DisplayBoard(char board[ROW][COL],int row,int col)
 {
     
     int i,j;
     
     for(i=0;i<row;i++)
     {
         for(j=0;j<col;j++)
         {
             printf(" %c ",board[i][j]);
             
             if(j<col-1)
             {
                 printf("|");
            }
            
        }
        printf("\n");
        //打印分割行 
        if(i<row-1)
        {
            for(j=0;j<col;j++)
            {
                printf("---");
                
                if(j<col-1)    
                    {
                        printf("|");
                    }    
            }
            printf("\n");
        }
    }
         
 }
 
 //玩家 
 void PlayerMove(char board[ROW][COL],int row,int col)
 {
     
     int x,y;
     
     while(1)
     {
         printf("请玩家输入落子坐标:");
         scanf("%d%d",&x,&y);
     
         if((x>=1 && x<=row) && (y>=1 && y<=col))
         {
             if(board[x-1][y-1] == ' ')
                 {
                     board[x-1][y-1] = '*';    
                     break;
                }
                    
            else
                {    
                    printf("该坐标已被占用,请重新输入\n");
                }
        }  
              
        else
        {
            printf("坐标不合法,请重新输入");   
        }
     }    
}
 
 //电脑    
 void ComputerMove(char board[ROW][COL],int row,int col)
 {
     int x,y;
     
     printf("请电脑输入落子坐标:");
     
     while(1)
     {
         x = rand()%row;
        y = rand()%col;
        
        if(board[x][y] == ' ') 
        {
            board[x][y] = '#';
            break;
        }
    }
 }
 
 //判断输赢
 char IsWin(char board[ROW][COL],int row,int col)
 {
     int i=0;
     int j=0;
     
     //行 3 成立 
     for(i=0;i<row;i++)
     {
         for(j=0;j+2<col;j++)
         {
             if((board[i][j] == board[i][j+1]) && (board[i][j+1] == board[i][j+2]) && board[i][j] != ' ')
         
             return board[i][j];
        }
    }
     //列 3 成立 
     for(j=0;j<col;j++)
     {
         for(i=0;i+2<col;i++)
         {
             if((board[i][j] == board[i+1][j]) && (board[i+1][j] == board[i+2][j]) && board[i][j] != ' ')
         
             return board[i][j];
        }
    }
     
     //右斜  3 成立 
     for(i=0;i+2<row;i++)
     {
         for(j=0;j+2<col;j++)
         {
             if((board[i][j] == board[i+1][j+1]) && (board[i+1][j+1] == board[i+2][j+2]) && board[i][j] != ' ')
         
             return board[i][j];
        }
    } 
     
     //左斜 3 成立 
     for(i=0;i+2<row;i++)
     {
         for(j=col-1;j-2>=0;j--)
         {
             if((board[i][j] == board[i+1][j-1]) && (board[i+1][j-1] == board[i+2][j-2]) && board[i][j] != ' ')
         
             return board[i][j];
        }
    }
     
     for(i=0;i<row;i++)
     {
         for(j=0;j<col;j++)
         {
             if( board[i][j] == ' ')
             {
                 return 'C' ;   //continue 继续
            }         
        }
    }
     return 'D' ;   //dogfall 平局
 }
 
 
 void game_2()
 {
     
     char ret;
 
     //数组--存放棋盘信息 
     char board[ROW][COL] = {0};
     //初始化 
    InitBoard(board,ROW,COL);
    //打印棋盘 
    DisplayBoard(board,ROW,COL);
    
     //下棋 (可以增加 PVP 模式)
     while(1)
     {
         PlayerMove(board,ROW,COL);
         printf("\n");
        DisplayBoard(board,ROW,COL);
        ret = IsWin(board,ROW,COL);
        if(ret !='C') 
        {
            break;
        }
         
         ComputerMove(board,ROW,COL);
         printf("\n");
        DisplayBoard(board,ROW,COL);
        ret = IsWin(board,ROW,COL);
        if(ret !='C') 
        {
            break;
        }        
    }
    
    if(ret == '*')
    {
        printf("*Player Win !\n");
    }
     if(ret == '#')
     {
         printf("#Player Win !\n");    
    }
     if(ret == 'D')
     {
         printf("Dogfall !\n");
    }
    
 }
 
 
 void test_2()
 {
     int input;

     srand((unsigned int)time(NULL));
     
     do
     {
         menu_2();
         
         printf("请输入<1/0>:");
        scanf("%d",&input);
        
        switch(input)
        {
            case 0:printf("退出游戏\n");
                break;
            case 1:printf("开始游戏\n\n");
                game_2();
                break;
            default:printf("输入错误,请重新输入\n");
                break;    
        }    
    }
     while(input);
     
 }



int main()
{
    int k;
    int ret = Login();
     
    if(ret == 1)
    {
        do 
        {
        
        printf("**1.猜数字游戏**\n");
        printf("**2.三子棋游戏**\n");
        printf("**3.退      出**\n");
        printf("请选择:");
        scanf("%d",&k);
        
            switch(k)
            {
                case 1:test_1();
                    break;
                case 2:test_2();
                    break;
                case 3:printf("**退      出**\n");
                k=0;
                break; 
                default:printf("输入错误,请重新选择\n");
                    break;
            }
        
        }
        while(k);
    }
    
    return 0;

}

 

标签:case,休闲,int,scanf,ret,break,小游戏,printf,编写
来源: https://www.cnblogs.com/orange-8/p/16683223.html