Final
#include <bits/stdc++.h>
#include <conio.h>
#include "game.h"
using namespace std;
#define pii pair<int,int>
#define F first
#define S second
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define function_key 224
#define CPS (double)(CLOCKS_PER_SEC)
pii body[3005] , dot;
int dx[] = {0 , 0 , -1 , 1} , dy[] = {-1 , 1 , 0 , 0};
int die , len , way; // 0 -> up 1 -> down 2 -> left 3 -> right
clock_t time1 , time2;
struct str{
int Len , Time;
string name;
}S[7];
bool cmp(str a , str b){
if(a.Len == b.Len) return a.Time < b.Time;
else return a.Len > b.Len;
}
void init();
void gmstart();
int gminit();
void playinit();
void draw();
void drawsnake();
void drawinit();
void drawdot();
void play();
void mvsnake();
void isdie();
void hard();
void drawinfor();
void endgm();
void sortRK();
void RK();
int main(){
init();
while(1){
gmstart();
if(gminit() == 0) break;
}
return 0;
}
void init(){
srand(time(NULL));
SetCursorVisible(0 , 25);
}
void gmstart(){
system("CLS");
SetColor(3 , 0);
gotoxy(27 , 3);
cout << "Press S to start the game";
gotoxy(27 , 4);
cout << "Press E to end the game";
gotoxy(27 , 5);
cout << "Press R to check the rank";
}
int gminit(){
time1 = clock();
char ch = getch();
ch = tolower(ch);
if(ch == 'e') return 0;
if(ch == 's'){
playinit();
}
if(ch == 'r') RK();
return 1;
}
void playinit(){
system("CLS");
way = 3;
die = 0;
len = 4;
draw();
while(!die) play();
endgm();
}
void draw(){
string s;
fstream file;
gotoxy(0 , 0);
file.open("snakemap.txt" , ios::in);
while(file >> s){
int len = s.length();
for(int i = 0; i < len; i++){
if(s[i] == '1'){
SetColor(7 , 2);
cout << " ";
}
else{
SetColor(0 , 0);
cout << " ";
}
}
cout << endl;
}
drawsnake();
drawdot();
}
void drawsnake(){
gotoxy(3 , 10);
SetColor(5 , 0);
cout << "*****O";
for(int i = 0; i < 5; i++){
body[i].F = 6 - i;
body[i].S = 10;
}
}
void drawdot(){
SetColor(11 , 0);
dot.F = rand() % 73 + 4;
dot.S = rand() % 24 + 1;
for(int i = 0; i < len; i++)
while(dot.F == body[i].F && dot.S == body[i].S){
dot.F = rand() % 73 + 4;
dot.S = rand() % 15 + 4;
}
gotoxy(dot.F , dot.S);
cout << "#";
}
void play(){
if(kbhit()){
int ch = getch();
if(ch == 224){
int chh = getch();
switch(chh){
case UP: if(way != 1) way = 0; break;
case DOWN: if(way != 0) way = 1; break;
case LEFT: if(way != 3) way = 2; break;
case RIGHT: if(way != 2) way = 3; break;
}
}
}
mvsnake();
isdie();
hard();
drawinfor();
}
void mvsnake(){
SetColor(13 , 0);
for(int i = ++len; i >= 1 ; i--)
body[i] = body[i - 1];
body[0].F += dx[way];
body[0].S += dy[way];
gotoxy(body[0].F , body[0].S); cout << "O";
gotoxy(body[1].F , body[1].S); cout << "X";
if(body[0].F != dot.F || body[0].S != dot.S){
len--;
gotoxy(body[len].F , body[len].S);
cout << " ";
}
else drawdot();
}
void isdie(){
for(int i = 1; i <= len; i++)
if(body[0].F == body[i].F && body[0].S == body[i].S) die = 1;
if(body[0].F < 2 || body[0].F > 77) die = 1;
if(body[0].S < 1 || body[0].S > 24) die = 1;
}
void hard(){
int sleep = 220;
for(int i = 0; i < len; i += 3)
sleep -= 10;
if(way > 1) sleep -= 20;
sleep = max(12 , sleep);
Sleep(sleep);
}
void drawinfor(){
SetColor(15 , 0);
gotoxy(81 , 24);
cout << " LEN : " << len << endl;
gotoxy(81 , 25);
time2 = clock();
cout << "TIME : " << (time2 - time1) / CPS;
}
void endgm(){
system("CLS");
gotoxy(25 , 10);
SetColor(5 , 0);
cout << "Name : ";
cin >> S[0].name;
S[0].Len = len;
S[0].Time = (time2 - time1) / CPS;
sortRK();
}
void sortRK(){
fstream file , file2;
file.open("snakeRK.txt" , ios::in);
for(int i = 1; i <= 5; i++)
file >> S[i].name >> S[i].Len >> S[0].Time;
sort(S , S + 6 , cmp);
file2.open("snakeRK.txt" , ios::out | ios::trunc);
for(int i = 0; i < 5; i++)
file2 << S[i].name << S[i].Len << S[i].Time;
}
void RK(){
system("CLS");
SetColor(15 , 0);
fstream file;
file.open("snakeRK.txt" , ios::in);
gotoxy(25 , 2);
cout << " name" << " " << "length" << " " << "time";
for(int k = 0; k < 5; k++){
string s1 , s2 , s3;
file >> s1 >> s2 >> s3;
cout << "#" << k + 1 << " ";
for(int i = 0; i < 15 - s1.length(); i++) cout << " ";
cout << s1 << " ";
for(int i = 0; i < 3 - s2.length(); i++) cout << " ";
cout << s2 << " ";
for(int i = 0; i < 4 - s3.length(); i++) cout << " ";
cout << s3;
}
}