如何用自定义对象填充二维数组?迅速

时间:2019-06-26 08:19:51

标签: arrays swift oop grid

我试图在有网格板和Fish和Orca等角色的项目中进行制作。我创建了Animal的父类和两个继承了Animal的子类。我想用5%的逆戟鲸和35%的鱼填充木板(n * m)。如何用海怪和鱼填充网格?在这里,我试图用数字填满董事会。

class Animal {


}
class Fish: Animal{
}
class Orca:Animal{
}
class Board{
private var content: [[Int?]]
private static func setupForNewGame(width: Int,height: Int)->[[Int]]{

    var matrix:[[Int]] = Array(repeating: Array(repeating: 0, count: width), count: height)
    let cellCount = width * height

    var penguinCount = Double(round(Double(cellCount) * 50.0 / 100.0))
    var grampusCount = Double(round(Double(cellCount) * 5.0 / 100.0))

    var arr:[Int] = Array(repeating: 0, count: cellCount)
    for i in 0...cellCount - 1{
        if (penguinCount > 0){
            arr[i] = 1
            penguinCount = penguinCount - 1
        }else if (grampusCount > 0){
            arr[i] = 2
            grampusCount = grampusCount - 1
        }else{
            arr[i] = 0
        }
    }
    let shuffledArr = arr.shuffled()
    var counter = 0
    for i in 0...width - 1{
        for j in 0...height - 1{
           matrix[i][j] = shuffledArr[counter]
            counter = counter + 1
        }

    }

    return matrix

}
}

1 个答案:

答案 0 :(得分:2)

声明类型为#include <ros/ros.h> #include <example_msg/Num.h> #include <math.h> int main(int argc, char **argv) { ros::init(argc, argv, "example_ros_message_publisher"); // name of this node ros::NodeHandle n; // two lines to create a publisher object that can talk to ROS ros::Publisher my_publisher_object = n.advertise<example_msg::Num>("example_topic", 1); //"example_topic" is the name of the topic to which we will publish // the "1" argument says to use a buffer size of 1; could make larger, if expect network backups example_msg::Num my_new_message; //create a variable of type "example_msg", // as defined in this package ros::Rate naptime(1.0); //create a ros object from the ros “Rate” class; //set the sleep timer for 1Hz repetition rate (arg is in units of Hz) // put some data in the header. Do: rosmsg show std_msgs/Header // to see the definition of "Header" in std_msgs my_new_message.header.stamp = ros::Time::now(); //set the time stamp in the header; my_new_message.header.seq=0; // call this sequence number zero my_new_message.header.frame_id = "base_frame"; // would want to put true reference frame name here, if needed for coord transforms my_new_message.demo_int= 1; my_new_message.demo_double=100.0; double sqrt_arg; // do work here in infinite loop (desired for this example), but terminate if detect ROS has faulted while (ros::ok()) { my_new_message.header.seq++; //increment the sequence counter my_new_message.header.stamp = ros::Time::now(); //update the time stamp my_new_message.demo_int*=2.0; //double the integer in this field sqrt_arg = my_new_message.demo_double; my_new_message.demo_double = sqrt(sqrt_arg); my_publisher_object.publish(my_new_message); // publish the data in new message format on topic "example_topic" //the next line will cause the loop to sleep for the balance of the desired period // to achieve the specified loop frequency naptime.sleep(); } } 的{​​{1}}数组,并使用matrix代替[[Animal?]]

nil