2016-11-13 126 views
0

我是一名编程初学者,需要编写一些代码来创建一个简单的游戏,在屏幕上提供一个10x10矩阵板。 (上),D(下),左(左)& R(右)键盘笔画,同时在途中吃甜甜圈,并避免与墙壁达到家庭'H'位置的目标。Java甜甜圈游戏

该程序应该随机放置两个垂直的墙壁,其中包括一排5'X',以及一个水平墙壁包含一个5'X'的水平排。 两个垂直墙不应相交,但可能与水平线相交或邻接。否'X'应放置在董事会最左边,最右边,最上面或最下面的'禁止区'。

五个象征为'@'的甜甜圈应该随机放置。不能将甜甜圈放在'禁区'或墙壁上的任何广场上。没有被墙壁,甜甜圈,玩家或家庭占据的广场标有'。'。一旦一名玩家登上他吃的甜甜圈,在离开那个方格时,'@'应该被替换为'。'。

我正在努力将随机放置的墙壁和甜甜圈合并到上述规则中。

package doughnut; 

import java.util.Random; 
import java.util.Scanner; 

/** 
* 
* @author Torrin 
*/ 
public class Doughnut { 

    static int row = 10; 
    static int column = 10; 
    static char[][] table = new char[row][column]; 
    static int num = 5; 

    private static Random random = new Random(); 

    public static void main(String[] args) { 

     setArray(); 
     displayArray(); 
     //buildWallsArray(); 
     Scanner scan = new Scanner(System.in); 

    } 

    static void setArray() { 
     for (int i = 0; i < row; i++) { 
      for (int j = 0; j < column; j++) { 
       table[i][j] = '.'; 

      } 
     } 
     table[9][0] = 'P'; 
     table[0][9] = 'H'; 
    } 

    static void displayArray() { 
     for (int i = 0; i < row; i++) { 
      for (int j = 0; j < column; j++) { 
       System.out.print(table[i][j]); 

      } 
      System.out.println(); 
     } 
    } 

    /*static void buildWallsArray() { 
     int counter = 0; 
     int X, Y; 
     while (counter < num) { 
      int i = random.nextInt(8); 
      X = i + 1; 
      int j = random.nextInt(4); 
      Y = j + 3; 
      if (table[i][j] == '.') { 
       table[i][j] = 'X'; 
       ++counter; 
       System.out.print(table[i][j]); 
      } 
      System.out.println(); 
    */ 
} 

package rand; 

import java.util.Random; 

    public class Rand { 

     private static Random random = new Random(); 
    //Random placement for vertical walls 

     public static void main(String[] args) { 
      system.out.print("Running Test"); 
      vertical(); 
      horizontal(); 

     } 

     public static void vertical(String[] args) { 
      int X, Y; 
      int n = random.nextInt(7); 
      X = n + 1; 
      System.out.print("Vertical X" + "=" + X); 
      int m = random.nextInt(3); 
      Y = m + 3; 
      System.out.println("Vertical Y" + "=" + Y); 
     } 

    //Random placement of horizontal wall 
     public static void horizontal(String[] args) { 
      int X, Y; 
      int n = random.nextInt(4); 
      X = n + 3; 
      System.out.print("Horizontal X" + "=" + X); 
      int m = random.nextInt(8); 
      Y = m + 1; 
      System.out.println("Horizontal Y" + "=" + Y); 

     } 
    } 
+1

请详细说明你说你在哪里挣扎。你的输出错了吗?你有错误吗?还是有你缺少的概念? –

回答

0

开始用原始的方法:

let x = random position 
while(there is already a wall/donut at position x) { 
    x = random position 
} 
place wall/donut at position x 

这种方法将工作得很好,除非你的主板变得非常混乱 - 于是算法将不得不尝试这样很多随机职位在发现 是一个工作之前,它会减慢程序的速度。

继续使用朴素算法,直到它成为一个问题(过早优化是邪恶的)。

如果你需要,设计更聪明的东西。我只会用文字来形容这一个。如果我有10个用于放置标记的空白空间,我可以选择1到10之间的随机数字,并标记该数字。现在我只有9个空格。所以我应该选择一个1到9之间的随机数,并标记该数字,跳过已使用的方块。所以,如果我已经填满了5号插槽,并且我的随机数是6,那么我把标记放在空间7中,因为那是第6个空白空间。

所以:

  • 数很多空的空间有多么的
  • ñ是和1之间的随机数的空白空间的数量
  • 使用ñ个空的空间

我已经从1开始计算,因为我一直使用简单的英语。您可能会使用0索引的空间计数。

+0

由于这是一个初学者编码器,我强烈不鼓励提供非伪代码的其他答案。OP学习的最好方法是将伪代码自己翻译成Java。 – slim