-
个人简介
天佑吾皇,常胜利,沐荣光! 菜鸡一枚!! 如果你敢当My son,我就不介意成为Your father!!! 《关于我用个十六中账号冲到LV10这件事》 命运,永远站在我这边!
//最完整版头文件 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include <complex.h> #include <fenv.h> #include <inttypes.h> #include <stdbool.h> #include <stdint.h> #include <tgmath.h> #include <windows.h>
/*贪吃蛇/ /*2012-11-20/
#include #include #include #include #include <conio.h> #include #include <windows.h> using namespace std;
/*** 光标定位 ***/ HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE); COORD coord;
void locate(int x,int y) { coord.X=y; coord.Y=x; SetConsoleCursorPosition(hout,coord); };
/*** 隐藏光标 ***/ void hide() { CONSOLE_CURSOR_INFO cursor_info={1,0}; SetConsoleCursorInfo(hout, &cursor_info); }
/*** 生成随机数 ***/ double random(double start, double end) { return start+(end-start)*rand()/(RAND_MAX + 1.0); }
/*** 定义地图的长宽,蛇的坐标,长度,方向,食物的位置 ***/ int m,n;
struct node { int x,y; }snake[1000];
int snake_length,dir; node food; int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
/*** 输出墙 ***/ void print_wall() { cout << " "; for (int i=1;i<=n;i++) cout << "-"; cout << endl; for (int j=0;j<=m-1;j++) { cout << "|"; for (int i=1;i<=n;i++) cout << " "; cout << "|" << endl; } cout << " "; for (int i=1;i<=n;i++) cout << "-"; }
/*** 首次输出蛇,其中snake[0]代表头 **/ void print_snake() { locate(snake[0].x,snake[0].y); cout << "@"; for (int i=1;i<=snake_length-1;i++) { locate(snake[i].x,snake[i].y); cout << ""; } }
/*** 判断是否撞墙或者自撞 ***/ bool is_correct() { if (snake[0].x0 || snake[0].y0 || snake[0].xm+1 || snake[0].yn+1) return false; for (int i=1;i<=snake_length-1;i++) { if (snake[0].xsnake[i].x && snake[0].ysnake[i].y) return false; } return true; }
/*** 随机生成并输出食物位置 ***/ bool print_food() { srand((unsigned)time(0)); bool e; while (1) { e=true; int i=(int) random(0,m)+1,j=(int) random(0,n)+1; food.x=i;food.y=j; for (int k=0;k<=snake_length-1;k++) { if (snake[k].xfood.x && snake[k].yfood.y) { e=false;break; } } if (e) break; } locate(food.x,food.y); cout << "$"; return true; }
/*** 蛇的前进 / bool go_ahead() { node temp; bool e=false; temp=snake[snake_length-1]; for (int i=snake_length-1;i>=1;i--) snake[i]=snake[i-1]; snake[0].x+=direct[dir][0]; snake[0].y+=direct[dir][1]; locate(snake[1].x,snake[1].y); cout << ""; /* 吃到了食物 / if (snake[0].xfood.x && snake[0].yfood.y) { snake_length++; e=true; snake[snake_length-1]=temp; } / 输出此时蛇状态 / if (!e) { locate(temp.x,temp.y); cout << " "; } else print_food(); locate(snake[0].x,snake[0].y); cout << "@"; / 如果自撞 ***/ if (!is_correct()) { system("cls"); cout << "You lose!" << endl << "Length: " << snake_length << endl; return false; } return true; }
/*** 主函数 / int main() { cout << "--------------------贪吃蛇---------------------" << endl; cout << "请注意窗口大小,以免发生错位.建议将窗口调为最大." << endl; cout << "先选择难度.请在1-10中输入1个数,1最简单,10则最难" << endl; cout << "然后进入游戏画面,以方向键控制方向.祝你游戏愉快!" << endl; cout << "-----------------------------------------------" << endl; m=25; n=40; if (m<10 || n<10 || m>25 || n>40) { cout << "ERROR" << endl; system("pause"); return 0; } int hard; cin >> hard; if (hard<=0 || hard>100) { cout << "ERROR" << endl; system("pause"); return 0; } / 数据全部初始化,包括蛇长,位置,方向 / snake_length=5; clock_t a,b; char ch; double hard_len; for (int i=0;i<=4;i++) { snake[i].x=1; snake[i].y=5-i; } dir=3; / 输出初始地图,蛇与食物 / system("cls"); hide(); print_wall(); print_food(); print_snake(); locate(m+2,0); cout << "Now length: "; / 开始游戏 / while (1) { / 难度随长度增加而提高 / hard_len=(double)snake_length/(double) (mn); /* 调节时间,单位是ms / a=clock(); while (1) { b=clock(); if (b-a>=(int)(400-30hard)(1-sqrt(hard_len))) break; } /** 接受键盘输入的上下左右,并以此改变方向 / if (kbhit()) { ch=getch(); if (ch==-32) { ch=getch(); switch(ch) { case 72: if (dir2 || dir3) dir=0; break; case 80: if (dir2 || dir3) dir=1; break; case 75: if (dir0 || dir1) dir=2; break; case 77: if (dir0 || dir1) dir=3; break; } } } / 前进 / if (!go_ahead()) break; / 在最后输出此时长度 ***/ locate(m+2,12); cout << snake_length; } system("pause"); return 0; }
-
通过的题目
- 1
- 2
- 3
- 5
- 6
- 7
- 8
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 41
- 43
- 45
- 46
- 47
- 48
- 49
- 50
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 63
- 65
- 66
- 67
- 68
- 69
- 70
- 72
- 73
- 75
- 76
- 77
- 78
- 79
- 80
- 83
- 84
- 85
- 86
- 87
- 88
- 90
- 91
- 92
- 93
- 94
- 95
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 121
- 122
- 125
- 130
- 132
- 137
- 142
- 143
- 144
- 164
- 168
- 173
- 182
- 183
- 186
- 188
- 193
- 194
- 195
- 196
- 197
- 198
- 213
- 222
- 241
- 250
- 251
- 297
- 312
- 323
- 364
- 378
- 407
- 468
- 475
- 477
- 502
- 518
- 545
- 547
- 554
- 561
- 569
- 577
- 584
- 585
- 589
- 596
- 602
- 641
- 649
- 653
- 657
- 659
- 660
- 661
- 665
- 672
- 676
- 677
- 684
- 691
- 692
- 694
- 696
- 697
- 699
- 701
- 704
- 708
- 711
- 712
- 713
- 714
- 715
- 718
- 724
- 726
- 749
- 754
- 762
- 765
- 769
- 785
- 800
- 811
- 813
- 814
- 815
- 816
- 817
- 818
- 819
- 820
- 821
- 822
- 823
- 824
- 825
- 826
- 827
- 828
- 829
- 830
- 831
- 832
- 833
- 834
- 835
- 836
- 837
- 838
- 839
- 840
- 841
- 842
- 843
- 844
- 845
- 846
- 848
- 849
- 850
- 851
- 852
- 853
- 854
- 855
- 856
- 857
- 858
- 859
- 860
- 861
- 862
- 863
- 864
- 865
- 866
- 867
- 868
- 869
- 870
- 871
- 872
- 873
- 874
- 875
- 876
- 877
- 878
- 879
- 880
- 881
- 882
- 883
- 884
- 885
- 886
- 887
- 888
- 889
- 890
- 891
- 892
- 894
- 895
- 896
- 897
- 898
- 899
- 900
- 901
- 902
- 903
- 904
- 905
- 906
- 907
- 908
- 909
- 910
- 911
- 912
- 913
- 914
- 915
- 916
- 917
- 918
- 919
- 920
- 921
- 922
- 923
- 924
- 925
- 926
- 927
- 928
- 929
- 930
- 931
- 932
- 933
- 934
- 935
- 936
- 937
- 938
- 939
- 940
- 941
- 942
- 943
- 944
- 946
- 947
- 948
- 949
- 951
- 952
- 953
- 954
- 955
- 956
- 957
- 958
- 959
- 960
- 961
- 962
- 963
- 964
- 967
- 970
- 972
- 974
- 975
- 977
- 978
- 980
- 981
- 982
- 983
- 984
- 985
- 986
- 987
- 988
- 989
- 990
- 991
- 993
- 994
- 995
- 996
- 997
- 998
- 999
- 1000
- 1001
- 1002
- 1003
- 1004
- 1006
- 1007
- 1008
- 1012
- 1014
- 1015
- 1016
- 1017
- 1018
- 1019
- 1020
- 1021
- 1023
- 1024
- 1025
- 1026
- 1027
- 1028
- 1030
- 1031
- 1034
- 1035
- 1036
- 1038
- 1040
- 1042
- 1043
- 1044
- 1048
- 1049
- 1052
- 1055
- 1057
- 1058
- 1062
- 1063
- 1064
- 1068
- 1071
- 1073
- 1075
- 1078
- 1079
- 1080
- 1084
- 1085
- 1088
- 1089
- 1090
- 1091
- 1092
- 1093
- 1094
- 1095
- 1098
- 1099
- 1102
- 1104
- 1107
- 1110
- 1114
- 1115
- 1118
- 1120
- 1128
- 1132
- 1134
- 1135
- 1138
- 1140
- 1153
- 1162
- 1163
- 1166
- 1178
- 1182
- 1184
- 1185
- 1186
- 1187
- 1188
- 1189
- 1190
- 1191
- 1193
- 1194
- 1195
- 1196
- 1197
- 1198
- 1200
- 1201
- 1202
- 1218
- 1219
- 1222
- 1223
- 1224
- 1225
- 1230
- 1237
- 1238
- 1239
- 1242
- 1243
- 1259
- 1263
- 1264
- 1280
- 1282
- 1283
- 1284
- 1285
- 1295
- 1296
- 1300
- 1304
- 1325
- 1326
- 1328
- 1341
- 1345
- 1352
- 1353
- 1355
- 1357
- 1358
- 1359
- 1363
- 1364
- 1365
- 1366
- 1367
- 1368
- 1373
- 1374
- 1376
- 1378
- 1382
- 1384
- 1388
- 1389
- 1390
- 1393
- 1399
- 1401
- 1402
- 1404
- 1405
- 1407
- 1408
- 1412
- 1429
- 1433
- 1483
- 1489
- 1490
- 1501
- 1502
- 1505
- 1509
- 1538
- 1551
- 1553
- 1595
- 1639
- 1675
- 1676
- 1677
- 1684
- 1701
- 1763
- 1828
- 1848
- 1929
- 1954
- 1966
- 2000
- 2275
- 2303
- 2474
- 2554
- 2555
- 2583
- 2585
- 2607
- 2627
- 2670
- 2720
- 2721
- 2722
- 2723
- 2800
- 2910
- 2948
- 2953
- 3007
- 3022
- 3047
- 3070
- 3074
- 3086
- 3088
- 3130
- 3142
- P0168
- 3172
- 3173
- 3181
- 3191
- P3199
- 3219
- 3222
-
最近活动
- 红盾寒假下午班test IOI
- 省秀南山班寒假练习 作业
- 十六中寒假练习 作业
- 红盾周六上午班test IOI
- 少年宫周六晚上中级班寒假作业【吴飞】 作业
- 少年宫中级C3班期末综合测试 IOI
- 省秀初一南山班test8(20241227) 作业
- 少年宫周三下午五点班(20241225)【陈潮雄】 作业
- 少年宫周六晚上中级班(20241221)【吴飞】 作业
- 少年宫周日上午八点班(20241222)【陈潮雄】 作业
- C++初级A1班06-字符串2 IOI
- 少年宫周六晚上中级班(20241214)【吴飞】 作业
- 少年宫周日上午八点班(20241215)【陈潮雄】 作业
- 少年宫周日上午十点班(20241215)【陈潮雄】 作业
- 少年宫周三下午七点班(20241211)【陈潮雄】 作业
- 少年宫周六晚上中级班(20241207)【吴飞】 作业
- C++初级C2C3班06-循环结构2 IOI
- 少年宫周三下午五点班(20241204)【陈潮雄】 作业
- 少年宫周三下午七点班(20241204)【陈潮雄】 作业
- 少年宫周六晚上中级班(20241123)【吴飞】 作业
- 省秀初一南山班test6(20241213) 作业
- 少年宫周三下午七点班(20241127)【陈潮雄】 作业
- 少年宫周六晚上中级班(20241123)【吴飞】 作业
- 红盾周六上午班test IOI
- 省秀初一南山班test4(20241122) 作业
- 少年宫周三下午七点班(20241120)【陈潮雄】 作业
- 少年宫周日晚上班(20241215)【张正标】 作业
- 少年宫周六晚上中级班(20241116)【吴飞】 作业
- 少年宫周六晚上中级班(20241109)【吴飞】 作业
- 少年宫周六晚上中级班(20241102)【吴飞】 作业
- 少年宫周六晚上中级班(20241026)【吴飞】 作业
- 少年宫周六晚上中级班(20241019)【吴飞】 作业
- 红盾周六上午班test 作业
- 少年宫周六晚上中级班(20240928)【吴飞】 作业
- 红盾周六上午班test1 作业
- 红盾周六上午班测试 IOI
- 少年宫周六晚上中级班(20240921)【吴飞】 作业
- 红盾零基础 作业
- DAY5 IOI
- 零基础比赛 IOI
-
最近编写的题解
题目标签
- 语言基础
- 138
- 循环语句
- 91
- 竞赛
- 62
- NOIP
- 48
- 搜索
- 47
- 字符串
- 45
- 动态规划
- 44
- 选择语句
- 41
- 其他
- 39
- 一维数组
- 39
- 语言入门
- 35
- 字符数组
- 31
- 贪心
- 30
- 高精度
- 25
- 年份
- 25
- python
- 23
- 数学知识
- 22
- 二维数组
- 22
- 数据结构
- 21
- 普及组
- 20