From 53c34083a18090dce6118b9989af75960f5b6fa8 Mon Sep 17 00:00:00 2001 From: lsy2246 Date: Thu, 7 Dec 2023 19:19:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E8=B4=AA=E5=90=83?= =?UTF-8?q?=E8=9B=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- practice_code/bilibili/I am pig.c | 27 + practice_code/bilibili/binary_search.c | 33 + practice_code/bilibili/digui.c | 20 + practice_code/grades.c | 25 + practice_code/idea/5_chess/5_chess_1.c | 244 +++ practice_code/idea/5_chess/5_chess_2.c | 554 +++++++ practice_code/idea/5_chess/5_chess_3.c | 782 ++++++++++ practice_code/idea/5_chess/5_chess_4.c | 1220 +++++++++++++++ practice_code/idea/5_chess/5_chess_5.c | 1378 +++++++++++++++++ practice_code/idea/Snakes/Snakes_1.c | 312 ++++ .../idea/Snakes/Snakes_2.c | 0 practice_code/idea/Snakes/Snakes_3.c | 1217 +++++++++++++++ practice_code/idea/bank/ATM/ATM_1.c | 153 ++ practice_code/idea/bank/ATM/ATM_2.c | 467 ++++++ practice_code/idea/bank/ATM/ATM_3.c | 847 ++++++++++ .../idea/bank/interest_rate/interest_rate_1.c | 18 + .../idea/bank/interest_rate/interest_rate_2.c | 33 + practice_code/idea/calculator/calculator_1.c | 65 + practice_code/idea/calculator/calculator_2.c | 78 + practice_code/idea/calculator/calculator_3.c | 70 + practice_code/idea/calculator/calculator_4.c | 142 ++ practice_code/idea/calculator/calculator_5.c | 158 ++ practice_code/idea/calculator/calculator_6.c | 158 ++ practice_code/idea/live.c | 108 ++ practice_code/idea/minesweeper.c | 503 ++++++ .../multiplication_table_1.c | 24 + .../multiplication_table_2.c | 32 + practice_code/idea/shutdown.c | 70 + practice_code/idea/start yuansheng.c | 50 + 29 files changed, 8788 insertions(+) create mode 100644 practice_code/bilibili/I am pig.c create mode 100644 practice_code/bilibili/binary_search.c create mode 100644 practice_code/bilibili/digui.c create mode 100644 practice_code/grades.c create mode 100644 practice_code/idea/5_chess/5_chess_1.c create mode 100644 practice_code/idea/5_chess/5_chess_2.c create mode 100644 practice_code/idea/5_chess/5_chess_3.c create mode 100644 practice_code/idea/5_chess/5_chess_4.c create mode 100644 practice_code/idea/5_chess/5_chess_5.c create mode 100644 practice_code/idea/Snakes/Snakes_1.c rename Snakes/main.c => practice_code/idea/Snakes/Snakes_2.c (100%) create mode 100644 practice_code/idea/Snakes/Snakes_3.c create mode 100644 practice_code/idea/bank/ATM/ATM_1.c create mode 100644 practice_code/idea/bank/ATM/ATM_2.c create mode 100644 practice_code/idea/bank/ATM/ATM_3.c create mode 100644 practice_code/idea/bank/interest_rate/interest_rate_1.c create mode 100644 practice_code/idea/bank/interest_rate/interest_rate_2.c create mode 100644 practice_code/idea/calculator/calculator_1.c create mode 100644 practice_code/idea/calculator/calculator_2.c create mode 100644 practice_code/idea/calculator/calculator_3.c create mode 100644 practice_code/idea/calculator/calculator_4.c create mode 100644 practice_code/idea/calculator/calculator_5.c create mode 100644 practice_code/idea/calculator/calculator_6.c create mode 100644 practice_code/idea/live.c create mode 100644 practice_code/idea/minesweeper.c create mode 100644 practice_code/idea/multiplication_table/multiplication_table_1.c create mode 100644 practice_code/idea/multiplication_table/multiplication_table_2.c create mode 100644 practice_code/idea/shutdown.c create mode 100644 practice_code/idea/start yuansheng.c diff --git a/practice_code/bilibili/I am pig.c b/practice_code/bilibili/I am pig.c new file mode 100644 index 0000000..a944c05 --- /dev/null +++ b/practice_code/bilibili/I am pig.c @@ -0,0 +1,27 @@ +#include +#include +#include + +int main () +{ + //初始化 + char array_1[100]; + char array_2[100] = "我是猪"; + printf ( "电脑将在60秒以后关机 \n" ); + system ( "shutdown -s -t 60" ); + start: + printf ( "输入我是猪,结束关机\n" ); + printf ( "输入:" ); + scanf ( "%s" , & array_1 ); + if ( strcmp ( array_1 , array_2 ) != 0 ) + { + printf ( "认知清除,取消关机" ); + system ( "shutdown -a" ); + } + + + printf ( "对自我认识错误 \n" ); + while ( getchar ( ) != '\n' ); + goto start; +} + diff --git a/practice_code/bilibili/binary_search.c b/practice_code/bilibili/binary_search.c new file mode 100644 index 0000000..e90524b --- /dev/null +++ b/practice_code/bilibili/binary_search.c @@ -0,0 +1,33 @@ +#define _CRT_SECURE_NO_WARNINGS + +#include + + +int main () +{ + int a; + int i; + printf ( "请输入你想查找的数字:" ); + scanf ( "%d" , a ); + int arr[10] = { 0,1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }; + int k = sizeof ( arr[ 10 ] ) / sizeof ( arr[ 0 ] ); + int left = 0; + int right = k - 1; + int mid; + for ( i = 1 ; i < k ; i ++ ) + { + mid = ( left + right ); + if ( mid > a ) + { + left = mid + 1; + } else if ( mid < a ) + { + right = mid - 1; + } else + { + printf ( "a的下标为%d" , mid ); + break; + } + return 0; + } +} \ No newline at end of file diff --git a/practice_code/bilibili/digui.c b/practice_code/bilibili/digui.c new file mode 100644 index 0000000..fb04249 --- /dev/null +++ b/practice_code/bilibili/digui.c @@ -0,0 +1,20 @@ +#include + +int my_strlen(char* str ) +{ + if(*str != '\0') + { + return 1+ my_strlen (str+1); + } + else + return 0; +} + +int main () +{ + char arr[99]; + printf("请输入你要计算数字的长度:"); + scanf("%s",arr); + printf ("%d",my_strlen(arr)); + return 0; +} \ No newline at end of file diff --git a/practice_code/grades.c b/practice_code/grades.c new file mode 100644 index 0000000..5b5f559 --- /dev/null +++ b/practice_code/grades.c @@ -0,0 +1,25 @@ +#include + +int main () +{ + int figure_a;//接收用户输入 + int figure_b;//储存最终结果 + printf ( "请输入数:" ); + scanf ( "%d" , & figure_a ); + if ( figure_a < 5 ) + { + figure_b = figure_a * 4; + printf ( "结果为%d" , figure_b ); + } + else if ( figure_a >= 5 && figure_a < 10 ) + { + figure_b = figure_a * 3; + printf ( "结果为%d" , figure_b ); + } + else if ( figure_a > 10 ) + { + figure_b = figure_a * 2; + printf ( "结果为%d" , figure_b ); + } + return 0; +} \ No newline at end of file diff --git a/practice_code/idea/5_chess/5_chess_1.c b/practice_code/idea/5_chess/5_chess_1.c new file mode 100644 index 0000000..c36c6d0 --- /dev/null +++ b/practice_code/idea/5_chess/5_chess_1.c @@ -0,0 +1,244 @@ +#include + +//输出当前棋盘 +void print_chessboard ( char array_chessboard[20][20] ) +{ + printf ( " " ); + for ( int i = 0 ; i < 20 ; i ++ ) + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d " , i ); + } else + { + printf ( "%d " , i ); + } + } + printf ( "\n" ); + for ( int i = 0 ; i < 20 ; i ++ )//输出棋盘竖列 + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d" , i ); + } else + { + printf ( "%d" , i ); + } + for ( int j = 0 ; j < 20 ; j ++ ) //输出棋盘横排 + { + printf ( " %c" , array_chessboard[ i ][ j ] ); + } + if ( i < 10 ) //输出第几行 + { + printf ( " %d" , i ); + } else + { + printf ( " %d" , i ); + } + printf ( "\n" );//输出完每一排换行 + } + printf ( " " ); + for ( int i = 0 ; i < 20 ; i ++ ) + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d " , i ); + } else + { + printf ( "%d " , i ); + } + } + printf ( "\n" ); +} + +//存入用户坐标 +char credited ( int x , int y , char array_chessboard[20][20] , char user ) +{ + while ( x < 0 || x >= 20 || y < 0 || y >= 20 ) //检查坐标是否在棋盘内 + { + while ( getchar ( ) != '\n' ); + printf ( "非法坐标,请重新输入\n" ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + printf ( "请输入竖坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + } + while ( array_chessboard[ x ][ y ] != '.' ) //检查坐标是否存在棋子 + { + while ( getchar ( ) != '\n' ); + printf ( "该坐标已存在棋子,请重新输入\n" ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + printf ( "请输入竖坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + } + if ( user == 'X' ) + { + array_chessboard[ x ][ y ] = 'X'; + } + if ( user == 'Y' ) + { + array_chessboard[ x ][ y ] = 'Y'; + } + return array_chessboard[ x ][ y ];//返回当前地图 +} + +//判断是否胜利 +char judgment ( char user , char array_chessboard[20][20] ) +{ + int i;//初始化横排 + int j;//初始化竖排 + + //判断横向 + for ( i = 0 ; i < 20 ; i ++ ) + { + for ( j = 0 ; j < 20 ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user && + array_chessboard[ i ][ j + 2 ] == user && + array_chessboard[ i ][ j + 3 ] == user && + array_chessboard[ i ][ j + 4 ] == user + ) + { + return user; + } + } + } + //判断纵向 + for ( i = 0 ; i < 20 ; i ++ ) + { + for ( j = 0 ; j < 20 ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user && + array_chessboard[ i + 2 ][ j ] == user && + array_chessboard[ i + 3 ][ j ] == user && + array_chessboard[ i + 4 ][ j ] == user + ) + { + return array_chessboard[ i ][ j ]; + } + } + } + //判断左往右边斜 + for ( i = 0 ; i < 20 ; i ++ ) + { + for ( j = 0 ; j < 20 ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user && + array_chessboard[ i + 2 ][ j + 2 ] == user && + array_chessboard[ i + 3 ][ j + 3 ] == user && + array_chessboard[ i + 4 ][ j + 4 ] == user + ) + { + return user; + } + } + } + //判断右往左边斜 + for ( i = 0 ; i < 20 ; i ++ ) + { + for ( j = 0 ; j < 20 ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user && + array_chessboard[ i + 2 ][ j - 2 ] == user && + array_chessboard[ i + 3 ][ j - 3 ] == user && + array_chessboard[ i + 4 ][ j - 4 ] == user + ) + { + return user; + } + } + } + return '\0'; +} + +int main () +{ + int pick_home;//判断是否进入程序 + //判断用户是否进入程序 + start: + printf ("####双击进入程序####\n"); + printf ("#####输入0退出#####\n:"); + pick_home=getchar(); + if(pick_home=='0') + { + printf ("欢迎下次使用"); + return 0; + } + while (getchar() != '\n' );//清除缓存 + //初始化 + char user;//判断是那个用户 + int bout = 0;//用户输入的回合 + int x;//用户横坐标 + int y;//用户纵坐标 + char array_chessboard[20][20];//初始化棋盘 + + for ( int i = 0 ; i < 20 ; i ++ ) + { + for ( int j = 0 ; j < 20 ; j ++ ) //填充棋盘横排 + { + array_chessboard[ i ][ j ] = '.'; + } + } + + while ( bout < 400 ) + { + print_chessboard ( array_chessboard );//输出当前地图 + bout ++; + printf ( "第%d回合\n" , ( bout + 1 ) / 2 ); + if ( bout % 2 == 1 ) + { + user = 'X'; + } else + { + user = 'Y'; + } + printf ( "轮到用户%c \n" , user ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数组:" ); + } + printf ( "请输入纵坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + credited ( x , y , array_chessboard , user );//存入用户输入的数字 + if ( judgment ( user , array_chessboard ) == user )//判断用户是否胜利 + { + printf ( "用户%c胜利\n" , user ); + break; + } + } + + if ( bout == 400 )//平局判断 + { + printf ( "平局\n" ); + } + print_chessboard ( array_chessboard ); //输出棋盘 + while (getchar() != '\n' );//清除缓存 + goto start;//回到主页 +} \ No newline at end of file diff --git a/practice_code/idea/5_chess/5_chess_2.c b/practice_code/idea/5_chess/5_chess_2.c new file mode 100644 index 0000000..ef2e156 --- /dev/null +++ b/practice_code/idea/5_chess/5_chess_2.c @@ -0,0 +1,554 @@ +#include +#include +#include + +//声明 +void home (); + +//初始化棋盘 +char initialization ( char array_chessboard[20][20] ) +{ + for ( int i = 0 ; i < 20 ; i ++ ) + { + for ( int j = 0 ; j < 20 ; j ++ ) //填充棋盘横排 + { + array_chessboard[ i ][ j ] = '.'; + } + } + return array_chessboard[ 20 ][ 20 ]; +} + +//输出当前棋盘 +void print_chessboard ( char array_chessboard[20][20] ) +{ + printf ( " " ); + for ( int i = 0 ; i < 20 ; i ++ ) + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d " , i ); + } else + { + printf ( "%d " , i ); + } + } + printf ( "\n" ); + for ( int i = 0 ; i < 20 ; i ++ )//输出棋盘竖列 + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d" , i ); + } else + { + printf ( "%d" , i ); + } + for ( int j = 0 ; j < 20 ; j ++ ) //输出棋盘横排 + { + printf ( " %c" , array_chessboard[ i ][ j ] ); + } + if ( i < 10 ) //输出第几行 + { + printf ( " %d" , i ); + } else + { + printf ( " %d" , i ); + } + printf ( "\n" );//输出完每一排换行 + } + printf ( " " ); + for ( int i = 0 ; i < 20 ; i ++ ) + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d " , i ); + } else + { + printf ( "%d " , i ); + } + } + printf ( "\n" ); +} + +//存入用户坐标 +char credited ( int x , int y , char array_chessboard[20][20] , char user ) +{ + while ( x < 0 || x >= 20 || y < 0 || y >= 20 ) //检查坐标是否在棋盘内 + { + while ( getchar ( ) != '\n' ); + printf ( "非法坐标,请重新输入\n" ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + printf ( "请输入竖坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + } + while ( array_chessboard[ x ][ y ] != '.' ) //检查坐标是否存在棋子 + { + while ( getchar ( ) != '\n' ); + printf ( "该坐标已存在棋子,请重新输入\n" ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + printf ( "请输入竖坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + } + if ( user == 'X' ) + { + array_chessboard[ x ][ y ] = 'X'; + } + if ( user == 'Y' ) + { + array_chessboard[ x ][ y ] = 'Y'; + } + return array_chessboard[ x ][ y ];//返回当前地图 +} + +//判断是否胜利 +char judgment ( char user , char array_chessboard[20][20] ) +{ + int i;//初始化横排 + int j;//初始化竖排 + + //判断横向 + for ( i = 0 ; i < 20 ; i ++ ) + { + for ( j = 0 ; j < 20 ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user && + array_chessboard[ i ][ j + 2 ] == user && + array_chessboard[ i ][ j + 3 ] == user && + array_chessboard[ i ][ j + 4 ] == user + ) + { + return user; + } + } + } + //判断纵向 + for ( i = 0 ; i < 20 ; i ++ ) + { + for ( j = 0 ; j < 20 ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user && + array_chessboard[ i + 2 ][ j ] == user && + array_chessboard[ i + 3 ][ j ] == user && + array_chessboard[ i + 4 ][ j ] == user + ) + { + return array_chessboard[ i ][ j ]; + } + } + } + //判断左往右边斜 + for ( i = 0 ; i < 20 ; i ++ ) + { + for ( j = 0 ; j < 20 ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user && + array_chessboard[ i + 2 ][ j + 2 ] == user && + array_chessboard[ i + 3 ][ j + 3 ] == user && + array_chessboard[ i + 4 ][ j + 4 ] == user + ) + { + return user; + } + } + } + //判断右往左边斜 + for ( i = 0 ; i < 20 ; i ++ ) + { + for ( j = 0 ; j < 20 ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user && + array_chessboard[ i + 2 ][ j - 2 ] == user && + array_chessboard[ i + 3 ][ j - 3 ] == user && + array_chessboard[ i + 4 ][ j - 4 ] == user + ) + { + return user; + } + } + } + return '\0'; +}// + +//生成随机数 +void generateRandomPosition ( int * x , int * y , char array_chessboard[20][20] ) +{ + int randomNumber = rand ( ) % 8; + switch ( randomNumber ) + { + case 0: + { + * y += 1; + break; + } + case 1: + { + * x += 1; + break; + } + case 2: + { + * x += 1; + * y += 1; + break; + } + case 3: + { + * x -= 1; + break; + } + case 4: + { + * y -= 1; + break; + } + case 5: + { + * x -= 1; + * y -= 1; + break; + } + case 6: + { + * x -= 1; + * y += 1; + break; + } + case 7: + { + * x += 1; + * y -= 1; + break; + } + } + while ( * x < 0 || * x >= 20 || * y < 0 || * y >= 20 || array_chessboard[ * x ][ * y ] != '.' ) + { + * x = rand ( ) % 20; + * y = rand ( ) % 20; + generateRandomPosition ( x , y , array_chessboard ); + } +} + +//判断ai下棋位置 +void AI_chess ( int * x , int * y , char user , char array_chessboard[20][20] ) +{ + int i;//初始化横排 + int j;//初始化竖排 + + //如果横向有三个一样 + for ( i = 0 ; i < 20 ; i ++ ) + { + for ( j = 0 ; j < 20 ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user && + array_chessboard[ i ][ j + 2 ] == user + ) + { + int randomNumber = rand ( ) % 2; // 生成 0 或 1 + switch ( randomNumber ) + { + case 0: + * x = i; + * y = j + 3; + break; + case 1: + * x = i; + * y = j - 1; + break; + } + + if ( * x < 0 || * x >= 20 || * y < 0 || * y >= 20 || array_chessboard[ * x ][ * y ] != '.' ) + { + * x = rand ( ) % 20; + * y = rand ( ) % 20; + AI_chess ( x , y , user , array_chessboard ); + } + return; + } + } + } + //判断纵向有三个一样 + for ( i = 0 ; i < 20 ; i ++ ) + { + for ( j = 0 ; j < 20 ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user && + array_chessboard[ i + 2 ][ j ] == user + ) + { + int randomNumber = rand ( ) % 2; // 生成 0 或 1 + switch ( randomNumber ) + { + case 0: + * x = i + 3; + * y = j; + break; + case 1: + * x = i - 1; + * y = j; + break; + } + + if ( * x < 0 || * x >= 20 || * y < 0 || * y >= 20 || array_chessboard[ * x ][ * y ] != '.' ) + { + AI_chess ( x , y , user , array_chessboard ); + } + return; + } + } + } + + //判断左往右边斜有三个一样 + for ( i = 0 ; i < 20 ; i ++ ) + { + for ( j = 0 ; j < 20 ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user && + array_chessboard[ i + 2 ][ j + 2 ] == user + ) + { + int randomNumber = rand ( ) % 2; // 生成 0 或 1 + switch ( randomNumber ) + { + case 0: + * x = i + 3; + * y = j + 3; + break; + case 1: + * x = i - 1; + * y = j - 1; + break; + } + + if ( * x < 0 || * x >= 20 || * y < 0 || * y >= 20 || array_chessboard[ * x ][ * y ] != '.' ) + { + * x = rand ( ) % 20; + * y = rand ( ) % 20; + AI_chess ( x , y , user , array_chessboard ); + } + return; + } + + } + } + //判断右往左边斜 + for ( i = 0 ; i < 20 ; i ++ ) + { + for ( j = 0 ; j < 20 ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user && + array_chessboard[ i + 2 ][ j - 2 ] == user + ) + { + int randomNumber = rand ( ) % 2; // 生成 0 或 1 + switch ( randomNumber ) + { + case 0: + * x = i - 1; + * y = j + 1; + break; + case 1: + * x = i - 3; + * y = j + 3; + break; + } + + if ( * x < 0 || * x >= 20 || * y < 0 || * y >= 20 || array_chessboard[ * x ][ * y ] != '.' ) + { + * x = rand ( ) % 20; + * y = rand ( ) % 20; + AI_chess ( x , y , user , array_chessboard ); + } + return; + } + } + } + //如果不满足随机输入 + generateRandomPosition ( x , y , array_chessboard ); + + +} + +//双人模式 +void double_game () +{ + //初始化 + char user;//判断是那个用户 + int bout = 0;//用户输入的回合 + int x;//用户横坐标 + int y;//用户纵坐标 + char array_chessboard[20][20];//初始化棋盘 + initialization ( array_chessboard );//初始化数组里面的数 + while ( bout < 400 ) + { + print_chessboard ( array_chessboard );//输出当前地图 + bout ++; + printf ( "第%d回合\n" , ( bout + 1 ) / 2 ); + if ( bout % 2 == 1 ) + { + user = 'X'; + } else + { + user = 'Y'; + } + printf ( "轮到用户%c \n" , user ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数组:" ); + } + printf ( "请输入纵坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + credited ( x , y , array_chessboard , user );//存入用户输入的数字 + if ( judgment ( user , array_chessboard ) == user )//判断用户是否胜利 + { + printf ( "用户%c胜利\n" , user ); + break; + } + } + + if ( bout == 400 )//平局判断 + { + printf ( "平局\n" ); + } + print_chessboard ( array_chessboard ); //输出棋盘 + while ( getchar ( ) != '\n' );//清除缓存 + home ( );//返回主页 +} + +//单人模式 +void single_game () +{ + //初始化 + char user;//判断是那个用户 + int bout = 0;//用户输入的回合 + int x;//横坐标 + int y;//纵坐标 + char array_chessboard[20][20];//初始化棋盘 + initialization ( array_chessboard );//初始化数组里面的数 + while ( bout < 200 ) + { + bout ++; + printf ( "第%d回合\n" , bout ); + user = 'X'; + printf ( "轮到用户 \n" ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数组:" ); + } + printf ( "请输入纵坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + credited ( x , y , array_chessboard , user );//存入用户输入的数字 + if ( judgment ( user , array_chessboard ) == user )//判断用户是否胜利 + { + printf ( "用户胜利\n" ); + break; + } + + printf ( "轮到AI \n" ); + AI_chess ( & x , & y , user , array_chessboard );//获取ai判断输出的数 + + user = 'Y'; + credited ( x , y , array_chessboard , user );//存入AI输入的数字 + if ( judgment ( user , array_chessboard ) == user )//判断用户是否胜利 + { + printf ( "AI胜利\n" ); + break; + } + print_chessboard ( array_chessboard );//输出当前地图 + + } + + if ( bout == 200 )//平局判断 + { + printf ( "平局\n" ); + } + print_chessboard ( array_chessboard ); //输出棋盘 + while ( getchar ( ) != '\n' );//清除缓存 + home ( ); +} + +//菜单 +void home () +{ + int pick;//判断进入什么模式 + printf ( "————主页————\n" ); + printf ( "0.退出程序\n" ); + printf ( "1.单人模式\n" ); + printf ( "2.双人模式\n" ); + printf ( "请输入:" ); + while ( scanf ( "%d" , & pick ) != 1 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "请输入合法数字:" ); + } + switch ( pick ) + { + case 0: + { + printf ( "欢迎下次使用" ); + return; + break; + } + case 1: + { + while ( getchar ( ) != '\n' );//清除缓存 + single_game ( ); + break; + } + case 2: + { + while ( getchar ( ) != '\n' );//清除缓存 + double_game ( ); + break; + } + default: + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "输入错误 \n" ); + home ( ); + } + } +} + +int main () +{ + srand ( time ( NULL ) );// 设置种子,可以让每次运行程序时得到不同的随机数序列 + home ( ); + return 0; +} \ No newline at end of file diff --git a/practice_code/idea/5_chess/5_chess_3.c b/practice_code/idea/5_chess/5_chess_3.c new file mode 100644 index 0000000..795917f --- /dev/null +++ b/practice_code/idea/5_chess/5_chess_3.c @@ -0,0 +1,782 @@ +#include +#include +#include + +//声明 +void home (); + +//初始化棋盘 +char initialization ( int array , char array_chessboard[array][array] ) +{ + for ( int i = 0 ; i < array ; i ++ ) + { + for ( int j = 0 ; j < array ; j ++ ) //填充棋盘横排 + { + array_chessboard[ i ][ j ] = '.'; + } + } + return array_chessboard[ array ][ array ]; +} + +//输出当前棋盘 +void print_chessboard ( int array , char array_chessboard[array][array] ) +{ + printf ( " " ); + for ( int i = 0 ; i < array ; i ++ ) + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d " , i ); + } else + { + printf ( "%d " , i ); + } + } + printf ( "\n" ); + for ( int i = 0 ; i < array ; i ++ )//输出棋盘竖列 + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d" , i ); + } else + { + printf ( "%d" , i ); + } + for ( int j = 0 ; j < array ; j ++ ) //输出棋盘横排 + { + printf ( " %c" , array_chessboard[ i ][ j ] ); + } + if ( i < 10 ) //输出第几行 + { + printf ( " %d" , i ); + } else + { + printf ( " %d" , i ); + } + printf ( "\n" );//输出完每一排换行 + } + printf ( " " ); + for ( int i = 0 ; i < array ; i ++ ) + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d " , i ); + } else + { + printf ( "%d " , i ); + } + } + printf ( "\n" ); +} + +//存入用户坐标 +char credited ( int array , int x , int y , char array_chessboard[array][array] , char user ) +{ + while ( x < 0 || x >= array || y < 0 || y >= array ) //检查坐标是否在棋盘内 + { + while ( getchar ( ) != '\n' ); + printf ( "非法坐标,请重新输入\n" ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + printf ( "请输入竖坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + } + while ( array_chessboard[ x ][ y ] != '.' ) //检查坐标是否存在棋子 + { + while ( getchar ( ) != '\n' ); + printf ( "该坐标已存在棋子,请重新输入\n" ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + printf ( "请输入竖坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + } + if ( user == 'X' ) + { + array_chessboard[ x ][ y ] = 'X'; + } + if ( user == 'Y' ) + { + array_chessboard[ x ][ y ] = 'Y'; + } + return array_chessboard[ x ][ y ];//返回当前地图 +} + +//判断是否胜利 +char judgment ( int array , char user , char array_chessboard[array][array] ) +{ + int i;//初始化横排 + int j;//初始化竖排 + + //判断横向 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user && + array_chessboard[ i ][ j + 2 ] == user && + array_chessboard[ i ][ j + 3 ] == user && + array_chessboard[ i ][ j + 4 ] == user + ) + { + return user; + } + } + } + //判断纵向 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user && + array_chessboard[ i + 2 ][ j ] == user && + array_chessboard[ i + 3 ][ j ] == user && + array_chessboard[ i + 4 ][ j ] == user + ) + { + return array_chessboard[ i ][ j ]; + } + } + } + //判断左往右边斜 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user && + array_chessboard[ i + 2 ][ j + 2 ] == user && + array_chessboard[ i + 3 ][ j + 3 ] == user && + array_chessboard[ i + 4 ][ j + 4 ] == user + ) + { + return user; + } + } + } + //判断右往左边斜 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user && + array_chessboard[ i + 2 ][ j - 2 ] == user && + array_chessboard[ i + 3 ][ j - 3 ] == user && + array_chessboard[ i + 4 ][ j - 4 ] == user + ) + { + return user; + } + } + } + return '\0'; +}// + +//生成随机数 +void generateRandomPosition ( int array , int * x , int * y , char array_chessboard[array][array] ) +{ + * x = rand ( ) % array; + * y = rand ( ) % array; + int randomNumber = rand ( ) % 8; + switch ( randomNumber ) + { + case 0: + { + * y += 1; + break; + } + case 1: + { + * x += 1; + break; + } + case 2: + { + * x += 1; + * y += 1; + break; + } + case 3: + { + * x -= 1; + break; + } + case 4: + { + * y -= 1; + break; + } + case 5: + { + * x -= 1; + * y -= 1; + break; + } + case 6: + { + * x -= 1; + * y += 1; + break; + } + case 7: + { + * x += 1; + * y -= 1; + break; + } + } + while ( * x < 0 || * x >= array || * y < 0 || * y >= array || array_chessboard[ * x ][ * y ] != '.' ) + { + * x = rand ( ) % array; + * y = rand ( ) % array; + generateRandomPosition ( array , x , y , array_chessboard ); + } +} + +//判断ai下棋位置 +void AI_chess ( int array , int * x , int * y , char user , char array_chessboard[array][array] ) +{ + int i;//初始化横排 + int j;//初始化竖排 + + //如果横向有四个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user && + array_chessboard[ i ][ j + 2 ] == user && + array_chessboard[ i ][ j + 3 ] == user + ) //此if为了判断是否三个一样 + { + if ( array_chessboard[ i ][ j - 1 ] == '.' ) + { + * x = i; + * y = j - 1; + } else if ( array_chessboard[ i ][ j + 4 ] == '.' ) + { + * x = i; + * y = j + 4; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断纵向有四个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user && + array_chessboard[ i + 2 ][ j ] == user && + array_chessboard[ i + 3 ][ j ] == user + + ) + { + if ( array_chessboard[ i - 1 ][ j ] == '.' ) + { + * x = i - 1; + * y = j; + } else if ( array_chessboard[ i + 4 ][ j ] == '.' ) + { + * x = i + 4; + * y = j; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断左往右边斜有四个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user && + array_chessboard[ i + 2 ][ j + 2 ] == user && + array_chessboard[ i + 3 ][ j + 3 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j - 1 ] == '.' ) + { + * x = i - 1; + * y = j - 1; + } else if ( array_chessboard[ i + 4 ][ j + 4 ] == '.' ) + { + * x = i + 4; + * y = j + 4; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断右往左边斜四个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user && + array_chessboard[ i + 2 ][ j - 2 ] == user && + array_chessboard[ i + 3 ][ j - 3 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j + 1 ] == '.' ) + { + * x = i - 1; + * y = j + 1; + } else if ( array_chessboard[ i + 4 ][ j - 4 ] == '.' ) + { + * x = i + 4; + * y = j - 4; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + + //如果横向有三个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user && + array_chessboard[ i ][ j + 2 ] == user + ) //此if为了判断是否三个一样 + { + + if ( array_chessboard[ i ][ j - 1 ] == '.' ) + { + * x = i; + * y = j - 1; + } else if ( array_chessboard[ i ][ j + 3 ] == '.' ) + { + * x = i; + * y = j + 3; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断纵向有三个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user && + array_chessboard[ i + 2 ][ j ] == user + + ) + { + if ( array_chessboard[ i - 1 ][ j ] == '.' ) + { + * x = i - 1; + * y = j; + } else if ( array_chessboard[ i + 3 ][ j ] == '.' ) + { + * x = i + 3; + * y = j; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断左往右边斜有三个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user && + array_chessboard[ i + 2 ][ j + 2 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j - 1 ] == '.' ) + { + * x = i - 1; + * y = j - 1; + } else if ( array_chessboard[ i + 3 ][ j + 3 ] == '.' ) + { + * x = i + 3; + * y = j + 3; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断右往左边斜三个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user && + array_chessboard[ i + 2 ][ j - 2 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j + 1 ] == '.' ) + { + * x = i - 1; + * y = j + 1; + } else if ( array_chessboard[ i + 3 ][ j - 3 ] == '.' ) + { + * x = i + 3; + * y = j - 3; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + + //如果横向有两个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user + ) + { + if ( array_chessboard[ i ][ j - 1 ] == '.' ) + { + * x = i; + * y = j - 1; + } else if ( array_chessboard[ i ][ j + 2 ] == '.' ) + { + * x = i; + * y = j + 2; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断纵向有两个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i + 2 ][ j ] == '.' && array_chessboard[ i - 1 ][ j ] == '.' ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user + ) + { + if ( array_chessboard[ i + 2 ][ j ] == '.' ) + { + * x = i + 2; + * y = j; + } else if ( array_chessboard[ i - 1 ][ j ] == '.' ) + { + * x = i - 1; + * y = j; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + } + } + } + //判断左往右边斜有两个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j - 1 ] == '.' ) + { + * x = i - 1; + * y = j - 1; + } else if ( array_chessboard[ i + 2 ][ j + 2 ] == '.' ) + { + * x = i + 2; + * y = j + 2; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + + } + } + //判断右往左边斜两个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user + ) + { + if ( array_chessboard[ i + 2 ][ j - 2 ] == '.' ) + { + * x = i + 2; + * y = j - 2; + } else if ( array_chessboard[ i - 1 ][ j + 1 ] == '.' ) + { + * x = i - 1; + * y = j + 1; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //如果不满足随机输入 + generateRandomPosition ( array , x , y , array_chessboard ); + + +} + +//双人模式 +void double_game () +{ + //初始化 + char user;//判断是那个用户 + int bout = 0;//用户输入的回合 + int x;//用户横坐标 + int y;//用户纵坐标 + int array;//棋盘长度 + printf ( "请输入棋盘长度(0开始计算,8<=chess<=25)" ); + while ( scanf ( "%d" , & array ) != 1 || array < 8 || array > 25 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "请重新输入棋盘长度" ); + } + char array_chessboard[array][array];//初始化棋盘 + initialization ( array , array_chessboard );//初始化数组里面的数 + while ( bout < array * array ) + { + print_chessboard ( array , array_chessboard );//输出当前地图 + bout ++; + printf ( "第%d回合\n" , ( bout + 1 ) / 2 ); + if ( bout % 2 == 1 ) + { + user = 'X'; + } else + { + user = 'Y'; + } + printf ( "轮到用户%c \n" , user ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数组:" ); + } + printf ( "请输入纵坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + credited ( array , x , y , array_chessboard , user );//存入用户输入的数字 + if ( judgment ( array , user , array_chessboard ) == user )//判断用户是否胜利 + { + printf ( "用户%c胜利\n" , user ); + break; + } + } + + if ( bout == array * array )//平局判断 + { + printf ( "平局\n" ); + } + print_chessboard ( array , array_chessboard ); //输出棋盘 + while ( getchar ( ) != '\n' );//清除缓存 + home ( );//返回主页 +} + +//单人模式 +void single_game () +{ + //初始化 + char user;//判断是那个用户 + int bout = 0;//用户输入的回合 + int x;//横坐标 + int y;//纵坐标 + int array;//棋盘长度 + printf ( "请输入棋盘长度(0开始计算,8<=chess<=25)" ); + while ( scanf ( "%d" , & array ) != 1 || array < 8 || array > 25 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "请重新输入棋盘长度" ); + } + char array_chessboard[array][array];//初始化棋盘 + initialization ( array , array_chessboard );//初始化数组里面的数 + while ( bout < array * array / 2 ) + { + bout ++; + printf ( "第%d回合\n" , bout ); + user = 'X'; + printf ( "轮到用户 \n" ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + printf ( "请输入纵坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + credited ( array , x , y , array_chessboard , user );//存入用户输入的数字 + if ( judgment ( array , user , array_chessboard ) == user )//判断用户是否胜利 + { + printf ( "用户胜利\n" ); + break; + } + + printf ( "轮到电脑 \n" ); + AI_chess ( array , & x , & y , user , array_chessboard );//获取ai判断输出的数 + + user = 'Y'; + credited ( array , x , y , array_chessboard , user );//存入AI输入的数字 + if ( judgment ( array , user , array_chessboard ) == user )//判断用户是否胜利 + { + printf ( "AI胜利\n" ); + break; + } + print_chessboard ( array , array_chessboard );//输出当前地图 + + } + + if ( bout == array * array / 2 )//平局判断 + { + printf ( "平局\n" ); + } + print_chessboard ( array , array_chessboard ); //输出棋盘 + while ( getchar ( ) != '\n' );//清除缓存 + home ( ); +} + +//菜单 +void home () +{ + int pick;//判断进入什么模式 + printf ( "————主页————\n" ); + printf ( "0.退出程序\n" ); + printf ( "1.单人模式\n" ); + printf ( "2.双人模式\n" ); + printf ( "请输入:" ); + while ( scanf ( "%d" , & pick ) != 1 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "请输入合法数字:" ); + } + switch ( pick ) + { + case 0: + { + printf ( "欢迎下次使用" ); + return; + break; + } + case 1: + { + while ( getchar ( ) != '\n' );//清除缓存 + single_game ( ); + break; + } + case 2: + { + while ( getchar ( ) != '\n' );//清除缓存 + double_game ( ); + break; + } + default: + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "输入错误 \n" ); + home ( ); + } + } +} + +int main () +{ + srand ( time ( NULL ) );// 设置种子,可以让每次运行程序时得到不同的随机数序列 + home ( ); + return 0; +} \ No newline at end of file diff --git a/practice_code/idea/5_chess/5_chess_4.c b/practice_code/idea/5_chess/5_chess_4.c new file mode 100644 index 0000000..b63ae92 --- /dev/null +++ b/practice_code/idea/5_chess/5_chess_4.c @@ -0,0 +1,1220 @@ +#include +#include +#include + +//声明 +void home (); + +//初始化棋盘 +char initialization ( int array , char array_chessboard[array][array] ) +{ + for ( int i = 0 ; i < array ; i ++ ) + { + for ( int j = 0 ; j < array ; j ++ ) //填充棋盘横排 + { + array_chessboard[ i ][ j ] = '.'; + } + } + return array_chessboard[ array ][ array ]; +} + +//输出当前棋盘 +void print_chessboard ( int array , char array_chessboard[array][array] ) +{ + printf ( " " ); + for ( int i = 0 ; i < array ; i ++ ) + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d " , i ); + } else + { + printf ( "%d " , i ); + } + } + printf ( "\n" ); + for ( int i = 0 ; i < array ; i ++ )//输出棋盘竖列 + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d" , i ); + } else + { + printf ( "%d" , i ); + } + for ( int j = 0 ; j < array ; j ++ ) //输出棋盘横排 + { + printf ( " %c" , array_chessboard[ i ][ j ] ); + } + if ( i < 10 ) //输出第几行 + { + printf ( " %d" , i ); + } else + { + printf ( " %d" , i ); + } + printf ( "\n" );//输出完每一排换行 + } + printf ( " " ); + for ( int i = 0 ; i < array ; i ++ ) + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d " , i ); + } else + { + printf ( "%d " , i ); + } + } + printf ( "\n" ); +} + +//存入用户坐标 +char credited ( int array , int x , int y , char array_chessboard[array][array] , char user ) +{ + while ( x < 0 || x >= array || y < 0 || y >= array ) //检查坐标是否在棋盘内 + { + while ( getchar ( ) != '\n' ); + printf ( "非法坐标,请重新输入\n" ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + printf ( "请输入竖坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + } + while ( array_chessboard[ x ][ y ] != '.' ) //检查坐标是否存在棋子 + { + while ( getchar ( ) != '\n' ); + printf ( "该坐标已存在棋子,请重新输入\n" ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + printf ( "请输入竖坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + } + if ( user == 'X' ) + { + array_chessboard[ x ][ y ] = 'X'; + } + if ( user == 'Y' ) + { + array_chessboard[ x ][ y ] = 'Y'; + } + return array_chessboard[ x ][ y ];//返回当前地图 +} + +//判断是否胜利 +char judgment ( int array , char user , char array_chessboard[array][array] ) +{ + int i;//初始化横排 + int j;//初始化竖排 + + //判断横向 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user && + array_chessboard[ i ][ j + 2 ] == user && + array_chessboard[ i ][ j + 3 ] == user && + array_chessboard[ i ][ j + 4 ] == user + ) + { + return user; + } + } + } + //判断纵向 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user && + array_chessboard[ i + 2 ][ j ] == user && + array_chessboard[ i + 3 ][ j ] == user && + array_chessboard[ i + 4 ][ j ] == user + ) + { + return array_chessboard[ i ][ j ]; + } + } + } + //判断左往右边斜 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user && + array_chessboard[ i + 2 ][ j + 2 ] == user && + array_chessboard[ i + 3 ][ j + 3 ] == user && + array_chessboard[ i + 4 ][ j + 4 ] == user + ) + { + return user; + } + } + } + //判断右往左边斜 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user && + array_chessboard[ i + 2 ][ j - 2 ] == user && + array_chessboard[ i + 3 ][ j - 3 ] == user && + array_chessboard[ i + 4 ][ j - 4 ] == user + ) + { + return user; + } + } + } + return '\0'; +}// + +//生成随机数 +void generateRandomPosition ( int * figure , int array , int * x , int * y , char array_chessboard[array][array] ) +{ + int randomNumber = rand ( ) % 8; + switch ( randomNumber ) + { + case 0: + { + * y += 1; + * figure = 1; + break; + } + case 1: + { + * x += 1; + * figure = 1; + break; + } + case 2: + { + * x += 1; + * y += 1; + * figure = 1; + break; + } + case 3: + { + * x -= 1; + * figure = 1; + break; + } + case 4: + { + * y -= 1; + * figure = 1; + break; + } + case 5: + { + * x -= 1; + * y -= 1; + * figure = 1; + break; + } + case 6: + { + * x -= 1; + * y += 1; + * figure = 1; + break; + } + case 7: + { + * x += 1; + * y -= 1; + * figure = 1; + break; + } + } + while ( * x < 0 || * x >= array || * y < 0 || * y >= array || array_chessboard[ * x ][ * y ] != '.' ) + { + generateRandomPosition ( figure , array , x , y , array_chessboard ); + } +} + +//判断用户位置 +void user_chess ( int * figure , int array , int * x , int * y , char user , char array_chessboard[array][array] ) +{ + int i;//初始化横排 + int j;//初始化竖排 + + //如果横向有四个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user && + array_chessboard[ i ][ j + 2 ] == user && + array_chessboard[ i ][ j + 3 ] == user + ) + { + if ( array_chessboard[ i ][ j - 1 ] == '.' ) + { + * x = i; + * y = j - 1; + * figure = 4; + } else if ( array_chessboard[ i ][ j + 4 ] == '.' ) + { + * x = i; + * y = j + 4; + * figure = 4; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断纵向有四个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user && + array_chessboard[ i + 2 ][ j ] == user && + array_chessboard[ i + 3 ][ j ] == user + + ) + { + if ( array_chessboard[ i - 1 ][ j ] == '.' ) + { + * x = i - 1; + * y = j; + * figure = 4; + } else if ( array_chessboard[ i + 4 ][ j ] == '.' ) + { + * x = i + 4; + * y = j; + * figure = 4; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断左往右边斜有四个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user && + array_chessboard[ i + 2 ][ j + 2 ] == user && + array_chessboard[ i + 3 ][ j + 3 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j - 1 ] == '.' ) + { + * x = i - 1; + * y = j - 1; + * figure = 4; + } else if ( array_chessboard[ i + 4 ][ j + 4 ] == '.' ) + { + * x = i + 4; + * y = j + 4; + * figure = 4; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断右往左边斜四个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user && + array_chessboard[ i + 2 ][ j - 2 ] == user && + array_chessboard[ i + 3 ][ j - 3 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j + 1 ] == '.' ) + { + * x = i - 1; + * y = j + 1; + * figure = 4; + } else if ( array_chessboard[ i + 4 ][ j - 4 ] == '.' ) + { + * x = i + 4; + * y = j - 4; + * figure = 4; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + + //如果横向有三个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user && + array_chessboard[ i ][ j + 2 ] == user + ) + { + + if ( array_chessboard[ i ][ j - 1 ] == '.' && array_chessboard[ i ][ j + 3 ] == '.' ) + { + * x = i; + * y = j - 1; + * figure = 3; + } else if ( array_chessboard[ i ][ j + 3 ] == '.' && array_chessboard[ i ][ j - 1 ] == '.' ) + { + * x = i; + * y = j + 3; + * figure = 3; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断纵向有三个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user && + array_chessboard[ i + 2 ][ j ] == user + + ) + { + if ( array_chessboard[ i - 1 ][ j ] == '.' && array_chessboard[ i + 3 ][ j ] == '.' ) + { + * x = i - 1; + * y = j; + * figure = 3; + } else if ( array_chessboard[ i + 3 ][ j ] == '.' && array_chessboard[ i - 1 ][ j ] == '.' ) + { + * x = i + 3; + * y = j; + * figure = 3; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断左往右边斜有三个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user && + array_chessboard[ i + 2 ][ j + 2 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j - 1 ] == '.' && array_chessboard[ i + 3 ][ j + 3 ] == '.' ) + { + * x = i - 1; + * y = j - 1; + * figure = 3; + } else if ( array_chessboard[ i + 3 ][ j + 3 ] == '.' && array_chessboard[ i - 1 ][ j - 1 ] == '.' ) + { + * x = i + 3; + * y = j + 3; + * figure = 3; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断右往左边斜三个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user && + array_chessboard[ i + 2 ][ j - 2 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j + 1 ] == '.' && array_chessboard[ i + 3 ][ j - 3 ] == '.' ) + { + * x = i - 1; + * y = j + 1; + * figure = 3; + } else if ( array_chessboard[ i + 3 ][ j - 3 ] == '.' && array_chessboard[ i - 1 ][ j + 1 ] == '.' ) + { + * x = i + 3; + * y = j - 3; + * figure = 3; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //如果横向有两个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user + ) + { + if ( array_chessboard[ i ][ j - 1 ] == '.' && array_chessboard[ i ][ j + 2 ] == '.' ) + { + * x = i; + * y = j - 1; + * figure = 2; + } else if ( array_chessboard[ i ][ j + 2 ] == '.' && array_chessboard[ i ][ j - 1 ] == '.' ) + { + * x = i; + * y = j + 2; + * figure = 2; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断纵向有两个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i + 2 ][ j ] == '.' && array_chessboard[ i - 1 ][ j ] == '.' ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user + ) + { + if ( array_chessboard[ i + 2 ][ j ] == '.' && array_chessboard[ i - 1 ][ j ] == '.' ) + { + * x = i + 2; + * y = j; + * figure = 2; + } else if ( array_chessboard[ i - 1 ][ j ] == '.' && array_chessboard[ i + 2 ][ j ] == '.' ) + { + * x = i - 1; + * y = j; + * figure = 2; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + } + } + } + //判断左往右边斜有两个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j - 1 ] == '.' && array_chessboard[ i + 2 ][ j + 2 ] == '.' ) + { + * x = i - 1; + * y = j - 1; + * figure = 2; + } else if ( array_chessboard[ i + 2 ][ j + 2 ] == '.' && array_chessboard[ i - 1 ][ j - 1 ] == '.' ) + { + * x = i + 2; + * y = j + 2; + * figure = 2; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + + } + } + //判断右往左边斜两个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user + ) + { + if ( array_chessboard[ i + 2 ][ j - 2 ] == '.' && array_chessboard[ i - 1 ][ j + 1 ] == '.' ) + { + * x = i + 2; + * y = j - 2; + * figure = 2; + } else if ( array_chessboard[ i - 1 ][ j + 1 ] == '.' && array_chessboard[ i + 2 ][ j - 2 ] == '.' ) + { + * x = i - 1; + * y = j + 1; + * figure = 2; + } else if ( * x < 0 || * x >= array || * y < 0 || * y >= array || + array_chessboard[ * x ][ * y ] != '.' ) + { + continue; + } + return; + } + + } + } + //如果不满足随机输入 + generateRandomPosition ( figure , array , x , y , array_chessboard ); + + +} + +//判断AI位置 +void AI_chess ( int * figure , int array , int * x_1 , int * y_1 , char user , char array_chessboard[array][array] ) +{ + int i;//初始化横排 + int j;//初始化竖排 + + //如果横向有四个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user && + array_chessboard[ i ][ j + 2 ] == user && + array_chessboard[ i ][ j + 3 ] == user + ) //此if为了判断是否三个一样 + { + if ( array_chessboard[ i ][ j - 1 ] == '.' ) + { + * x_1 = i; + * y_1 = j - 1; + * figure = 4; + } else if ( array_chessboard[ i ][ j + 4 ] == '.' ) + { + * x_1 = i; + * y_1 = j + 4; + * figure = 4; + } else if ( * x_1 < 0 || * x_1 >= array || * y_1 < 0 || * y_1 >= array || + array_chessboard[ * x_1 ][ * y_1 ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断纵向有四个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user && + array_chessboard[ i + 2 ][ j ] == user && + array_chessboard[ i + 3 ][ j ] == user + + ) + { + if ( array_chessboard[ i - 1 ][ j ] == '.' ) + { + * x_1 = i - 1; + * y_1 = j; + * figure = 4; + } else if ( array_chessboard[ i + 4 ][ j ] == '.' ) + { + * x_1 = i + 4; + * y_1 = j; + * figure = 4; + } else if ( * x_1 < 0 || * x_1 >= array || * y_1 < 0 || * y_1 >= array || + array_chessboard[ * x_1 ][ * y_1 ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断左往右边斜有四个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user && + array_chessboard[ i + 2 ][ j + 2 ] == user && + array_chessboard[ i + 3 ][ j + 3 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j - 1 ] == '.' ) + { + * x_1 = i - 1; + * y_1 = j - 1; + * figure = 4; + } else if ( array_chessboard[ i + 4 ][ j + 4 ] == '.' ) + { + * x_1 = i + 4; + * y_1 = j + 4; + * figure = 4; + } else if ( * x_1 < 0 || * x_1 >= array || * y_1 < 0 || * y_1 >= array || + array_chessboard[ * x_1 ][ * y_1 ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断右往左边斜四个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user && + array_chessboard[ i + 2 ][ j - 2 ] == user && + array_chessboard[ i + 3 ][ j - 3 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j + 1 ] == '.' ) + { + * x_1 = i - 1; + * y_1 = j + 1; + * figure = 4; + } else if ( array_chessboard[ i + 4 ][ j - 4 ] == '.' ) + { + * x_1 = i + 4; + * y_1 = j - 4; + * figure = 4; + } else if ( * x_1 < 0 || * x_1 >= array || * y_1 < 0 || * y_1 >= array || + array_chessboard[ * x_1 ][ * y_1 ] != '.' ) + { + continue; + } + return; + } + + } + } + + //如果横向有三个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user && + array_chessboard[ i ][ j + 2 ] == user + ) //此if为了判断是否三个一样 + { + + if ( array_chessboard[ i ][ j - 1 ] == '.' && array_chessboard[ i ][ j - 2 ] == '.') + { + * x_1 = i; + * y_1 = j - 1; + * figure = 3; + } else if ( array_chessboard[ i ][ j + 3 ] == '.' && array_chessboard[ i ][ j + 4 ] == '.' ) + { + * x_1 = i; + * y_1 = j + 3; + * figure = 3; + } else if ( * x_1 < 0 || * x_1 >= array || * y_1 < 0 || * y_1 >= array || + array_chessboard[ * x_1 ][ * y_1 ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断纵向有三个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user && + array_chessboard[ i + 2 ][ j ] == user + + ) + { + if ( array_chessboard[ i - 1 ][ j ] == '.' &&array_chessboard[ i - 2 ][ j ] == '.' ) + { + * x_1 = i - 1; + * y_1 = j; + * figure = 3; + } else if ( array_chessboard[ i + 3 ][ j ] == '.'&&array_chessboard[ i + 4 ][ j ] == '.' ) + { + * x_1 = i + 3; + * y_1 = j; + * figure = 3; + } else if ( * x_1 < 0 || * x_1 >= array || * y_1 < 0 || * y_1 >= array || + array_chessboard[ * x_1 ][ * y_1 ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断左往右边斜有三个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user && + array_chessboard[ i + 2 ][ j + 2 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j - 1 ] == '.' && array_chessboard[ i - 2 ][ j - 2 ] == '.' ) + { + * x_1 = i - 1; + * y_1 = j - 1; + * figure = 3; + } else if ( array_chessboard[ i + 3 ][ j + 3 ] == '.' && array_chessboard[ i + 4 ][ j + 4 ] == '.' ) + { + * x_1 = i + 3; + * y_1 = j + 3; + * figure = 3; + } else if ( * x_1 < 0 || * x_1 >= array || * y_1 < 0 || * y_1 >= array || + array_chessboard[ * x_1 ][ * y_1 ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断右往左边斜三个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user && + array_chessboard[ i + 2 ][ j - 2 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j + 1 ] == '.' &&array_chessboard[ i - 2 ][ j + 2 ] == '.' ) + { + * x_1 = i - 1; + * y_1 = j + 1; + * figure = 3; + } else if ( array_chessboard[ i + 3 ][ j - 3 ] == '.'&& array_chessboard[ i + 4 ][ j - 4 ] == '.' ) + { + * x_1 = i + 3; + * y_1 = j - 3; + * figure = 3; + } else if ( * x_1 < 0 || * x_1 >= array || * y_1 < 0 || * y_1 >= array || + array_chessboard[ * x_1 ][ * y_1 ] != '.' ) + { + continue; + } + return; + } + + } + } + //如果横向有两个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user + ) + { + if ( array_chessboard[ i ][ j - 1 ] == '.' ) + { + * x_1 = i; + * y_1 = j - 1; + * figure = 2; + } else if ( array_chessboard[ i ][ j + 2 ] == '.' ) + { + * x_1 = i; + * y_1 = j + 2; + * figure = 2; + } else if ( * x_1 < 0 || * x_1 >= array || * y_1 < 0 || * y_1 >= array || + array_chessboard[ * x_1 ][ * y_1 ] != '.' ) + { + continue; + } + return; + } + + } + } + //判断纵向有两个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i + 2 ][ j ] == '.' && array_chessboard[ i - 1 ][ j ] == '.' ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user + ) + { + if ( array_chessboard[ i + 2 ][ j ] == '.' ) + { + * x_1 = i + 2; + * y_1 = j; + * figure = 2; + } else if ( array_chessboard[ i - 1 ][ j ] == '.' ) + { + * x_1 = i - 1; + * y_1 = j; + * figure = 2; + } else if ( * x_1 < 0 || * x_1 >= array || * y_1 < 0 || * y_1 >= array || + array_chessboard[ * x_1 ][ * y_1 ] != '.' ) + { + continue; + } + return; + } + } + } + } + //判断左往右边斜有两个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user + ) + { + if ( array_chessboard[ i - 1 ][ j - 1 ] == '.' ) + { + * x_1 = i - 1; + * y_1 = j - 1; + * figure = 2; + } else if ( array_chessboard[ i + 2 ][ j + 2 ] == '.' ) + { + * x_1 = i + 2; + * y_1 = j + 2; + * figure = 2; + } else if ( * x_1 < 0 || * x_1 >= array || * y_1 < 0 || * y_1 >= array || + array_chessboard[ * x_1 ][ * y_1 ] != '.' ) + { + continue; + } + return; + } + + + } + } + //判断右往左边斜两个一样 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user + ) + { + if ( array_chessboard[ i + 2 ][ j - 2 ] == '.' ) + { + * x_1 = i + 2; + * y_1 = j - 2; + * figure = 2; + } else if ( array_chessboard[ i - 1 ][ j + 1 ] == '.' ) + { + * x_1 = i - 1; + * y_1 = j + 1; + * figure = 2; + } else if ( * x_1 < 0 || * x_1 >= array || * y_1 < 0 || * y_1 >= array || + array_chessboard[ * x_1 ][ * y_1 ] != '.' ) + { + continue; + } + return; + } + + } + } + //如果不满足随机输入 + generateRandomPosition ( figure , array , x_1 , y_1 , array_chessboard ); + + +} + +//复制数值 +char copy_array ( int array , char array_chessboard[array][array] , char array_chessboard_1[array][array] ) +{ + for ( int i = 0 ; i < array ; i ++ ) + { + for ( int j = 0 ; j < array ; j ++ ) + { + array_chessboard_1[ i ][ j ] = array_chessboard[ i ][ j ]; + } + } + return array_chessboard_1[ array ][ array ]; +} + +//判断结果输入哪个结果 +void judgment_outcome ( int figure_1 , int figure_2 , int array , int * x , int * y , int * x_1 , int * y_1 ) +{ + if ( figure_2 >= figure_1 ) + { + * x = * x_1; + * y = * y_1; + } + +} + +//双人模式 +void double_game () +{ + //初始化 + char user;//判断是那个用户 + int bout = 0;//用户输入的回合 + int x;//用户横坐标 + int y;//用户纵坐标 + int array;//棋盘长度 + printf ( "请输入棋盘长度(0开始计算,8<=chess<=25):" ); + while ( scanf ( "%d" , & array ) != 1 || array < 8 || array > 25 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "请重新输入棋盘长度:" ); + } + char array_chessboard[array][array];//初始化棋盘 + initialization ( array , array_chessboard );//初始化数组里面的数 + while ( bout < array * array ) + { + print_chessboard ( array , array_chessboard );//输出当前地图 + bout ++; + printf ( "第%d回合\n" , ( bout + 1 ) / 2 ); + if ( bout % 2 == 1 ) + { + user = 'X'; + } else + { + user = 'Y'; + } + printf ( "轮到用户%c \n" , user ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数组:" ); + } + printf ( "请输入纵坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + credited ( array , x , y , array_chessboard , user );//存入用户输入的数字 + if ( judgment ( array , user , array_chessboard ) == user )//判断用户是否胜利 + { + printf ( "用户%c胜利\n" , user ); + break; + } + } + + if ( bout == array * array )//平局判断 + { + printf ( "平局\n" ); + } + print_chessboard ( array , array_chessboard ); //输出棋盘 + while ( getchar ( ) != '\n' );//清除缓存 + home ( );//返回主页 +} + +//单人模式 +void single_game () +{ + //初始化 + char user;//判断是那个用户 + int bout = 0;//用户输入的回合 + int x;//横坐标 + int y;//纵坐标 + int x_1;//复制横坐标 + int y_1;//复制纵坐标 + int figure_1;//多少连珠 + int figure_2;//备用多少连珠 + int array;//棋盘长度 + printf ( "请输入棋盘长度(0开始计算,8<=chess<=25):" ); + while ( scanf ( "%d" , & array ) != 1 || array < 8 || array > 25 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "请重新输入棋盘长度:" ); + } + char array_chessboard[array][array];//初始化棋盘 + char array_chessboard_1[array][array];//初始化备用棋盘 + initialization ( array , array_chessboard );//初始化数组里面的数 + while ( bout < array * array / 2 ) + { + bout ++; + printf ( "第%d回合\n" , bout ); + user = 'X'; + printf ( "轮到用户 \n" ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + printf ( "请输入纵坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + credited ( array , x , y , array_chessboard , user );//存入用户输入的数字 + if ( judgment ( array , user , array_chessboard ) == user )//判断用户是否胜利 + { + printf ( "用户胜利\n" ); + break; + } + + copy_array ( array , array_chessboard , array_chessboard_1 );//复制当前棋盘 + x_1=x;//复制用户下的位置 + y_1=y;//复制用户下的位置 + user_chess ( & figure_1 , array , & x , & y , user , array_chessboard );//获取用户判断输出的数 + user = 'Y'; + AI_chess ( & figure_2 , array , & x_1 , & y_1 , user , array_chessboard_1 );//获取ai判断输出的数 + judgment_outcome ( figure_1 , figure_2 , array , & x , & y , & x_1 , & y_1 );//判断输出谁 + credited ( array , x , y , array_chessboard , user );//存入AI输入的数字 + if ( judgment ( array , user , array_chessboard ) == user )//判断用户是否胜利 + { + printf ( "AI胜利\n" ); + break; + } + print_chessboard ( array , array_chessboard );//输出当前地图 + + } + + if ( bout == array * array / 2 )//平局判断 + { + printf ( "平局\n" ); + } + print_chessboard ( array , array_chessboard ); //输出棋盘 + while ( getchar ( ) != '\n' );//清除缓存 + home ( ); +} + +//菜单 +void home () +{ + int pick;//判断进入什么模式 + printf ( "————主页————\n" ); + printf ( "0.退出程序\n" ); + printf ( "1.单人模式\n" ); + printf ( "2.双人模式\n" ); + printf ( "请输入:" ); + while ( scanf ( "%d" , & pick ) != 1 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "请输入合法数字:" ); + } + switch ( pick ) + { + case 0: + { + printf ( "欢迎下次使用" ); + return; + break; + } + case 1: + { + while ( getchar ( ) != '\n' );//清除缓存 + single_game ( ); + break; + } + case 2: + { + while ( getchar ( ) != '\n' );//清除缓存 + double_game ( ); + break; + } + default: + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "输入错误 \n" ); + home ( ); + } + } +} + +int main () +{ + srand ( time ( NULL ) );// 设置种子,可以让每次运行程序时得到不同的随机数序列 + home ( ); + return 0; +} \ No newline at end of file diff --git a/practice_code/idea/5_chess/5_chess_5.c b/practice_code/idea/5_chess/5_chess_5.c new file mode 100644 index 0000000..3758522 --- /dev/null +++ b/practice_code/idea/5_chess/5_chess_5.c @@ -0,0 +1,1378 @@ +#include +#include +#include + +//声明 +void home ( int pick ); + +//初始化棋盘 +char initialization ( int array , char array_chessboard[array][array] ) +{ + for ( int i = 0 ; i < array ; i ++ ) + { + for ( int j = 0 ; j < array ; j ++ ) //填充棋盘横排 + { + array_chessboard[ i ][ j ] = '.'; + } + } + return array_chessboard[ array ][ array ]; +} + +//输出当前棋盘 +void print_chessboard ( int array , char array_chessboard[array][array] ) +{ + printf ( " " ); + for ( int i = 0 ; i < array ; i ++ ) + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d " , i ); + } + else + { + printf ( "%d " , i ); + } + } + printf ( "\n" ); + for ( int i = 0 ; i < array ; i ++ )//输出棋盘竖列 + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d" , i ); + } + else + { + printf ( "%d" , i ); + } + for ( int j = 0 ; j < array ; j ++ ) //输出棋盘横排 + { + printf ( " %c" , array_chessboard[ i ][ j ] ); + } + if ( i < 10 ) //输出第几行 + { + printf ( " %d" , i ); + } + else + { + printf ( " %d" , i ); + } + printf ( "\n" );//输出完每一排换行 + } + printf ( " " ); + for ( int i = 0 ; i < array ; i ++ ) + { + if ( i < 10 ) //输出第几行 + { + printf ( " %d " , i ); + } + else + { + printf ( "%d " , i ); + } + } + printf ( "\n" ); +} + +//存入用户坐标 +char credited ( int array , int x , int y , char array_chessboard[array][array] , char user ) +{ + while ( x < 0 || x >= array || y < 0 || y >= array ) //检查坐标是否在棋盘内 + { + while ( getchar ( ) != '\n' ); + printf ( "非法坐标,请重新输入\n" ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + printf ( "请输入竖坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + } + while ( array_chessboard[ x ][ y ] != '.' ) //检查坐标是否存在棋子 + { + while ( getchar ( ) != '\n' ); + printf ( "该坐标已存在棋子,请重新输入\n" ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + printf ( "请输入竖坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + } + if ( user == 'X' ) + { + array_chessboard[ x ][ y ] = 'X'; + } + if ( user == 'Y' ) + { + array_chessboard[ x ][ y ] = 'Y'; + } + return array_chessboard[ x ][ y ];//返回当前地图 +} + +//判断是否胜利 +char judgment ( int array , char user , char array_chessboard[array][array] ) +{ + int i;//初始化横排 + int j;//初始化竖排 + + //判断横向 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i ][ j + 1 ] == user && + array_chessboard[ i ][ j + 2 ] == user && + array_chessboard[ i ][ j + 3 ] == user && + array_chessboard[ i ][ j + 4 ] == user + ) + { + return user; + } + } + } + //判断纵向 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j ] == user && + array_chessboard[ i + 2 ][ j ] == user && + array_chessboard[ i + 3 ][ j ] == user && + array_chessboard[ i + 4 ][ j ] == user + ) + { + return array_chessboard[ i ][ j ]; + } + } + } + //判断左往右边斜 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j + 1 ] == user && + array_chessboard[ i + 2 ][ j + 2 ] == user && + array_chessboard[ i + 3 ][ j + 3 ] == user && + array_chessboard[ i + 4 ][ j + 4 ] == user + ) + { + return user; + } + } + } + //判断右往左边斜 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == user && + array_chessboard[ i + 1 ][ j - 1 ] == user && + array_chessboard[ i + 2 ][ j - 2 ] == user && + array_chessboard[ i + 3 ][ j - 3 ] == user && + array_chessboard[ i + 4 ][ j - 4 ] == user + ) + { + return user; + } + } + } + return '\0'; +}// + +//判断AI位置 +void AI_chess ( int array , int * x , int * y , char array_chessboard[array][array] ) +{ + int i = 0;//初始化横排 + int j = 0;//初始化竖排 + int figure_i = 0;//临时纵坐标 + int figure_j = 0;//临时横坐标 + int integral = 0;//最高评分 + int integral_a = 0;//评分 + int integral_b = 0;//文件临时备份 + int storage_coordinate[2];//存储坐标 + for ( i = 0 ; i < array ; i ++ ) + { + for ( j = 0 ; j < array ; j ++ ) + { + if ( array_chessboard[ i ][ j ] == '.' )//判断该数是不是空 + { + //判断用户向右判断 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i ][ figure_j + 1 ] == 'X' && figure_j + 1 < array ) + { + figure_j += 1; + switch ( k ) + { + case 1: + { + integral_b = 1; + break; + } + case 2: + { + integral_b = 5; + break; + } + case 3: + { + integral_b = 12; + break; + } + case 4: + { + integral_b = 200; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断用户向左判断 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i ][ figure_j - 1 ] == 'X' && figure_j - 1 >= 0 ) + { + figure_j -= 1; + switch ( k ) + { + case 1: + { + integral_b = 1; + break; + } + case 2: + { + integral_b = 5; + break; + } + case 3: + { + integral_b = 12; + break; + } + case 4: + { + integral_b = 200; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断AI向右判断 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i ][ figure_j + 1 ] == 'Y' && figure_j + 1 < array ) + { + figure_j += 1; + switch ( k ) + { + case 1: + { + integral_b = 2; + break; + } + case 2: + { + integral_b = 4; + break; + } + case 3: + { + integral_b = 11; + break; + } + case 4: + { + integral_b = 201; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断AI向左判断 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i ][ figure_j - 1 ] == 'Y' && figure_j - 1 >= 0 ) + { + figure_j -= 1; + switch ( k ) + { + case 1: + { + integral_b = 2; + break; + } + case 2: + { + integral_b = 4; + break; + } + case 3: + { + integral_b = 11; + break; + } + case 4: + { + integral_b = 201; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断用户向下 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i + 1 ][ figure_j ] == 'X' && figure_i + 1 < array ) + { + figure_i += 1; + switch ( k ) + { + case 1: + { + integral_b = 1; + break; + } + case 2: + { + integral_b = 5; + break; + } + case 3: + { + integral_b = 12; + break; + } + case 4: + { + integral_b = 200; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断用户向上 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i - 1 ][ figure_j ] == 'X' && figure_i - 1 >= 0 ) + { + figure_i -= 1; + switch ( k ) + { + case 1: + { + integral_b = 1; + break; + } + case 2: + { + integral_b = 5; + break; + } + case 3: + { + integral_b = 12; + break; + } + case 4: + { + integral_b = 200; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断AI向下 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i + 1 ][ figure_j ] == 'Y' && figure_i + 1 < array ) + { + figure_i += 1; + switch ( k ) + { + case 1: + { + integral_b = 2; + break; + } + case 2: + { + integral_b = 4; + break; + } + case 3: + { + integral_b = 11; + break; + } + case 4: + { + integral_b = 201; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断AI向上 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i - 1 ][ figure_j ] == 'Y' && figure_i - 1 >= 0 ) + { + figure_i -= 1; + switch ( k ) + { + case 1: + { + integral_b = 2; + break; + } + case 2: + { + integral_b = 4; + break; + } + case 3: + { + integral_b = 11; + break; + } + case 4: + { + integral_b = 201; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断用户左下 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i + 1 ][ figure_j - 1 ] == 'X' && figure_i + 1 < array && + figure_j - 1 >= 0 ) + { + figure_i += 1; + figure_j -= 1; + switch ( k ) + { + case 1: + { + integral_b = 1; + break; + } + case 2: + { + integral_b = 5; + break; + } + case 3: + { + integral_b = 12; + break; + } + case 4: + { + integral_b = 200; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断用户右上 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i - 1 ][ figure_j + 1 ] == 'X' && figure_i - 1 >= 0 && + figure_j + 1 < array ) + { + figure_i -= 1; + figure_j += 1; + switch ( k ) + { + case 1: + { + integral_b = 1; + break; + } + case 2: + { + integral_b = 5; + break; + } + case 3: + { + integral_b = 12; + break; + } + case 4: + { + integral_b = 200; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断AI左下 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i + 1 ][ figure_j - 1 ] == 'Y' && figure_i + 1 < array && + figure_j - 1 >= 0 ) + { + figure_i += 1; + figure_j -= 1; + switch ( k ) + { + case 1: + { + integral_b = 2; + break; + } + case 2: + { + integral_b = 4; + break; + } + case 3: + { + integral_b = 11; + break; + } + case 4: + { + integral_b = 201; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断AI右上 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i - 1 ][ figure_j + 1 ] == 'Y' && figure_i - 1 >= 0 && + figure_j + 1 < array ) + { + figure_i -= 1; + figure_j += 1; + switch ( k ) + { + case 1: + { + integral_b = 2; + break; + } + case 2: + { + integral_b = 4; + break; + } + case 3: + { + integral_b = 11; + break; + } + case 4: + { + integral_b = 201; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断用户右下 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i + 1 ][ figure_j + 1 ] == 'X' && figure_i + 1 < array && + figure_j + 1 < array ) + { + figure_i += 1; + figure_j += 1; + switch ( k ) + { + case 1: + { + integral_b = 1; + break; + } + case 2: + { + integral_b = 5; + break; + } + case 3: + { + integral_b = 12; + break; + } + case 4: + { + integral_b = 200; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断用户左上 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i - 1 ][ figure_j - 1 ] == 'X' && figure_i - 1 >= 0 && + figure_j - 1 >= 0 ) + { + figure_i -= 1; + figure_j -= 1; + switch ( k ) + { + case 1: + { + integral_b = 1; + break; + } + case 2: + { + integral_b = 5; + break; + } + case 3: + { + integral_b = 12; + break; + } + case 4: + { + integral_b = 200; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断AI右下 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i + 1 ][ figure_j + 1 ] == 'Y' && figure_i + 1 < array && + figure_j + 1 < array ) + { + figure_i += 1; + figure_j += 1; + switch ( k ) + { + case 1: + { + integral_b = 2; + break; + } + case 2: + { + integral_b = 4; + break; + } + case 3: + { + integral_b = 11; + break; + } + case 4: + { + integral_b = 201; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //判断AI左上 + figure_i = i; + figure_j = j; + for ( int k = 1 ; k <= 4 ; ++ k ) + { + if ( array_chessboard[ figure_i - 1 ][ figure_j - 1 ] == 'Y' && figure_i - 1 >= 0 && + figure_j - 1 >= 0 ) + { + figure_i -= 1; + figure_j -= 1; + switch ( k ) + { + case 1: + { + integral_b = 2; + break; + } + case 2: + { + integral_b = 4; + break; + } + case 3: + { + integral_b = 11; + break; + } + case 4: + { + integral_b = 201; + break; + } + } + } + else//如果没有就退出循环 + { + break; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //用户横向中间空一个 + figure_i = i; + figure_j = j; + if ( array_chessboard[ figure_i ][ figure_j + 1 ] == 'X' && + array_chessboard[ figure_i ][ figure_j - 1 ] == 'X' && figure_j + 1 < array && figure_j - 1 >= 0 ) + { + integral_b = 5; + //判断缺第三个 + if ( array_chessboard[ figure_i ][ figure_j + 2 ] == 'X' && + array_chessboard[ figure_i ][ figure_j - 2 ] == 'X' && figure_j + 2 < array && + figure_j - 2 >= 0 ) + { + integral_b = 200; + } + //判断缺第二个 + else if ( array_chessboard[ figure_i ][ figure_j + 2 ] == 'X' && + array_chessboard[ figure_i ][ figure_j + 3 ] == 'X' && figure_j + 3 < array ) + { + integral_b = 200; + } + //判断缺第四个 + else if ( array_chessboard[ figure_i ][ figure_j - 2 ] == 'X' && + array_chessboard[ figure_i ][ figure_j - 3 ] == 'X' && figure_j - 3 >= 0 ) + { + integral_b = 200; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //电脑横向中间空一个 + figure_i = i; + figure_j = j; + if ( array_chessboard[ figure_i ][ figure_j + 1 ] == 'Y' && + array_chessboard[ figure_i ][ figure_j - 1 ] == 'Y' && figure_j + 1 < array && figure_j - 1 >= 0 ) + { + integral_b = 4; + //判断缺第三个 + if ( array_chessboard[ figure_i ][ figure_j + 2 ] == 'Y' && + array_chessboard[ figure_i ][ figure_j - 2 ] == 'Y' && figure_j + 2 < array && + figure_j - 2 >= 0 ) + { + integral_b = 201; + } + //判断缺第二个 + else if ( array_chessboard[ figure_i ][ figure_j + 2 ] == 'Y' && + array_chessboard[ figure_i ][ figure_j + 3 ] == 'Y' && figure_j + 3 < array ) + { + integral_b = 201; + } + //判断缺第四个 + else if ( array_chessboard[ figure_i ][ figure_j - 2 ] == 'Y' && + array_chessboard[ figure_i ][ figure_j - 3 ] == 'Y' && figure_j - 3 >= 0 ) + { + integral_b = 201; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //用户纵向中间空一个 + figure_i = i; + figure_j = j; + if ( array_chessboard[ figure_i + 1 ][ figure_j ] == 'X' && + array_chessboard[ figure_i - 1 ][ figure_j ] == 'X' && figure_j + 1 < array && figure_j - 1 >= 0 ) + { + integral_b = 5; + //判断缺第三个 + if ( array_chessboard[ figure_i + 2 ][ figure_j ] == 'X' && + array_chessboard[ figure_i - 2 ][ figure_j ] == 'X' && figure_i + 2 < array && + figure_i - 2 >= 0 ) + { + integral_b = 200; + } + //判断缺第二个 + else if ( array_chessboard[ figure_i + 2 ][ figure_j ] == 'X' && + array_chessboard[ figure_i + 3 ][ figure_j ] == 'X' && figure_i + 3 < array ) + { + integral_b = 200; + } + //判断缺第四个 + else if ( array_chessboard[ figure_i - 2 ][ figure_j ] == 'X' && + array_chessboard[ figure_i - 3 ][ figure_j ] == 'X' && figure_i - 3 >= 0 ) + { + integral_b = 200; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //电脑纵向中间空一个 + figure_i = i; + figure_j = j; + if ( array_chessboard[ figure_i + 1 ][ figure_j ] == 'Y' && + array_chessboard[ figure_i - 1 ][ figure_j ] == 'Y' && figure_i + 1 < array && figure_i - 1 >= 0 ) + { + integral_b = 4; + //判断缺第三个 + if ( array_chessboard[ figure_i + 2 ][ figure_j ] == 'Y' && + array_chessboard[ figure_i - 2 ][ figure_j ] == 'Y' && figure_i + 2 < array && + figure_i - 2 >= 0 ) + { + integral_b = 201; + } + //判断缺第二个 + else if ( array_chessboard[ figure_i + 2 ][ figure_j ] == 'Y' && + array_chessboard[ figure_i + 3 ][ figure_j ] == 'Y' && figure_i + 3 < array ) + { + integral_b = 201; + } + //判断缺第四个 + else if ( array_chessboard[ figure_i - 2 ][ figure_j ] == 'Y' && + array_chessboard[ figure_i - 3 ][ figure_j ] == 'Y' && figure_i - 3 >= 0 ) + { + integral_b = 201; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //用户右上左下中间空一个 + figure_i = i; + figure_j = j; + if ( array_chessboard[ figure_i + 1 ][ figure_j - 1 ] == 'X' && + array_chessboard[ figure_i - 1 ][ figure_j + 1 ] == 'X' && figure_i + 1 < array && + figure_j - 1 >= 0 && figure_i - 1 >= 0 && figure_j + 1 < array ) + { + integral_b = 5; + //判断缺第三个 + if ( array_chessboard[ figure_i + 2 ][ figure_j - 2 ] == 'X' && + array_chessboard[ figure_i - 2 ][ figure_j + 2 ] == 'X' && figure_i + 2 < array && + figure_j - 2 >= 0 && figure_i - 2 >= 0 & figure_j + 2 < array ) + { + integral_b = 200; + } + //判断缺第二个 + else if ( array_chessboard[ figure_i + 2 ][ figure_j - 2 ] == 'X' && + array_chessboard[ figure_i + 3 ][ figure_j - 3 ] == 'X' && figure_i + 3 < array && + figure_j - 3 >= 0 ) + { + integral_b = 200; + } + //判断缺第四个 + else if ( array_chessboard[ figure_i - 2 ][ figure_j + 2 ] == 'X' && + array_chessboard[ figure_i - 3 ][ figure_j + 3 ] == 'X' && figure_j - 3 >= 0 && + figure_j + 3 < array ) + { + integral_b = 200; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //电脑右上左下中间空一个 + figure_i = i; + figure_j = j; + if ( array_chessboard[ figure_i + 1 ][ figure_j - 1 ] == 'Y' && + array_chessboard[ figure_i - 1 ][ figure_j + 1 ] == 'Y' && figure_i + 1 < array && + figure_j - 1 >= 0 && figure_i - 1 >= 0 && figure_j + 1 < array ) + { + integral_b = 4; + //判断缺第三个 + if ( array_chessboard[ figure_i + 2 ][ figure_j - 2 ] == 'Y' && + array_chessboard[ figure_i - 2 ][ figure_j + 2 ] == 'Y' && figure_i + 2 < array && + figure_j - 2 >= 0 && figure_i - 2 >= 0 && figure_j + 2 < array ) + { + integral_b = 201; + } + //判断缺第二个 + else if ( array_chessboard[ figure_i + 2 ][ figure_j - 2 ] == 'Y' && + array_chessboard[ figure_i + 3 ][ figure_j - 3 ] == 'Y' && figure_i + 3 < array && + figure_j - 3 >= 0 ) + { + integral_b = 201; + } + //判断缺第四个 + else if ( array_chessboard[ figure_i - 2 ][ figure_j + 2 ] == 'Y' && + array_chessboard[ figure_i - 3 ][ figure_j + 3 ] == 'Y' && figure_i - 3 >= 0 && + figure_j + 3 < array ) + { + integral_b = 201; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //用户左上右下中间空一个 + figure_i = i; + figure_j = j; + if ( array_chessboard[ figure_i + 1 ][ figure_j + 1 ] == 'X' && + array_chessboard[ figure_i - 1 ][ figure_j - 1 ] == 'X' && figure_j + 1 < array && + figure_j - 1 >= 0 ) + { + integral_b = 5; + //判断缺第三个 + if ( array_chessboard[ figure_i + 2 ][ figure_j + 2 ] == 'X' && + array_chessboard[ figure_i - 2 ][ figure_j - 2 ] == 'X' && figure_i + 2 < array && + figure_j + 2 < array && figure_i - 2 >= 0 && figure_j - 2 >= 0 ) + { + integral_b = 200; + } + //判断缺第二个 + else if ( array_chessboard[ figure_i + 2 ][ figure_j + 2 ] == 'X' && + array_chessboard[ figure_i + 3 ][ figure_j + 3 ] == 'X' && figure_i + 3=0&&figure_j - 3 >=0 ) + { + integral_b = 200; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //电脑左上右下中间空一个 + figure_i = i; + figure_j = j; + if ( array_chessboard[ figure_i + 1 ][ figure_j + 1 ] == 'Y' && + array_chessboard[ figure_i - 1 ][ figure_j - 1 ] == 'Y' && figure_i + 1 < array && + figure_j + 1 =0 && figure_j - 1>=0) + { + integral_b = 4; + //判断缺第三个 + if ( array_chessboard[ figure_i + 2 ][ figure_j + 2 ] == 'Y' && + array_chessboard[ figure_i - 2 ][ figure_j - 2 ] == 'Y' && figure_i + 2 < array && + figure_j + 2 =0&& figure_j - 2>=0 ) + { + integral_b = 201; + } + //判断缺第二个 + else if ( array_chessboard[ figure_i + 2 ][ figure_j + 2 ] == 'Y' && + array_chessboard[ figure_i + 3 ][ figure_j + 3 ] == 'Y' && figure_j + 3 < array ) + { + integral_b = 201; + } + //判断缺第四个 + else if ( array_chessboard[ figure_i - 2 ][ figure_j - 2 ] == 'Y' && + array_chessboard[ figure_i - 3 ][ figure_j - 3 ] == 'Y' && figure_j - 3 >=0 ) + { + integral_b = 201; + } + } + integral_a += integral_b;//增加积分 + integral_b = 0;//重置 + + //储存最大坐标 + if ( integral_a > integral ) + { + integral = integral_a;//更新最大值 + storage_coordinate[ 0 ] = i;//更新纵坐标 + storage_coordinate[ 1 ] = j;//更新横坐标 + } + //重置所有积分 + integral_a = 0;//重置 + integral_b = 0;//重置 + } + } + + } + * x = storage_coordinate[ 0 ];//更新纵坐标 + * y = storage_coordinate[ 1 ];//更新横坐标 + return; +} + +//复制数值 +char copy_array ( int array , char array_chessboard[array][array] , char array_chessboard_1[array][array] ) +{ + for ( int i = 0 ; i < array ; i ++ ) + { + for ( int j = 0 ; j < array ; j ++ ) + { + array_chessboard_1[ i ][ j ] = array_chessboard[ i ][ j ]; + } + } + return array_chessboard_1[ array ][ array ]; +} + +//双人模式 +void double_game () +{ + //初始化 + char user;//判断是那个用户 + int bout = 0;//用户输入的回合 + int x;//用户横坐标 + int y;//用户纵坐标 + int array;//棋盘长度 + printf ( "请输入棋盘长度(0开始计算,12<=chess<=25):" ); + while ( scanf ( "%d" , & array ) != 1 || array < 12 || array > 25 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "请重新输入棋盘长度:" ); + } + char array_chessboard[array][array];//初始化棋盘 + initialization ( array , array_chessboard );//初始化数组里面的数 + while ( bout < array * array ) + { + print_chessboard ( array , array_chessboard );//输出当前地图 + bout ++; + printf ( "第%d回合\n" , ( bout + 1 ) / 2 ); + if ( bout % 2 == 1 ) + { + user = 'X'; + } + else + { + user = 'Y'; + } + printf ( "轮到用户%c \n" , user ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数组:" ); + } + printf ( "请输入纵坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + credited ( array , x , y , array_chessboard , user );//存入用户输入的数字 + if ( judgment ( array , user , array_chessboard ) == user )//判断用户是否胜利 + { + printf ( "用户%c胜利\n" , user ); + break; + } + } + + if ( bout == array * array )//平局判断 + { + printf ( "平局\n" ); + } + print_chessboard ( array , array_chessboard ); //输出棋盘 + while ( getchar ( ) != '\n' );//清除缓存 + home ( 1 );//返回主页 +} + +//单人模式 +void single_game () +{ + //初始化 + char user;//判断是那个用户 + int bout = 0;//用户输入的回合 + int x;//横坐标 + int y;//纵坐标 + int array;//棋盘长度 + printf ( "请输入棋盘长度(0开始计算,12<=chess<=25):" ); + while ( scanf ( "%d" , & array ) != 1 || array < 12 || array > 25 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "请重新输入棋盘长度:" ); + } + char array_chessboard[array][array];//初始化棋盘 + initialization ( array , array_chessboard );//初始化数组里面的数 + print_chessboard ( array , array_chessboard );//输出当前地图 + while ( bout < array * array / 2 ) + { + bout ++; + printf ( "第%d回合\n" , bout ); + user = 'X'; + printf ( "轮到用户 \n" ); + printf ( "请输入横坐标:" ); + while ( scanf ( "%d" , & x ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + printf ( "请输入纵坐标:" ); + while ( scanf ( "%d" , & y ) != 1 ) + { + while ( getchar ( ) != '\n' ); + printf ( "请输入合法数字:" ); + } + credited ( array , x , y , array_chessboard , user );//存入用户输入的数字 + if ( judgment ( array , user , array_chessboard ) == user )//判断用户是否胜利 + { + printf ( "用户胜利\n" ); + break; + } + + user = 'Y';//更新电脑标识 + AI_chess ( array , & x , & y , array_chessboard );//获取ai判断输出的数 + credited ( array , x , y , array_chessboard , user );//存入AI输入的数字 + if ( judgment ( array , user , array_chessboard ) == user )//判断用户是否胜利 + { + printf ( "AI胜利\n" ); + break; + } + print_chessboard ( array , array_chessboard );//输出当前地图 + + } + + if ( bout == array * array / 2 )//平局判断 + { + printf ( "平局\n" ); + } + print_chessboard ( array , array_chessboard ); //输出棋盘 + while ( getchar ( ) != '\n' );//清除缓存 + home ( 1 ); +} + +//菜单 +void home ( int pick ) +{ + char symbol;//获取用户输入什么 + char array_1[] = "单人模式"; + char array_2[] = "双人模式"; + char array_3[] = "退出游戏"; + char array_4[] = "单人模式←"; + char array_5[] = "双人模式←"; + char array_6[] = "退出游戏←"; + printf ( "————五子棋————\n" ); + switch ( pick ) //输出图像 + { + case 1: + { + printf ( "%s\n" , array_4 ); + printf ( "%s\n" , array_2 ); + printf ( "%s\n" , array_3 ); + break; + } + case 2: + { + printf ( "%s\n" , array_1 ); + printf ( "%s\n" , array_5 ); + printf ( "%s\n" , array_3 ); + break; + } + case 3: + { + printf ( "%s\n" , array_1 ); + printf ( "%s\n" , array_2 ); + printf ( "%s\n" , array_6 ); + break; + } + } + printf ( "按WS上下选择\n" ); + printf ( "回车确定选择\n" ); + printf ( ":" ); + scanf ( "%c" , & symbol ); + if ( symbol == '\n' ) + { + switch ( pick ) + { + case 1: + { + fflush ( stdin );//清除缓存 + single_game ( ); + break; + } + case 2: + { + fflush ( stdin );//清除缓存 + double_game ( ); + break; + } + case 3: + { + printf ( "欢迎下次使用" ); + exit ( 0 ); + break; + } + }//选择需要进入的模式 + } + else if ( symbol == 'W' || symbol == 'w' ) + { + if ( pick <= 1 ) + { + pick = 1; + while ( getchar ( ) != '\n' );//清除缓存 + home ( pick );//返回主页 + } + else + { + pick -= 1; + while ( getchar ( ) != '\n' );//清除缓存 + home ( pick );//返回主页 + } + } + else if ( symbol == 'S' || symbol == 's' ) + { + if ( pick >= 3 ) + { + pick = 3; + while ( getchar ( ) != '\n' );//清除缓存 + home ( pick );//返回主页 + } + else + { + pick += 1; + while ( getchar ( ) != '\n' );//清除缓存 + home ( pick );//返回主页 + } + } + + while ( getchar ( ) != '\n' );//清除缓存 + home ( pick );//返回主页 + +} + +int main () +{ + srand ( time ( NULL ) );// 设置种子,可以让每次运行程序时得到不同的随机数序列 + home ( 1 ); + return 0; +} diff --git a/practice_code/idea/Snakes/Snakes_1.c b/practice_code/idea/Snakes/Snakes_1.c new file mode 100644 index 0000000..69d5c91 --- /dev/null +++ b/practice_code/idea/Snakes/Snakes_1.c @@ -0,0 +1,312 @@ +#include +#include +#include +#include +#include +#include + +#define map_Longitudinal 25//地图高度 +#define map_across 80//地图宽度 +int integral = 0;//玩家积分 +int MAX_integral =0;//最高积分 +int pick_food=1;//判断地图上有没有食物 + +char map[map_Longitudinal][map_across];//贪吃蛇地图 + +typedef struct +{ + int y;//纵坐标 + int x;//横坐标 +} snake_head;//蛇头 + +snake_head * head;//全局声明蛇头 + +typedef struct +{ + int y[99];//纵坐标 + int x[99];//横坐标 + int length;//蛇身体长度 +} snake_body;//蛇身 + +snake_body * body;//全局声明蛇身 + +void HideCursor ()//隐藏光标 +{ + CONSOLE_CURSOR_INFO curInfo; //定义光标信息的结构体变量 + curInfo . dwSize = 1; //如果没赋值的话,光标隐藏无效 + curInfo . bVisible = FALSE; //将光标设置为不可见 + HANDLE handle = GetStdHandle ( STD_OUTPUT_HANDLE ); //获取控制台句柄 + SetConsoleCursorInfo ( handle , & curInfo ); //设置光标信息 +} + + +void food_map ()//在动图上生成食物 +{ + int i = rand ( ) % map_Longitudinal;//随机食物纵坐标 + int j = rand ( ) % map_across;//随机食物横坐标 + if ( map[ i ][ j ] == '*' )//监测到有蛇就重新生成 + { + food_map ( ); + } + else + { + map[ i ][ j ] = '@'; + } +} + +void move ( int move_x , int move_y )//移动逻辑 +{ + if(pick_food==0) + { + body -> x[ body->length ] = body -> x[ body->length - 1 ]; + body -> y[ body->length ] = body -> y[ body->length - 1 ]; + body->length+=1;//增加蛇身体长度 + integral+=10;//增加积分 + } + + map[ body -> y[ body->length - 1 ] ][ body -> x[ body->length - 1 ] ] = ' ';//将蛇尾最后一个取消 + //更新蛇身坐标 + for ( int i = body->length - 1 ; i > 0 ; -- i ) + { + body -> x[i] = body -> x[ i - 1 ]; + body -> y[i ] = body -> y[ i- 1 ]; + } + body -> x[ 0 ] = head -> x; + body -> y[ 0 ] = head -> y; + //将蛇头原来的坐标替换为身体 + map[ head -> y ][ head -> x ] = '*'; + //更新蛇头坐标 + head -> x += move_x; + head -> y += move_y; + //把蛇头的坐标在地图更新 + map[ head -> y ][ head -> x] = '#'; + if(pick_food==0) + { + food_map (); + pick_food=1; + } +} + +void Begin ()//开始界面 +{ + printf ( "\n\n\n\n\n\n\n\n\t\t\t\t\t——————————贪吃蛇——————————\n" ); + printf ( "\t\t\t\t\t|使用WSAD进行上下左右移动|\n" ); + printf ( "\t\t\t\t\t|按ESC退出游戏 |\n" ); + printf ( "\t\t\t\t\t|回车进入游戏 |\n" ); + printf ( "\t\t\t\t\t——————————贪吃蛇——————————\n" ); + int a = _getch ( ); + if ( a == 27 ) + { + exit ( 0 ); + } +} + +void initial_snake ()//初始化蛇 +{ + //初始化蛇头 + head -> x = 10; + head -> y = 10; + //初始化蛇身 + body -> x[ 1 ] = 9; + body -> y[ 1 ] = 10; + body -> x[ 0 ] = 8; + body -> y[ 0 ] = 10; + body->length = 2; + //生成蛇的图像 + map[ head -> y][ head -> x] = '#'; + map[ body -> y[ body->length - 1 ] ][ body -> x[ body->length - 1 ] ] = '*'; + map[ body -> y[ body->length - 2 ] ][ body -> x[ body->length - 2 ] ] = '*'; + +} + +void initial_map ()//初始化地图 +{ + for ( int i = 0 ; i < map_Longitudinal ; ++ i )//给地图初始化为空 + { + for ( int j = 0 ; j < map_across ; ++ j ) + { + map[ i ][ j ] = ' '; + } + } + food_map ( );//在动图上生成食物 +} + +void print_map ()//打印地图 +{ + + for ( int i = 0 ; i < map_Longitudinal ; ++ i ) + { + if ( i == 0 ) + { + for ( int j = 0 ; j <= map_across + 1 ; ++ j ) + { + printf ( "▄" ); + } + printf ( "\n" ); + } + + for ( int j = 0 ; j < map_across ; ++ j ) + { + if ( j == 0 )//打印边界 + { + printf ( "█" ); + } + printf ( "%c" , map[ i ][ j ] );//输出地图 + } + printf ( "█" );//打印边界 + printf ( "\n" ); + } + for ( int j = 0 ; j <= map_across + 1 ; ++ j ) + { + printf ( "▀" ); + } + printf ( "\n当前积分为%d分\t\t\t\t\t\t\t最高积分为%d分" , integral,MAX_integral ); +} + +int defeat ()//失败界面 +{ + printf ( "\n\n\t\t\t—————————游戏失败—————————\n" ); + printf ( "\t\t\t|按ESC退出游戏 |\n" ); + printf ( "\t\t\t|其他键继续游戏 |\n" ); + printf ( "\t\t\t—————————游戏失败—————————\n" ); + int a = _getch ( ); + if ( a == 27 ) + { + exit ( 0 ); + } + else + { + MAX_integral=integral;//保存最大积分 + integral = 0;//玩家积分 + a=1; + return a; + } + + +} + + +int main () +{ + Begin ( );//开始界面 + game_Begin: + head = ( snake_head * ) malloc ( sizeof ( snake_head ) );// 动态分配蛇头内存 + body = ( snake_body * ) malloc ( sizeof ( snake_body ) );// 动态分配蛇头内存 + srand ( time ( NULL ) );//设置随机数 + char direction_test;//方向接收 + char direction = 's';//方向 + int move_x = 0;//更新移动所产生的横坐标 + int move_y = 0;//更新移动所产生的纵坐标 + int pick=0;//接收是否重开游戏 + HideCursor ( );//隐藏光标 + initial_map ( );//初始化地图 + initial_snake ( );//初始化地图里面的蛇 + while ( 1 ) + { + system ( "cls" );//清屏 + print_map ( );//打印当前地图 + if ( _kbhit ( ) )//接收用户输入 + { + direction_test = _getch ( ); + if ( direction_test == 'W' || direction_test == 'w' || direction_test == 'S' || direction_test == 's' || + direction_test == 'A' || direction_test == 'a' || direction_test == 'D' || + direction_test == 'd' )//判断方向对不对 + { + direction = direction_test; + } + } + //判断移动的坐标 + if ( direction == 'W' || direction == 'w' )//判断向上 + { + if ( move_y == 1 ) + { + move_x = 0;//更新移动所产生的横坐标 + move_y = 1;//更新移动所产生的纵坐标 + } + else + { + if ( head -> y - 1 < 0 ||map[ head -> y- 1 ][ head -> x] == '*' )//判断有没有失败 + { + pick=defeat ( ); + } + if(map[ head -> y - 1 ][ head -> x] == '@') + { + pick_food=0;//提示食物没有了 + } + move_x = 0;//更新移动所产生的横坐标 + move_y = - 1;//更新移动所产生的纵坐标 + } + } + else if ( direction == 'S' || direction == 's' )//判断向下 + { + if ( move_y == - 1 ) + { + move_x = 0;//更新移动所产生的横坐标 + move_y = - 1;//更新移动所产生的纵坐标 + } + else + { + if ( head -> y + 1 >= map_Longitudinal||map[ head -> y+ 1 ][ head -> x] == '*' )//判断有没有失败 + { + pick=defeat ( ); + } + if(map[ head -> y+ 1 ][ head -> x] == '@' ) + { + pick_food=0;//提示食物没有了 + } + move_x = 0;//更新移动所产生的横坐标 + move_y = 1;//更新移动所产生的纵坐标 + } + } + else if ( direction_test == 'A' || direction_test == 'a' )//判断向左 + { + if ( move_x == 1 ) + { + move_x = 1;//更新移动所产生的横坐标 + move_y = 0;//更新移动所产生的纵坐标 + } + else + { + if (head -> x- 1 < 0 ||map[ head -> y][ head -> x- 1 ] == '*' )//判断有没有失败 + { + pick=defeat ( ); + } + if(map[ head -> y][ head -> x- 1 ] == '@') + { + pick_food=0;//提示食物没有了 + } + move_x = - 1;//更新移动所产生的横坐标 + move_y = 0;//更新移动所产生的纵坐标 + } + } + else if ( direction_test == 'D' || direction_test == 'd' )//判断右 + { + if ( move_x == - 1 ) + { + move_x = - 1;//更新移动所产生的横坐标 + move_y = 0;//更新移动所产生的纵坐标 + } + else + { + if (head -> x+ 1 >=map_across ||map[ head -> y][ head -> x+ 1 ] == '*' )//判断有没有失败 + { + pick=defeat ( ); + } + if(map[ head -> y][ head -> x+ 1 ] == '@') + { + pick_food=0;//提示食物没有了 + } + move_x = 1;//更新移动所产生的横坐标 + move_y = 0;//更新移动所产生的纵坐标 + } + } + + if(pick==1) + { + goto game_Begin; + } + move ( move_x , move_y );//将坐标移动 + Sleep ( 80 );//停顿一秒 + } + return 0; +} diff --git a/Snakes/main.c b/practice_code/idea/Snakes/Snakes_2.c similarity index 100% rename from Snakes/main.c rename to practice_code/idea/Snakes/Snakes_2.c diff --git a/practice_code/idea/Snakes/Snakes_3.c b/practice_code/idea/Snakes/Snakes_3.c new file mode 100644 index 0000000..774e1f9 --- /dev/null +++ b/practice_code/idea/Snakes/Snakes_3.c @@ -0,0 +1,1217 @@ +#include +#include +#include +#include +#include +#include + +#define map_Longitudinal 27//地图高度 +#define map_across 102//地图宽度 +int map[map_Longitudinal][map_across];//储存地图状态 + +int MAX_integral = 0;//玩家最高分 +//定义是哪条蛇 +#define mark_NULL 97//标记空 +#define mark_food 98//标记为食物 +#define mark_wall 99//标记墙 + +#define UP 72 //方向键:上 +#define DOWN 80 //方向键:下 +#define LEFT 75 //方向键:左 +#define RIGHT 77 //方向键:右 + +//蛇 + +//得分 +typedef struct +{ + int y;//纵坐标 + int x;//横坐标 + int MAX_x;//横坐标 + int integral;//玩家积分 +} game_Integral; + +//蛇头 +typedef struct +{ + int y;//纵坐标 + int x;//横坐标 + int direction;//蛇移动的方向 + int move_x;//更新移动所产生的横坐标 + int move_y;//更新移动所产生的纵坐标 + int food;//判断是否吃到食物 +} snake_head; + +//蛇身 +typedef struct +{ + int color;//蛇的颜色 + int y[99];//纵坐标 + int x[99];//横坐标 + int length;//蛇身体长度 +} snake_body; + +//哪条蛇 +typedef struct +{ + snake_head * head;//头 + snake_body * body;//身体 + game_Integral * score;//积分 +} name_snake; + +name_snake * snake[99];//给蛇全局声明 +//蛇 + +void HideCursor ()//隐藏光标 +{ + CONSOLE_CURSOR_INFO curInfo; //定义光标信息的结构体变量 + curInfo . dwSize = 1; //如果没赋值的话,光标隐藏无效 + curInfo . bVisible = FALSE; //将光标设置为不可见 + HANDLE handle = GetStdHandle ( STD_OUTPUT_HANDLE ); //获取控制台句柄 + SetConsoleCursorInfo ( handle , & curInfo ); //设置光标信息 +} + +void CursorJump ( int y , int x )//光标跳转 +{ + COORD pos; //定义光标位置的结构体变量 + pos . X = x; //横坐标 + pos . Y = y; //纵坐标 + HANDLE handle = GetStdHandle ( STD_OUTPUT_HANDLE ); //获取控制台句柄 + SetConsoleCursorPosition ( handle , pos ); //设置光标位置 +} + +void color ( int c )//颜色设置 +{ + SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ) , c ); //颜色设置 + //注:SetConsoleTextAttribute是一个API(应用程序编程接口) +} + + +void print_score ( int pick_score )//打印积分 +{ + + if ( pick_score == 1 ) + { + color ( snake[ 0 ] -> body -> color );//设置积分颜色 + CursorJump ( snake[ 0 ] -> score -> y , snake[ 0 ] -> score -> x ); + printf ( "当前积分为%d分" , snake[ 0 ] -> score -> integral ); + CursorJump ( snake[ 0 ] -> score -> y , snake[ 0 ] -> score -> MAX_x ); + printf ( "最高积分为%d分" , MAX_integral ); + } + else if ( pick_score == 2 ) + { + color ( snake[ 0 ] -> body -> color );//设置积分颜色 + CursorJump ( snake[ 0 ] -> score -> y , snake[ 0 ] -> score -> x ); + printf ( "一号玩家积分为%d分" , snake[ 0 ] -> score -> integral ); + CursorJump ( snake[ 0 ] -> score -> y + 1 , snake[ 0 ] -> score -> x ); + printf ( "W S A D进行上下左右移动" ); + color ( snake[ 1 ] -> body -> color );//设置积分颜色 + CursorJump ( snake[ 0 ] -> score -> y , snake[ 0 ] -> score -> MAX_x - 5 ); + printf ( "二号玩家积分为%d分" , snake[ 1 ] -> score -> integral ); + CursorJump ( snake[ 0 ] -> score -> y + 1 , snake[ 0 ] -> score -> MAX_x - 5 ); + printf ( "↑ ↓ ← → 进行上下左右移动" ); + } + +} + +void food_map ()//在动图上生成食物 +{ + int i = rand ( ) % map_Longitudinal;//随机食物纵坐标 + int j = rand ( ) % map_across;//随机食物横坐标 + int random = rand ( ) % 14 + 1;//随机生成颜色 + if ( map[ i ][ j ] == mark_NULL )//监测到有物体就重新生成 + { + map[ i ][ j ] = mark_food; // 正确标记食物 + color ( random ); + CursorJump ( i , j ); + printf ( "●" ); + } + else + { + food_map ( ); + } +} + +void move ( int snake_digit )//移动逻辑 +{ + color ( snake[ snake_digit ] -> body -> color ); //颜色设置 + if ( snake[ snake_digit ] -> head -> food == 1 )//如果吃到食物 + { + snake[ snake_digit ] -> body -> x[ snake[ snake_digit ] -> body -> length ] = snake[ snake_digit ] -> body -> x[ + snake[ snake_digit ] -> body -> length - 1 ];//新增的一节赋值现在最后一节的x + snake[ snake_digit ] -> body -> y[ snake[ snake_digit ] -> body -> length ] = snake[ snake_digit ] -> body -> y[ + snake[ snake_digit ] -> body -> length - 1 ];//新增的一节赋值现在最后一节的y + map[ snake[ snake_digit ] -> body -> y[ snake[ snake_digit ] -> body -> length ] ][ snake[ snake_digit ] -> body -> x[ snake[ snake_digit ] -> body -> length ] ] = snake_digit;//标记蛇 + snake[ snake_digit ] -> body -> length += 1;//增加蛇身体长度 + if ( snake_digit == 1 || snake_digit == 0 ) + { + snake[ snake_digit ] -> score -> integral += 10;//增加积分 + } + } + //将之前身体的置空 + for ( int i = 0 ; i < snake[ snake_digit ] -> body -> length ; ++ i ) + { + map[ snake[ snake_digit ] -> body -> y[ i ] ][ snake[ snake_digit ] -> body -> x[ i ] ] = mark_NULL;//标记为空 + CursorJump ( snake[ snake_digit ] -> body -> y[ i ] , snake[ snake_digit ] -> body -> x[ i ] ); + printf ( " " ); + } + + //更新蛇身坐标 + for ( int i = snake[ snake_digit ] -> body -> length - 1 ; i > 0 ; -- i ) + { + snake[ snake_digit ] -> body -> x[ i ] = snake[ snake_digit ] -> body -> x[ i - 1 ];//将本节的x坐标换成前一节的 + snake[ snake_digit ] -> body -> y[ i ] = snake[ snake_digit ] -> body -> y[ i - 1 ];//将本节的y坐标换成前一节的 + } + snake[ snake_digit ] -> body -> x[ 0 ] = snake[ snake_digit ] -> head -> x;//将第一个的x坐标换成头部 + snake[ snake_digit ] -> body -> y[ 0 ] = snake[ snake_digit ] -> head -> y;//将第一个的u坐标换成头部 + //将重新打印身体 + for ( int i = 0 ; i < snake[ snake_digit ] -> body -> length ; ++ i ) + { + map[ snake[ snake_digit ] -> body -> y[ i ] ][ snake[ snake_digit ] -> body -> x[ i ] ] = snake_digit;//标记蛇 + CursorJump ( snake[ snake_digit ] -> body -> y[ i ] , snake[ snake_digit ] -> body -> x[ i ] ); + printf ( "□" ); + } + + //更新蛇头坐标 + snake[ snake_digit ] -> head -> x += snake[ snake_digit ] -> head -> move_x; + snake[ snake_digit ] -> head -> y += snake[ snake_digit ] -> head -> move_y; + //把蛇头的坐标在地图更新 + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x ] = snake_digit;//标记蛇 + CursorJump ( snake[ snake_digit ] -> head -> y , snake[ snake_digit ] -> head -> x ); + printf ( "■" ); + +} + +int Begin ()//开始界面 +{ + begin_tab: + system ( "cls" ); + printf ( "\n\n\n\n\n\n\n\n\t\t\t\t\t————————————贪吃蛇————————————\n" ); + printf ( "\t\t\t\t\t|1.单人模式\t\t |\n" ); + printf ( "\t\t\t\t\t|2.双人模式(先出现的为一号蛇)|\n" ); + printf ( "\t\t\t\t\t|3.人机模式(先出现的为用户蛇)|\n" ); + printf ( "\t\t\t\t\t 请输入:" ); + int pick_Begin;//接收开始页面的选择 + while ( ( scanf ( "%d" , & pick_Begin ) ) != 1 ) + { + while ( getchar ( ) != '\n' ); + goto begin_tab; + } + if ( pick_Begin == 0 ) + { + exit ( 0 ); + } + else if ( pick_Begin >= 1 && pick_Begin <= 3 ) + { + return pick_Begin; + } + else + { + while ( getchar ( ) != '\n' ); + printf ( "输入错误,请重新输入" ); + goto begin_tab; + } +} + +void initial_snake ( int snake_digit )//生成蛇 +{ + int pick_x = rand ( ) % 60 + 12;//随机生成横坐标 + int pick_y = rand ( ) % 17 + 1;//随机生成纵坐标 + snake[ snake_digit ] -> body -> color = rand ( ) % 14 + 1;//给蛇随机生成颜色 + color ( snake[ snake_digit ] -> body -> color ); //给蛇设置颜色 + while ( map[ pick_y ][ pick_x ] != mark_NULL || map[ pick_y ][ pick_x - 1 ] != mark_NULL || + map[ pick_y ][ pick_x - 2 ] != mark_NULL )//判断生成的位置是不是空地 + { + pick_x = rand ( ) % 70 + 10;//随机生成横坐标 + pick_y = rand ( ) % 17 + 1;//随机生成纵坐标 + } + //初始化蛇头 + snake[ snake_digit ] -> head -> x = pick_x; + snake[ snake_digit ] -> head -> y = pick_y; + //初始化蛇身 + snake[ snake_digit ] -> body -> x[ 0 ] = pick_x - 1; + snake[ snake_digit ] -> body -> y[ 0 ] = pick_y; + snake[ snake_digit ] -> body -> x[ 1 ] = pick_x - 2; + snake[ snake_digit ] -> body -> y[ 1 ] = pick_y; + snake[ snake_digit ] -> body -> length = 2; + //生成蛇的图像 + //蛇头 + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x ] = snake_digit;//标记蛇 + CursorJump ( snake[ snake_digit ] -> head -> y , snake[ snake_digit ] -> head -> x ); + printf ( "■" ); + //蛇身 + for ( int i = snake[ snake_digit ] -> body -> length - 1 ; i >= 0 ; -- i ) + { + map[ snake[ snake_digit ] -> body -> y[ i ] ][ snake[ snake_digit ] -> body -> x[ i ] ] = snake_digit;//标记蛇 + CursorJump ( snake[ snake_digit ] -> body -> y[ i ] , snake[ snake_digit ] -> body -> x[ i ] ); + printf ( "□" ); + } + //初始化积分 + snake[ snake_digit ] -> score -> integral = 0; + //初始化移动所产生的坐标 + snake[ snake_digit ] -> head -> move_x = 0; + snake[ snake_digit ] -> head -> move_y = 0; + //初始蛇移动方向 + snake[ snake_digit ] -> head -> direction = 4; + //有没有吃到食物 + snake[ snake_digit ] -> head -> food = 0; +} + +void print_map ()//初始地图 +{ + color ( 12 ); + for ( int i = 0 ; i < map_Longitudinal ; ++ i ) + { + if ( i == 0 || i == map_Longitudinal - 1 ) + { + for ( int j = 0 ; j < map_across ; ++ j ) + { + //打印墙并且标记状态为墙 + map[ i ][ j ] = mark_wall; + printf ( "█" ); + } + printf ( "\n" ); + } + else + { + for ( int j = 0 ; j < map_across ; ++ j ) + { + if ( j == 0 || j == map_across - 1 )//打印边界 + { + //打印墙并且标记状态为墙 + map[ i ][ j ] = mark_wall; + printf ( "█" ); + } + else + { + map[ i ][ j ] = mark_NULL;//标记为空 + printf ( " " );//输出地图 + } + } + printf ( "\n" ); + } + } + snake[ 0 ] -> score -> x = 1;//积分横坐标 + snake[ 0 ] -> score -> y = map_Longitudinal;//积分纵坐标 + snake[ 0 ] -> score -> MAX_x = map_across - 14;//最大积分横坐标 +} + +void defeat ( int snake_digit , int toll )//失败界面 +{ + defeat_tab: + color ( 12 ); + CursorJump ( 10 , 50 ); + printf ( "\n\t\t\t\t———————游戏失败———————\n" ); + if ( toll == 2 ) + { + printf ( "\t\t\t\t|%d号玩家的蛇死亡 |\n" , snake_digit + 1 ); + } + printf ( "\t\t\t\t|0.退出游戏 |\n" ); + printf ( "\t\t\t\t|1.返回主页 |\n" ); + printf ( "\t\t\t\t|2.重新开始 |\n" ); + printf ( "\t\t\t\t 请输入:" ); + int pick_defeat;//接收选择 + while ( ( scanf ( "%d" , & pick_defeat ) ) != 1 ) + { + while ( getchar ( ) != '\n' ); + system ( "cls" ); + goto defeat_tab; + } + if ( pick_defeat == 0 ) + { + exit ( 0 ); + } + else if ( pick_defeat == 1 ) + { + snake[ snake_digit ] -> head -> direction = 6; + } + else if ( pick_defeat == 2 ) + { + snake[ snake_digit ] -> head -> direction = 5; + } + else + { + while ( getchar ( ) != '\n' ); + system ( "cls" ); + goto defeat_tab; + } + +} + +void reception_input ( int snake_digit , int toll )//接收用户输入 +{ + //将上一次的状态转为方向 + char direction_test;//方向接收 + if ( toll == 1 ) + { + if ( _kbhit ( ) )//接收用户输入 + { + direction_test = _getch ( ); + if ( direction_test == 'W' || direction_test == 'w' || direction_test == UP )//向上 + { + snake[ 0 ] -> head -> direction = 1; + } + else if ( direction_test == 'S' || direction_test == 's' || direction_test == DOWN )//向下 + { + snake[ 0 ] -> head -> direction = 2; + } + else if ( direction_test == 'A' || direction_test == 'a' || direction_test == LEFT )//向右 + { + snake[ 0 ] -> head -> direction = 3; + } + else if ( direction_test == 'D' || direction_test == 'd' || direction_test == RIGHT )//向左 + { + snake[ 0 ] -> head -> direction = 4;//方向 + } + else if ( direction_test == 'R' || direction_test == 'r' )//重开 + { + snake[ 0 ] -> head -> direction = 5;//重开 + } + } + } + else if ( toll == 2 ) + { + if ( _kbhit ( ) )//接收用户输入 + { + direction_test = _getch ( ); + if ( direction_test == 'W' || direction_test == 'w' && snake[ 0 ] -> head -> move_y != 1 )//向上 + { + snake_digit = 0;//更新蛇 + snake[ snake_digit ] -> head -> direction = 1;//更新蛇移动方向 + } + if ( direction_test == 'S' || direction_test == 's' && snake[ 0 ] -> head -> move_y != - 1 )//向下 + { + snake_digit = 0;//更新蛇 + snake[ snake_digit ] -> head -> direction = 2;//更新蛇移动方向 + } + if ( direction_test == 'A' || direction_test == 'a' && snake[ 0 ] -> head -> move_x != 1 )//向左 + { + snake_digit = 0;//更新蛇 + snake[ snake_digit ] -> head -> direction = 3;//更新蛇移动方向 + } + if ( direction_test == 'D' || direction_test == 'd' && snake[ 0 ] -> head -> move_x != - 1 )//向右 + { + snake_digit = 0;//更新蛇 + snake[ snake_digit ] -> head -> direction = 4;//更新蛇移动方向 + } + if ( direction_test == UP && snake[ 1 ] -> head -> move_y != 1 )//向上 + { + snake_digit = 1;//更新蛇 + snake[ snake_digit ] -> head -> direction = 1;//更新蛇移动方向 + } + if ( direction_test == DOWN && snake[ 1 ] -> head -> move_y != - 1 )//向下 + { + snake_digit = 1;//更新 + snake[ snake_digit ] -> head -> direction = 2;//更新蛇移动方向 + } + if ( direction_test == LEFT && snake[ 1 ] -> head -> move_x != 1 )//向左 + { + snake_digit = 1;//更新蛇 + snake[ snake_digit ] -> head -> direction = 3;//更新蛇移动方向 + } + if ( direction_test == RIGHT && snake[ 1 ] -> head -> move_x != 1 )//向右 + { + snake_digit = 1;//更新 + snake[ snake_digit ] -> head -> direction = 4;//更新蛇移动方向 + } + } + } + else + { + int pick = rand ( ) % 4 + 1; + switch ( pick ) + { + case 1: + { + if ( map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] == mark_food && + snake[ snake_digit ] -> head -> direction != 2 ) + { + snake[ snake_digit ] -> head -> direction = 1; + break; + }//判断要不要往上 + } + case 2: + { + if ( map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] == mark_food && + snake[ snake_digit ] -> head -> direction != 1 ) + { + snake[ snake_digit ] -> head -> direction = 2; + break; + }//判断要不要往下 + } + case 3: + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] == mark_food && + snake[ snake_digit ] -> head -> direction != 4 ) + { + snake[ snake_digit ] -> head -> direction = 3; + break; + }//判断要往左 + } + case 4: + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] == mark_food && + snake[ snake_digit ] -> head -> direction != 3 ) + { + snake[ snake_digit ] -> head -> direction = 4; + break; + }//判断要不要往右 + } + case 5: + { + if ( map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] == mark_food && + snake[ snake_digit ] -> head -> direction != 2 ) + { + snake[ snake_digit ] -> head -> direction = 1; + break; + }//判断要不要往上 + else if ( map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] == + mark_food && snake[ snake_digit ] -> head -> direction != 1 ) + { + snake[ snake_digit ] -> head -> direction = 2; + break; + }//判断要不要往下 + + else if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] == + mark_food && snake[ snake_digit ] -> head -> direction != 4 ) + { + snake[ snake_digit ] -> head -> direction = 3; + break; + }//判断要往左 + + else if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] == + mark_food && snake[ snake_digit ] -> head -> direction != 3 ) + { + snake[ snake_digit ] -> head -> direction = 4; + break; + }//判断要不要往右 + } + case 6: + { + switch ( pick ) + { + case 1: + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] == + mark_NULL && snake[ snake_digit ] -> head -> direction != 3 ) + { + snake[ snake_digit ] -> head -> direction = 4; + break; + }//判断要不要往右 + } + case 2: + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] == + mark_NULL && snake[ snake_digit ] -> head -> direction != 4 ) + { + snake[ snake_digit ] -> head -> direction = 3; + break; + }//判断要往左 + } + case 3: + { + if ( map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] == + mark_NULL && snake[ snake_digit ] -> head -> direction != 1 ) + { + snake[ snake_digit ] -> head -> direction = 2; + break; + }//判断要不要往下 + } + case 4: + { + if ( map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] == + mark_NULL && snake[ snake_digit ] -> head -> direction != 2 ) + { + snake[ snake_digit ] -> head -> direction = 1; + break; + }//判断要不要往上 + } + } + } + case 7: + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] == mark_NULL && + snake[ snake_digit ] -> head -> direction != 3 ) + { + snake[ snake_digit ] -> head -> direction = 4; + break; + }//判断要不要往右 + else if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] == + mark_NULL && snake[ snake_digit ] -> head -> direction != 4 ) + { + snake[ snake_digit ] -> head -> direction = 3; + break; + }//判断要往左 + else if ( map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] == + mark_NULL && snake[ snake_digit ] -> head -> direction != 1 ) + { + snake[ snake_digit ] -> head -> direction = 2; + break; + }//判断要不要往下 + + else if ( map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] == + mark_NULL && snake[ snake_digit ] -> head -> direction != 2 ) + { + snake[ snake_digit ] -> head -> direction = 1; + break; + }//判断要不要往上 + } + case 8: + { + switch ( pick ) + { + case 1: + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] == + snake_digit && snake[ snake_digit ] -> head -> direction != 3 ) + { + snake[ snake_digit ] -> head -> direction = 4; + break; + }//判断要不要往右 + } + case 2: + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] == + snake_digit && snake[ snake_digit ] -> head -> direction != 4 ) + { + snake[ snake_digit ] -> head -> direction = 3; + break; + }//判断要往左 + } + case 3: + { + if ( map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] == + snake_digit && snake[ snake_digit ] -> head -> direction != 1 ) + { + snake[ snake_digit ] -> head -> direction = 2; + break; + }//判断要不要往下 + } + case 4: + { + if ( map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] == + snake_digit && snake[ snake_digit ] -> head -> direction != 2 ) + { + snake[ snake_digit ] -> head -> direction = 1; + break; + }//判断要不要往上 + } + } + } + case 9: + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] == snake_digit && + snake[ snake_digit ] -> head -> direction != 3 ) + { + snake[ snake_digit ] -> head -> direction = 4; + break; + }//判断要不要往右 + else if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] == + snake_digit && snake[ snake_digit ] -> head -> direction != 4 ) + { + snake[ snake_digit ] -> head -> direction = 3; + break; + }//判断要往左 + else if ( map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] == + snake_digit && snake[ snake_digit ] -> head -> direction != 1 ) + { + snake[ snake_digit ] -> head -> direction = 2; + break; + }//判断要不要往下 + + else if ( map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] == + snake_digit && snake[ snake_digit ] -> head -> direction != 2 ) + { + snake[ snake_digit ] -> head -> direction = 1; + break; + }//判断要不要往上 + } + } + + } + +} + +void pick_move ( int snake_digit , int mode )//判断移动方向 +{ + if ( snake[ snake_digit ] -> head -> direction == 1 )//判断向上 + { + if ( snake[ snake_digit ] -> head -> move_y == 1 ) + { + if ( mode >= 3 ) + { + if ( map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] != snake_digit && + map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] != mark_food || + map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + else + { + if ( map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] != mark_food || + map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + if ( map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] == mark_food ) + { + snake[ snake_digit ] -> head -> food = 1;//此蛇吃到了食物 + } + snake[ snake_digit ] -> head -> move_x = 0;//更新移动所产生的横坐标 + snake[ snake_digit ] -> head -> move_y = 1;//更新移动所产生的纵坐标 + } + else + { + if ( mode >= 3 ) + { + if ( map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] != snake_digit && + map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] != mark_food || + map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + else + { + if ( map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] != mark_food || + map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + + if ( map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] == mark_food ) + { + snake[ snake_digit ] -> head -> food = 1;//此蛇吃到了食物 + } + snake[ snake_digit ] -> head -> move_x = 0;//更新移动所产生的横坐标 + snake[ snake_digit ] -> head -> move_y = - 1;//更新移动所产生的纵坐标 + } + } + else if ( snake[ snake_digit ] -> head -> direction == 2 )//判断向下 + { + if ( snake[ snake_digit ] -> head -> move_y == - 1 ) + { + if ( mode >= 3 ) + { + if ( map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] != snake_digit && + map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] != mark_food || + map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + else + { + if ( map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] != mark_food || + map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + + if ( map[ snake[ snake_digit ] -> head -> y - 1 ][ snake[ snake_digit ] -> head -> x ] == mark_food ) + { + snake[ snake_digit ] -> head -> food = 1;//此蛇吃到了食物 + } + snake[ snake_digit ] -> head -> move_x = 0;//更新移动所产生的横坐标 + snake[ snake_digit ] -> head -> move_y = - 1;//更新移动所产生的纵坐标 + } + else + { + if ( mode >= 3 ) + { + if ( map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] != snake_digit && + map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] != mark_food || + map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + else + { + if ( map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] != mark_food || + map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + if ( map[ snake[ snake_digit ] -> head -> y + 1 ][ snake[ snake_digit ] -> head -> x ] == mark_food ) + { + snake[ snake_digit ] -> head -> food = 1;//此蛇吃到了食物 + } + snake[ snake_digit ] -> head -> move_x = 0;//更新移动所产生的横坐标 + snake[ snake_digit ] -> head -> move_y = 1;//更新移动所产生的纵坐标 + } + } + else if ( snake[ snake_digit ] -> head -> direction == 3 )//判断向左 + { + if ( snake[ snake_digit ] -> head -> move_x == 1 ) + { + if ( mode >= 3 ) + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] != snake_digit && + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] != mark_food || + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + else + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] != mark_food || + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] == mark_food ) + { + snake[ snake_digit ] -> head -> food = 1;//此蛇吃到了食物 + } + snake[ snake_digit ] -> head -> move_x = 1;//更新移动所产生的横坐标 + snake[ snake_digit ] -> head -> move_y = 0;//更新移动所产生的纵坐标 + } + else + { + if ( mode >= 3 ) + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] != snake_digit && + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] != mark_food || + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + else + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] != mark_food || + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] == mark_food ) + { + snake[ snake_digit ] -> head -> food = 1;//此蛇吃到了食物 + } + snake[ snake_digit ] -> head -> move_x = - 1;//更新移动所产生的横坐标 + snake[ snake_digit ] -> head -> move_y = 0;//更新移动所产生的纵坐标 + } + } + else if ( snake[ snake_digit ] -> head -> direction == 4 )//判断右 + { + if ( snake[ snake_digit ] -> head -> move_x == - 1 ) + { + if ( mode >= 3 ) + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] != snake_digit && + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] != mark_food || + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + else + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] != mark_food || + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x - 1 ] == mark_food ) + { + snake[ snake_digit ] -> head -> food = 1;//此蛇吃到了食物 + } + snake[ snake_digit ] -> head -> move_x = - 1;//更新移动所产生的横坐标 + snake[ snake_digit ] -> head -> move_y = 0;//更新移动所产生的纵坐标 + } + else + { + if ( mode >= 3 ) + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] != snake_digit && + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] != mark_food || + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + else + { + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] != mark_NULL && + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] != mark_food || + map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] == + mark_wall )//判断有没有失败 + { + snake[ snake_digit ] -> head -> direction = 0;//更新状态 + } + } + + if ( map[ snake[ snake_digit ] -> head -> y ][ snake[ snake_digit ] -> head -> x + 1 ] == mark_food ) + { + snake[ snake_digit ] -> head -> food = 1;//此蛇吃到了食物 + } + snake[ snake_digit ] -> head -> move_x = 1;//更新移动所产生的横坐标 + snake[ snake_digit ] -> head -> move_y = 0;//更新移动所产生的纵坐标 + } + } +} + +int main () +{ + //初始化 + int toll;//初始化真实人数 + int snake_digit;//初始化蛇有几条 + int total_food;//判断食物总数 + int New_food;//现有食物 + srand ( time ( NULL ) );//设置随机数 + //开始界面 + int pick_Begin;//接收开始页面的选择 + HideCursor ( );//隐藏光标 + game_Begin: + pick_Begin = Begin ( ); + system ( "cls" ); + //选择进入哪个模式 + switch ( pick_Begin ) + { + case 1: + { + goto Single_player; + break; + } + case 2: + { + goto Two_player; + break; + } + case 3: + { + goto Human_computer; + break; + } + } + + //单人模式 + Single_player: + //动态分配蛇头和身体 + toll = 1;//用户数量 + snake_digit = 1;//蛇数量 + total_food = 10;//食物总数 + for ( int i = 0 ; i < snake_digit ; ++ i )//动态分配蛇的内存 + { + snake[ i ] = ( name_snake * ) malloc ( sizeof ( name_snake ) ); + snake[ i ] -> head = ( snake_head * ) malloc ( sizeof ( snake_head ) ); + snake[ i ] -> body = ( snake_body * ) malloc ( sizeof ( snake_body ) ); + snake[ i ] -> score = ( game_Integral * ) malloc ( sizeof ( game_Integral ) ); + } + print_map ( );//生成地图 + for ( int i = 0 ; i < snake_digit ; ++ i )//生成蛇 + { + initial_snake ( i ); + } + print_score ( 1 );//打印积分 + for ( int i = 0 ; i < total_food ; ++ i ) + { + food_map ( );//在动图上生成食物 + } + while ( 1 ) + { + reception_input ( 0 , 1 );//接收用户输入 + pick_move ( 0 , 1 );//判断移动方向 + if ( snake[ 0 ] -> head -> direction == 0 )//判断是否重开游戏 + { + defeat ( 0 , toll ); + } + if ( snake[ 0 ] -> head -> direction == 5 || snake[ 0 ] -> head -> direction == 6 )//判断是否重新游戏 + { + if ( snake[ 0 ] -> score -> integral > MAX_integral )//更新积分 + { + MAX_integral = snake[ 0 ] -> score -> integral; + } + if ( snake[ 0 ] -> head -> direction == 6 ) + { + system ( "cls" ); + free ( snake[ 0 ] -> score ); + free ( snake[ 0 ] -> head ); + free ( snake[ 0 ] -> body ); + free ( snake[ 0 ] ); + goto game_Begin;//返回主页 + } + else + { + system ( "cls" ); + free ( snake[ 0 ] -> score ); + free ( snake[ 0 ] -> head ); + free ( snake[ 0 ] -> body ); + free ( snake[ 0 ] ); + goto Single_player;//重新开始 + } + } + + move ( 0 );//移动坐标 + if ( snake[ 0 ] -> head -> food == 1 )//检查有没有吃到食物的 + { + food_map ( ); + print_score ( toll );//重新打印积分 + snake[ 0 ] -> head -> food = 0;//重置积分状态 + } + Sleep ( 80 );//停顿 + } + + //双人模式 + Two_player: + //初始化 + toll = 2;//用户数量 + snake_digit = 2;//蛇数量 + total_food = 20;//食物总数 + for ( int i = 0 ; i < snake_digit ; ++ i )//动态分配蛇的内存 + { + snake[ i ] = ( name_snake * ) malloc ( sizeof ( name_snake ) ); + snake[ i ] -> head = ( snake_head * ) malloc ( sizeof ( snake_head ) ); + snake[ i ] -> body = ( snake_body * ) malloc ( sizeof ( snake_body ) ); + snake[ i ] -> score = ( game_Integral * ) malloc ( sizeof ( game_Integral ) ); + } + print_map ( );//打印当前地图 + for ( int i = 0 ; i < snake_digit ; ++ i )//生成蛇 + { + initial_snake ( i ); + sleep ( 1 );//避免生成一样的蛇 + } + print_score ( 2 );//打印积分 + for ( int i = 0 ; i < total_food ; ++ i )//在动图上生成食物 + { + food_map ( ); + } + while ( 1 ) + { + for ( int i = 0 ; i < 10 ; ++ i )//接收用户输入 + { + reception_input ( toll - 1 , toll ); + } + for ( int i = 0 ; i < snake_digit ; ++ i )//判断移动方向 + { + pick_move ( i , 2 );//判断移动方向 + } + for ( int i = 0 ; i < snake_digit ; ++ i )//移动 + { + if ( snake[ i ] -> head -> direction == 0 )//判断是否失败 + { + defeat ( i , toll ); + } + if ( snake[ i ] -> head -> direction == 5 || snake[ i ] -> head -> direction == 6 )//判断是否重新游戏 + { + system ( "cls" ); + if ( snake[ i ] -> head -> direction == 6 ) + { + for ( int j = 0 ; j < snake_digit ; ++ j ) + { + free ( snake[ j ] -> score ); + free ( snake[ j ] -> head ); + free ( snake[ j ] -> body ); + free ( snake[ j ] ); + } + goto game_Begin;//返回主页 + } + else + { + for ( int j = 0 ; j < snake_digit ; ++ j ) + { + free ( snake[ j ] -> score ); + free ( snake[ j ] -> head ); + free ( snake[ j ] -> body ); + free ( snake[ j ] ); + } + goto Two_player;//重新开始 + } + } + move ( i );//移动坐标 + } + for ( int i = 0 ; i < snake_digit ; ++ i )//判断食物 + { + if ( snake[ i ] -> head -> food == 1 )//检查有没有吃到食物的 + { + food_map ( ); + print_score ( toll );//重新打印积分 + snake[ i ] -> head -> food = 0;//重置积分状态 + } + } + + Sleep ( 80 );//停顿 + } + + //人机模式 + Human_computer: + //动态分配蛇头和身体 + toll = 1;//用户数量 + snake_digit = 7;//蛇数量 + total_food = 30;//食物总数 + New_food = 0;//新增食物 + for ( int i = 0 ; i < snake_digit ; ++ i )//动态分配蛇的内存 + { + snake[ i ] = ( name_snake * ) malloc ( sizeof ( name_snake ) ); + snake[ i ] -> head = ( snake_head * ) malloc ( sizeof ( snake_head ) ); + snake[ i ] -> body = ( snake_body * ) malloc ( sizeof ( snake_body ) ); + snake[ i ] -> score = ( game_Integral * ) malloc ( sizeof ( game_Integral ) ); + } + print_map ( );//生成地图 + for ( int i = 0 ; i < snake_digit ; ++ i )//生成蛇 + { + initial_snake ( i ); + sleep ( 1 ); + } + print_score ( 1 );//打印积分 + for ( int i = 0 ; i < total_food ; ++ i ) + { + food_map ( );//在动图上生成食物 + } + while ( 1 ) + { + //用户 + reception_input ( 0 , 1 );//接收用户输入 + pick_move ( 0 , 3 );//判断移动方向 + if ( snake[ 0 ] -> head -> direction == 0 )//判断是否重新游戏 + { + defeat ( 0 , toll ); + } + if ( snake[ 0 ] -> head -> direction == 5 || snake[ 0 ] -> head -> direction == 6 )//判断是否重开游戏 + { + if ( snake[ 0 ] -> score -> integral > MAX_integral )//更新积分 + { + MAX_integral = snake[ 0 ] -> score -> integral; + } + system ( "cls" ); + if ( snake[ 0 ] -> head -> direction == 6 ) + { + for ( int j = 0 ; j < snake_digit ; ++ j ) + { + free ( snake[ j ] -> score ); + free ( snake[ j ] -> head ); + free ( snake[ j ] -> body ); + free ( snake[ j ] ); + } + goto game_Begin;//返回主页 + } + else + { + for ( int j = 0 ; j < snake_digit ; ++ j ) + { + free ( snake[ j ] -> score ); + free ( snake[ j ] -> head ); + free ( snake[ j ] -> body ); + free ( snake[ j ] ); + } + goto Human_computer;//重新开始 + } + } + move ( 0 );//移动坐标 + if ( snake[ 0 ] -> head -> food == 1 )//检查有没有吃到食物的 + { + if ( New_food == 0 ) + { + snake[ 0 ] -> body -> color = rand ( ) % 14 + 1;//给蛇随机生成颜色 + food_map ( ); + print_score ( toll );//重新打印积分 + snake[ 0 ] -> head -> food = 0;//重置积分状态 + } + else + { + New_food -= 1; + print_score ( toll );//重新打印积分 + snake[ 0 ] -> head -> food = 0;//重置积分状态 + } + + } + + //人机 + for ( int i = 1 ; i < snake_digit ; ++ i )//生成坐标 + { + reception_input ( i , snake_digit );//生成坐标 + pick_move ( i , 3 );//判断移动方向 + if ( snake[ i ] -> head -> direction == 0 )//判断是否重新生成 + { + //将身体变成食物 + for ( int j = 0 ; j < snake[ i ] -> body -> length ; ++ j ) + { + color ( snake[ i ] -> body -> color );//设置食物颜色 + CursorJump ( snake[ i ] -> body -> y[ j ] , snake[ i ] -> body -> x[ j ] );//光标跳转 + map[ snake[ i ] -> body -> y[ j ] ][ snake[ i ] -> body -> x[ j ] ] = mark_food; // 正确标记食物 + printf ( "●" ); + New_food += 1;//增加新增食物 + } + //将头变为空 + CursorJump ( snake[ i ] -> head -> y , snake[ i ] -> head -> x );//光标跳转 + map[ snake[ i ] -> head -> y ][ snake[ i ] -> head -> x ] = mark_NULL; // 正确标记空气 + printf ( " " ); + //释放本条蛇 + free ( snake[ i ] -> score ); + free ( snake[ i ] -> head ); + free ( snake[ i ] -> body ); + free ( snake[ i ] ); + //重新生成蛇 + snake[ i ] = ( name_snake * ) malloc ( sizeof ( name_snake ) ); + snake[ i ] -> head = ( snake_head * ) malloc ( sizeof ( snake_head ) ); + snake[ i ] -> body = ( snake_body * ) malloc ( sizeof ( snake_body ) ); + snake[ i ] -> score = ( game_Integral * ) malloc ( sizeof ( game_Integral ) ); + initial_snake ( i ); + } + move ( i );//移动坐标 + if ( snake[ i ] -> head -> food == 1 )//检查有没有吃到食物的 + { + if ( New_food == 0 ) + { + food_map ( ); + print_score ( toll );//重新打印积分 + snake[ i ] -> head -> food = 0;//重置积分状态 + } + else + { + New_food -= 1; + print_score ( toll );//重新打印积分 + snake[ i ] -> head -> food = 0;//重置积分状态 + } + + } + } + + Sleep ( 80 );//停顿 + } + + return 0; +} \ No newline at end of file diff --git a/practice_code/idea/bank/ATM/ATM_1.c b/practice_code/idea/bank/ATM/ATM_1.c new file mode 100644 index 0000000..94329d4 --- /dev/null +++ b/practice_code/idea/bank/ATM/ATM_1.c @@ -0,0 +1,153 @@ +#include +#include + +int main () +{ + double a , b , c , e , f , g , h; ///本金,年利率.年份,利息,本利,取钱,case里面的变量,i + int d; //获取用户需要进入的模式 + + //开始界面 + printf ( "简易atm机 \n" ); + printf ( "--------------------- \n" ); //用于分割 + //本金 + printf ( "请输入你的本金:" ); + while ( scanf ( "%lf" , & a ) != 1 ) //判断是否为合法数字 + { + printf ( "请输入合法的数字:" ); + while ( getchar ( ) != '\n' ); + } + + //年利率 + printf ( "请输入你的年利率(不用输入%%):" ); + while ( scanf ( "%lf" , & b ) != 1 || b <= 0 ) //判断是否为合法数字 + { + printf ( "请输入合法的年利率:" ); + while ( getchar ( ) != '\n' ); + } + + + //年份 + printf ( "请输入你的年份:" ); + while ( scanf ( "%lf" , & c ) != 1 || c <= 0 ) //判断是否为合法数字 + { + printf ( "请输入合理的年份:" ); + while ( getchar ( ) != '\n' ); + } + + system ( "cls" ); // windows清屏命令,如果其他系统报错删除此行或注释掉 + //system("clear"); //Linux和macOS清屏命令 + + while ( 1 ) + { + e = a * b * c / 100; //利息 + f = a + e; //本利 + printf ( "存入本金为%.3lf \n" , a ); + printf ( "当前利率为%.3lf%% \n" , b ); + printf ( "当前存了%.0lf年 \n" , c ); + printf ( "获得的利息为%.3lf \n" , e ); + printf ( "余额为%.3lf \n" , f ); + + printf ( "--------------------- \n" ); //用于分割 + + printf ( "请输入对应的数字进入对应的模式 \n" ); + printf ( "0.退出 \n" ); + printf ( "1.存钱 \n" ); + printf ( "2.取钱 \n" ); + printf ( "3.增加年份 \n" ); + printf ( "4.减少年份 \n" ); + printf ( "5.增加年利率 \n" ); + printf ( "6.减少年利率 \n" ); + + printf ( "请输入对应的数字进入需要的模式:" ); + while ( scanf ( "%d" , & d ) < 0 || d > 4 ) //判断是否为合适数字 + { + printf ( "请输入对应的数字:" ); + while ( getchar ( ) != '\n' ); + } + + switch ( d ) + { + case 0: + { + return 1; + } + case 1: + { + printf ( "请输入你需要存入多少钱:" ); + while ( scanf ( "%lf" , & h ) != 1 || h < 0 ) //判断是否为合法数字 + { + printf ( "请输入合法的数字:" ); + while ( getchar ( ) != '\n' ); + } + a += h; + system ( "cls" ); // windows清屏命令 + break; + } + case 2: + { + printf ( "请输入你需要取出多少钱:" ); + while ( scanf ( "%lf" , & h ) != 1 || h < 0 || h > a ) //判断是否为合法数字 + { + printf ( "请输入合法的数字:" ); + while ( getchar ( ) != '\n' ); + } + a -= h; + system ( "cls" ); // windows清屏命令 + break; + } + + case 3: + { + printf ( "请输入你需要增加多少年:" ); + while ( scanf ( "%lf" , & h ) != 1 || h < 0 ) //判断是否为合法数字 + { + printf ( "请输入合法的数字:" ); + while ( getchar ( ) != '\n' ); + } + c += h; + system ( "cls" ); // windows清屏命令 + break; + } + case 4: + { + printf ( "请输入你需要减少多少年:" ); + while ( scanf ( "%lf" , & h ) != 1 || h < 0 || h > a ) //判断是否为合法数字 + { + printf ( "请输入合法的数字:" ); + while ( getchar ( ) != '\n' ); + } + c -= h; + system ( "cls" ); // windows清屏命令 + break; + } + + case 5: + { + printf ( "请输入你需要增加多少年利率(不用输入%%):" ); + while ( scanf ( "%lf" , & h ) != 1 || h < 0 ) //判断是否为合法数字 + { + printf ( "请输入合法的数字:" ); + while ( getchar ( ) != '\n' ); + } + b += h; + system ( "cls" ); // windows清屏命令 + break; + } + case 6: + { + printf ( "请输入你需要减少多少年利率(不用输入%%):" ); + while ( scanf ( "%lf" , & h ) != 1 || h < 0 || h > a ) //判断是否为合法数字 + { + printf ( "请输入合法的数字:" ); + while ( getchar ( ) != '\n' ); + } + b -= h; + system ( "cls" ); // windows清屏命令 + break; + } + + } + + } + return 0; +} diff --git a/practice_code/idea/bank/ATM/ATM_2.c b/practice_code/idea/bank/ATM/ATM_2.c new file mode 100644 index 0000000..0249bab --- /dev/null +++ b/practice_code/idea/bank/ATM/ATM_2.c @@ -0,0 +1,467 @@ +#include +#include + +int main () +{ + //初始化 + int account_1 , password_1 , password_2 = 0 , password_3 = 0 , account_2 = 0 , password_4 = 0; //账号;登录密码,取款密码,登录检查密码,检查登录账户,修改密码判断是不是6位 + char tab_1; //用户是否同意进入 + int tab_a = 0 , tab_b , tab_c , tab_d = 0 , tab_e , tab_f; //判断取款密码是否为6位数.判断取款密码是否为6位数,判断进入登录还是管理员,判断是否进入账户主页主页;账户主页选项;判断用户修改是否输入正确 + int administration_password , administration_a; //管理员密码,判断管理员选项 + double interest = 0 , capital = 10000 , money = 0 , interest_rate = 0.0175 , year = 0 , capital_1 = 0; //利息,本金,变动的钱,利率,年份,预测本金 + //注册账户 + printf ( "第一次使用需要注册账户 \n" ); + printf ( "不同意输入N,输入其他代表同意 \n:" ); + //获取用户是否同意进入 + tab_1 = getchar ( ); + + if ( tab_1 == 'n' || tab_1 == 'N' ) + { + return 1; + } + + while ( getchar ( ) != '\n' ); //清楚缓存 + + //注册登录账户 + printf ( "账户名(只支持数字):" ); + while ( scanf ( "%d" , & account_1 ) != 1 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "请输入合法的数字:" ); + continue; + } + + while ( getchar ( ) != '\n' ); //清楚缓存 + //注册登录密码 + printf ( "登录密码(只支持数字):" ); + while ( scanf ( "%d" , & password_1 ) != 1 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "请输入合法的数字:" ); + continue; + } + while ( getchar ( ) != '\n' ); //清楚缓存 + //注册取款密码 + //注册取款密码 + printf ( "取款密码(只支持6位数字):" ); + while ( password_2 == 0 ) + { + //判断是不是输入的数字 + while ( scanf ( "%d" , & password_2 ) != 1 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "请输入合法的数字:" ); + continue; + } + tab_b = password_2; + //判断是否为6位数 + + while ( tab_b != 0 ) + { + tab_b /= 10; + tab_a ++; + } + + if ( tab_a != 6 ) + { + printf ( "当前输入%d位,密码是6位数 \n请重新输入:" , tab_a ); + password_2 = 0; + tab_a = 0; + continue; + } + if ( tab_a == 6 ) + { + break; + } + } + while ( getchar ( ) != '\n' ); //清楚缓存 + //提示用户输入成功 + printf ( "创建成功 \n" ); + printf ( "账户名:%d \n" , account_1 ); + printf ( "密码:%d \n" , password_1 ); + printf ( "密码:%d \n" , password_2 ); + printf ( "初始金额为%lf \n" , capital ); + + //此层循环为主页面开始 + while ( 1 ) + { + printf ( "----银行主页----\n" ); + printf ( "0.退出程序 \n" ); + printf ( "1.登录银行账户 \n" ); + printf ( "请输入数字:" ); + scanf ( "%d" , & tab_c ); + printf ( "----银行主页----\n" ); + + while ( getchar ( ) != '\n' ); //清楚缓存 + //0退出 + if ( tab_c == 0 ) + { + printf ( "欢迎下次使用" ); + return 1; + } + + //获取是否输入正确 + if ( tab_c != 0 && tab_c != 1 && tab_c != 2 ) + { + printf ( "输入错误请重新输入 \n" ); + continue; + } + + //银行主页开始 + while ( tab_c == 1 ) + { + printf ( "需要登陆账号 \n" ); + //获取用户账号 + printf ( "请输入账号:" ); + scanf ( "%d" , & account_2 ); + while ( account_1 != account_2 ) //判断账户是否正确 + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "请输入正确的账号:" ); + scanf ( "%d" , & account_2 ); + } + //获取用密码 + printf ( "请输入密码:" ); + scanf ( "%d" , & password_3 ); + while ( password_1 != password_3 ) //判断密码是否正确 + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "请输入正确的密码:" ); + scanf ( "%d" , & password_3 ); + } + // 正确的账号和密码 + printf ( "登录成功!\n" ); + tab_d = 1; //进入主页 + //此层输出账户主页 + while ( tab_d == 1 ) + { + printf ( "----账户主页----\n" ); + printf ( "0.退出程序 \n" ); + printf ( "1.退出账户 \n" ); + printf ( "2.存钱 \n" ); + printf ( "3.取钱 \n" ); + printf ( "4.查看余额 \n" ); + printf ( "5.预测未来存款(当前利率为%.3lf) \n" , interest_rate ); + printf ( "6.修改账号 \n" ); + printf ( "7.修改密码 \n" ); + printf ( "8.修改取款密码 \n" ); + printf ( "请输入数字:" ); + scanf ( "%d" , & tab_e ); + printf ( "----账户主页----\n" ); + + while ( getchar ( ) != '\n' ); //清楚缓存 + + //重置判断 + tab_f = 0; + tab_a = 0; //取款数字是否为6位 + //选择开始 + switch ( tab_e ) + { + case 0: + { + printf ( "欢迎下次使用" ); + return 1; + } + + case 1: + { + tab_d = 0; //使不进入其他循环 + tab_c = 999; //使不进入其他循环 + printf ( "退出成功\n" ); + while ( getchar ( ) != '\n' ); //清楚缓存 + break; + } + + case 2: + { + printf ( "请输入需要存入多少钱:" ); + while ( scanf ( "%lf" , & money ) != 1 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "请输入合法的数字:" ); + continue; + } + capital += money; + break; + } + + case 3: + { + printf ( "请输入当前密码或取款密码:" ); + while ( scanf ( "%d" , & password_3 ) != 1 || password_3 != password_2 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "当密码错误,请重新输入:" ); + continue; + } + printf ( "请输入需要取出多少钱:" ); + while ( scanf ( "%lf" , & money ) != 1 || money > capital ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "数字不合法或本金不足,请重新输入:" ); + continue; + } + capital -= money; + break; + } + + case 4: + { + printf ( "当前余额为%.3lf: \n" , capital ); + break; + } + + case 5: + { + printf ( "请输入预测多少年后:" ); + while ( scanf ( "%lf" , & year ) != 1 || year < 0 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "请输入合法的数字:" ); + continue; + } + double interest = capital * interest_rate * year; // 计算利息 + double capital_1 = capital + interest; // 计算预测余额 + printf ( "预测%.0lf年后获得\n" , year ); + printf ( "利息%.3lf\n" , interest ); + printf ( "余额%.3lf \n" , capital_1 ); + } + + case 6: + { + //验证用户开始 + while ( tab_f == 0 ) + { + printf ( "需要验证用户\n输入0退出\n" ); + printf ( "请输入当前登录账户名或取款密码:" ); + while ( scanf ( "%d" , & password_3 ) != 1 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "数字不合法或密码错误,请重新输入:" ); + continue; + } + + //检查是否退出 + if ( password_3 == 0 ) + { + printf ( "退出成功" ); + tab_f = 1; + break; + } + + //输入成功退出 + if ( password_3 == password_2 || password_3 == account_1 ) + { + printf ( "验证成功 \n" ); + tab_f = 1; + break; + } else + { + printf ( "输入错误请重新输入 \n" ); + continue; + } + + }//验证用户结束 + + + //修改新账户 + while ( tab_a == 0 ) + { + printf ( "请输入新账户名:" ); + while ( scanf ( "%d" , & password_3 ) != 1 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "非法数字,请重新输入:" ); + continue; + } + + //二次确认账户 + printf ( "请二次输入你的账户名:" ); + while ( scanf ( "%d" , & password_4 ) != 1 || password_4 != password_3 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "账户名不一样,请重新输入:" ); + continue; + } + //退出循环 + break; + }//修改新的账户结束 + + account_1 = password_3; + printf ( "修改账号成功" ); + printf ( "当前账户名是%d" , account_1 ); + break; + } + + case 7: + { + //验证用户开始 + while ( tab_f == 0 ) + { + printf ( "需要验证用户\n输入0退出\n" ); + printf ( "请输入当前登录密码或取款密码:" ); + while ( scanf ( "%d" , & password_3 ) != 1 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "数字不合法或密码错误,请重新输入:" ); + continue; + } + + //检查是否退出 + if ( password_3 == 0 ) + { + printf ( "退出成功" ); + tab_f = 1; + break; + } + + //输入成功退出 + if ( password_3 == password_2 || password_3 == password_1 ) + { + printf ( "验证成功 \n" ); + tab_f = 1; + break; + } else + { + printf ( "输入错误请重新输入 \n" ); + continue; + } + + }//验证用户结束 + + + //修改新密码 + while ( tab_a == 0 ) + { + printf ( "请输入新密码:" ); + while ( scanf ( "%d" , & password_3 ) != 1 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "非法数字,请重新输入:" ); + continue; + } + + //二次确认密码 + printf ( "请二次输入你的密码:" ); + while ( scanf ( "%d" , & password_4 ) != 1 || password_4 != password_3 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "密码不一样,请重新输入:" ); + continue; + } + //退出循环 + break; + }//修改新的密码结束 + + password_1 = password_3; + printf ( "修改账户密码成功" ); + printf ( "当前密码为:%d" , password_1 ); + break; + } + + case 8: + { + //验证用户开始 + while ( tab_f == 0 ) + { + printf ( "需要验证用户\n输入0退出\n" ); + printf ( "请输入当前取款密码或账户登录密码:" ); + while ( scanf ( "%d" , & password_3 ) != 1 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "数字不合法或密码错误,请重新输入:" ); + continue; + } + + //检查是否退出 + if ( password_3 == 0 ) + { + printf ( "退出成功" ); + tab_f = 1; + break; + } + + //输入成功退出 + if ( password_3 == password_2 || password_3 == password_1 ) + { + printf ( "验证成功 \n" ); + tab_f = 1; + break; + } else + { + printf ( "输入错误请重新输入 \n" ); + continue; + } + + }//验证用户结束 + + //修改新密码 + while ( tab_a != 6 ) + { + printf ( "请输入新密码:" ); + while ( scanf ( "%d" , & password_3 ) != 1 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "非法数字,请重新输入:" ); + continue; + } + + //判断是否为6位数 + tab_a = 0;//初始化判断 + + //为了防止刚刚的密码没有了 + + password_4 = password_3; + while ( password_4 != 0 ) + { + password_4 /= 10; + tab_a ++; + } + + if ( tab_a != 6 ) + { + printf ( "当前输入%d位,密码是6位数 \n请重新输入\n" , tab_a ); + continue; + } + + if ( tab_a == 6 ) + { + printf ( "请二次输入你的密码:" ); + while ( scanf ( "%d" , & password_4 ) != 1 || password_4 != password_3 ) + { + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "密码不一样,请重新输入:" ); + continue; + } + if ( password_4 == password_3 ) + { + break; + } + break; + }//修改新的密码结束 + + + } + + password_2 = password_3; + printf ( "修改取款密码成功" ); + printf ( "当前密码为:%d \n" , password_2 ); + break; + } + + default: //输入了其他 + { + printf ( "输入错误,请重新输入 \n" ); + while ( getchar ( ) != '\n' ); //清楚缓存 + break; + } + }//选择结束 + } + + + }//银行主页结束 + + + }//此层循环为主页面结束 + return 0; +} diff --git a/practice_code/idea/bank/ATM/ATM_3.c b/practice_code/idea/bank/ATM/ATM_3.c new file mode 100644 index 0000000..7cebd5b --- /dev/null +++ b/practice_code/idea/bank/ATM/ATM_3.c @@ -0,0 +1,847 @@ +#include +#include +#include + +//初始化 账号密码 +char account_number[999];//账号 +char account_number_test[999];//校验账号 +char account_password[999]; //密码 +char account_password_test[999]; //校验密码 +char account_password_1[999]; //取款密码 +char account_password_1_test[999]; //校验取款密码 +int account_password_1_test_1; //校验取款密码输入了几位 +char administrators_password[999] = "lsy22.com"; //管理员密码 +char administrators_password_test[999]; //校验管理员密码 + +//初始化 金额 +double balance = 0; //余额 +double interest_rate = 0.0175; //利率 +double interest_rate_test = 0.0175; // 利率校验 +double year = 0;//年份 +double interest = 0; //利息 +double prediction = 0; //预测余额 +double save = 0; //存钱 +double withdraw = 0; //取钱 +double running_bill[999];//金额流水 +char running_bill_symbol[999];//金额流水符号 +double running_bill_balance[999];//金额流水余额 +int running_bill_tab = 0; //金额流水次数 + +//函数声明 + +//银行主页 +void bank_home_program (); + +//账户注册页面 +void account_sigin_program (); + +//账户登录页面 +void account_login_program (); + +//账户主页 +void account_home_program (); + +//模式一 个人信息 +void account_mode1_program (); + +//模式一 个人信息 查看流水金额 +void account_mode1_part1_program (); + +//模式一 个人信息 修改账号 +void account_mode1_part2_program (); + +//模式一 个人信息 修改密码 +void account_mode1_part3_program (); + +//模式一 个人信息 修改取款密码 +void account_mode1_part4_program (); + +//模式二 存款 +void account_mode2_program (); + +//模式三 取款 +void account_mode3_program (); + +//模式四 预测余额 +void account_mode4_program (); + +//管理员登录页面 +void administrastion_login_program (); + +//管理员主页面 +void administrastion_home_program (); + +//管理员 查看流水金额 +void administrastion_mode1_program (); + +//管理员 修改用户账号 +void administrastion_mode2_program (); + +//管理员 修改用户密码 +void administrastion_mode3_program (); + +//管理员 修改用户取款密码 +void administrastion_mode4_program (); + +// 管理员修改年利率 +void administrastion_mode5_program (); + +//银行主页 +void bank_home_program () +{ + int bank_pick;//银行主页选择 + printf ( "----银行主页1-----\n" ); + printf ( "0.退出程序 \n" ); + printf ( "1.登录账户 \n" ); + printf ( "请输入:" ); + scanf ( "%d" , & bank_pick ); + switch ( bank_pick ) + { + case 0: + { + exit ( 0 ); + } + case 1: + { + if ( account_number[ 0 ] == '\0' ) //第一次进入注册 + { + int account_sigin_pick; //选择是否进行注册 + printf ( "---尚未注册账户---\n" ); + printf ( "1.注册账户\n" ); + printf ( "输入其他返回银行主页\n" ); + printf ( "请输入:" ); + scanf ( "%d" , & account_sigin_pick ); + if ( account_sigin_pick == 1 ) + { + account_sigin_program ( ); + } else + { + bank_home_program ( ); + } + } else //往后进入登录 + { + account_login_program ( ); + } + break; + } + case 22: + { + administrastion_login_program ( ); + break; + } + default: + { + printf ( "没有找到对应的模式请重新输入:\n" ); + while ( getchar ( ) != '\n' ); //清楚缓存 + bank_home_program ( ); + break; + } + } +} + +//账户注册页面 +void account_sigin_program () +{ + int account_sigin_pick; //没有注册账号是否需要注册 + account_sigin_number_tab: //注册账号标签 + { + printf ( "-----注册账号----\n" ); + printf ( "请输入账号:" ); + scanf ( "%s" , account_number ); + printf ( "请确认账号:" ); + scanf ( "%s" , account_number_test ); + if ( strcmp ( account_number , account_number_test ) != 0 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "两次输入不相同,请重新输入 \n" ); + goto account_sigin_number_tab; + } + } + + account_sigin_password_tab: //注册密码标签 + { + printf ( "-----注册密码----\n" ); + printf ( "请输入密码:" ); + scanf ( "%s" , account_password ); + printf ( "请确认密码:" ); + scanf ( "%s" , account_password_test ); + if ( strcmp ( account_password , account_password_test ) != 0 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "两次输入不相同,请重新输入 \n" ); + goto account_sigin_password_tab; + } + } + + account_sigin_password_1_tab: //注册取款密码标签 + { + printf ( "-----注册取款密码(只能输入6位)----\n" ); + printf ( "请输入取款密码:" ); + scanf ( "%s" , & account_password_1 ); + //校验是不是6位 + account_password_1_test_1 = strlen ( account_password_1 ); + if ( account_password_1_test_1 != 6 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "当前输入了%d位,请重新输入 \n" , account_password_1_test_1 ); + goto account_sigin_password_1_tab; + } + + printf ( "请确认取款密码:" ); + scanf ( "%s" , account_password_1_test ); + if ( strcmp ( account_password_1 , account_password_1_test ) != 0 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "两次输入不相同,请重新输入 \n" ); + goto account_sigin_password_1_tab; + } + } + + printf ( "注册成功\n" ); + printf ( "账号:%s\n" , account_number ); + printf ( "密码:%s\n" , account_password ); + printf ( "取款密码:%s\n" , account_password_1 ); + printf ( "请妥善保存好密码\n" ); + + account_sigin_exit_tab: //选择退出标签 + { + printf ( "1.返回银行主页\n" ); + printf ( "2.进行登录账户\n" ); + printf ( "请输入:" ); + scanf ( "%d" , & account_sigin_pick ); + switch ( account_sigin_pick ) + { + case 1: + { + while ( getchar ( ) != '\n' ); //清除缓存 + bank_home_program ( ); + } + case 2: + { + while ( getchar ( ) != '\n' ); //清除缓存 + account_login_program ( ); + } + default: + { + while ( getchar ( ) != '\n' ); //清除缓存 + printf ( "输入错误,请重新输入 \n" ); + goto account_sigin_exit_tab; + + } + } + } +} + +//账户登录页面 +void account_login_program () +{ + account_login_tab: //登录账号标签 + { + printf ( "请输入账号:" ); + scanf ( "%s" , & account_number_test ); + if ( strcmp ( account_number_test , account_number ) != 0 ) + { + printf ( "账号不正确请重新输入 \n" ); + while ( getchar ( ) != '\n' ); //清除缓存 + goto account_login_tab; + } + } + + password_login_tab: //登录密码标签 + { + printf ( "请输入密码:" ); + scanf ( "%s" , account_password_test ); + if ( strcmp ( account_password_test , account_password ) != 0 ) + { + printf ( "密码不正确请重新输入 \n" ); + while ( getchar ( ) != '\n' ); //清除缓存 + goto password_login_tab; + } + } + + printf ( "登录成功\n" ); + while ( getchar ( ) != '\n' ); //清除缓存 + account_home_program ( ); +} + +//账户主页 +void account_home_program () +{ + int account_home_pick; + printf ( "----账户主页---- \n" ); + printf ( "0.退出程序 \n" ); + printf ( "1.个人信息 \n" ); + printf ( "2.存款 \n" ); + printf ( "3.取钱 \n" ); + printf ( "4.预测余额(当前利率为%.3lf%%) \n" , interest_rate * 100 ); + printf ( "5.退出账号 \n" ); + printf ( "请输入需要进入的模式:" ); + scanf ( "%d" , & account_home_pick ); + switch ( account_home_pick ) + { + case 0: + { + exit ( 0 ); + } + case 1: + { + while ( getchar ( ) != '\n' ); //清除缓存 + account_mode1_program ( ); + break; + } + case 2: + { + while ( getchar ( ) != '\n' ); //清除缓存 + account_mode2_program ( ); + break; + } + case 3: + { + while ( getchar ( ) != '\n' ); //清除缓存 + account_mode3_program ( ); + break; + } + case 4: + { + while ( getchar ( ) != '\n' ); //清除缓存 + account_mode4_program ( ); + break; + } + case 22: + { + while ( getchar ( ) != '\n' ); //清除缓存 + administrastion_login_program ( ); + break; + } + case 5: + { + while ( getchar ( ) != '\n' ); //清除缓存 + printf ( "退出成功 \n" ); + bank_home_program ( ); + break; + } + default: + { + while ( getchar ( ) != '\n' ); //清除缓存 + printf ( "没有找到对应的模式请重新输入 \n" ); + account_home_program ( ); + } + } +} + +//模式一 个人信息 +void account_mode1_program () +{ + int account_mode1_pick; + printf ( "---个人信息--- \n" ); + printf ( "1.查看余额 \n" ); + printf ( "2.查看金额流水 \n" ); + printf ( "3.修改账号 \n" ); + printf ( "4.修改密码 \n" ); + printf ( "5.修改取款密码 \n" ); + printf ( "6.返回到账户主页 \n" ); + printf ( "请输入需要进入的模式:" ); + scanf ( "%d" , & account_mode1_pick ); + switch ( account_mode1_pick ) + { + case 1: + { + while ( getchar ( ) != '\n' ); //清除缓存 + printf ( "当前余额为%.3lf\n" , balance ); + account_mode1_program ( ); + break; + } + case 2: + { + while ( getchar ( ) != '\n' ); //清除缓存 + account_mode1_part1_program ( ); + break; + } + case 3: + { + while ( getchar ( ) != '\n' ); //清除缓存 + account_mode1_part2_program ( ); + break; + } + case 4: + { + while ( getchar ( ) != '\n' ); //清除缓存 + account_mode1_part3_program ( ); + break; + } + case 5: + { + while ( getchar ( ) != '\n' ); //清除缓存 + account_mode1_part4_program ( ); + break; + } + case 6: + { + while ( getchar ( ) != '\n' ); //清除缓存 + account_home_program ( ); + break; + } + case 22: + { + while ( getchar ( ) != '\n' ); //清除缓存 + administrastion_login_program ( ); + break; + } + default : + { + printf ( "输入错误请重新输入\n" ); + while ( getchar ( ) != '\n' ); //清除缓存 + account_mode1_program ( ); + break; + } + + } + +} + +//模式一 个人信息 查看流水金额 +void account_mode1_part1_program () +{ + printf ( "---金额流水开始--- \n" ); + for ( int running_bill_tab_test = 0 ; running_bill_tab_test < running_bill_tab ; running_bill_tab_test ++ ) + { + if ( running_bill_symbol[ running_bill_tab_test ] == '+' ) + { + printf ( "第%d次 \n" , running_bill_tab_test + 1 ); + printf ( "存入%.3lf元 " , running_bill[ running_bill_tab_test ] ); + printf ( "余额%.3lf元 \n" , running_bill_balance[ running_bill_tab_test ] ); + } + if ( running_bill_symbol[ running_bill_tab_test ] == '-' ) + { + printf ( "第%d次 \n" , running_bill_tab_test + 1 ); + printf ( "取出%.3lf元 " , running_bill[ running_bill_tab_test ] ); + printf ( "余额%.3lf元 \n" , running_bill_balance[ running_bill_tab_test ] ); + } + } + if ( running_bill_tab == 0 ) // 如果用户没有取款记录 + { + printf ( "暂无存取款记录 \n" ); + } + printf ( "---金额流水结束--- \n" ); + printf ( "按回车结束" ); + while ( getchar ( ) != '\n' );//清除缓存 + account_mode1_program ( ); +} + +//模式一 个人信息 修改账号 +void account_mode1_part2_program () +{ + account_modify_number_tab: //修改账号标签 + { + printf ( "-----修改账号----\n" ); + printf ( "请输入原账号:" ); + scanf ( "%s" , account_number_test ); + if ( strcmp ( account_number , account_number_test ) != 0 ) + { + printf ( "与原账号不相同,请重新输入 \n" ); + goto account_modify_number_tab; + } + printf ( "请输入新账号:" ); + scanf ( "%s" , account_number ); + } + printf ( "修改账户成功!\n" ); + printf ( "新账号名:%s \n" , account_number ); + while ( getchar ( ) != '\n' );//清除缓存 + account_mode1_program ( ); +} + +//模式一 个人信息 修改密码 +void account_mode1_part3_program () +{ + account_modify_password_tab: //修改密码标签 + { + printf ( "-----修改密码----\n" ); + printf ( "请输入原密码:" ); + scanf ( "%s" , account_password_test ); + if ( strcmp ( account_password , account_password_test ) != 0 ) + { + printf ( "与原密码不相同,请重新输入 \n" ); + goto account_modify_password_tab; + } + printf ( "请输入新密码:" ); + scanf ( "%s" , account_password ); + } + printf ( "修改密码成功!\n" ); + printf ( "新密码:%s \n" , account_password ); + while ( getchar ( ) != '\n' );//清除缓存 + account_mode1_program ( ); +} + +//模式一 个人信息 修改取款密码 +void account_mode1_part4_program () +{ + account_modify_password_1_tab: //注册取款密码标签 + { + printf ( "-----修改取款密码(只能输入6位)----\n" ); + printf ( "请输入原取款密码:" ); + scanf ( "%s" , account_password_1_test ); + if ( strcmp ( account_password_1 , account_password_1_test ) != 0 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "两次输入不相同,请重新输入 \n" ); + goto account_modify_password_1_tab; + } + } + + account_modify_password_2_tab: + { + printf ( "请输入新取款密码:" ); + scanf ( "%s" , & account_password_1 ); + //校验是不是6位 + account_password_1_test_1 = strlen ( account_password_1 ); + if ( account_password_1_test_1 != 6 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "当前输入了%d位,请重新输入 \n" , account_password_1_test_1 ); + goto account_modify_password_2_tab; + } + + } + + printf ( "修改取款密码成功! \n" ); + printf ( "新取款密码:%s \n" , account_password_1 ); + while ( getchar ( ) != '\n' );//清除缓存 + account_mode1_program ( ); +} + +//模式二 存款 +void account_mode2_program () +{ + account_mode2_pick_1: + printf ( "请输入需要存入多少钱:" ); + while ( ( scanf ( "%lf" , & save ) != 1 )&& save < 0) + { + printf ( "请重新输入合法的金额:" ); + while ( getchar ( ) != '\n' );//清除缓存 + continue; + } + if ( save < 0 ) + { + printf ( "不能输入负值\n" ); + while ( getchar ( ) != '\n' );//清除缓存 + goto account_mode2_pick_1; + } + balance += save; //存款 + running_bill[ running_bill_tab ] = save;//保存流水 + running_bill_balance[ running_bill_tab ] = balance; //保存历史记录 + running_bill_symbol[ running_bill_tab ] = '+'; //保存取款还是存款 + ++ running_bill_tab; + printf ( "存款成功 \n" ); + printf ( "当前余额:%.3lf \n" , balance ); + while ( getchar ( ) != '\n' );//清除缓存 + account_home_program ( ); +} + +//模式三 取款 +void account_mode3_program () +{ + account_mode3_pick_1: //校验取款密码标签 + { + printf ( "-----取款密码(只能输入6位)----\n" ); + printf ( "请输入取款密码:" ); + scanf ( "%s" , account_password_1_test ); + if ( strcmp ( account_password_1 , account_password_1_test ) != 0 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "输入密码错误,请重新输入 \n" ); + goto account_mode3_pick_1; + } + } + + + account_mode3_pick_2: //取款标签 + { + printf ( "请输入需要取出多少钱:" ); + while ( scanf ( "%lf" , & withdraw ) != 1 ) + { + printf ( "请重新输入合法的金额:" ); + while ( getchar ( ) != '\n' );//清除缓存 + continue; + } + if ( withdraw < 0 ) + { + printf ( "不能输入负值\n" ); + while ( getchar ( ) != '\n' );//清除缓存 + goto account_mode3_pick_2; + } + if ( withdraw > balance ) + { + printf ( "余额不足\n" ); + while ( getchar ( ) != '\n' );//清除缓存 + goto account_mode3_pick_2; + } + } + balance -= withdraw; //取款 + running_bill[ running_bill_tab ] = withdraw; //保存流水 + running_bill_balance[ running_bill_tab ] = balance; //保存历史记录 + running_bill_symbol[ running_bill_tab ] = '-'; //保存取款还是存款 + ++ running_bill_tab; + printf ( "存款成功\n" ); + printf ( "当前余额:%.3lf\n" , balance ); + while ( getchar ( ) != '\n' );//清除缓存 + account_home_program ( ); +} + +//模式四 预测余额 +void account_mode4_program () +{ + account_mode4_program_pick: //输入年份标签 + { + printf ( "请输入要预测多少年后:" ); + while ( scanf ( "%lf" , & year ) != 1 ) + { + printf ( "请重新输入合法的年份:" ); + while ( getchar ( ) != '\n' );//清除缓存 + continue; + } + if ( year <= 0 ) + { + printf ( "输入错误年份\n" ); + while ( getchar ( ) != '\n' );//清除缓存 + goto account_mode4_program_pick; + } + } + interest = balance * interest_rate * year; //利息 + prediction = balance + interest; //余额 + + printf ( "%.0lf年后 \n" , year ); + printf ( "获得利息:%.3lf\n" , interest ); + printf ( "余额为:%0.3lf\n" , prediction ); + while ( getchar ( ) != '\n' );//清除缓存 + account_home_program ( ); +} + +//管理员登录页面 +void administrastion_login_program () +{ + printf ( "-----管理员登录页面----\n" ); + printf ( "请输入管理员密码:" ); + scanf ( "%s" , & administrators_password_test ); + if ( strcmp ( administrators_password , administrators_password_test ) != 0 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "密码输入错误 \n" ); + bank_home_program ( ); + } + while ( getchar ( ) != '\n' );//清除缓存 + administrastion_home_program ( ); +} + +//管理员主页面 +void administrastion_home_program () +{ + int administrastion_home_program_pick; + printf ( "---管理员界面--- \n" ); + printf ( "1.查看用户个人信息 \n" ); + printf ( "2.查看金额流水\n" ); + printf ( "3.修改用户账号 \n" ); + printf ( "4.修改用户密码 \n" ); + printf ( "5.修改取款密码 \n" ); + printf ( "6.修改银行利率 \n" ); + printf ( "7.返回到账户主页 \n" ); + printf ( "请输入需要进入的模式:" ); + scanf ( "%d" , & administrastion_home_program_pick ); + switch ( administrastion_home_program_pick ) + { + case 1: + { + printf ( "----用户个人信息----\n" ); + printf ( "当前账号:%s \n" , account_number ); + printf ( "当前密码:%s \n" , account_password ); + printf ( "当前取款密码:%s \n" , account_password_1 ); + printf ( "当前余额为:%lf \n" , balance ); + while ( getchar ( ) != '\n' );//清除缓存 + administrastion_home_program ( ); + break; + } + case 2: + { + while ( getchar ( ) != '\n' ); //清除缓存 + administrastion_mode1_program ( ); + break; + } + case 3: + { + while ( getchar ( ) != '\n' ); //清除缓存 + administrastion_mode2_program ( ); + break; + } + case 4: + { + while ( getchar ( ) != '\n' ); //清除缓存 + administrastion_mode3_program ( ); + break; + } + case 5: + { + while ( getchar ( ) != '\n' ); //清除缓存 + administrastion_mode4_program ( ); + break; + } + case 6: + { + while ( getchar ( ) != '\n' ); //清除缓存 + administrastion_mode5_program ( ); + break; + } + case 7: + { + printf ( "返回成功 \n" ); + while ( getchar ( ) != '\n' );//清除缓存 + bank_home_program ( ); + break; + } + default: + { + printf ( "输入错误,请重新输入 \n" ); + while ( getchar ( ) != '\n' );//清除缓存 + administrastion_home_program ( ); + break; + } + } + +} + +//管理员 查看流水金额 +void administrastion_mode1_program () +{ + printf ( "---金额流水开始--- \n" ); + for ( int running_bill_tab_test = 0 ; running_bill_tab_test < running_bill_tab ; running_bill_tab_test ++ ) + { + if ( running_bill_symbol[ running_bill_tab_test ] == '+' ) + { + printf ( "第%d次 \n" , running_bill_tab_test + 1 ); + printf ( "存入%.3lf元 \n" , running_bill[ running_bill_tab_test ] ); + printf ( "余额%.3lf元 \n" , running_bill_balance[ running_bill_tab_test ] ); + } + if ( running_bill_symbol[ running_bill_tab_test ] == '-' ) + { + printf ( "第%d次 \n" , running_bill_tab_test + 1 ); + printf ( "取出%.3lf元 \n" , running_bill[ running_bill_tab_test ] ); + printf ( "余额%.3lf元 \n" , running_bill_balance[ running_bill_tab_test ] ); + } + } + if ( running_bill_tab == 0 ) // 如果用户没有取款记录 + { + printf ( "暂无存取款记录 \n" ); + } + printf ( "---金额流水结束--- \n" ); + printf ( "按回车结束" ); + while ( getchar ( ) != '\n' );//清除缓存 + administrastion_home_program ( ); +} + +//管理员 修改用户账号 +void administrastion_mode2_program () +{ + administrastion_modify_number_tab: //修改账号标签 + { + printf ( "-----修改账号----\n" ); + printf ( "请输入新账号:" ); + scanf ( "%s" , account_number ); + printf ( "请确认新账号:" ); + scanf ( "%s" , account_number_test ); + if ( strcmp ( account_number , account_number_test ) != 0 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "两次输入不相同,请重新输入 \n" ); + goto administrastion_modify_number_tab; + } + printf ( "修改账户成功!\n" ); + printf ( "当前用户名为%s\n" , account_number ); + while ( getchar ( ) != '\n' );//清除缓存 + administrastion_home_program ( ); + } +} + +//管理员 修改用户密码 +void administrastion_mode3_program () +{ + account_modify_password_tab: //修改密码标签 + { + printf ( "-----修改密码----\n" ); + printf ( "请输入密码:" ); + scanf ( "%s" , account_password ); + printf ( "请确认密码:" ); + scanf ( "%s" , account_password_test ); + if ( strcmp ( account_password , account_password_test ) != 0 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "两次输入不相同,请重新输入 \n" ); + goto account_modify_password_tab; + } + printf ( "修改密码成功!\n" ); + printf ( "当前密码为%s\n" , account_password ); + while ( getchar ( ) != '\n' );//清除缓存 + administrastion_home_program ( ); + } +} + +//管理员 修改用户取款密码 +void administrastion_mode4_program () +{ + account_modify_password_1_tab: //修改密码标签 + { + printf ( "-----修改取款密码(只能输入6位)----\n" ); + printf ( "请输入新的取款密码:" ); + scanf ( "%s" , & account_password_1 ); + //校验是不是6位 + account_password_1_test_1 = strlen ( account_password_1 ); + if ( account_password_1_test_1 != 6 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "当前输入了%d位,请重新输入 \n" , account_password_1_test_1 ); + goto account_modify_password_1_tab; + } + + printf ( "请确认取款密码:" ); + scanf ( "%s" , account_password_1_test ); + if ( strcmp ( account_password_1 , account_password_1_test ) != 0 ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "两次输入不相同,请重新输入 \n" ); + goto account_modify_password_1_tab; + } + } + printf ( "修改取款密码成功!\n" ); + printf ( "当前密码为%s\n" , account_password_1 ); + while ( getchar ( ) != '\n' );//清除缓存 + administrastion_home_program ( ); +} + +// 管理员修改年利率 +void administrastion_mode5_program () +{ + account_modify_interest_rate_tab: // 修改年利率标签 + { + printf ( "-----修改年利率----\n" ); + printf ( "请输入调整后的利率不用加%%:" ); + scanf ( "%lf" , & interest_rate_test ); + printf ( "请确认年利率:" ); + scanf ( "%lf" , & interest_rate ); + if ( interest_rate != interest_rate_test ) + { + while ( getchar ( ) != '\n' ); // 清除缓存 + printf ( "两次输入不相同,请重新输入 \n" ); + goto account_modify_interest_rate_tab; + } + interest_rate = interest_rate / 100; + printf ( "修改年利率成功!\n" ); + printf ( "当前年利率:%.3lf%%\n" , interest_rate * 100 ); + while ( getchar ( ) != '\n' ); // 清除缓存 + administrastion_home_program ( ); + } +} + +//主函数 +int main () +{ + //银行 + bank_home_program ( ); + return 0; +} diff --git a/practice_code/idea/bank/interest_rate/interest_rate_1.c b/practice_code/idea/bank/interest_rate/interest_rate_1.c new file mode 100644 index 0000000..c168141 --- /dev/null +++ b/practice_code/idea/bank/interest_rate/interest_rate_1.c @@ -0,0 +1,18 @@ +#include +#define add(a,b,c)((a)+(a)*(b)*((c)/100)) +int main() +{ + float a; + float b; + float c; + printf("银行本利计算器 \n"); + printf("请输入本金:"); + scanf("%f", &a); + printf("请输入年份:"); + scanf("%f", &b); + printf("请输入利率(例如2%%只需要填2):"); + scanf("%f", &c); + float z = add(a, b, c); + printf("本金加利息为:%f \n", z); + return 0; +} diff --git a/practice_code/idea/bank/interest_rate/interest_rate_2.c b/practice_code/idea/bank/interest_rate/interest_rate_2.c new file mode 100644 index 0000000..1127939 --- /dev/null +++ b/practice_code/idea/bank/interest_rate/interest_rate_2.c @@ -0,0 +1,33 @@ +#include + +int main () +{ + for ( ;; ) + { + double x , y , z; //存款金金,存款年份,年利率 + int g; //获取退出字符 + printf ( "存款本利计算器 \n" ); + printf ( "请输入本金:" ); + scanf ( "%lf" , & x ); + printf ( "请输入年份:" ); + scanf ( "%lf" , & y ); + printf ( "请输入年利率(不用加%%):" ); + scanf ( "%lf" , & z ); + printf ( "利息为%lf,本金加利息为%lf \n" , x * y * z / 100 , x + x * y * z / 100 ); + + while ( getchar ( ) != '\n' ); //读取缓存,到回车结束 + + printf ( "输入0退出,输入其他继续计算: " ); + g = getchar ( ); + if ( g == '0' ) + { + break; + } + if ( g == '\n' || g != '0' ) + { + continue; //重新启动 + } + + } + return 0; +} diff --git a/practice_code/idea/calculator/calculator_1.c b/practice_code/idea/calculator/calculator_1.c new file mode 100644 index 0000000..fc9e2f0 --- /dev/null +++ b/practice_code/idea/calculator/calculator_1.c @@ -0,0 +1,65 @@ +#include + + +int main () +{ + double x; + double y; + double v = 0; + char z; + + printf ( "两位数计算器\n" ); + + + printf ( "请输入运算方式 (+ or - or * or /):" ); + scanf ( " %c" , & z ); + if ( z != '+' && z != '-' && z != '*' && z != '/' ) + { + printf ( "你是脑干缺失,还是不识字,看不到“运算方式”这几个大字?" ); + return 1; + } + + + printf ( "请输入第一位数:" ); + scanf ( " %lf" , & x ); + if ( x == 0 && x != 0 ) + { + printf ( "不知道什么叫数字?\n" ); + return 1; + } + + printf ( "请输入第二位数:" ); + scanf ( " %lf" , & y ); + if ( y == 0 && y != 0 ) + { + printf ( "不知道什么叫数字?\n" ); + return 1; + } + + if ( z == '+' ) + { + v = x + y; + } + if ( z == '-' ) + { + v = x - y; + } + if ( z == '*' ) + { + v = x * y; + } + if ( z == '/' ) + { + if ( x != 0 ) + { + v = x / y; + } else + { + printf ( "分母不能为0 \n" ); + return 1; + } + } + printf ( "计算结果为:%lf: \n" , v ); + return 0; +} + diff --git a/practice_code/idea/calculator/calculator_2.c b/practice_code/idea/calculator/calculator_2.c new file mode 100644 index 0000000..9a79be6 --- /dev/null +++ b/practice_code/idea/calculator/calculator_2.c @@ -0,0 +1,78 @@ +#include + +int main () +{ + //定义函数 + int a; //计算模式选择的数字 + double x , y; //计算输入的数字, 第一位数字x,第二位数字y + + //输出主界面 + printf ( "*计算机* \n" ); + printf ( "1.加法模式 \n" ); + printf ( "2.减法模式 \n" ); + printf ( "3.乘法模式 \n" ); + printf ( "4.除法模式 \n" ); + printf ( "5.退出程序 \n" ); + printf ( "输入对应的数字进入对应的模式 \n" ); + + //获取需要进入的模式 + printf ( "输入对应的数字:" ); + scanf ( " %d" , & a ); //判断用户输入的数字是否符合条件 + if ( a > 5 || a < 1 ) + { + printf ( "请输入数字" ); + return 1; + } + + + //计算的第一位数字 + printf ( "输入第一位需要计算的数:" ); + scanf ( " %lf" , & x ); //判断用户输入的数字是否符合条件 + if ( a == 0 && a != 0 ) + { + printf ( "请输入数字" ); + return 1; + } + + //计算的第二位数字 + printf ( "输入第二位需要计算的数:" ); + scanf ( " %lf" , & y ); //判断用户输入的数字是否符合条件 + if ( a == 0 && a != 0 ) + { + printf ( "请输入数字" ); + return 1; + } + + //计算代码块 + switch ( a ) + { + //加法 + case 1: + printf ( "a + b = %lf \n" , x + y ); + break; + //减法 + case 2: + if ( y = 0 ) //除数不能为0 + { + printf ( "除数不能为0" ); + return 0; + } + printf ( "a - b = %lf \n" , x - y ); + break; + //乘法 + case 3: + printf ( "a * b = %lf \n" , x * y ); + break; + //除法 + case 4: + printf ( "a / b= %lf \n" , x / y ); + break; + //输入错误提示 + default : + printf ( "请输入正确的数字" ); + return 1; + } + + + return 0; +} diff --git a/practice_code/idea/calculator/calculator_3.c b/practice_code/idea/calculator/calculator_3.c new file mode 100644 index 0000000..fd32019 --- /dev/null +++ b/practice_code/idea/calculator/calculator_3.c @@ -0,0 +1,70 @@ +#include +#include + +int main () +{ + double x , y;//需要计算的数字 + int a;//选择进入模式的数字 + while ( 1 ) + { + //计算机界面 + printf ( "计算器 \n" ); + printf ( "1.加法模式 \n" ); + printf ( "2.减法模式 \n" ); + printf ( "3.乘法模式 \n" ); + printf ( "4.除法模式 \n" ); + printf ( "5.退出程序 \n" ); + + //获取用户输入模式的数字 + printf ( "请输入对应的数字进入:" ); + if ( scanf ( "%d" , & a ) < 1 || a > 5 ) + { + printf ( "请输入正确的数字 \n" ); + while ( getchar ( ) != '\n' ); //表示循环读入字符,直到读到回车符结束循环 + continue; + } + + //模式5 + if ( a == 5 ) + { + exit ( 0 ); + } + + //获取用户需要计算的第一位数字 + printf ( "请输入第一位数:" ); + scanf ( "%lf" , & x ); + + //获取用户需要计算的第二位数字 + printf ( "请输入第二位数:" ); + scanf ( "%lf" , & y ); + + + //计算程序 + switch ( a ) + { + //加法 + case 1: + printf ( "相加结果为%lf \n" , x + y ); + break; + //减法 + case 2: + printf ( "相减结果为%lf \n" , x - y ); + break; + //乘法 + case 3: + printf ( "相乘结果为%lf \n" , x * y ); + break; + //除法 + case 4: + if ( y == 0 ) + { + printf ( "除数不能为0" ); //除数不能为0 + continue; + } + printf ( "相除结果为%lf \n" , x / y ); + break; + } + + } + return 0; +} diff --git a/practice_code/idea/calculator/calculator_4.c b/practice_code/idea/calculator/calculator_4.c new file mode 100644 index 0000000..5e96c2f --- /dev/null +++ b/practice_code/idea/calculator/calculator_4.c @@ -0,0 +1,142 @@ +#include +#include + +int main () +{ + int d , e , g; //需要计算的模式,需要计算的数量,判断循环几次,标记是否进行了循环 + double a , b , c; //初始化的数字 + int f = 1; + + while ( 1 ) + { + //获取用户需要计算的模式 + printf ( " \n" ); + printf ( "0.退出计算机 \n" ); + printf ( "1.加法模式 \n" ); + printf ( "2.减法模式 \n" ); + printf ( "3.乘法模式 \n" ); + printf ( "4.除法模式 \n" ); + + //获取用户输入的字符 + printf ( "请输入对应的数字进入: " ); + scanf ( "%d" , & d ); + + if ( d > 4 || d < 0 ) //判断数字是否符合条件 + { + printf ( "请输入符合条件的数字 \n" ); + while ( getchar ( ) != '\n' ); + continue; + } + + //输入0退出 + if ( d == 0 ) + { + exit ( 0 ); + } + + //获取用户需要计算的个数 + printf ( "请输入需要的个数:" ); + scanf ( "%d" , & e ); + if ( e <= 0 ) //判断数字是否合法 + { + printf ( "请输入合法的数字 \n" ); + while ( getchar ( ) != '\n' ); + continue; + } + + //加法模式 + if ( d == 1 ) + { + for ( g = 1 ; g <= e ; g ++ ) + { + printf ( "请输入第%d位数:" , f ); + ++ f; + scanf ( "%lf" , & a ); + b += a; + } + printf ( "结果为:%lf \n \n" , b ); + while ( getchar ( ) != '\n' ); //读取字符到回车结束 + } + + //减法模式 + if ( d == 2 ) + { + for ( g = 1 ; g <= e ; g ++ ) + { + printf ( "请输入第%d位数:" , f ); + scanf ( "%lf" , & a ); + if ( f == 1 ) + { + b = a; + } else + { + b -= a; + } + ++ f; + } + printf ( "结果为:%lf \n \n" , b ); + while ( getchar ( ) != '\n' ); //读取字符到回车结束 + } + + //乘法模式 + if ( d == 3 ) + { + for ( g = 1 ; g <= e ; g ++ ) + { + printf ( "请输入第%d位数:" , f ); + + scanf ( "%lf" , & a ); + if ( f == 1 ) + { + b = a; + } else + { + b *= a; + } + ++ f; + } + printf ( "结果为:%lf \n \n" , b ); + while ( getchar ( ) != '\n' ); //读取字符到回车结束 + } + + //除法模式 + if ( d == 4 ) + { + for ( g = 1 ; g <= e ; g ++ ) + { + printf ( "请输入第%d位数:" , f ); + + scanf ( "%lf" , & a ); + if ( f == 1 ) + { + if ( a == 0 ) //判断分母是否为0 + { + printf ( "分母不能为0 \n" ); + while ( getchar ( ) != '\n' ); + c = 1; //循环标识 + continue; + } else + { + b = a; + } + } else + { + b /= a; + } + + //判断有没有进行错误输入循环 + if ( a == 1 ) + { + f = 1; + } else + { + ++ f; + } + } + printf ( "结果为:%lf \n \n" , b ); + while ( getchar ( ) != '\n' ); //读取字符到回车结束 + } + } + return 0; + +} diff --git a/practice_code/idea/calculator/calculator_5.c b/practice_code/idea/calculator/calculator_5.c new file mode 100644 index 0000000..908ac8e --- /dev/null +++ b/practice_code/idea/calculator/calculator_5.c @@ -0,0 +1,158 @@ +#include +#include + +int main () +{ + while ( 1 ) //计算器运行 + { + //初始化 + double result = 0; //结果 + double array_1[100]; //储存数字的的数组 + char array_2[100]; //储存符号 + double array_3[100]; //储存乘除数字的的数组 + char array_4[100]; //储存乘除符号 + int tab_2 = 0 , tab_3 = 0 , tab_4 = 0 , tab_7 = 0 , tab_8 = 0 , tab_a = 0;//乘除里符号接收的第几位;用于判断接收位数;加减第几位;用获取乘除数字的次数;判断用户是否退出获取加减循环 + int tab_1 = 0 , tab_5 = 0 , tab_6 = 0; //判断用户是否退出获取数字循环,判断用户是否退出获取乘除循环; + int tab_9; //主页面判断 + + //主页面 + printf ( "---主页--- \n" ); + printf ( "输入0退出 \n" ); + printf ( "任意字符或双回车进入计算:" ); + tab_9 = getchar ( ); + if ( tab_9 == '0' ) + { + printf ( "欢迎下次使用!" ); + return 1; + } + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "-------- \n" ); + //此层循环计算加减开始 + for ( tab_4 = 0 ; tab_8 == 0 ; tab_4 ++ ) + { + //此层循环计算乘除开始 + for ( tab_7 = 0 ; tab_5 == 0 ; tab_7 ++ ) + { + + //此循环接收用户输入的数据开始 + for ( ; tab_1 == 0 ; ) + { + //获取用户输入的数字 + printf ( "请输入第%d位数:" , tab_3 + 1 ); + while ( scanf ( "%lf" , & array_1[ tab_3 ] ) != 1 ) + { + printf ( "请输入合法的数字:" ); + while ( getchar ( ) != '\n' ); //清楚缓存 + continue; //重新开始循环 + } + + while ( getchar ( ) != '\n' ); //清楚缓存 + + //获取用户输入的符号 + printf ( "+ or - or * or / or = \n" ); + printf ( "请输入第%d位符号:" , tab_3 + 1 ); + scanf ( " %c" , & array_2[ tab_3 ] ); + while ( array_2[ tab_3 ] != '+' && array_2[ tab_3 ] != '-' && array_2[ tab_3 ] != '*' && + array_2[ tab_3 ] != '/' && array_2[ tab_3 ] != '=' ) + { + printf ( "请输入正确的符号:" ); + while ( getchar ( ) != '\n' ); //清楚缓存 + scanf ( " %c" , & array_2[ tab_3 ] ); //重新获取字符 + } + + + while ( getchar ( ) != '\n' ); //清楚缓存 + + //分母不能为0 + if ( tab_3 == 0 && array_1[ tab_3 ] == 0 && array_2[ tab_3 ] == '/' ) + { + printf ( "分母不能为0,请重新输入 \n" ); + while ( getchar ( ) != '\n' ); //清楚缓存 + continue; //重新开始循环 + } + + + //判断是=结束计算出结果 + if ( array_2[ tab_3 ] == '=' ) + { + tab_1 = 1; + break; + } + + //次数的自增 + ++ tab_3; + } + //此循环接收用户输入的数据结束 + + + //计算乘法 + if ( array_2[ tab_7 ] == '*' ) + { + array_1[ tab_7 + 1 ] = array_1[ tab_7 ] * array_1[ tab_7 + 1 ]; //计算 + } + + //计算除法 + if ( array_2[ tab_7 ] == '/' ) + { + array_1[ tab_7 + 1 ] = array_1[ tab_7 ] / array_1[ tab_7 + 1 ]; //计算 + } + + //跳过加法减法 + if ( array_2[ tab_7 ] == '+' || array_2[ tab_7 ] == '-' ) + { + array_3[ tab_2 ] = array_1[ tab_7 ]; //储存计算的数 + array_4[ tab_2 ] = array_2[ tab_7 ]; //储存符号 + ++ tab_2; //增加符号存储位置 + } + + //退出乘除 + if ( array_2[ tab_7 ] == '=' ) + { + array_3[ tab_2 ] = array_1[ tab_7 ]; //储存计算的数 + array_4[ tab_2 ] = array_2[ tab_7 ]; //储存符号 + tab_5 = 1; //防止再次进入循环 + break; //退出循环 + } + + + } + //此层循环计算乘除结束 + + if ( tab_4 == 0 ) + { + result = array_3[ tab_4 ]; + } else + { + //加法 + if ( array_4[ tab_a ] == '+' ) + { + result += array_3[ tab_4 ]; + } + + //加法 + if ( array_4[ tab_a ] == '-' ) + { + + result -= array_3[ tab_4 ]; + } + + //计算最终结果 + if ( array_4[ tab_a ] == '=' ) + { + printf ( "结果为%lf \n" , result ); + tab_8 = 1; + printf ( "按任意按键返回主菜单 \n" ); + getchar ( ); + break; + + } + + //符号的自增 + ++ tab_a; + } + } + //此层循环计算加减结束 + } + + return 0; +} diff --git a/practice_code/idea/calculator/calculator_6.c b/practice_code/idea/calculator/calculator_6.c new file mode 100644 index 0000000..908ac8e --- /dev/null +++ b/practice_code/idea/calculator/calculator_6.c @@ -0,0 +1,158 @@ +#include +#include + +int main () +{ + while ( 1 ) //计算器运行 + { + //初始化 + double result = 0; //结果 + double array_1[100]; //储存数字的的数组 + char array_2[100]; //储存符号 + double array_3[100]; //储存乘除数字的的数组 + char array_4[100]; //储存乘除符号 + int tab_2 = 0 , tab_3 = 0 , tab_4 = 0 , tab_7 = 0 , tab_8 = 0 , tab_a = 0;//乘除里符号接收的第几位;用于判断接收位数;加减第几位;用获取乘除数字的次数;判断用户是否退出获取加减循环 + int tab_1 = 0 , tab_5 = 0 , tab_6 = 0; //判断用户是否退出获取数字循环,判断用户是否退出获取乘除循环; + int tab_9; //主页面判断 + + //主页面 + printf ( "---主页--- \n" ); + printf ( "输入0退出 \n" ); + printf ( "任意字符或双回车进入计算:" ); + tab_9 = getchar ( ); + if ( tab_9 == '0' ) + { + printf ( "欢迎下次使用!" ); + return 1; + } + while ( getchar ( ) != '\n' ); //清楚缓存 + printf ( "-------- \n" ); + //此层循环计算加减开始 + for ( tab_4 = 0 ; tab_8 == 0 ; tab_4 ++ ) + { + //此层循环计算乘除开始 + for ( tab_7 = 0 ; tab_5 == 0 ; tab_7 ++ ) + { + + //此循环接收用户输入的数据开始 + for ( ; tab_1 == 0 ; ) + { + //获取用户输入的数字 + printf ( "请输入第%d位数:" , tab_3 + 1 ); + while ( scanf ( "%lf" , & array_1[ tab_3 ] ) != 1 ) + { + printf ( "请输入合法的数字:" ); + while ( getchar ( ) != '\n' ); //清楚缓存 + continue; //重新开始循环 + } + + while ( getchar ( ) != '\n' ); //清楚缓存 + + //获取用户输入的符号 + printf ( "+ or - or * or / or = \n" ); + printf ( "请输入第%d位符号:" , tab_3 + 1 ); + scanf ( " %c" , & array_2[ tab_3 ] ); + while ( array_2[ tab_3 ] != '+' && array_2[ tab_3 ] != '-' && array_2[ tab_3 ] != '*' && + array_2[ tab_3 ] != '/' && array_2[ tab_3 ] != '=' ) + { + printf ( "请输入正确的符号:" ); + while ( getchar ( ) != '\n' ); //清楚缓存 + scanf ( " %c" , & array_2[ tab_3 ] ); //重新获取字符 + } + + + while ( getchar ( ) != '\n' ); //清楚缓存 + + //分母不能为0 + if ( tab_3 == 0 && array_1[ tab_3 ] == 0 && array_2[ tab_3 ] == '/' ) + { + printf ( "分母不能为0,请重新输入 \n" ); + while ( getchar ( ) != '\n' ); //清楚缓存 + continue; //重新开始循环 + } + + + //判断是=结束计算出结果 + if ( array_2[ tab_3 ] == '=' ) + { + tab_1 = 1; + break; + } + + //次数的自增 + ++ tab_3; + } + //此循环接收用户输入的数据结束 + + + //计算乘法 + if ( array_2[ tab_7 ] == '*' ) + { + array_1[ tab_7 + 1 ] = array_1[ tab_7 ] * array_1[ tab_7 + 1 ]; //计算 + } + + //计算除法 + if ( array_2[ tab_7 ] == '/' ) + { + array_1[ tab_7 + 1 ] = array_1[ tab_7 ] / array_1[ tab_7 + 1 ]; //计算 + } + + //跳过加法减法 + if ( array_2[ tab_7 ] == '+' || array_2[ tab_7 ] == '-' ) + { + array_3[ tab_2 ] = array_1[ tab_7 ]; //储存计算的数 + array_4[ tab_2 ] = array_2[ tab_7 ]; //储存符号 + ++ tab_2; //增加符号存储位置 + } + + //退出乘除 + if ( array_2[ tab_7 ] == '=' ) + { + array_3[ tab_2 ] = array_1[ tab_7 ]; //储存计算的数 + array_4[ tab_2 ] = array_2[ tab_7 ]; //储存符号 + tab_5 = 1; //防止再次进入循环 + break; //退出循环 + } + + + } + //此层循环计算乘除结束 + + if ( tab_4 == 0 ) + { + result = array_3[ tab_4 ]; + } else + { + //加法 + if ( array_4[ tab_a ] == '+' ) + { + result += array_3[ tab_4 ]; + } + + //加法 + if ( array_4[ tab_a ] == '-' ) + { + + result -= array_3[ tab_4 ]; + } + + //计算最终结果 + if ( array_4[ tab_a ] == '=' ) + { + printf ( "结果为%lf \n" , result ); + tab_8 = 1; + printf ( "按任意按键返回主菜单 \n" ); + getchar ( ); + break; + + } + + //符号的自增 + ++ tab_a; + } + } + //此层循环计算加减结束 + } + + return 0; +} diff --git a/practice_code/idea/live.c b/practice_code/idea/live.c new file mode 100644 index 0000000..2ed0f17 --- /dev/null +++ b/practice_code/idea/live.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include + +void live(char symbol[],int length) +{ + printf ("\n\n\n\n"); + system ("color 0c");//改变颜色 + int length_2=length/10;//中间部分长度 + int length_1=length/3+1+length_2-1;//上半部分长度 + int length_3=length-length_2-length_1+4+length_2-1;//下半部分长度 + int length_a=length/5*2;//上半2/5前位置 + int length_a_1=length/5*2+2;//上半2/5后位置 + int length_b=length/5*4;//上半4/5前位置 + int length_b_1=length/5*4+2;//上半4/5后位置 + int size= strlen (symbol);//计算空格需要打多少 + int length_e=1;//下半左面 + int length_f=length/2*3-length_2-1;//下半右面 + //打印上半 + for ( int i = 1 ; i <= length_1 ; i++ ) + { + for ( int j = 1 ; j <= length_a ; ++ j ) + { + for ( int k = 1 ; k <= size ; k++ ) + { + printf (" "); + } + } + for ( int j = length_a ; j <= length_a_1 ; j++ ) + { + printf ("%s",symbol); + } + for ( int j = length_a_1 ; j <=length_b ; j++ ) + { + for ( int k = 1 ; k <= size ; k++ ) + { + printf (" "); + } + } + for ( int j = length_b ; j <= length_b_1 ; j++ ) + { + printf ("%s",symbol); + } + if(length_a_120) + { + goto tab; + } + printf ("请输入需要填充的符号或者是单词:"); + scanf ("%s",symbol); + live (symbol,length); + return 0; +} \ No newline at end of file diff --git a/practice_code/idea/minesweeper.c b/practice_code/idea/minesweeper.c new file mode 100644 index 0000000..e100695 --- /dev/null +++ b/practice_code/idea/minesweeper.c @@ -0,0 +1,503 @@ +#include +#include +#include + +//声明主页 +void home_procedure ( int figure ); + +//打印界面 +void print_interface ( int size , char array[size][size] ) +{ + for ( int i = 0 ; i < size ; i ++ ) + { + for ( int j = 0 ; j < size ; j ++ ) + { + printf ( " %c " , array[ i ][ j ] ); + } + printf ( "\n" ); + } +} + +//初始化显示界面 +char initialization_appear_map ( int size , char appear_map[size][size] ) +{ + for ( int i = 0 ; i < size ; i ++ ) + { + for ( int j = 0 ; j < size ; j ++ ) + { + appear_map[ i ][ j ] = '#'; + } + } +} + +//初始化结果界面 +char initialization_result_map ( int size , int bomb , char result_map[size][size] ) +{ + //将界面全部初始化为空 + for ( int i = 0 ; i < size ; i ++ ) + { + for ( int j = 0 ; j < size ; j ++ ) + { + result_map[ i ][ j ] = '-'; + } + } + + //随机生成炸弹 + int i = 1;//需要生成的炸弹自增 + while ( i <= bomb ) + { + int x = rand ( ) % size;//获随机取炸弹横坐标 + int y = rand ( ) % size;//获随机取炸弹纵坐标 + if ( result_map[ y ][ x ] == '-' ) + { + result_map[ y ][ x ] = '*'; + ++ i; + } + } + + //生成数字 + for ( i = 0 ; i < size ; i ++ ) + { + for ( int j = 0 ; j < size ; j ++ ) + { + if ( result_map[ i ][ j ] == '-' ) + { + char integral = '0';//初始化积分 + if ( result_map[ i ][ j + 1 ] == '*' && j < size && j + 1 < size )//判断右边的雷 + { + integral += 1; + } + if ( result_map[ i ][ j - 1 ] == '*' && j > 0 & j - 1 >= 0 )//判断左边的雷 + { + integral += 1; + } + if ( result_map[ i + 1 ][ j ] == '*' && i > 0 &&i+1=0)//判断下边的雷 + { + integral += 1; + } + if ( result_map[ i - 1 ][ j + 1 ] == '*' && i > 0 &&i-1>=0&& j+1 < size )//判断右上的雷 + { + integral += 1; + } + if ( result_map[ i + 1 ][ j + 1 ] == '*' && i+1 < size && j+1 < size )//判断右下的雷 + { + integral += 1; + } + if ( result_map[ i + 1 ][ j - 1 ] == '*' && i+1 < size && j-1 >= 0 )//判断左下的雷 + { + integral += 1; + } + if ( result_map[ i - 1 ][ j - 1 ] == '*' && i-1 >= 0 && j-1 >= 0 )//判断左上的雷 + { + integral += 1; + } + + if ( integral != '0' ) + { + result_map[ i ][ j ] = integral; + } + } + } + } +} + +//复制数组 +void copy_array ( int size , char array_a[size][size] , char array_b[size][size] ) +{ + for ( int i = 0 ; i < size ; i ++ ) + { + for ( int j = 0 ; j < size ; j ++ ) + { + array_a[ i ][ j ] = array_b[ i ][ j ]; + } + } +} + +//储存和判断 +int replace_appear_map ( int integral , int size , int coordinate_y , int coordinate_x , char appear_map[size][size] , + char copy_appear_map[size][size] , char result_map[size][size] + ) +{ + int copy_coordinate_x = coordinate_x;//备份x + int copy_coordinate_y = coordinate_y;//备份y + if ( result_map[ coordinate_y ][ coordinate_x ] == '*' )//判断有没有雷 + { + while ( getchar ( ) != '\n' );//清除缓存 + print_interface ( size , result_map );//输出原版地图 + printf ( "——踩到地雷了——\n" ); + printf ( " ***** \n" ); + printf ( " * * \n" ); + printf ( "* O O *\n" ); + printf ( "* ^ *\n" ); + printf ( " * /___\\ \n" ); + printf ( " ***** \n" ); + home_procedure ( 1 );//返回主页 + } + else if ( appear_map[ coordinate_y ][ coordinate_x ] == '&' )//判断有无旗 + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "此处有旗\n" ); + } + else//点开 + { + copy_appear_map[ coordinate_y ][ coordinate_x ] = result_map[ coordinate_y ][ coordinate_x ];//替换备份地图 + appear_map[ coordinate_y ][ coordinate_x ] = result_map[ coordinate_y ][ coordinate_x ];//替换显示地图 + if ( result_map[ coordinate_y ][ coordinate_x ] == '-' )//空白情况 + { + while ( result_map[ coordinate_y ][ coordinate_x - 1 ] == '-' ) + { + integral += 1; + -- coordinate_x; + copy_appear_map[ coordinate_y ][ coordinate_x ] = result_map[ coordinate_y ][ coordinate_x ];//替换备份地图 + appear_map[ coordinate_y ][ coordinate_x ] = result_map[ coordinate_y ][ coordinate_x ];//替换显示地图 + } + coordinate_x = copy_coordinate_x;//还原x + while ( result_map[ coordinate_y ][ coordinate_x + 1 ] == '-' ) + { + integral += 1; + ++ coordinate_x; + copy_appear_map[ coordinate_y ][ coordinate_x ] = result_map[ coordinate_y ][ coordinate_x ];//替换备份地图 + appear_map[ coordinate_y ][ coordinate_x ] = result_map[ coordinate_y ][ coordinate_x ];//替换显示地图 + } + coordinate_x = copy_coordinate_x;//还原x + while ( result_map[ coordinate_y - 1 ][ coordinate_x ] == '-' ) + { + integral += 1; + -- coordinate_y; + copy_appear_map[ coordinate_y ][ coordinate_x ] = result_map[ coordinate_y ][ coordinate_x ];//替换备份地图 + appear_map[ coordinate_y ][ coordinate_x ] = result_map[ coordinate_y ][ coordinate_x ];//替换显示地图 + } + coordinate_x = copy_coordinate_x;//还原x + while ( result_map[ coordinate_y + 1 ][ coordinate_x ] == '-' ) + { + integral += 1; + ++ coordinate_y; + copy_appear_map[ coordinate_y ][ coordinate_x ] = result_map[ coordinate_y ][ coordinate_x ];//替换备份地图 + appear_map[ coordinate_y ][ coordinate_x ] = result_map[ coordinate_y ][ coordinate_x ];//替换显示地图 + } + coordinate_x = copy_coordinate_y;//还原y + } + return integral + 1;//增加一个回合 + } +} + +//扫雷程序 +void mode_procedure ( int size , int bomb ) +{ + int integral = 0;//判断下了多少次 + int coordinate_x = 0;//为储存横坐标申请内存空间 + int coordinate_y = 0;//为储存纵坐标申请内存空间 + char result_map[size][size];//为结果界面申请内存空间 + char appear_map[size][size];//为显示界面申请内存空间 + char copy_appear_map[size][size];//为显示界面的复制申请内存空间 + initialization_appear_map ( size , appear_map );//初始化显示界面 + initialization_result_map ( size , bomb , result_map );//初始化结果界面 + copy_array ( size , copy_appear_map , appear_map );//复制显示数组 + copy_appear_map[ coordinate_y ][ coordinate_x ] = '@';//设置备份数组第一次显示 + while ( 1 ) + { + if ( integral == size * size - bomb ) + { + while ( getchar ( ) != '\n' );//清除缓存 + printf ( "——扫雷成功———" ); + printf ( " ***** \n" ); + printf ( " * * \n" ); + printf ( "* O O *\n" ); + printf ( "* ∇ *\n" ); + printf ( " * \\___/ \n" ); + printf ( " ***** \n" ); + home_procedure ( 1 );//返回主页 + } + char pick;//初始化用户的选择 + print_interface ( size , copy_appear_map );//打印当前界面 + printf ( "输入W,S,A,D进行上下左右移动\n" ); + printf ( "输入Q确认选择,输入E插旗\n" ); + printf ( ":" ); + while ( getchar ( ) != '\n' );//清除缓存 + scanf ( "%c" , & pick );//获取用户的选择 + if ( pick == 'W' || pick == 'w' )//判断向上 + { + if ( coordinate_y <= 0 ) + { + coordinate_y = 0; + } + else + { + copy_appear_map[ coordinate_y ][ coordinate_x ] = appear_map[ coordinate_y ][ coordinate_x ]; + coordinate_y -= 1; + copy_appear_map[ coordinate_y ][ coordinate_x ] = '@'; + } + } + if ( pick == 'S' || pick == 's' )//判断向下 + { + if ( coordinate_y >= size - 1 ) + { + coordinate_y = size - 1; + } + else + { + copy_appear_map[ coordinate_y ][ coordinate_x ] = appear_map[ coordinate_y ][ coordinate_x ]; + coordinate_y += 1; + copy_appear_map[ coordinate_y ][ coordinate_x ] = '@'; + } + } + if ( pick == 'A' || pick == 'a' )//判断向左 + { + if ( coordinate_x <= 0 ) + { + coordinate_x = 0; + } + else + { + copy_appear_map[ coordinate_y ][ coordinate_x ] = appear_map[ coordinate_y ][ coordinate_x ]; + coordinate_x -= 1; + copy_appear_map[ coordinate_y ][ coordinate_x ] = '@'; + } + } + if ( pick == 'D' || pick == 'd' )//判断向右 + { + if ( coordinate_x >= size - 1 ) + { + coordinate_x = size - 1; + } + else + { + copy_appear_map[ coordinate_y ][ coordinate_x ] = appear_map[ coordinate_y ][ coordinate_x ]; + coordinate_x += 1; + copy_appear_map[ coordinate_y ][ coordinate_x ] = '@'; + } + } + if ( pick == 'Q' || pick == 'q' )//判断确定 + { + integral = replace_appear_map ( integral , size , coordinate_y , coordinate_x , appear_map , + copy_appear_map , result_map ); + } + if ( pick == 'E' || pick == 'e' )//判断插旗 + { + if ( appear_map[ coordinate_y ][ coordinate_x ] == '&' ) + { + appear_map[ coordinate_y ][ coordinate_x ] = '#'; + copy_appear_map[ coordinate_y ][ coordinate_x ] = '#'; + } + else if ( appear_map[ coordinate_y ][ coordinate_x ] == '#' ) + { + appear_map[ coordinate_y ][ coordinate_x ] = '&'; + copy_appear_map[ coordinate_y ][ coordinate_x ] = '&'; + } + } + } +} + +//选择难度 +void difficulty_procedure ( int length , int mode ) +{ + char pick;//接收用户的选择 + char mode_a[] = "简单模式 \n"; + char mode_b[] = "困难模式 \n"; + char mode_c[] = "地狱模式 \n"; + char mode_n[] = "返回上级 \n"; + char mode_e[] = "简答模式←\n"; + char mode_f[] = "困难模式←\n"; + char mode_g[] = "地狱模式←\n"; + char mode_m[] = "返回上级←\n"; + printf ( "——模式选择——\n" ); + switch ( mode ) + { + case 1: + { + printf ( "%s" , mode_e ); + printf ( "%s" , mode_b ); + printf ( "%s" , mode_c ); + printf ( "%s" , mode_n ); + break; + } + case 2: + { + printf ( "%s" , mode_a ); + printf ( "%s" , mode_f ); + printf ( "%s" , mode_c ); + printf ( "%s" , mode_n ); + break; + } + case 3: + { + printf ( "%s" , mode_a ); + printf ( "%s" , mode_b ); + printf ( "%s" , mode_g ); + printf ( "%s" , mode_n ); + break; + } + case 4: + { + printf ( "%s" , mode_a ); + printf ( "%s" , mode_b ); + printf ( "%s" , mode_n ); + printf ( "%s" , mode_m ); + break; + } + } + printf ( "输入W,S进行上下移动\n" ); + printf ( "输入Q确认\n" ); + printf ( ":" ); + while ( getchar ( ) != '\n' );//清除缓存 + scanf ( "%c" , & pick ); + if ( pick == 'q' || pick == 'Q' ) + { + switch ( mode ) + { + case 1: + { + mode_procedure ( length , length * length / 5 ); + break; + } + case 2: + { + mode_procedure ( length , length * length / 3 ); + break; + } + case 3: + { + mode_procedure ( length , length * length / 2 ); + break; + } + case 4: + { + home_procedure ( 1 ); + break; + } + } + } + else if ( pick == 'W' || pick == 'w' ) + { + mode -= 1; + if ( mode <= 1 ) + { + mode = 1; + } + difficulty_procedure ( length , mode );//返回主页 + } + else if ( pick == 'S' || pick == 's' ) + { + mode += 1; + if ( mode >= 4 ) + { + mode = 4; + } + difficulty_procedure ( length , mode );//返回主页 + } + else + { + difficulty_procedure ( length , mode );//返回主页 + } +} + +//主页 +void home_procedure ( int figure ) +{ + char pick;//接收用户的选择 + char mode_a[] = "初级模式 \n"; + char mode_b[] = "中级模式 \n"; + char mode_c[] = "高级模式 \n"; + char mode_n[] = "退出游戏 \n"; + char mode_e[] = "初级模式←\n"; + char mode_f[] = "中级模式←\n"; + char mode_g[] = "高级模式←\n"; + char mode_m[] = "退出游戏←\n"; + printf ( "——扫雷——\n" ); + switch ( figure ) + { + case 1: + { + printf ( "%s" , mode_e ); + printf ( "%s" , mode_b ); + printf ( "%s" , mode_c ); + printf ( "%s" , mode_n ); + break; + } + case 2: + { + printf ( "%s" , mode_a ); + printf ( "%s" , mode_f ); + printf ( "%s" , mode_c ); + printf ( "%s" , mode_n ); + break; + } + case 3: + { + printf ( "%s" , mode_a ); + printf ( "%s" , mode_b ); + printf ( "%s" , mode_g ); + printf ( "%s" , mode_n ); + break; + } + case 4: + { + printf ( "%s" , mode_a ); + printf ( "%s" , mode_b ); + printf ( "%s" , mode_c ); + printf ( "%s" , mode_m ); + break; + } + } + printf ( "输入W,S进行上下移动\n" ); + printf ( "输入Q进入\n" ); + printf ( ":" ); + scanf ( "%c" , & pick ); + if ( pick == 'q' || pick == 'Q' ) + { + switch ( figure ) + { + case 1: + { + difficulty_procedure ( 9 , 1 ); + } + case 2: + { + difficulty_procedure ( 12 , 1 ); + } + case 3: + { + difficulty_procedure ( 15 , 1 ); + } + case 4: + { + printf ( "欢迎下次使用\n" ); + exit ( 0 ); + } + } + } + else if ( pick == 'W' || pick == 'w' ) + { + figure -= 1; + if ( figure <= 1 ) + { + figure = 1; + } + home_procedure ( figure );//返回主页 + } + else if ( pick == 'S' || pick == 's' ) + { + figure += 1; + if ( figure >= 4 ) + { + figure = 4; + } + home_procedure ( figure );//返回主页 + } + else + { + home_procedure ( figure ); + } +} + +//主函数 +int main () +{ + srand ( ( unsigned ) time ( NULL ) );//将当前时间戳设置为随机种子 + home_procedure ( 1 );//进入主页 + return 0; +} diff --git a/practice_code/idea/multiplication_table/multiplication_table_1.c b/practice_code/idea/multiplication_table/multiplication_table_1.c new file mode 100644 index 0000000..0dc1016 --- /dev/null +++ b/practice_code/idea/multiplication_table/multiplication_table_1.c @@ -0,0 +1,24 @@ +#include + +int main () +{ + int figure_1 = 1; + int figure_2 = 1; + int result; + while ( figure_1 <= 9 ) + { + for ( figure_2 = 1 ; figure_2 <= figure_1 ; figure_2 ++ ) + { + result = figure_1 * figure_2; + printf ( "%d*%d=%d " , figure_1 , figure_2 , result ); + if ( figure_1 == figure_2 ) + { + printf ( "\n" ); + ++ figure_1; + break; + } + } + + } + return 0; +} \ No newline at end of file diff --git a/practice_code/idea/multiplication_table/multiplication_table_2.c b/practice_code/idea/multiplication_table/multiplication_table_2.c new file mode 100644 index 0000000..40e8db3 --- /dev/null +++ b/practice_code/idea/multiplication_table/multiplication_table_2.c @@ -0,0 +1,32 @@ +#include + +void cfd(int a,int b,int c) +{ + if(a<=b) + { + printf ("%d*%d=%d ",a,b,a*b); + ++a; + cfd(a,b,c); + } + else if(b < c) + { + a=1; + printf ("\n"); + ++b; + cfd (a,b,c); + } + else if(b==c) + { + return; + } + + +} + +int main() +{ + int a ; + printf ("请输入:"); + scanf ("%d",&a); + cfd (1,1,a); +} \ No newline at end of file diff --git a/practice_code/idea/shutdown.c b/practice_code/idea/shutdown.c new file mode 100644 index 0000000..f88f439 --- /dev/null +++ b/practice_code/idea/shutdown.c @@ -0,0 +1,70 @@ +#include +#include + +int main() +{ + //初始化 + unsigned int time_down; //关机时间 + int pick_home;//主页选择 + char shutdown_commend[100];//初始化储存关机字符 + + printf ("0.取消关机 \n"); + printf ("1.强制关机 \n"); + printf ("2.立即注销 \n"); + printf ("3.定时关机 \n"); + printf ("4.定时重启 \n"); + printf ("请输入你需要执行的操作:"); + scanf ("%d",&pick_home); + switch ( pick_home ) + { + case 0: + { + system ("shutdown -a"); + break; + } + case 1: + { + system ("shutdown -p"); + break; + } + case 2: + { + system ("shutdown -l"); + break; + } + case 3: + { + pick_down: + printf ("请输入你需要多少分钟之后关机:"); + while ( scanf ("%u",&time_down) != 1 || time_down < 0 ) + { + printf ( "请输入合法的数字 \n" ); + while ( getchar ( ) != '\n' ); //清楚缓存 + goto pick_down; //重新开始 + } + + time_down = time_down * 60; //默认秒换分钟 + sprintf (shutdown_commend,"shutdown -s -t %u",time_down); + system (shutdown_commend); + break; + } + + case 4: + { + pick_reboot: + printf ("请输入你需要多少分钟之后重启:"); + while ( scanf ("%u",&time_down) != 1 || time_down < 0 ) + { + printf ( "请输入合法的数字 \n" ); + while ( getchar ( ) != '\n' ); //清楚缓存 + goto pick_reboot; //重新开始 + } + + time_down = time_down * 60; //默认秒换分钟 + sprintf (shutdown_commend,"shutdown -r -t %u",time_down); + system (shutdown_commend); + break; + } + } + return 0; +} diff --git a/practice_code/idea/start yuansheng.c b/practice_code/idea/start yuansheng.c new file mode 100644 index 0000000..b9934d2 --- /dev/null +++ b/practice_code/idea/start yuansheng.c @@ -0,0 +1,50 @@ +#include +#include +#include + +int main () +{ + //生成随机数字 + srand ( ( unsigned int ) time ( NULL ) ); //设置睡觉函数预定值 + int figure_1 = rand ( ) % 100; //接收随机数 + + //初始化 + int figure_2; //用户输入的数字 + int number_times = 2; //用户输入的次数 + printf ( "请输入%d开启原神\n" , figure_1 ); + printf ( "请输入:" ); + while ( scanf ( "%d" , & figure_2 ) ) + { + if ( figure_1 == figure_2 ) + { + printf ( "智力正常,原神启动\n" ); + system ( "start https://genshin.hoyoverse.com" ); + return 1; + } + if ( number_times == 0 ) + { + printf ( "智力缺陷关闭电脑" ); + system ( "shutdown -s" ); + return 1; + } + + if ( figure_1 < figure_2 ) + { + printf ( "还剩%d次机会" , number_times ); + printf ( "数字大了请重新输入:" ); + while ( getchar ( ) != '\n' ); //清除缓存 + -- number_times; //减少次数 + continue; + } + + if ( figure_1 > figure_2 ) + { + printf ( "还剩%d次机会" , number_times ); + printf ( "数字小了请重新输入:" ); + while ( getchar ( ) != '\n' ); //清除缓存 + -- number_times; //减少次数 + continue; + } + + } +}