2016-03-06 92 views
2

新手程序員在這裏,使用Java 8.我試圖構建一個PacMan遊戲,並且正在構建網格的方法。我對該計劃的開場評論告訴你你需要知道的一切。我試圖連接隨機#發生器的變量來打印相同數量的cookie(「O」),並用點(「。」)填充數組的其餘部分。如何填充隨機分佈符號的數組?

/** 
* This program is meant to get dimensions for a 2D array from the player. 
* A grid is then displayed to player's specs filled with dots and cookies. 
* The cookies must compose 20% of the total grid and be randomly 
* distributed. The user will then be offered a menu of options to either 
* turn left or right, or move, or exit the game. The player's choice moves 
* "PacMan" around grid to eat cookies. The grid must be displayed throughout 
* the game showing changes as player continues moves. If a cookie is eaten, 
* a statement is printed that indicates a cookie was eaten and adds 1 to 
* your score. At the end of the game, it tracks the number of moves it took 
* to eat all the cookies. 
*/ 

import java.util.Scanner; 

public class PacManGame 
{ 
public static void main(String[] args) 
{ 
int X, Y;  //Variables for number of grid rows X, and columns Y 

Scanner input = new Scanner(System.in); 

System.out.println(); 
System.out.print("Enter the number of rows you would like in your game grid: "); 
X = input.nextInt(); 
System.out.println(); 
System.out.print("Enter the number of columns you would like in your game grid: "); 
Y = input.nextInt(); 
System.out.println(); 

buildGrid(X, Y); // Calls buildGrid method 

} // Closes main method 

public static void buildGrid(int X, int Y) // Method for actually building the grid 
{ 
    int gameGrid [][] = new int [X][Y];  // Array built from user's input for dimensions 
    int totalGridSize = X * Y;    // Gets the total grid size 
    double cookieTotal = totalGridSize * (.2); // Calculates the 20% of cookies that will be on grid 
    int theCookies = (int)(cookieTotal*Math.random())+1; //Assigns the randomly generated number 

    int i, j, k = 0;       // Initialize loop counters 
    for (i = 0; i < X; i++) 
     { 
      for (j = 0; j < Y; j++) 
       { 
        gameGrid[X][Y] = k; 
        k++; 
        System.out.print("." + ("O" * theCookies)); // I know I can't do this, but how to fix? 
       } 
     } 
} // Closes buildGrid method 
} // Closes PacManGame class 
+1

您已經標記了你的問題爲「 java-8「而不是」java「。這就是爲什麼你有一個Java-8特定的答案。如果您想要基本的Java答案,即使您使用的是Java 8,也請使用「java」而不是「java-8」。 – Holger

回答

2

這是更好地交換陣列的座標,所以首先去Y,然後X。你可以在你的數組1中保留cookie,其餘的保留0。把你可以使用下面的代碼餅乾cookieTotal

new Random().ints(0, totalGridSize).distinct().limit(cookieTotal) 
     .forEach(pos -> gameGrid[pos/X][pos%X] = 1); 

在這裏,我們生成隨機數從0totalGridSize-1並獲得cookieTotal與此不同的。之後,我們將這些數字轉換爲座標並設置相應的數組元素。

要打印的遊戲場,你需要0轉化爲'.'和1至"O"

for (int[] row : gameGrid) 
    System.out.println(IntStream.of(row).mapToObj(val -> val == 1 ? "O" : ".") 
      .collect(Collectors.joining())); 

這裏是你的buildGrid的完全體:

int gameGrid[][] = new int[Y][X]; 
int totalGridSize = X * Y; 
int cookieTotal = totalGridSize/5; 
new Random().ints(0, totalGridSize).distinct().limit(cookieTotal) 
     .forEach(pos -> gameGrid[pos/X][pos % X] = 1); 
for (int[] row : gameGrid) 
    System.out.println(IntStream.of(row).mapToObj(val -> val == 1 ? "O" : ".") 
      .collect(Collectors.joining())); 
+0

非常感謝您的時間和幫助!所以我一直在查找你在這裏的一些代碼,這遠遠超出了我在這裏介紹的intro課程中學到的東西。我相信這是超級限制,但是你能指導我一個只包含更基本概念的解決方案嗎?我們只涉及數據類型,變量,數組,操作符,控制語句,方法和字符串構造函數。 –