项目二:用C++做一个战舰游戏
開啟我的菜鳥小旅程
- Battleship Game
- 艦船戰(zhàn)爭背景描述
- MVC模式基礎(chǔ)理解
- 顯示10*10玩家戰(zhàn)艦位置分布功能
- 代碼部分講解
- 第一部分
- 定義CMakeLists.txt 文件
- [ C++里面 類(Class)的學(xué)習(xí)](https://www.cnblogs.com/mr-wid/archive/2013/02/18/2916309.html)
- 第二部分
- [C結(jié)構(gòu)體、C++結(jié)構(gòu)體 和 C++類的區(qū)別](https://blog.csdn.net/u013925378/article/details/51661081)
- 附[EN]note:
- one possible choice
- create a new
- coord
Battleship Game
艦船戰(zhàn)爭背景描述
在一個10*10的網(wǎng)格里,每一個玩家有一定數(shù)量的艦船。評判玩家勝利的標準是擊沉敵方所有的艦船。嘿嘿,不會玩Battleship Game的小朋友們?nèi)ゾW(wǎng)上看看相關(guān)游戲直播或者親自玩耍一下。
| 航空母艦(Aircraft carrier) [A] | 5 |
| 巡洋艦(Cruiser) [C] | 4 |
| 驅(qū)逐艦(Destroyer) [D] | 3 |
| 潛艇(Submarine) [S] | 3 |
| 掃雷艦(Minesweeper) [M] | 2 |
MVC模式基礎(chǔ)理解
我們首先要建立一個思想,或者說,是一種理念。MVC模式,維基百科里簡單介紹,這是軟件架構(gòu)里面的一種架構(gòu)模式,把軟件系統(tǒng)分為三個基本部分:模型(Model)、視圖(View)和控制器(Controller)。
MVC模式的目的是實現(xiàn)一種動態(tài)的程序設(shè)計,使后續(xù)對程序的修改和擴展簡化,并且使程序某一部分的重復(fù)利用成為可能。除此之外,此模式透過對復(fù)雜度的簡化,使程序結(jié)構(gòu)更加直觀。軟件系統(tǒng)透過對自身基本部分分離的同時也賦予了各個基本部分應(yīng)有的功能。專業(yè)人員可以依據(jù)自身的專長分組。 ——維基百科
所以我們在面對復(fù)雜的程序設(shè)計時,要學(xué)會先把復(fù)雜的問題簡單化。在艦船戰(zhàn)爭這個游戲里面,我們要做的無非是兩個功能:一是顯示玩家艦船的位置,二是實現(xiàn)艦船攻擊的手段。
顯示10*10玩家戰(zhàn)艦位置分布功能
如何用C++輸出網(wǎng)格圖形,是肥鼠想到的第一個問題。不過,我們這里單純地省略網(wǎng)絡(luò)圖界面設(shè)計啦。最開始簡單的游戲思想是規(guī)定兩個玩家two players,分別顯示兩個玩家的戰(zhàn)艦分布圖形。
戰(zhàn)艦攻擊游戲玩家創(chuàng)建create players創(chuàng)建游戲背景網(wǎng)絡(luò)格子generate grid把每個玩家的戰(zhàn)艦進行隨機位置以及角度的擺放顯示被攻擊玩家的地圖那么,我們小菜鳥的C++飛行之旅,開始啦
先放上我們最開始大體的思想,稍后做解釋。
哈哈,Player是什么東西?Player 類型的p1,p2怎么會有.shoot和.display的這種形式?怎么這個主函數(shù)寫的這么簡單?嘿嘿,一切的奧秘都在**#include “player.h”**里面呀。
我們在游戲所在工程里面新建立一個.h以及.cpp 文件名字都叫player。
play class create.h
has can do
grid void display()
bool shoot(player)
main 里面有
class Player
{
Play();
void display();
bool shoot();
}
代碼部分講解
第一部分
定義CMakeLists.txt 文件
首先我們還是要在一個工程里面建立我們的CMakeLists.txt 文件
project(battleships) set(CMAKE_CXX_STANDARD 11) add_executable(battleships battleships.cpp player.h player.cpp)C++里面 類(Class)的學(xué)習(xí)
我們用到了一個新的C++知識點,先學(xué)習(xí)一遍Class呀。
//基本用法class 類名{public://公共的行為或?qū)傩?/span>private://公共的行為或?qū)傩?/span>}; //結(jié)束時的;一定要記得下面我們來看如何定義我們的玩家類,
#ifndef PLAYER_H #define PLAYER_H #include<iostream> #include<array> #include<vector> #include<string> #include<sstream> class Player {private:std::array<int,100> grid;int cell (uint row,uint col){return grid[row +10*col];}public:Player();void display();bool shoot(Player & other); };#endif // PLAYER_H這樣,我們的主程序可以簡單的先寫成如下的形式,在還沒有編寫具體的類成員函數(shù)Player(), void display() 和shoot(Player & other)前,我們的main()函數(shù)可以有一個淺顯易懂的思路。之前我們就看過下面的代碼了,現(xiàn)在是否有深入的理解了呢?
#include"player.h"int main(){// std::array<std::array<int,10>10> grid;Player p1,p2;while(true){p2.display();if(p1.shoot(p2))break;p1.display();if(p2.shoot(p1))break;}}我們有兩個玩家p1,p2.,哈哈,C++的編程哲學(xué)就是做到可以讓讀者在沒有注釋的情況下完全讀懂自己的代碼。
之后,我們要給自己的代碼賦予靈魂了,最重要的環(huán)節(jié)來了:編寫類成員函數(shù)的功能。有兩種方法,一種是類內(nèi)部定義,另外一種是類外定義。我們下面的代碼采用了類外定義的方式。(在player.cpp文件中)
#include "player.h"Player::Player() {for(auto &v:grid)v = 0;}//our function are defined in this file. void Player::display() {std::cout<<"0 1 2 3 4 5 6 7 8 9 10"<<std::endl;for(auto row = 0;row<10;++row){std::cout<<row+1<<" ";if(row!=9)std::cout <<" ";for (auto col =0;col<10;++col) {std::cout<<cell(row,col)<<" ";}std::cout<< std::endl;}注意我們的雙引號里面是有空格的,不然顯示出來的10*10會出現(xiàn)排版問題呀,數(shù)字序號和內(nèi)容對應(yīng)不上。
第二部分
下面,我們要開始進一步完善我們的代碼了。
在顯示部分,我們要把戰(zhàn)艦以及戰(zhàn)艦的長度聯(lián)系到一塊,并且有一定的符號表示在程序運行時顯現(xiàn)出來。這里對于empty也就是water的狀態(tài)我還是有一個疑問的,用0去表示還是用6去表示呢?
| A | 5 | 5 | |
| C | 4 | 4 | |
| D | 3 | 3 | |
| S | 3 | 2 | |
| M | 2 | 1 | |
| empty | 0 |
CONTENT:FIVE number represents the ship/water,(6)
VISIBLE:OUI,NON
C結(jié)構(gòu)體、C++結(jié)構(gòu)體 和 C++類的區(qū)別
我們要定義一個新的結(jié)構(gòu)體Cell用來存儲我們在上文中體積到的表格信息。由于Battleship Game 游戲本身的特征,游戲玩家的船只在攻擊方射擊時是不可見的。我們還需要存儲可見不可見的信息,簡單的可以用‘+’,‘-’兩個符號來代替。
比如,我們單獨顯示‘5’代表Aircraft carrier[A]。
| +5 | visible Aircraft carrier |
| -5 | hidden Aircraft carrier |
| +6 | sea |
為了把顯示船只分類的字符和在計算機里的數(shù)字聯(lián)系起來,我們需要用到C++的一個知識點pair。所以現(xiàn)在我們需要更新我們的player.cpp中Player()。
const std::vector<std::pair<char,uint>>boats{{'A',5}, {'C',4}, {'D',3}, {'S',3}, {'M',2}};溜了,等待更新中。
附[EN]note:
one possible choice
if we write 1,we say that ship M,
visible +M and hidden -M, ±5,
± tell us it is visible or not
1,-1 _M?
6,-6 _sea?
create a new
create a grid,std::array<>GRID,this is our grid
we would like to insert something to tell the computer,
we can not insert A in the grid.it is no mean
we can say make a link between A and something we can insert in he computer
in code how to make a representation
1,-1,6
5,-6,-6
5,-6,-6
by doing the coding
if 1,we visible M,-1,?
one for computer,one for user
first thing we do:
Cell
Class A{private:}
struct A{public:} the only different
char single letter我們想要存儲一個變量val,at the same time char val;
Cell{
char val; //what should we initially? set
bool visible;
cell( char _val )
char content();// mapping the representation
bool make_visible();//we use this function,help myself in a stupid way
}
Cell(“A”)
Cell(“M”)
content () make the number turn into char,what we have put,and we can see in the grid directly.
pair<char,int>
struct pair
{
char first;
int second;
}
cols.front()
cols.back()
generate the position and the orientation now
5 first generate col 0-9
than we have row 0-5s
row,col
row+1,col
**
row+length-1,col
in this way ,the computer will use one second to generate it
coord
struct Coord {uint row,col; }; for(const auto &boat:boats) {const auto type = boat.first;const auto length = boat.second;std::vector<Coord> coordinates(length);const uint row0 = rand()%10;const uint col0 = rand()%(10-length);for(uint i=0;i<length:++i){coordinates[i].row=row0;coordinates[i].col=col0+i;}if(rand()%2 ==0){for(auto &coord:coordinates)std::swap(coord.row,coord.col);}//check are emptyif(std::all_of(coordinates.begin(),coordinates.end(),[&](const Coord &coord){return cell(coord.row,coord.col).type =='.';})){}}然后,我們要shoot the boat ,然后呢,每打擊一次 看我們的結(jié)果
Player::shoot(Player &other)
總結(jié)
以上是生活随笔為你收集整理的项目二:用C++做一个战舰游戏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 西门子PLC S1200新手项目程序(含
- 下一篇: 如何在sqlexpress上安装norw