⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client_interface_linux.hpp

📁 本程序是主要是扫雷
💻 HPP
字号:
/*     Copyright(c) Ben Bear 2003-2004  */

//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License as
//  published by the Free Software Foundation; either version 2 of the
//  License, or (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
//  02111-1307, USA.

#ifndef __client_interface_win_hpp
#define __client_interface_win_hpp

#include <cstdio>
#include <cstring>

#include "info.hpp"
#include "honey_hole.hpp"

#include <curses.h>

const int COLOR_TEXT = 7;	// for print text

const int COLOR_NONE = 7;
const int COLOR_PLAYER[6] = {1, 2, 3, 4, 5, 6};
const int COLOR_HELP = 7;//8;
const int COLOR_JUMP = 7;//9;
const int COLOR_INSIDE = 10;

const char SIGN_NONE = '.';
const char SIGN_PLAYER = 'o';
const char SIGN_HELP = 'e';
const char SIGN_JUMP = '*';
const char SIGN_INSIDE = SIGN_NONE;

const int X_CENTER = 40;
const int Y_CENTER = 12;

class client_interface
{
protected:

public:
  client_interface ();
  ~client_interface ();

  bool move (int y, int x);

  bool kbhit ();

  int command ();

  void text_color (int color);
  void show_chess (int h, int ch);
  void show_name (int p, int g, const char* name);
  void clear ();
  void out_text (int y, int x, const char* str);
};

client_interface::client_interface ()
{
}

client_interface::~client_interface ()
{
}

bool
client_interface::move (int y, int x)
{
  ::move (y, x);
}

bool
client_interface::kbhit ()
{
}

int
client_interface::command ()
{
  int ch = getch();

  switch (ch)
    {
    case KEY_END:
    case '1': ch = KB_L_DOWN;   break;
    case KEY_DOWN:
    case '2': ch = KB_DOWN;     break;
    case KEY_NPAGE:
    case '3': ch = KB_R_DOWN;   break;
    case KEY_LEFT:
    case '4': ch = KB_LEFT;     break;
    case KEY_B2:
    case '5': ch = KB_MODE;     break;
    case KEY_RIGHT:
    case '6': ch = KB_RIGHT;    break;
    case KEY_HOME:
    case '7': ch = KB_L_UP;     break;
    case KEY_UP:
    case '8': ch = KB_UP;       break;
    case KEY_PPAGE:
    case '9': ch = KB_R_UP;     break;
    case 'O':
    case 'o': ch = KB_FINISH;   break;
    case 'Q':
    case 'q': ch = KB_QUIT;     break;
    default:  ch += 0xFF00;     break;
    }
  return ch;
}

void
client_interface::text_color (int color)
{
  attron (COLOR_PAIR(color));
}

void
client_interface::show_chess (int h, int ch)
{
  int x, y;
  honey_hole hole (h);
  hole.get (x, y);
  int c;
  char s;
  if (ch == CHESS_NONE)
    {
      c = COLOR_NONE;
      s = SIGN_NONE;
    }
  else if ((ch >= 0) && (ch < 6))
    {
      c = COLOR_PLAYER[ch];
      s = SIGN_PLAYER;
    }
  else if (ch == CHESS_HELP)
    {
      c = COLOR_HELP;
      s = SIGN_HELP;
    }
  else if (ch == CHESS_JUMP)
    {
      c = COLOR_JUMP;
      s = SIGN_JUMP;
    }
  else if (ch == CHESS_INSIDE)
    {
      c = COLOR_INSIDE;
      s = SIGN_INSIDE;
    }
  text_color (c);
  move (Y_CENTER + y, X_CENTER + x * 2 - y);
  printw ("%c", s);
  refresh ();
}

void
client_interface::show_name (int p, int g, const char* name)
{
  int x, y, c;
  c = std::strlen (name)+2;
  if (p == 0)
    {
      x = 65;
      y = 18;
      c = x;
    }
  else if (p == 1)
    {
      x = 35;
      y = 23;
      c = X_CENTER - c / 2;
    }
  else if (p == 2)
    {
      x = 5;
      y = 18;
      c = PLAYER_NAME_LEN + x - c;
    }
  else if (p == 3)
    {
      x = 5;
      y = 5;
      c = PLAYER_NAME_LEN + x - c;
    }
  else if (p == 4)
    {
      x = 65;
      y = 1;
      c = X_CENTER - c / 2;
    }
  else if (p == 5)
    {
      x = 65;
      y = 5;
      c = x;
    }
  move (y, x);
  printw ("          ");
  text_color (COLOR_PLAYER[g]);
  move (y, c);
  printw ("%d:%s", g, name);
  refresh ();
}

void
client_interface::clear ()
{
  int col, row;
  char tmp[8];
  getmaxyx (stdscr, row, col);
  ::sprintf (tmp, "%%%ds", col);
  for (int i = 0; i < row; ++i)
    mvprintw (i, 0, tmp, "");
}

void
client_interface::out_text (int y, int x, const char* str)
{
  text_color (COLOR_TEXT);
  move (y, x);
  printw ("%s", str);
  refresh ();
}

class _init_interface
{
public:
  _init_interface ()
  {
    initscr ();
    start_color ();
    cbreak ();
    keypad (stdscr, TRUE);
    noecho ();
    
    // init color
    init_pair (1, COLOR_RED, COLOR_BLACK);      // PLAYER[0]
    init_pair (2, COLOR_GREEN, COLOR_BLACK);    // 1
    init_pair (3, COLOR_YELLOW, COLOR_BLACK);   // 2
    init_pair (4, COLOR_BLUE, COLOR_BLACK);     // 3
    init_pair (5, COLOR_MAGENTA, COLOR_BLACK);  // 4
    init_pair (6, COLOR_CYAN, COLOR_BLACK);     // PLAYER[5]
    init_pair (7, COLOR_WHITE, COLOR_BLACK);    // CHESS_NONE
    init_pair (8, COLOR_MAGENTA, COLOR_WHITE);  // CHESS_HELP
    init_pair (9, COLOR_WHITE, COLOR_BLUE);     // CHESS_JUMP
    init_pair (10, COLOR_WHITE, COLOR_BLACK);   // CHESS_INSIDE

    move (0, 0);
  }
  ~_init_interface ()
  {
    endwin ();
  }
};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -