Home
Problem
Status
Contest
Workbook
User
Group
Forum
Register
Login
{"managingGroups":{},"author":"zy2008","updateTime":1714627176000,"title":"tcs","dislikeCnt":0,"content":"```\n#include \u003ciostream\u003e\n#include \u003cconio.h\u003e\n#include \u003cwindows.h\u003e\nusing namespace std;\nconst int width \u003d 20;\nconst int height \u003d 20;\nint x, y;\nint fruitX, fruitY;\nint tailX[100], tailY[100];\nint nTail;\nenum eDirection { STOP \u003d 0, LEFT, RIGHT, UP, DOWN };\neDirection dir;\nbool gameOver;\nvoid Setup() {\n\tgameOver \u003d false;\n\tdir \u003d STOP;\n\tx \u003d width / 2;\n\ty \u003d height / 2;\n\tfruitX \u003d rand() % width;\n\tfruitY \u003d rand() % height;\n\tnTail \u003d 0;\n}\nvoid Draw() {\n\tsystem(\"cls\");\n\tfor (int i \u003d 0; i \u003c width + 2; i++)\n\t\tcout \u003c\u003c \"#\";\n\tcout \u003c\u003c endl;\n\tfor (int i \u003d 0; i \u003c height; i++) {\n\t\tfor (int j \u003d 0; j \u003c width; j++) {\n\t\t\tif (j \u003d\u003d 0)\n\t\t\t\tcout \u003c\u003c \"#\";\n\t\t\tif (i \u003d\u003d y \u0026\u0026 j \u003d\u003d x)\n\t\t\t\tcout \u003c\u003c \"O\";\n\t\t\telse if (i \u003d\u003d fruitY \u0026\u0026 j \u003d\u003d fruitX)\n\t\t\t\tcout \u003c\u003c \"F\";\n\t\t\telse {\n\t\t\t\tbool print \u003d false;\n\t\t\t\tfor (int k \u003d 0; k \u003c nTail; k++) {\n\t\t\t\t\tif (tailX[k] \u003d\u003d j \u0026\u0026 tailY[k] \u003d\u003d i) {\n\t\t\t\t\t\tcout \u003c\u003c \"o\";\n\t\t\t\t\t\tprint \u003d true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!print)\n\t\t\t\t\tcout \u003c\u003c \" \";\n\t\t\t}\n\t\t\tif (j \u003d\u003d width - 1)\n\t\t\t\tcout \u003c\u003c \"#\";\n\t\t}\n\t\tcout \u003c\u003c endl;\n\t}\n\tfor (int i \u003d 0; i \u003c width + 2; i++)\n\t\tcout \u003c\u003c \"#\";\n\tcout \u003c\u003c endl;\n}\nvoid Input() {\n\tif (_kbhit()) {\n\t\tswitch (_getch()) {\n\t\t\tcase \u0027a\u0027:\n\t\t\t\tif (dir !\u003d RIGHT)\n\t\t\t\t\tdir \u003d LEFT;\n\t\t\t\tbreak;\n\t\t\tcase \u0027d\u0027:\n\t\t\t\tif (dir !\u003d LEFT)\n\t\t\t\t\tdir \u003d RIGHT;\n\t\t\t\tbreak;\n\t\t\tcase \u0027w\u0027:\n\t\t\t\tif (dir !\u003d DOWN)\n\t\t\t\t\tdir \u003d UP;\n\t\t\t\tbreak;\n\t\t\tcase \u0027s\u0027:\n\t\t\t\tif (dir !\u003d UP)\n\t\t\t\t\tdir \u003d DOWN;\n\t\t\t\tbreak;\n\t\t\tcase \u0027x\u0027:\n\t\t\t\tgameOver \u003d true;\n\t\t\t\tbreak;\n\t\t}\n\t}\n}\nvoid Logic() {\n\tint prevX \u003d tailX[0];\n\tint prevY \u003d tailY[0];\n\tint prev2X, prev2Y;\n\ttailX[0] \u003d x;\n\ttailY[0] \u003d y;\n\tfor (int i \u003d 1; i \u003c nTail; i++) {\n\t\tprev2X \u003d tailX[i];\n\t\tprev2Y \u003d tailY[i];\n\t\ttailX[i] \u003d prevX;\n\t\ttailY[i] \u003d prevY;\n\t\tprevX \u003d prev2X;\n\t\tprevY \u003d prev2Y;\n\t}\n\tswitch (dir) {\n\t\tcase LEFT:\n\t\t\tx--;\n\t\t\tbreak;\n\t\tcase RIGHT:\n\t\t\tx++;\n\t\t\tbreak;\n\t\tcase UP:\n\t\t\ty--;\n\t\t\tbreak;\n\t\tcase DOWN:\n\t\t\ty++;\n\t\t\tbreak;\n\t}\n\tif (x \u003e\u003d width) x \u003d 0;\n\telse if (x \u003c 0) x \u003d width - 1;\n\tif (y \u003e\u003d height) y \u003d 0;\n\telse if (y \u003c 0) y \u003d height - 1;\n\tfor (int i \u003d 0; i \u003c nTail; i++)\n\t\tif (tailX[i] \u003d\u003d x \u0026\u0026 tailY[i] \u003d\u003d y)\n\t\t\tgameOver \u003d true;\n\tif (x \u003d\u003d fruitX \u0026\u0026 y \u003d\u003d fruitY) {\n\t\tnTail++;\n\t\tfruitX \u003d rand() % width;\n\t\tfruitY \u003d rand() % height;\n\t}\n}\nvoid MainMenu() {\n\tsystem(\"cls\");\n\tcout \u003c\u003c \" 贪吃蛇\" \u003c\u003c endl;\n\tcout \u003c\u003c \"按任意键开始游戏...\" \u003c\u003c endl;\n\t_getch();\n}\n\nvoid GameOverMenu() {\n\tcout \u003c\u003c \"Game Over!\" \u003c\u003c endl;\n\tcout \u003c\u003c \"是否再玩一局(y/n)\" \u003c\u003c endl;\n\tchar choice;\n\tcin \u003e\u003e choice;\n\tif (choice \u003d\u003d \u0027y\u0027 || choice \u003d\u003d \u0027Y\u0027) {\n\t\tgameOver \u003d false;\n\t\tnTail \u003d 0;\n\t\tSetup();\n\t} else {\n\t\tgameOver \u003d true;\n\t}\n}\nint main() {\n\tMainMenu();\n\tSetup();\n\twhile (!gameOver) {\n\t\tDraw();\n\t\tInput();\n\t\tLogic();\n\t\tif (gameOver) {\n\t\t\tGameOverMenu();\n\t\t}\n\t\tSleep(100);\n\t}\n\treturn 0;\n}\n```","threadId":190964,"likeCnt":1,"createTime":1714545554000,"isWorkbook":false,"viewCnt":30,"openness":2,"fav":false,"id":4969,"trustable":false}