标签:
#include<stdio.h> #include<stdlib.h> #include<time.h> int main() { int i=1; int input=0; int count=0; //控制游戏执行的次数 int random=0; while(i) { printf("********欢迎来到猜数字游戏*********\n"); printf("请输入您的选择:1.开始游戏0.退出游戏\n"); scanf("%d",&i); switch(i) { case 1: srand((unsigned int)time(NULL)); random=rand()%101; count=0; while(count < 5) { printf("请输入一个您猜测的数字在1-100之间:\n"); scanf("%d",&input); if(input == random) { printf("恭喜您,猜对了\n"); break; } else if(input > random) { count++; printf("猜大了\n"); } else { count++; printf("猜小了\n"); } } if(count == 5) { printf("您的机会已用完,游戏结束\n"); } break; case 0: break; default: printf("您的选择错误\n"); break; } } system("pause"); return 0; }
#define _CRT_SECURE_NO_WARNINGS 1 #ifndef __GAME_H__ //防止头文件被重复定义 #define __GAME_H__ #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 3 #define COIUMN 3 void menu(); void Init_board(char arr[ROW][COIUMN]); void Print_board(char arr[ROW][COIUMN]); void Player(char arr[ROW][COIUMN]); int Judgefull(char arr[ROW][COIUMN]); void Computer(char arr[ROW][COIUMN]); char Winner(char arr[ROW][COIUMN]); #endif //__GAME_H__
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void menu() { printf("*************欢迎来到三子棋游戏*************\n"); printf("*****请输入您的选择1.进入游戏0.退出游戏*****\n"); } void Init_board(char arr[ROW][COIUMN]) { int i=0; int j=0; for(i=0;i<ROW;i++) { for(j=0;j<COIUMN;j++) { arr[i][j]=' '; } } } void Print_board(char arr[ROW][COIUMN]) { int i=0; for(i=0;i<ROW;i++) { printf(" %c | %c | %c \n",arr[i][0],arr[i][1],arr[i][2]); if(i != ROW-1) { printf("---|---|---\n"); } } } void Player(char arr[ROW][COIUMN]) { int x=0; int y=0; while(1) { printf("请输入您要走的坐标>\n"); scanf("%d%d",&x,&y); x--; y--; if(arr[x][y] == ' ') { arr[x][y]='@'; break; } else { printf("您的输入错误,请重新输入\n"); continue; } } } int Judgefull(char arr[ROW][COIUMN]) { int i=0; int j=0; for(i=0;i<ROW;i++) { for(j=0;j<COIUMN;j++) { if(arr[i][j] == ' ') return 0; } } return -1; } void Computer(char arr[ROW][COIUMN]) { int x=0; int y=0; srand((unsigned int)time(NULL)); while(1) { x=rand()%ROW; y=rand()%COIUMN; if(arr[x][y] == ' ') { arr[x][y]='#'; break; } else { if(Judgefull(arr) == 0) continue; //棋盘未满 break; //棋盘已满 } } } char Winner(char arr[ROW][COIUMN]) { int i=0; for(i=0;i<ROW;i++) { if(arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2] && arr[i][0] != ' ') return arr[i][0]; } for(i=0;i<COIUMN;i++) { if(arr[0][i] == arr[1][i] && arr[1][i] == arr[2][i] && arr[0][i] != ' ') return arr[0][i]; } if(arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[0][0] != ' ') { return arr[0][0]; } if(arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0] && arr[0][2] != ' ') { return arr[0][2]; } if(Judgefull(arr) == -1) return -1; //棋盘已满 return 0; //未满未赢 }
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void text() { char ret=0; int input=1; char arr[ROW][COIUMN]={0}; while(input) { menu(); scanf("%d",&input); switch(input) { case 1: Init_board(arr); Print_board(arr); do { Player(arr); Computer(arr); Print_board(arr); ret=Winner(arr); }while(ret == 0); if(ret == '@') { printf("恭喜您赢得冠军\n"); } else if(ret == '#') { printf("不好意思电脑赢了\n"); } if(ret == -1) { printf("平局\n"); } break; case 0: break; default: printf("您的输入错误,请重新输入\n"); break; } } } int main() { text(); system("pause"); return 0; }
标签:
原文地址:http://blog.csdn.net/qq_34328833/article/details/52130418