日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

项目二:用C++做一个战舰游戏

發布時間:2023/12/29 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 项目二:用C++做一个战舰游戏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

開啟我的菜鳥小旅程

  • Battleship Game
    • 艦船戰爭背景描述
    • MVC模式基礎理解
      • 顯示10*10玩家戰艦位置分布功能
  • 代碼部分講解
    • 第一部分
      • 定義CMakeLists.txt 文件
      • [ C++里面 類(Class)的學習](https://www.cnblogs.com/mr-wid/archive/2013/02/18/2916309.html)
    • 第二部分
      • [C結構體、C++結構體 和 C++類的區別](https://blog.csdn.net/u013925378/article/details/51661081)
  • 附[EN]note:
  • one possible choice
  • create a new
    • coord

Battleship Game

艦船戰爭背景描述

在一個10*10的網格里,每一個玩家有一定數量的艦船。評判玩家勝利的標準是擊沉敵方所有的艦船。嘿嘿,不會玩Battleship Game的小朋友們去網上看看相關游戲直播或者親自玩耍一下。

艦船類型長度
航空母艦(Aircraft carrier) [A]5
巡洋艦(Cruiser) [C]4
驅逐艦(Destroyer) [D]3
潛艇(Submarine) [S]3
掃雷艦(Minesweeper) [M]2

MVC模式基礎理解

我們首先要建立一個思想,或者說,是一種理念。MVC模式,維基百科里簡單介紹,這是軟件架構里面的一種架構模式,把軟件系統分為三個基本部分:模型(Model)、視圖(View)和控制器(Controller)。

MVC模式的目的是實現一種動態的程序設計,使后續對程序的修改和擴展簡化,并且使程序某一部分的重復利用成為可能。除此之外,此模式透過對復雜度的簡化,使程序結構更加直觀。軟件系統透過對自身基本部分分離的同時也賦予了各個基本部分應有的功能。專業人員可以依據自身的專長分組。 ——維基百科

所以我們在面對復雜的程序設計時,要學會先把復雜的問題簡單化。在艦船戰爭這個游戲里面,我們要做的無非是兩個功能:一是顯示玩家艦船的位置,二是實現艦船攻擊的手段。

顯示10*10玩家戰艦位置分布功能

如何用C++輸出網格圖形,是肥鼠想到的第一個問題。不過,我們這里單純地省略網絡圖界面設計啦。最開始簡單的游戲思想是規定兩個玩家two players,分別顯示兩個玩家的戰艦分布圖形。

戰艦攻擊游戲玩家創建create players創建游戲背景網絡格子generate grid把每個玩家的戰艦進行隨機位置以及角度的擺放顯示被攻擊玩家的地圖

那么,我們小菜鳥的C++飛行之旅,開始啦
先放上我們最開始大體的思想,稍后做解釋。

#include "player.h"int main() {Player p1, p2;while(true){p2.display();if(p1.shoot(p2))break;p1.display();if(p2.shoot(p1))break;} } //2 player //display grid of p2 //p1 shoot at p2---may win //display grid of p1 //p2 shoot at p1--may win

哈哈,Player是什么東西?Player 類型的p1,p2怎么會有.shoot和.display的這種形式?怎么這個主函數寫的這么簡單?嘿嘿,一切的奧秘都在**#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)的學習

我們用到了一個新的C++知識點,先學習一遍Class呀。

//基本用法class 類名{public://公共的行為或屬性private://公共的行為或屬性}; //結束時的;一定要記得

下面我們來看如何定義我們的玩家類,

#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

這樣,我們的主程序可以簡單的先寫成如下的形式,在還沒有編寫具體的類成員函數Player(), void display() 和shoot(Player & other)前,我們的main()函數可以有一個淺顯易懂的思路。之前我們就看過下面的代碼了,現在是否有深入的理解了呢?

#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++的編程哲學就是做到可以讓讀者在沒有注釋的情況下完全讀懂自己的代碼。

之后,我們要給自己的代碼賦予靈魂了,最重要的環節來了:編寫類成員函數的功能。有兩種方法,一種是類內部定義,另外一種是類外定義。我們下面的代碼采用了類外定義的方式。(在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會出現排版問題呀,數字序號和內容對應不上。

第二部分

下面,我們要開始進一步完善我們的代碼了。
在顯示部分,我們要把戰艦以及戰艦的長度聯系到一塊,并且有一定的符號表示在程序運行時顯現出來。這里對于empty也就是water的狀態我還是有一個疑問的,用0去表示還是用6去表示呢?

Cell contentlength If anyint representationint representation if hit
A55
C44
D33
S32
M21
empty0

CONTENT:FIVE number represents the ship/water,(6)
VISIBLE:OUI,NON

C結構體、C++結構體 和 C++類的區別

我們要定義一個新的結構體Cell用來存儲我們在上文中體積到的表格信息。由于Battleship Game 游戲本身的特征,游戲玩家的船只在攻擊方射擊時是不可見的。我們還需要存儲可見不可見的信息,簡單的可以用‘+’,‘-’兩個符號來代替。
比如,我們單獨顯示‘5’代表Aircraft carrier[A]。

labelmeaning
+5visible Aircraft carrier
-5hidden Aircraft carrier
+6sea

為了把顯示船只分類的字符和在計算機里的數字聯系起來,我們需要用到C++的一個知識點pair。所以現在我們需要更新我們的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.

struct Cell {char type;bool visible = false;Cell }

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 ,然后呢,每打擊一次 看我們的結果
Player::shoot(Player &other)

總結

以上是生活随笔為你收集整理的项目二:用C++做一个战舰游戏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。