Skip to content

Instantly share code, notes, and snippets.

@furai-no-ffff
Last active September 12, 2018 08:01
Show Gist options
  • Save furai-no-ffff/f52edcc8f8ce014cb076 to your computer and use it in GitHub Desktop.
Save furai-no-ffff/f52edcc8f8ce014cb076 to your computer and use it in GitHub Desktop.
部屋数sim
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WIDTH 5
#define HEIGHT 3
#define N_ROOMDATA 0x70
int roomdata[N_ROOMDATA];
int rand_range(int min, int max)
{
return min + (((max - min + 1) * (rand() & 0xFF)) >> 8);
}
void init_roomdata(void)
{
int i;
for (i=0; i<N_ROOMDATA; i++){
int x = i & 0xF;
int y = (i >> 4) & 0xF;
roomdata[i] = (x == 0 || WIDTH < x || y == 0 || HEIGHT < y) ? 0x80 : 0xFF;
}
roomdata[0x23] = 0xFE;
}
void init_roomdata_treasury(void)
{
init_roomdata();
roomdata[0x11] = 0xFE;
roomdata[0x12] = 0xFE;
roomdata[0x21] = 0xFE;
roomdata[0x22] = 0xFE;
}
int room_occupied(x, y, w, h)
{
int cx,cy;
for (cx=x; cx<x+w; cx++){
for (cy=y; cy<y+h; cy++){
if (roomdata[cy<<4 | cx] != 0xFF){
return 1;
}
}
}
return 0;
}
void room_occupy(x, y, w, h, roomno)
{
int cx,cy;
for (cx=x; cx<x+w; cx++){
for (cy=y; cy<y+h; cy++){
roomdata[cy<<4 | cx] = roomno;
}
}
}
/*floor_type: 1 for 1 <= floor <= 6
* 2 otherwise */
int make_rooms(int floor_type)
{
int i,j;
int roomno = 0;
for (i=0; i<10; i++){
for (j=0; j<5; j++){
int w,h;
int x,y;
if (floor_type == 1){
w = h = 1;
}else{
w = rand_range(1, 2);
h = rand_range(1, 2);
}
x = rand_range(1, WIDTH - w + 1);
y = rand_range(1, HEIGHT - h + 1);
if (!room_occupied(x, y, w, h)){
room_occupy(x, y, w, h, roomno++);
break;
}
}
}
return roomno;
}
int main(int argc, char *argv[])
{
int i;
int floor_type;
int ntimes;
int roomnos[11] = {0};
int treasury = 0;
srand(time(NULL));
if (argc <= 1){
floor_type = 2;
}else{
floor_type = atoi(argv[1]);
}
if (argc <= 2){
ntimes = 100000;
}else{
ntimes = atoi(argv[2]);
}
if (argc > 3){
if (argv[3][0] == 't'){
treasury = 1;
printf("T\n");
}
}
for (i=0; i<ntimes; i++){
if (treasury){
init_roomdata_treasury();
}else{
init_roomdata();
}
roomnos[make_rooms(floor_type)]++;
}
for (i=0; i<=10; i++){
printf("%d:%d ", i, roomnos[i]);
}
return 0;
}
/*
$ ./room-sim 1 1000000 # 1 <= F <= 6
0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:228 8:15497 9:246222 10:738053
$ ./room-sim 2 1000000 # 7 <= F
0:0 1:0 2:9 3:998 4:19952 5:117651 6:296356 7:345994 8:180007 9:36913 10:2120
$ ./room-sim 1 1000000 t # 1 <= F <= 6 treasury
T
0:0 1:0 2:0 3:0 4:2 5:142 6:7790 7:108288 8:422145 9:403983 10:57650
$ ./room-sim 2 1000000 t # 7 <= F treasury
T
0:0 1:16 2:2672 3:51368 4:227391 5:380827 6:260748 7:70131 8:6649 9:196 10:2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment