Размер видео: 1280 X 720853 X 480640 X 360
Показать панель управления
Автовоспроизведение
Автоповтор
コメント失礼します。ソースコードを教えて頂けませんか?
コメントありがとうございます。長いので、複数に分けてコメントしていきます。よかったら参考にしてみてください。ごみごみしたソースですが・・・
ファイル名:main.cpp---------------------------------------------------------#include #include #include #include "common.h"#include "cursor.h"#include "bomb.h"class minesweeper{ std::map levelparams; std::map symbols ; level select_level; std::vector cells ; cursor cur ; bomb* pbomb ; void create_hints(); void dupli_check_in(std::vector& nums ,const size_t& pos); void create_bomb_set_pos(); key_event press_key_code_trans(const char& ch); const char* is_hint(const pos& p); void comb(const pos p); size_t board_count(); void main_loop(); void output();public: minesweeper(level l); ~minesweeper();};minesweeper::minesweeper(level l):levelparams{{level::BEGINNER ,{10 ,{ 9, 9}}},{level::INTERMEDIATE ,{40 ,{16,16}}},{level::ADVANCED ,{99 ,{16,30}}}},symbols{{symbol::NON ," "},{symbol::BOMB ,"×"},{symbol::BOARD,"■"}},select_level(l),cells( levelparams.at(l).cellsize.y,{levelparams.at(l).cellsize.x ,symbols.at(symbol::BOARD)}),pbomb(new bomb(levelparams.at(select_level).bomb_num ,levelparams.at(select_level).cellsize)){ const auto po = cur.appear(); cells[po.y][po.x] = cur.symbol; main_loop();}minesweeper::~minesweeper(){ delete pbomb;}key_event minesweeper::press_key_code_trans(const char& ch){ switch(ch){ case 'i': return key_event::UP ; case 'k': return key_event::DOWN ; case 'j': return key_event::LEFT ; case 'l': return key_event::RIGHT; case 13 : return key_event::SWEEP; case 'e': return key_event::END ; default : return key_event::NON ; }}void minesweeper::comb(const pos p){ int start_x = (p.x > 0 ? -1 : 0); int start_y = (p.y > 0 ? -1 : 0); int end_x = (p.x < levelparams.at(select_level).cellsize.x - 1 ? 2 : 1); int end_y = (p.y < levelparams.at(select_level).cellsize.y - 1 ? 2 : 1); for(int y = start_y;y < end_y;++y){ for(int x = start_x;x < end_x;++x){ if(y != 0 || x != 0){ int _y = p.y + y; int _x = p.x + x; if(cells[_y][_x] == symbols.at(symbol::BOARD)){ if(const char* str_num = pbomb->is_hint({size_t(_y),size_t(_x)})){ cells[_y][_x] = str_num; }else{ cells[_y][_x] = symbols.at(symbol::NON); comb({size_t(_y),size_t(_x)}); } } } } }}size_t minesweeper::board_count(){ size_t cnt = 0; for(const auto& row : cells){ for(const auto& col : row){ if(col == symbols.at(symbol::BOARD)) ++cnt; } } return cnt;}void minesweeper::main_loop(){ for(const char* backup_symbol = symbols.at(symbol::BOARD);;){ output(); const auto code = press_key_code_trans(_getch()); const auto def_pos = cur.appear(); switch(code){ case key_event::NON : continue; case key_event::END : return ; case key_event::SWEEP: if(backup_symbol == symbols.at(symbol::BOARD)){ if(*pbomb == def_pos){ cells[def_pos.y][def_pos.x] = symbols.at(symbol::BOMB); output(); std::cout is_hint(def_pos))){ backup_symbol = symbols.at(symbol::NON); cells[def_pos.y][def_pos.x] = symbols.at(symbol::NON); comb(def_pos); } if(levelparams.at(select_level).bomb_num == board_count()){ output(); std::cout
ファイル名:cursor.h----------------------------------------------------------------------#ifndef __CURSOR_H__#define __CURSOR_H__#include #include "common.h"class cursor { size_t y; size_t x; std::map move_list; void move_up (size_t range_min); void move_down (size_t range_max); void move_left (size_t range_min); void move_right(size_t range_max);public: const char* symbol; cursor(); void move (key_event code ,size_t range); pos appear( );};#endif----------------------------------------------------------------------ファイル名:cursor.cpp----------------------------------------------------------------------#include "cursor.h"cursor::cursor():y(),x(),move_list{{key_event::UP ,&cursor::move_up },{key_event::DOWN ,&cursor::move_down },{key_event::LEFT ,&cursor::move_left },{key_event::RIGHT,&cursor::move_right}},symbol("↓"){}void cursor::move_up (size_t range_min){y == range_min ? y = range_min : --y;}void cursor::move_down (size_t range_max){y == range_max ? y = range_max : ++y;}void cursor::move_left (size_t range_min){x == range_min ? x = range_min : --x;}void cursor::move_right(size_t range_max){x == range_max ? x = range_max : ++x;}void cursor::move(key_event code ,size_t range){ (this->*move_list.at(code))(range);}pos cursor::appear(){ return {y ,x}; }
ファイル名:bomb.h----------------------------------------------------------------------#ifndef __BOMB_H__#define __BOMB_H__#include "common.h"#include class bomb { std::vector bombs_pos; std::vector hints ; size_t quantity ; cell_size cellsize ; const char* symbol ; const char* menber_to_str[9] = {" ","1","2","3","4","5","6","7","8"}; void dupli_check_in (std::vector& nums ,const size_t& pos); void create_bomb_set_pos(); void create_hints ();public: bomb(size_t quantity ,cell_size cellsize); const char* is_hint(const pos& p); bool operator==(const pos& p);};#endif----------------------------------------------------------------------ファイル名:cursor.h----------------------------------------------------------------------#include "bomb.h"#include #include bomb::bomb(size_t quantity ,cell_size cellsize):quantity(quantity),cellsize(cellsize),symbol ("×"){ create_bomb_set_pos(); create_hints();}void bomb::dupli_check_in(std::vector& nums ,const size_t& pos){ for(auto& n :nums){ if(n == pos) return; } nums.push_back(pos);}void bomb::create_bomb_set_pos(){ srand( time(nullptr) ); std::vector nums; for(;nums.size() != quantity;){ const auto pos = rand() % (cellsize.y * cellsize.x); nums.size() > 1 ? dupli_check_in(nums ,pos) : nums.push_back(pos); } for(const auto& n : nums) bombs_pos.push_back({n / cellsize.x ,n % cellsize.x});}void bomb::create_hints(){ for(const auto& bp : bombs_pos){ for(int y = -1;y < 2;++y){ for(int x = -1;x < 2;++x){ if(y != 0 || x != 0){ int _y = bp.y + y; int _x = bp.x + x; if(0
ファイル名:common.h------------------------------------------------------------#ifndef __COMMON_H__#define __COMMON_H__enum class level{/*初級*/BEGINNER ,/*中級*/INTERMEDIATE,/*上級*/ADVANCED };enum class symbol{/*空白*/NON ,/*ボム*/BOMB ,/*床 */BOARD,/*数字*/NUM };enum class key_event{UP,DOWN,LEFT,RIGHT,SWEEP,END,NON};struct cell_size { /*縦*/size_t y; /*横*/size_t x;};struct params { size_t bomb_num; cell_size cellsize;};struct pos { size_t y; size_t x;};struct hint { pos p ; size_t num;};#endif
コメント失礼します。ソースコードを教えて頂けませんか?
コメントありがとうございます。長いので、複数に分けてコメントしていきます。よかったら参考にしてみてください。ごみごみしたソースですが・・・
ファイル名:main.cpp
---------------------------------------------------------
#include
#include
#include
#include "common.h"
#include "cursor.h"
#include "bomb.h"
class minesweeper{
std::map levelparams;
std::map symbols ;
level select_level;
std::vector cells ;
cursor cur ;
bomb* pbomb ;
void create_hints();
void dupli_check_in(std::vector& nums ,const size_t& pos);
void create_bomb_set_pos();
key_event press_key_code_trans(const char& ch);
const char* is_hint(const pos& p);
void comb(const pos p);
size_t board_count();
void main_loop();
void output();
public:
minesweeper(level l);
~minesweeper();
};
minesweeper::minesweeper(level l)
:levelparams
{{level::BEGINNER ,{10 ,{ 9, 9}}}
,{level::INTERMEDIATE ,{40 ,{16,16}}}
,{level::ADVANCED ,{99 ,{16,30}}}
}
,symbols
{{symbol::NON ," "}
,{symbol::BOMB ,"×"}
,{symbol::BOARD,"■"}
}
,select_level(l)
,cells
( levelparams.at(l).cellsize.y
,{levelparams.at(l).cellsize.x ,symbols.at(symbol::BOARD)}
)
,pbomb(new bomb(levelparams.at(select_level).bomb_num ,levelparams.at(select_level).cellsize))
{
const auto po = cur.appear();
cells[po.y][po.x] = cur.symbol;
main_loop();
}
minesweeper::~minesweeper()
{
delete pbomb;
}
key_event minesweeper::press_key_code_trans(const char& ch)
{
switch(ch){
case 'i': return key_event::UP ;
case 'k': return key_event::DOWN ;
case 'j': return key_event::LEFT ;
case 'l': return key_event::RIGHT;
case 13 : return key_event::SWEEP;
case 'e': return key_event::END ;
default : return key_event::NON ;
}
}
void minesweeper::comb(const pos p)
{
int start_x = (p.x > 0 ? -1 : 0);
int start_y = (p.y > 0 ? -1 : 0);
int end_x = (p.x < levelparams.at(select_level).cellsize.x - 1 ? 2 : 1);
int end_y = (p.y < levelparams.at(select_level).cellsize.y - 1 ? 2 : 1);
for(int y = start_y;y < end_y;++y){
for(int x = start_x;x < end_x;++x){
if(y != 0 || x != 0){
int _y = p.y + y;
int _x = p.x + x;
if(cells[_y][_x] == symbols.at(symbol::BOARD)){
if(const char* str_num = pbomb->is_hint({size_t(_y),size_t(_x)})){
cells[_y][_x] = str_num;
}else{
cells[_y][_x] = symbols.at(symbol::NON);
comb({size_t(_y),size_t(_x)});
}
}
}
}
}
}
size_t minesweeper::board_count()
{
size_t cnt = 0;
for(const auto& row : cells){
for(const auto& col : row){
if(col == symbols.at(symbol::BOARD))
++cnt;
}
}
return cnt;
}
void minesweeper::main_loop()
{
for(const char* backup_symbol = symbols.at(symbol::BOARD);;){
output();
const auto code = press_key_code_trans(_getch());
const auto def_pos = cur.appear();
switch(code){
case key_event::NON : continue;
case key_event::END : return ;
case key_event::SWEEP:
if(backup_symbol == symbols.at(symbol::BOARD)){
if(*pbomb == def_pos){
cells[def_pos.y][def_pos.x] = symbols.at(symbol::BOMB);
output();
std::cout is_hint(def_pos))){
backup_symbol = symbols.at(symbol::NON);
cells[def_pos.y][def_pos.x] = symbols.at(symbol::NON);
comb(def_pos);
}
if(levelparams.at(select_level).bomb_num == board_count()){
output();
std::cout
ファイル名:cursor.h
----------------------------------------------------------------------
#ifndef __CURSOR_H__
#define __CURSOR_H__
#include
#include "common.h"
class cursor {
size_t y;
size_t x;
std::map move_list;
void move_up (size_t range_min);
void move_down (size_t range_max);
void move_left (size_t range_min);
void move_right(size_t range_max);
public:
const char* symbol;
cursor();
void move (key_event code ,size_t range);
pos appear( );
};
#endif
----------------------------------------------------------------------
ファイル名:cursor.cpp
----------------------------------------------------------------------
#include "cursor.h"
cursor::cursor()
:y()
,x()
,move_list
{{key_event::UP ,&cursor::move_up }
,{key_event::DOWN ,&cursor::move_down }
,{key_event::LEFT ,&cursor::move_left }
,{key_event::RIGHT,&cursor::move_right}
}
,symbol("↓")
{
}
void cursor::move_up (size_t range_min){y == range_min ? y = range_min : --y;}
void cursor::move_down (size_t range_max){y == range_max ? y = range_max : ++y;}
void cursor::move_left (size_t range_min){x == range_min ? x = range_min : --x;}
void cursor::move_right(size_t range_max){x == range_max ? x = range_max : ++x;}
void cursor::move(key_event code ,size_t range)
{
(this->*move_list.at(code))(range);
}
pos cursor::appear()
{
return {y ,x};
}
ファイル名:bomb.h
----------------------------------------------------------------------
#ifndef __BOMB_H__
#define __BOMB_H__
#include "common.h"
#include
class bomb {
std::vector bombs_pos;
std::vector hints ;
size_t quantity ;
cell_size cellsize ;
const char* symbol ;
const char* menber_to_str[9] = {" ","1","2","3","4","5","6","7","8"};
void dupli_check_in (std::vector& nums ,const size_t& pos);
void create_bomb_set_pos();
void create_hints ();
public:
bomb(size_t quantity ,cell_size cellsize);
const char* is_hint(const pos& p);
bool operator==(const pos& p);
};
#endif
----------------------------------------------------------------------
ファイル名:cursor.h
----------------------------------------------------------------------
#include "bomb.h"
#include
#include
bomb::bomb(size_t quantity ,cell_size cellsize)
:quantity(quantity)
,cellsize(cellsize)
,symbol ("×")
{
create_bomb_set_pos();
create_hints();
}
void bomb::dupli_check_in(std::vector& nums ,const size_t& pos)
{
for(auto& n :nums){
if(n == pos)
return;
}
nums.push_back(pos);
}
void bomb::create_bomb_set_pos()
{
srand( time(nullptr) );
std::vector nums;
for(;nums.size() != quantity;){
const auto pos = rand() % (cellsize.y * cellsize.x);
nums.size() > 1 ? dupli_check_in(nums ,pos) : nums.push_back(pos);
}
for(const auto& n : nums)
bombs_pos.push_back({n / cellsize.x ,n % cellsize.x});
}
void bomb::create_hints()
{
for(const auto& bp : bombs_pos){
for(int y = -1;y < 2;++y){
for(int x = -1;x < 2;++x){
if(y != 0 || x != 0){
int _y = bp.y + y;
int _x = bp.x + x;
if(0
ファイル名:common.h
------------------------------------------------------------
#ifndef __COMMON_H__
#define __COMMON_H__
enum class level
{/*初級*/BEGINNER
,/*中級*/INTERMEDIATE
,/*上級*/ADVANCED
};
enum class symbol
{/*空白*/NON
,/*ボム*/BOMB
,/*床 */BOARD
,/*数字*/NUM
};
enum class key_event
{UP
,DOWN
,LEFT
,RIGHT
,SWEEP
,END
,NON
};
struct cell_size {
/*縦*/size_t y;
/*横*/size_t x;
};
struct params {
size_t bomb_num;
cell_size cellsize;
};
struct pos {
size_t y;
size_t x;
};
struct hint {
pos p ;
size_t num;
};
#endif