- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试在 http://thevirtuosi.blogspot.com/2011/10/linear-theory-of-battleship.html 复制发现结果.他的发现是,随后较小尺寸的船只的观察/经验概率图迫使它们离开中心,而尺寸增加的船只往往会聚集在那里。尽管他的结果在垂直轴和水平轴之间是对称的,但我编写的代码产生的结果是水平轴在船上的位置明显更多——尽管我正在尽我所能来彻底随机化垂直或水平之间的过程第一的。这种不对称性在任何大小的 map 上都存在。传达问题的最少代码是 171 行。这个论坛适合这个吗?这与家庭作业、工作或类无关。我只是对战舰算法感兴趣。输出是将某艘船放置在某个地点的实际迭代次数、这样的百分比以及这样的 ASCII 字符映射 - 以便更容易地观察不对称性。任何方向表示赞赏。
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#include <string.h>
int main(void)
{
clock_t start, end;
double cpu_time_used;
srand(time(NULL));
int obstruction;
int done;
int waysVert=0;
int waysHorz=0;
int counter=0;
int vertCounter=0;
int horzCounter=0;
int iGuess=0;
int jGuess=0;
int shipType=0;
int shotCounter=0;
int hitCounter=0;
int shipCellCounter=0;
int guessCounter=0;
int lenShip=0;
int highestProb=0;
int highestOccur=0;
int iteration=0;
int i=0;//x dimension
int j=0;//y dimension
int k=0;//z dimension and information dimension
int n=0;//number dimension
char shipChar;
char shipName[20]="";
char shipList[]="ABCDS";//question for later: How does placement order affect heat map?
int numberShips=strlen(shipList);
int jShipStart=0;
int iShipStart=0;
int polShipDir=0;
int biasShipDir=0;
int jShipDir=0;
int iShipDir=0;
int NUM_COLS=10;//x dimension
int NUM_ROWS=10;//y dimension
int NUM_LEVS=21;//[0,7] Theoretical Prob. [10,16] Empirical Prob. 20 Presentation Level
char grid[NUM_LEVS][NUM_ROWS][NUM_COLS];
long int empiricalGrid[NUM_LEVS][NUM_ROWS][NUM_COLS];
for(k=0;k<NUM_LEVS;k++){for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){ grid[k][j][i]=0;}}}//initialize 1-byte levels
for(k=0;k<NUM_LEVS;k++){for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){empiricalGrid[k][j][i]=0;}}}//initialize 4-byte levels
k=20;//k=20 is the presentation level
//for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){grid[k][j][i]='~';}}//initialize water character (first time, single run)
start = clock();
for(iteration=0;iteration<50000;iteration++){//has run at 50000000 iteration//could put scaling onto the printout using digit length of iterations
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){grid[k][j][i]='~';}}//initialize water character (every time, in loop)
//PLACEMENT ROUTINE FOR-LOOP AND DO-WHILE LOOP
for (shipType = 0; shipType < strlen(shipList); shipType++){
shipChar=shipList[shipType];
switch(shipChar){
case 'A':lenShip=5;break;
case 'B':lenShip=4;break;
case 'C':lenShip=3;break;
case 'D':lenShip=2;break;
case 'S':lenShip=3;break;//since ran out of 3, think low number as in low elevation, beneath any other ships
}
shipCellCounter+=lenShip;
k=20;
do{
jShipStart=0;
iShipStart=0;
polShipDir=0;
biasShipDir=0;
jShipDir=0;
iShipDir=0;
obstruction=0;//printf("\nSet to no obstruction to %d\n",obstruction);
if(rand()%2==0){
jShipStart=rand()%NUM_ROWS;//printf("jShipStart %d starting row (0-%d)\n",jShipStart,NUM_ROWS-1);//
iShipStart=rand()%NUM_COLS;//printf("iShipStart %d starting col (0-%d)\n",iShipStart,NUM_COLS-1);//
}
else{
iShipStart=rand()%NUM_COLS;//printf("iShipStart %d starting col (0-%d)\n",iShipStart,NUM_COLS-1);//
jShipStart=rand()%NUM_ROWS;//printf("jShipStart %d starting row (0-%d)\n",jShipStart,NUM_ROWS-1);//
}
polShipDir = (rand()%2) * 90;//printf("polShipDir %d degrees\n",polShipDir);//polarity 00 deg => prop horz, 90 = prop vert
biasShipDir = (rand()%2) * (-2) + 1;//printf("biasShipDir %d\n",biasShipDir);
if(rand()%2==0){
if( polShipDir==90 && jShipStart > lenShip-1){ jShipDir=(rand()%2) * (-2) + 1; }
if( polShipDir==90 && jShipStart <= lenShip-1){ jShipDir=(rand()%2) * (-2) + 1; }
if( polShipDir== 0 && iShipStart > lenShip-1){ iShipDir=(rand()%2) * (-2) + 1; }
if( polShipDir== 0 && iShipStart <= lenShip-1){ iShipDir=(rand()%2) * (-2) + 1; }
}
else{
if( polShipDir== 0 && iShipStart <= lenShip-1){ iShipDir=(rand()%2) * (-2) + 1; }
if( polShipDir== 0 && iShipStart > lenShip-1){ iShipDir=(rand()%2) * (-2) + 1; }
if( polShipDir==90 && jShipStart <= lenShip-1){ jShipDir=(rand()%2) * (-2) + 1; }
if( polShipDir==90 && jShipStart > lenShip-1){ jShipDir=(rand()%2) * (-2) + 1; }
}
for(n=0;n<lenShip;n++){//Check to see if ANY part of the ship runs off map.
if(rand()%2==0){
if( polShipDir== 90 && jShipStart + n * jShipDir >= NUM_ROWS || polShipDir== 0 && iShipStart + n * iShipDir >= NUM_COLS ){
obstruction=1;
}
}
else{
if( polShipDir== 0 && iShipStart + n * iShipDir >= NUM_COLS || polShipDir== 90 && jShipStart + n * jShipDir >= NUM_ROWS){
obstruction=1;
}
}
}
for(n=0;n<lenShip;n++){//Check to see if ANY part of the ship DID NOT hit water.
if( grid [ k ] [ jShipStart + n * jShipDir ] [ iShipStart + n * iShipDir ] != '~' ){
obstruction=1;
}//ANY single obstruction means start over for this ship.
}
if(obstruction==0){//If prior loop HAS NOT resulted in ANY obstruction: THEN assign this ship
for(n=0;n<lenShip;n++){
grid [ k ] [ jShipStart + n * jShipDir ] [ iShipStart + n * iShipDir ] = shipChar;
}
}
}
while(obstruction==1);
}
for(i=0;i<NUM_COLS;i++){for(j=0;j<NUM_ROWS;j++){
if(grid[20][j][i]=='A'){empiricalGrid[15][j][i]++;}
else if(grid[20][j][i]=='B'){empiricalGrid[14][j][i]++;}
else if(grid[20][j][i]=='C'){empiricalGrid[13][j][i]++;}
else if(grid[20][j][i]=='S'){empiricalGrid[10][j][i]++;}//DONT CHANGE THIS RECORD OF SEQUENCE ANYWHERE ELSE. KEEP IT HERE.
else if(grid[20][j][i]=='D'){empiricalGrid[12][j][i]++;}
}
}
}
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
for(k=16;k>=10;k--){for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){if(highestOccur<=empiricalGrid[k][j][i]){highestOccur=empiricalGrid[k][j][i];}}}}
printf("Level %d Highest occurance = %d\n",k,highestOccur);
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%10lu", empiricalGrid[15][j][i]);}printf("\n");}printf("A\n");
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%10lu", empiricalGrid[14][j][i]);}printf("\n");}printf("B\n");
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%10lu", empiricalGrid[13][j][i]);}printf("\n");}printf("C\n");
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%10lu", empiricalGrid[12][j][i]);}printf("\n");}printf("D\n");
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%10lu", empiricalGrid[10][j][i]);}printf("\n");}printf("S\n");
printf("Iterations: %d. CPU time used: %lf seconds. CLOCKS_PER_SEC = %d\n",iteration,cpu_time_used,CLOCKS_PER_SEC);
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%2.2lf ", (double) empiricalGrid[15][j][i]/highestOccur);}printf("\n");}printf("A^\n");
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%2.2lf ", (double) empiricalGrid[14][j][i]/highestOccur);}printf("\n");}printf("B^\n");
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%2.2lf ", (double) empiricalGrid[13][j][i]/highestOccur);}printf("\n");}printf("C^\n");
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%2.2lf ", (double) empiricalGrid[12][j][i]/highestOccur);}printf("\n");}printf("D^\n");
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%2.2lf ", (double) empiricalGrid[10][j][i]/highestOccur);}printf("\n");}printf("S^\n");
printf("Iterations: %d. CPU time used: %lf seconds. CLOCKS_PER_SEC = %d\n",iteration,cpu_time_used,CLOCKS_PER_SEC);
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%2c", 33+(int)(10*((double) empiricalGrid[15][j][i]/highestOccur)));}printf("\n");}printf("A^\n");
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%2c", 33+(int)(10*((double) empiricalGrid[14][j][i]/highestOccur)));}printf("\n");}printf("B^\n");
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%2c", 33+(int)(10*((double) empiricalGrid[13][j][i]/highestOccur)));}printf("\n");}printf("C^\n");
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%2c", 33+(int)(10*((double) empiricalGrid[10][j][i]/highestOccur)));}printf("\n");}printf("S^\n");//SWITCH BACK
for(j=0;j<NUM_ROWS;j++){for(i=0;i<NUM_COLS;i++){printf("%2c", 33+(int)(10*((double) empiricalGrid[12][j][i]/highestOccur)));}printf("\n");}printf("D^\n");
printf("Iterations: %d. CPU time used: %lf seconds. CLOCKS_PER_SEC = %d\n",iteration,cpu_time_used,CLOCKS_PER_SEC);
printf("!=00s, \"=10s, #=20s, $=30s, %%=40s, &=50s, \'=60s, (=70s, )=80s, *=90s\n");
return 0;
}
最佳答案
我没有看到任何会产生您所说的偏见的错误,但我确实看到了一些不必要的并发症,我可以提供一些建议:
正如 wildplasser 在评论中所建议的那样,使用 %
从 C 运行时库 rand()
中提取小的随机数通常会产生非常糟糕的性能。尝试使用 /dev/urandom
之类的更强的 RNG,甚至更好的实际随机性。至少,尝试使用它来播种另一个 RNG。
作为快速但非决定性的测试,尝试将 polShipDir = (rand()%2) * 90;
更改为 polShipDir = (1 - rand()%2) * 90;
。现在对垂直船有偏见吗?如果是这样,那么问题肯定是 RNG,因为有了好的 RNG,前面两个语句平均应该是等价的。
在两个不同的地方,您不必要地随机化执行两个无关紧要的测试的顺序!首先:
(显然需要这一行来格式化......)
if(rand()%2==0){
if( polShipDir==90 && jShipStart > lenShip-1){ jShipDir=(rand()%2) * (-2) + 1; }
if( polShipDir==90 && jShipStart <= lenShip-1){ jShipDir=(rand()%2) * (-2) + 1; }
if( polShipDir== 0 && iShipStart > lenShip-1){ iShipDir=(rand()%2) * (-2) + 1; }
if( polShipDir== 0 && iShipStart <= lenShip-1){ iShipDir=(rand()%2) * (-2) + 1; }
}
else{
if( polShipDir== 0 && iShipStart <= lenShip-1){ iShipDir=(rand()%2) * (-2) + 1; }
if( polShipDir== 0 && iShipStart > lenShip-1){ iShipDir=(rand()%2) * (-2) + 1; }
if( polShipDir==90 && jShipStart <= lenShip-1){ jShipDir=(rand()%2) * (-2) + 1; }
if( polShipDir==90 && jShipStart > lenShip-1){ jShipDir=(rand()%2) * (-2) + 1; }
}
可以简化为
if( polShipDir==90){ jShipDir=(rand()%2) * (-2) + 1; }
if( polShipDir== 0){ iShipDir=(rand()%2) * (-2) + 1; }
其次,在以注释 //Check to see if ANY part of the ship runs out map.
注释开始的循环中也发生了很多相同的事情。 (顺便说一句,你根本不需要这个测试的循环:检查船的起点和终点是否在有效位置就足够了。)
关于C "Battleship"程序在 10k+ 次迭代后的非统计输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33257659/
我正在尝试自学 C++,所以我正在做一个 Battleship 程序。我有一个 Ship、Board 和 BattleShip Driver 类。 这个版本相当标准。玩家输入一个单元格的坐标以尝试击中
题目地址:https://leetcode.com/problems/battleships-in-a-board/description/ 题目描述 Given an 2D board, cou
是否有人能够为我提供一种简单的方法,在我设计的只有一个敌人的游戏中实现第二艘敌方战舰,这是我目前的代码: import java.util.*; //Scanner that reads user i
我在随机化和将 2x2 飞船添加到游戏板时遇到了问题。我需要它看起来像下面这样: 目前我似乎只能得到一艘 1x1 的船,并且不太理解添加 2x2 并随机化以便它们全部连接的逻辑。 此外,当用户在主菜单
我正在尝试在 http://thevirtuosi.blogspot.com/2011/10/linear-theory-of-battleship.html 复制发现结果.他的发现是,随后较小尺寸的
我试图让我的网格显示如下: 1 2 3 4 A- - - - B- - - - C- - - - D- - - - 我尝试了各种方法,但似乎无法让它发挥作用。有人有什么建议吗? 此外,在
我已经为此苦苦挣扎了大约 2 个小时。出于某种原因,而不是将字符放在构成我的棋盘的 ~ 上,而是将字符放置在不是我输入的坐标的随机位置。希望有人能帮助我指引正确的方向。 void place_ship
我正在尝试自学 C++,所以我正在做一个 Battleship 程序。我有一个船舶和董事会类(class)。 这个版本相当标准。玩家输入一个单元格的坐标以尝试击中一艘船。说明船只是否被击中的程序。如果
我对这段代码有疑问: import java.util.Random; public class DotComObjects { public int[][] setBarcos(int table
人们!所以,我正在尝试使用基本的数组和方法知识用 Java 制作战舰游戏(经典!)。我创建了一个 2 维的 boolean 板来放置我的船,这是我的代码: public void placerBate
大家星期天快乐! 我正在尝试自学 C++,所以我正在做一个 Battleship 程序。 这个版本相当标准。玩家输入一个单元格的坐标以尝试击中一艘船。说明船只是否被击中的程序。如果一艘船占据的所有单元
我刚开始学习 python,在尝试编写一个简单的 1-D 版单人战舰时遇到了一些麻烦。 2 件我似乎无法解决的事情: 我创建了一个一维列表(这是游戏面板),但需要显示/打印列表重复元素的索引。换句话说
def random_row(board): return randint(0 , len(board) - 1) def random_col(board): return ra
我正在用 C++ 编写 Battleship 游戏,但无法使打印板代码正常工作。我收到以下错误 fatal error LNK1169:找到一个或多个多重定义的符号。任何帮助,将不胜感激。 代码如下:
我是一名优秀的程序员,十分优秀!