如何创建一个数组来将字符数固定为5? [英] How do I create an array to fix number of character to 5?

查看:102
本文介绍了如何创建一个数组来将字符数固定为5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我的任务想出一个战舰游戏。我正面临着创建一个数组的问题,该数组将把船的大小固定为5个字符。任何帮助都非常感谢,谢谢。



我尝试过:



I need to come up with a boom the battleship game for my assignment. I'm facing a problem with creating an array that will fix the size of ship to 5 characters. Any kind help is truly appreciated, thanks.

What I have tried:

void initializeShipsEasy(int ships[][2]) {

	int ship, last;

	for (ship = 0; ship < SHIPS_EASY; ship++) {
		ships[ship][0] = rand() % 20;
		ships[ship][1] = rand() % 60;

		//let's check if this shot was not tried
		//if it was, just get out of the 'do while' loop when draws a pair that was not tried 
		for (last = 0; last < ship; last++) {
			if ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]))
				do {
					ships[ship][0] = rand() % 20;
					ships[ship][1] = rand() % 60;
				} while ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]));
		}

	}
}

推荐答案

这很简单:你必须计算已经使用了多少个字段,而不是停止制造船只。你还必须检查船是否没有离开操场。最好是检查起点是否提供足够的空间,否则正确移动它。



使用修复操场数组作为棋盘的全局实例:

It is very easy: you must count how many fields are already used and than stop the ship making. You must also check that the ship isnt going out of the playground. At best is to check that the starting point provides enough space and else move it correctly.

Use a fix playground array as global instance like a chess board:
char playground[60][60] = {0};// 60 x 60 and set to zero



a水平船:


a horizontal ship:

int startx = 50;// or some random
int starty = 10;// or some random
//check border x 
if( startx + 5 > 60 ) {
  startx = 60 - 5;
}
//TODO: check x on the upper side and check y !!!
for(int i = 0; i < 5; i++ ) {
  playground[startx+i][starty] = 1; //build ship 
}

如果你写一个函数它,你可以让5有输入参数来构建其他船只。

If you write a function around it, you can make the "5" has input param to build other ships.


这是一项任务 - 我会给你一些关于你需要考虑的事情和一些指示的想法 - 但你应该自己考虑一下,这样你就可以自己学会这样做了!



1 - 你需要选择一个方向让它成长(v或h )

2 - 你需要选择一个船的起点(位置范围受1限制)

3 - 你需要将它的存储填充到你想要的任何长度的船上(如5)。

4 - 您需要确保其数据与其他船舶数据不重叠



另外 - 因为您的船只是直的,你只需要选择一个点来绘制余数。您可以通过约束起始范围将其保持在游戏板上,从而以相同的方向绘制它。



现在 - 您知道需要完成的工作(填充董事会) - 但现在你需要弄清楚如何去做。



另外,学会将问题分解为最基本的部分。这是问题解决中最重要的部分 - 如果你擅长这一点,就会变得本能。
This is an assignment - I'll give you thoughts about what you need to think about and some pointers - but you should be putting in the thought yourself so you can learn to do this yourself!

1 - You need to pick a direction for it to grow (v or h)
2 - You need to pick a starting point for a ship (position range constrained by 1)
3 - you need to populate it's storage to whatever length ship you wish (like 5).
4 - you need to make sure none of its data overlaps with other ship data

Also - because your ships are straight, you only need to pick one point to draw the remainder. You can always draw it in the same direction by constraining the start range to keep it on the playing board.

Now - you know the jobs that need to be done (to populate the board) - but now you need to figure out how to do them.

Also, learn to tear a problem apart into the most elementary pieces you can. That's the most important part of the problem solving - and becomes instinctive if you get good at this.


这篇关于如何创建一个数组来将字符数固定为5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆