- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我现在对 C++ 有点陌生,我必须编写一个控制台 Tank 游戏。我自己做了很多部分,大部分部分都没有错。这是我的坦克课:
class Tank {
protected:
string name;
static int number;
static int row;
static int col;
static char direction;
public:
Tank(int _number, string _name, int _row, int _col, char _direction){
this->name = _name;
this->number = _number;
setRow(_row);
setCol(_col);
setDirection(_direction);
}
///////////////////////////////
///----GETTERS FUNCTIONS----///
///////////////////////////////
char getdirection(){ return direction; }
int getnumber(){ return number; }
int getCol(){ return col; }
int getRow(){ return row; }
void setDirection(char _direction)
{
direction = _direction;
}
void setCol(int _col)
{
col = _col;
}
void setRow(int _row)
{
row = _row;
}
////////////////////////
///To String Function///
////////////////////////
string _toString(){
string r = "";
r = ("Tank No. " + toString(number + 1) + " " + name + " At (" + toString(row) + "," + toString(col) + ") " + direction);
return r;
}
};
int Tank::col = 0;
int Tank::row = 0;
int Tank::number = 0;
char Tank::direction = '\0';
问题是当我创建像这样的对象数组时:
Tank *players[4];
因为我需要一个四人游戏和一个坦克对象,所以它只使用最后一个对象。这是我想在每个回合中使用的代码,让每个玩家说出他们将要做什么:
static char action;
static int turn = 1;
static Game game(*players, map);
for (int count = 0; players[count] != NULL; count++)
{
clr_screen();
gotoxy(100, 15); cout << "Turn " << turn;
gotoxy(100, 16); cout << "Player " << players[count]->getnumber() + 1 << " Action: ";
cin >> action;
game.move(action, count);
}
turn++;
如您所见,代码存在问题。
正如您可能想知道的,这是 Game 类:
class Game{
private:
Tank *p_tank[5];
Map map;
public:
Game(Tank *tanks, Map _map)
{
*p_tank = tanks; map = _map;
}
//Tank getTank(){ return p_tank; }
Map getMap(){ return map; }
如果你需要这里的完整代码,它是:
#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <fstream>
#include <sstream>
using namespace std;
void SetWindow(int Width, int Height)
{
_COORD coord;
coord.X = Width;
coord.Y = Height;
_SMALL_RECT Rect;
Rect.Top = 0;
Rect.Left = 0;
Rect.Bottom = Height - 1;
Rect.Right = Width - 1;
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get Handle
SetConsoleScreenBufferSize(Handle, coord); // Set Buffer Size
SetConsoleWindowInfo(Handle, TRUE, &Rect); // Set Window Size
}
void gotoxy(int x, int y)
{
static HANDLE h = NULL;
if (!h)
h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD c = { x, y };
SetConsoleCursorPosition(h, c);
}
string toString(int x) {
stringstream ss;
ss << x;
return ss.str();
}
class Tank {
protected:
string name;
static int number;
static int row;
static int col;
static char direction;
public:
Tank(int _number, string _name, int _row, int _col, char _direction){
this->name = _name;
this->number = _number;
setRow(_row);
setCol(_col);
setDirection(_direction);
}
///////////////////////////////
///----GETTERS FUNCTIONS----///
///////////////////////////////
char getdirection(){ return direction; }
int getnumber(){ return number; }
int getCol(){ return col; }
int getRow(){ return row; }
void setDirection(char _direction)
{
direction = _direction;
}
void setCol(int _col)
{
col = _col;
}
void setRow(int _row)
{
row = _row;
}
////////////////////////
///To String Function///
////////////////////////
string _toString(){
string r = "";
r = ("Tank No. " + toString(number + 1) + " " + name + " At (" + toString(row) + "," + toString(col) + ") " + direction);
return r;
}
};
int Tank::col = 0;
int Tank::row = 0;
int Tank::number = 0;
char Tank::direction = '\0';
class Bullet : public Tank{
private:
Tank tank;
public:
Tank getTank(){ return tank; }
void setRow(int _row){ row = _row; }
void setCol(int _col){ col = _col; }
char getDirection(){ return direction; }
string __toString(){
string r = "";
r = ("Bullet At (" + toString(row) + "," + toString(col) + ") From Tank No " + toString(number));
return r;
}
};
class Block{//Characters: according to legends Lines: 316 - 322
protected:
int row;
int col;
char character;
public:
Block(char block)
{
this->character = block;
}
virtual string _toString(){
string r = "";
r = (character + (" at (" + toString(row) + toString(col) + "). "));
return r;
}
int getRow(){ return row; }
int getCol(){ return col; }
void setRow(int _row){ row = _row; }
void setCol(int _col) { row = _col; }
virtual char getCharacter() { return character; }
};
/*class Ground : public Block{
public:
Ground(){
character = '.';
Block block;
block.getCharacter();
}
};
class Wall : public Block{
public:
Wall(){
character = 'W';
Block block;
block.getCharacter();
}
};
class Box : public Block{
public:
Box(){
character = 'B';
Block block;
block.getCharacter();
}
};
class Ice : public Block{
public:
Ice(){
character = 'I';
Block block;
block.getCharacter();
}
};
class Trap : public Block{
public:
Trap(){
character = 'T';
Block block;
block.getCharacter();
}
};
class Random : public Block{
public:
Random(){
character = '?';
Block block;
block.getCharacter();
}
};
class Tblock : public Block{
public:
Tblock(){
character = 'T';
Block block;
block.getCharacter();
}
};*/
class Map{
private:
int rows;
int cols;
static const int MaxSize = 100;
Block *map[MaxSize][MaxSize];
public:
Map(int row = MaxSize, int col = MaxSize){
this->rows = row;
this->cols = col;
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++)
map[i][j] = new Block(' ');
}
}
void edit(int row, int col, char legend)
{
if (legend == '%')//bullet sign
map[row][col] = new Block('O');
else if (legend == '0')
map[row][col] = new Block('1');
else if (legend == '1')
map[row][col] = new Block('2');
else if (legend == '2')
map[row][col] = new Block('3');
else if (legend == '3')
map[row][col] = new Block('4');
else if (legend == 'G' || legend == 'g')
map[row][col] = new Block(' ');
else if (legend == 'w' || legend == 'W')
map[row][col] = new Block('W');
else if (legend == '?')
map[row][col] = new Block('?');
else if (legend == 'B' || legend == 'b')
map[row][col] = new Block('B');
else if (legend == 'X' || legend == 'x')
map[row][col] = new Block('X');
else if (legend == 'I' || legend == 'i')
map[row][col] = new Block('I');
else
map[row][col] = new Block('B');
_toGUI(row, col);
}
void _toGUI(int x, int y)
{
int X = 7 + 8 * x;
int Y = 6 + 4 * y;
gotoxy(X, Y);
cout << map[x][y]->getCharacter();
}
bool moveOk(int r, int c, char mov)
{
switch (mov)
{
case 'l':
if (r - 1 < 0) return false;
else return true;
break;
case 'r':
if (r > rows - 2) return false;
else return true;
break;
case 'd':
if (c < cols - 1) return true;
else return false;
break;
case 'u':
if (c > 0) return true;
else return false;
break;
}
}
int getRows(){ return cols; }
int getCols(){ return rows; }
};
class Game{
private:
Tank *p_tank[5];
Map map;
public:
Game(Tank *tanks, Map _map)
{
*p_tank = tanks; map = _map;
}
//Tank getTank(){ return p_tank; }
Map getMap(){ return map; }
void move(char move, int whoToPlay)
{
string wtp;
wtp = toString(whoToPlay);
int m_row, m_col;
switch (move)
{
case 'l':
m_row = p_tank[whoToPlay]->getRow() - 1;
m_col = p_tank[whoToPlay]->getCol() - 1;
if (map.moveOk(m_row, m_col, 'l')){
map.edit(m_row, m_col, 'G');
map.edit(--m_row, m_col, wtp[0]);
p_tank[whoToPlay]->setRow(m_row + 1);
p_tank[whoToPlay]->setCol(m_col + 1);
}
break;
case 'r':
m_row = p_tank[whoToPlay]->getRow() - 1;
m_col = p_tank[whoToPlay]->getCol() - 1;
if (map.moveOk(m_row, m_col, 'r')){
map.edit(m_row, m_col, 'G');
map.edit(++m_row, m_col, wtp[0]);
p_tank[whoToPlay]->setRow(m_row + 1);
p_tank[whoToPlay]->setCol(m_col + 1);
}
break;
case 'd':
m_row = p_tank[whoToPlay]->getRow() - 1;
m_col = p_tank[whoToPlay]->getCol() - 1;
if (map.moveOk(m_row, m_col, 'd')){
map.edit(m_row, m_col, 'G');
map.edit(m_row, ++m_col, wtp[0]);
p_tank[whoToPlay]->setRow(m_row + 1);
p_tank[whoToPlay]->setCol(m_col + 1);
}
break;
case 'u':
m_row = p_tank[whoToPlay]->getRow() - 1;
m_col = p_tank[whoToPlay]->getCol() - 1;
if (map.moveOk(m_row, m_col, 'u')){
map.edit(m_row, m_col, 'G');
map.edit(m_row, --m_col, wtp[0]);
p_tank[whoToPlay]->setRow(m_row + 1);
p_tank[whoToPlay]->setCol(m_col + 1);
}
break;
}
}
};
void draw_field(Map map)
{
////////////////////////
//-------Border-------//
////////////////////////
for (int i = 3; i <= 2 * (4 * map.getRows() + 3); i++)
{
gotoxy(i, 3);
printf("%c", 219);
gotoxy(i, (4 * map.getCols() + 7));
printf("%c", 219);
}
for (int i = 6; i < (4 * map.getCols() + 7); i++){
gotoxy(3, i);
printf("%c", 219);
gotoxy(2 * (4 * map.getRows() + 3), i);
printf("%c", 219);
}
gotoxy(3, 5);
printf("%c", 219);
gotoxy(3, 4);
printf("%c", 219);
gotoxy(3, 4 * map.getCols() + 7);
printf("%c", 219);
gotoxy(2 * (4 * map.getRows() + 3), 4);
printf("%c", 219);
gotoxy(2 * (4 * map.getRows() + 3), 5);
printf("%c", 219);
gotoxy(2 * (4 * map.getRows() + 3), map.getCols() + 7);
printf("%c", 219);
//end of BORDER
//****************\\
//---COORD GUI----\\
//****************\\
//numbers:
int j = 1;
int z = 1;
for (int i = 8; i < 2 * (4 * map.getRows() + 3); i += 8)
{
gotoxy(i, 2); cout << j;
j++;
}
for (int i = 6; i < (4 * map.getCols() + 6); i += 4)
{
gotoxy(1, i);
cout << z;
z++;
}
//lines:
for (int j = 8; j < (4 * map.getCols() + 6); j += 4)
for (int i = 4; i < 2 * (4 * map.getRows() + 3); i++)
{
gotoxy(i, j);
cout << "-";
}
for (int j = 12, c = 0; c < 4 * map.getRows() / 4 - 1; j += 8, c++)
for (int i = 4, t = 0; t < 4 * map.getCols() + 1; i++, t++)
{
Sleep(10);
gotoxy(j, i);
cout << "|";
}
}
void clr_screen()
{
for (int i = 100; i <= 136; i++){
for (int j = 15; j <= 55; j++){
gotoxy(i, j); cout << " ";
}
}
}
void legend()
{
gotoxy(100, 2); cout << "Legends: ";
gotoxy(100, 4); cout << "Tanks: #\tBullet: *";
gotoxy(100, 5); cout << "Walls: W\tRandom: ?";
gotoxy(100, 6); cout << "Box: B\tIce: I";
gotoxy(100, 7); cout << "Trap: X\t";
gotoxy(100, 9); cout << "Actions:";
gotoxy(100, 10); cout << "U D L R\tFire: F";
gotoxy(100, 11); cout << "----------------------";
}
void action_display(string str, Map &map)
{
Tank *players[5];
char reply = 'n';
if (str == "%edit%" || str == "%Edit%"){
while (reply != 'y'){
char legend; int c; int r;
clr_screen();
gotoxy(100, 14);
cout << "Actions:";
gotoxy(100, 15);
cout << "Edit Section:";
gotoxy(100, 16); cout << "Enter the legend: ";
gotoxy(100, 17); cin >> legend;
if (legend == '1' || legend == '2' || legend == '3' || legend == '4')
legend = '!';
gotoxy(100, 18); cout << "Col: "; cin >> r;
gotoxy(100, 19); cout << "Row: "; cin >> c;
map.edit(r - 1, c - 1, legend);
gotoxy(100, 20); cout << "Done?(Y|N)"; cin >> reply;
}
}
if (str == "%NewGame%")
{
static int P;
string name;
int r, c;
clr_screen();
gotoxy(100, 15);
cout << "How many players?";
cin >> P;
if (P >= 4) P = 4;
for (int i = 0; i < P; i++)
{
clr_screen();
gotoxy(100, 16); cout << "Enter player " << i + 1 << " start: ";
gotoxy(100, 17); cout << "col: "; cin >> r;
gotoxy(100, 18); cout << "row: "; cin >> c;
gotoxy(100, 19); cout << "Name: "; cin >> name;
players[i] = new Tank(i, name, r, c, 'u');
string a;
a = toString(i);
map.edit(r - 1, c - 1, a[0]);
clr_screen();
gotoxy(100, 15); cout << players[i]->_toString();
_getch();
}
}
if (str == "%%MovementDuringGame%%")
{
static char action;
static int turn = 1;
static Game game(*players, map);
for (int count = 0; players[count] != NULL; count++)
{
clr_screen();
gotoxy(100, 15); cout << "Turn " << turn;
gotoxy(100, 16); cout << "Player " << players[count]->getnumber() + 1 << " Action: ";
cin >> action;
game.move(action, count);
}
turn++;
clr_screen();
}
}
void menu()
{
cout << "Welcome to Tank CPP Game.\n";
cout << "At first, Please Enter the field's Size.\n!!Attention!! We Recommend 10x10. Max Row Size: 11 and Max Column Size: 11\n\npress Any Key to continue...";
_getch();
int r; int c;
system("cls");
cout << "Row:"; cin >> r; cout << "Col:"; cin >> c;
if (r >= 11) r = 11;
if (c >= 11) c = 11;
SetWindow(138, 60);
system("cls");
Map map(r, c);
draw_field(map);
_getch();
clr_screen();
legend();
//**********************//
//****** Editing *******//
//**********************//
action_display("%edit%", map);
clr_screen();
/****************************
******GAME START-Players*****
****************************/
action_display("%NewGame%", map);
_getch();
/************************
******* MOVEMENTS *******
*************************/
while (1)
action_display("%%MovementDuringGame%%", map);
_getch();
}
void main()
{
menu();
}
最佳答案
*p_tank = tanks;
是失败的线路,你应该告诉我们。
现在我可以告诉你如何解决这个问题,但你真的不应该使用原始指针和数组。当您使用 std::vector<Tank>
时,整个问题就消失了.
关于c++ - 一个简单的 Tank C++ 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27917092/
我正在努力实现以下目标, 假设我有字符串: ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ) ) ) ) ) 我想编写一个正则
给定: 1 2 3 4 5 6
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
大家好,我卡颂。 Svelte问世很久了,一直想写一篇好懂的原理分析文章,拖了这么久终于写了。 本文会围绕一张流程图和两个Demo讲解,正确的食用方式是用电脑打开本文,跟着流程图、Demo一
身份证为15位或者18位,15位的全为数字,18位的前17位为数字,最后一位为数字或者大写字母”X“。 与之匹配的正则表达式: ?
我们先来最简单的,网页的登录窗口; 不过开始之前,大家先下载jquery的插件 本人习惯用了vs2008来做网页了,先添加一个空白页 这是最简单的的做法。。。先在body里面插入 <
1、MySQL自带的压力测试工具 Mysqlslap mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出
前言 今天大姚给大家分享一款.NET开源(MIT License)、免费、简单、实用的数据库文档(字典)生成工具,该工具支持CHM、Word、Excel、PDF、Html、XML、Markdown等
Go语言语法类似于C语言,因此熟悉C语言及其派生语言( C++、 C#、Objective-C 等)的人都会迅速熟悉这门语言。 C语言的有些语法会让代码可读性降低甚至发生歧义。Go语言在C语言的
我正在使用快速将 mkv 转换为 mp4 ffmpeg 命令 ffmpeg -i test.mkv -vcodec copy -acodec copy new.mp4 但不适用于任何 mkv 文件,当
我想计算我的工作簿中的工作表数量,然后从总数中减去特定的工作表。我错过了什么?这给了我一个对象错误: wsCount = ThisWorkbook.Sheets.Count - ThisWorkboo
我有一个 perl 文件,用于查看文件夹中是否存在 ini。如果是,它会从中读取,如果不是,它会根据我为它制作的模板创建一个。 我在 ini 部分使用 Config::Simple。 我的问题是,如果
尝试让一个 ViewController 通过标准 Cocoa 通知与另一个 ViewController 进行通信。 编写了一个简单的测试用例。在我最初的 VC 中,我将以下内容添加到 viewDi
我正在绘制高程剖面图,显示沿路径的高程增益/损失,类似于下面的: Sample Elevation Profile with hand-placed labels http://img38.image
嗨,所以我需要做的是最终让 regStart 和 regPage 根据点击事件交替可见性,我不太担心编写 JavaScript 函数,但我根本无法让我的 regPage 首先隐藏。这是我的代码。请简单
我有一个非常简单的程序来测量一个函数花费了多少时间。 #include #include #include struct Foo { void addSample(uint64_t s)
我需要为 JavaScript 制作简单的 C# BitConverter。我做了一个简单的BitConverter class BitConverter{ constructor(){} GetBy
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是 Simple.Data 的新手。但我很难找到如何进行“分组依据”。 我想要的是非常基本的。 表格看起来像: +________+ | cards | +________+ | id |
我现在正在开发一个 JS UDF,它看起来遵循编码。 通常情况下,由于循环计数为 2,Alert Msg 会出现两次。我想要的是即使循环计数为 3,Alert Msg 也只会出现一次。任何想法都
我是一名优秀的程序员,十分优秀!