开启我的菜鸟小旅程
- 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,分别显示两个玩家的战舰分布图形。
那么,我们小菜鸟的C++飞行之旅,开始啦
先放上我们最开始大体的思想,稍后做解释。
哈哈,Player是什么东西ayer 类型的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 文件
C++里面 类(Class)的学习
我们用到了一个新的C++知识点,先学习一遍Class呀。
下面我们来看如何定义我们的玩家类,
这样,我们的主程序可以简单的先写成如下的形式,在还没有编写具体的类成员函数Player(), void display() 和shoot(Player & other)前,我们的main()函数可以有一个浅显易懂的思路。之前我们就看过下面的代码了,现在是否有深入的理解了呢p>
我们有两个玩家p1,p2.,哈哈,C++的编程哲学就是做到可以让读者在没有注释的情况下完全读懂自己的代码。
之后,我们要给自己的代码赋予灵魂了,最重要的环节来了:编写类成员函数的功能。有两种方法,一种是类内部定义,另外一种是类外定义。我们下面的代码采用了类外定义的方式。(在player.cpp文件中)
注意我们的双引 里面是有空格的,不然显示出来的10*10会出现排版问题呀,数字序 和内容对应不上。
第二部分
下面,我们要开始进一步完善我们的代码了。
在显示部分,我们要把战舰以及战舰的长度联系到一块,并且有一定的符 表示在程序运行时显现出来。这里对于empty也就是water的状态我还是有一个疑问的,用0去表示还是用6去表示呢mark>
Cell content | length If any | int representation | int representation if hit |
---|---|---|---|
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结构体、C++结构体 和 C++类的区别
我们要定义一个新的结构体Cell用来存储我们在上文中体积到的表格信息。由于Battleship Game 游戏本身的特征,游戏玩家的船只在攻击方射击时是不可见的。我们还需要存储可见不可见的信息,简单的可以用‘+’,‘-’两个符 来代替。
比如,我们单独显示‘5’代表Aircraft carrier[A]。
label | meaning |
---|---|
+5 | visible Aircraft carrier |
-5 | hidden Aircraft carrier |
+6 | sea |
为了把显示船只分类的字符和在计算机里的数字联系起来,我们需要用到C++的一个知识点pair。所以现在我们需要更新我们的player.cpp中Player()。
溜了,等待更新中。
附[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 _Mr> 6,-6 _seap>
create a new
create a grid,std::arrayGRID,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,r> 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 initiallyet
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
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
然后,我们要shoot the boat ,然后呢,每打击一次 看我们的结果
Player::shoot(Player &other)
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!