text: ﻿#include<iostream>
  #include<ctime>
  #include<conio.h>
  #include<windows.h>
  #include<stdlib.h>
  #include"Snake.h"
  #include"Food.h"
  #include"Wall.h"
  #include"Model.h"
  #include<iomanip>
  using namespace std;
  
  enum model 
  {
      easy = 1, normal, hard, model1
  };
  
  void gotoxy(HANDLE hOut, int x, int y) 
  {
      COORD pos;
      pos.X = x;
      pos.Y = y;
      SetConsoleCursorPosition(hOut, pos);
  }
  
  HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  
  int main() 
  {
  start:
      Wall wall;
      wall.initWall();
      int type;
      SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_BLUE);
      cout << "******************************************************************" << endl;
      cout << "*     Greedy Snake Game    *  Directions:                        *" << endl;
      cout << "*        Author:LHY        *        W,A,S,D - Move               *" << endl;
      cout << "*--------------------------*        P - Pause                    *" << endl;
      cout << "*      1-Easy Mode         *        Control the snake to eat \'#\' *"<< endl;
      cout << "*      2-Normal Mode       *        Avoid the poison \'$\'         *" << endl;
      cout << "*      3-Hard Mode         ***************************************" << endl;
      cout << "*      4-Special Mode      *" << endl;
      cout << "*      5-Exit Game         *" << endl;
      cout << "****************************" << endl;
      SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
      cout << "Please input your selection:" << endl;
      cin >> type;
      Model difficulty;
      if (type == easy) 
      {
          difficulty = Easy(300);
      }
      else if (type == normal)
      {
          difficulty = Normal(200);
      }
      else if (type == hard) 
      {
          difficulty = Hard(100);
      }
      else if (type == model1) 
      {
          difficulty = Model1(100);
          for (int i = 9; i < 14; i++) 
          {
              wall.setWall(5, i, '*');
          }
          for (int j = 12; j < 18; j++) 
          {
              wall.setWall(15, j, '*');
          }
      } else if(type == 5) {
          return 0;
      }
      system("cls");
      wall.drawWall();
      srand((unsigned int)time(NULL));
      bool isDead = false;
      char preKey = NULL;
      Food food(wall);
      food.setFood();
      if (difficulty.is_special == true) 
      {
          food.setPoison();
          wall.setWall(food.PoisonX, food.PoisonY, '$');
      }
      system("cls");
      wall.drawWall();
      Snake snake(wall, food);
      snake.InitSnake();
      gotoxy(hOut, 0, Wall::ROW);
      cout << "Score: " << snake.getScore() << endl;
      while (!isDead) 
      {
          char key = _getch();
          if (preKey == NULL && key == snake.LEFT) 
          {
              continue;
          }
          do 
          {
              if (key == snake.UP || key == snake.DOWN || key == snake.LEFT || key == snake.RIGHT) 
              {
                  if ((key == snake.LEFT && preKey == snake.RIGHT) ||
                      (key == snake.RIGHT && preKey == snake.LEFT) ||
                      (key == snake.UP && preKey == snake.DOWN) ||
                      (key == snake.DOWN && preKey == snake.UP))
                  {
                      key = preKey;
                  }
                  else 
                  {
                      preKey = key;
                  }
                  if (snake.move(key, difficulty) == true) 
                  {
                      gotoxy(hOut, 0, Wall::ROW);
                      cout << "Score:" << snake.getScore() << endl;
                      Sleep(difficulty.sleepTime - snake.getSize() > 10 ? (difficulty.sleepTime - snake.getSize()) : 10);
                  }
                  else 
                  {
                      isDead = true;
                      break;
                  }
              }
              else if (key == snake.PAUSE)
              {
                  system("pause");
                  break;
              }
              else 
              {
                  key = preKey;
              }
          } while (!_kbhit());
      }
      //system("pause");
      cout << "Do you want to try again?(Y/N)" << endl;
      char key = _getch();
      if(key == 'y') {
          system("cls");
          goto start;
      }
      return 0;
  }