📄 reversi.cpp
字号:
#include <monapi.h>#include <monapi/messages.h>#include "ReversiBoard.h"/*---------------------------------------------------------------------- Monaアプリケ〖ション----------------------------------------------------------------------*/class myApplication : public MonaApplication, public Observer { public: myApplication(); virtual ~myApplication() {} public: virtual int main(List<char*>* pekoe); virtual void onKeyDown(int keycode, int modifiers); virtual void onMouseClick(int x, int y); virtual void update(Observable* o, void* arg); public: virtual void init(); virtual void drawBoard(); virtual void drawPieces(int x, int y); public: static const int PIECE_SIZE = 40; protected: ReversiBoard* board; Screen* virtualScreen; Screen screen;};/*---------------------------------------------------------------------- Main簇眶----------------------------------------------------------------------*/int MonaMain(List<char*>* pekoe) { return (new myApplication())->main(pekoe);}/*---------------------------------------------------------------------- myApplication----------------------------------------------------------------------*/// コンストラクタmyApplication::myApplication() : MonaApplication() { if (!monapi_register_to_server(ID_MOUSE_SERVER, MONAPI_TRUE)) { printf("Reversi:Mouse regist error\n"); exit(1); }}// mainint myApplication::main(List<char*>* pekoe) { // 介袋借妄 init(); // オセロ饶の介袋觉轮を闪茶 drawBoard(); // イベントを略つ run(); return 0;}// オセロ饶を闪茶void myApplication::drawBoard() { for (int x = 0; x < ReversiBoard::BOARDW; x++) { for (int y = 0; y < ReversiBoard::BOARDH; y++) { drawPieces(x, y); } }}// (x, y)にひとこま闪茶void myApplication::drawPieces(int x, int y) { int piece = this->board->getPiece(x, y); int s = PIECE_SIZE; // 微バッファから悸VRAMに啪流 switch(piece) { case(ReversiBoard::BLACK): Screen::bitblt(&screen, x * s, y * s, s, s, virtualScreen, 0, 0); break; case(ReversiBoard::WHITE): Screen::bitblt(&screen, x * s, y * s, s, s, virtualScreen, s, 0); break; case(ReversiBoard::EMPTY): Screen::bitblt(&screen, x * s, y * s, s, s, virtualScreen, 2 * s, 0); break; default: break; }}// キ〖掐蜗はしないvoid myApplication::onKeyDown(int keycode, int modifiers) {}// マウスクリックイベントvoid myApplication::onMouseClick(int x, int y) { // オセロ饶のどこがクリックされた int bx = (x - x % PIECE_SIZE) / PIECE_SIZE; int by = (y - y % PIECE_SIZE) / PIECE_SIZE; // 认跋チェック if (!board->checkRange(bx, by)) { return; } // 附哼のプレイヤ〖がこまをおく int current = board->getCurrentHand(); if (!(board->setPiece(bx, by, current))) { return; } // ひっくり手されるこまを办绥ずつ闪茶 while (board->existNotReversedPieces()) { board->reverseNext(); } // 肌缄プレイヤ〖を冉们 board->setNextHand();}// オセロ饶の柒推に恃构があったことがこの簇眶に奶梦されるvoid myApplication::update(Observable* o, void* arg) { if (arg == NULL) { drawBoard(); } else { Point* point = (Point*)arg; sleep(100); drawPieces(point->x, point->y); }}void myApplication::init() { // オセロ饶を栏喇 this->board = new ReversiBoard(); // オセロ饶に恃构があったら奶梦してもらう this->board->addObserver(this); // 微バッファ脱に簿鳞スクリ〖ンを澄瘦 virtualScreen = new VirtualScreen(200 * 1024); // 闪茶婶墒として球摧ˇ辊摧ˇ鄂の婶墒を微バッファに闪茶 int s = PIECE_SIZE; int s1 = (int)(0.5 * s); virtualScreen->fillRect16(0, 0, s, s, Color::rgb(0x00, 0xFF, 0x00)); virtualScreen->fillRect16(s , 0, s, s, Color::rgb(0x00, 0xFF, 0x00)); virtualScreen->fillCircle16(s1 * 3, s1, s1 - 4, Color::rgb(0xFF, 0xFF, 0xFF)); virtualScreen->fillCircle16(s1, s1, s1 - 4, Color::rgb(0x00, 0x00, 0x00)); virtualScreen->fillRect16(s * 2, 0, s, s, Color::rgb(0x00, 0xFF, 0x00)); for (int x = 0; x < 3 * s; x++) { virtualScreen->putPixel16(x, s - 1, Color::rgb(0x00, 0x00, 0x00)); } for (int x = 0; x < 3 * s; x++) { virtualScreen->putPixel16(x, 0, Color::rgb(0x00, 0x00, 0x00)); } for (int x = 0; x <= 3 * s; x += s) { for (int y = 0; y < 3 * s; y++) { virtualScreen->putPixel16(x - 1, y, Color::rgb(0x00, 0x00, 0x00)); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -