c - 需要帮忙进行数组赋值

标签 c arrays double variable-assignment

我需要一些关于作业的帮助,其文档如下:

Your task is to simulate a game between two teams of nine players. The reason for nine players is to facilitate reasonable printing out of the playing field onto the screen or into an output file. Smashball is played on a 25 by 25 square field. When printing out game results, be sure to print out the field with a border around the field.

  1. Game Initialization

a. Use a random number generator to randomly place all the players in the playing field. If the random process produces two players starting on the same position, redraw new random positions such that all players start on unique spots.

b. Use a random number generator to generate an initial motion direction for each player. The four motion choices for this assignment are East, West, North and South. These directions correspond to the respective movement of each player on the playing field.

我的代码(到目前为止)如下:

#include <stdio.h>
#include <stdlib.h>

#define SIZE_AREA 25
#define SIZE_TEAM 25

int team [SIZE_TEAM];
int field [SIZE_AREA][SIZE_AREA];
int main (){

int i;
int j;

for (i = 0; i < SIZE_TEAM; i++){
team [i] = field [(rand() % 24)][(rand() % 24)];
}

enum move_direction {East, West, North, South};

for (j = 0; j < SIZE_TEAM; j++){

它不完整,但我仍然在这里或那里遇到一些障碍:

  1. 如果随机过程产生两个在同一位置开始的玩家怎么办?我需要将一名球员的位置与其他球员的位置进行比较,看看它是否独特。如果不是,那么我必须重新绘制位置,但是如何在 C 中递归地进行此操作?如果重新绘制的位置仍然与其他玩家的位置重复怎么办?

  2. 游戏初始化部分要求我随机生成玩家的初始运动方向,但我无法将方向分配给玩家,因为我会覆盖他们的位置。如何在不覆盖玩家初始位置的情况下为玩家编写方向,以及如何在回合之间改变他们的方向?

我要说的就是这些了。再次强调,在发表评论之前请阅读链接,并让我知道您是否可以帮助我(但请不要给我完整的答案。只需指出我的担忧来源并告诉我应该做什么)。

最佳答案

  1. 对于第二个及后续玩家,从空闲位置中随机选择。一种简单的方法是重新排列位置列表。将洗牌列表中的位置 N 分配给玩家 N。
  2. 考虑保留两个数据结构来表示游戏状态,一个表示当前位置,另一个表示下一个位置。在计算下一个仓位结束时,交换它们并清除新的下一个仓位结构。

关于c - 需要帮忙进行数组赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21356081/

相关文章:

jqGrid 单元格编辑 - 双击编辑?

java - 如何将 Double 中的秒数转换为两个 int 秒数和毫秒数?

java - 使用另一个数组中的字符串元素创建数组名称

java - 在 Java 中处理 NaN 的正确方法是什么?

c - FreeRtos在ADC任务和Streaming任务中的问题

c - 用不同的指针重新分配重叠指针

c - Intel 和 AMD 处理器有相同的汇编程序吗?

c - 如何在 C 中使用 unicode 正确解码 url

java - 我无法在 Java 中通过套接字发送对象

C++用平均值填充部分填充的二维数组