java 海战模式发生器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 海战模式发生器相关的知识,希望对你有一定的参考价值。
/** Sea Battle
* Pattern Generator
* v. 1.1
*/
import java.util.Random;
public class PatternGenerator {
private static final int boardSize = 10;
public static void main(String[] args) {
int[] shipsArray;
shipsArray = new int[boardSize * boardSize];
createEmptyBoard(shipsArray);
String pattern;
for (int i = 0; i < 100; i++)
{
placeShip(4, shipsArray);
placeShip(3, shipsArray);
placeShip(3, shipsArray);
placeShip(2, shipsArray);
placeShip(2, shipsArray);
placeShip(2, shipsArray);
placeShip(1, shipsArray);
placeShip(1, shipsArray);
placeShip(1, shipsArray);
placeShip(1, shipsArray);
pattern = printPattern(shipsArray);
displayPattern(shipsArray);
System.out.printf("Pattern" + i + ": ");
System.out.println(pattern);
createEmptyBoard(shipsArray);
}
}
private static void placeShip(int decks, int[] board) {
Random random = new Random();
int xPos;
int yPos;
boolean checkPlace;
boolean isHorizontally = random.nextBoolean();
if (isHorizontally)
{
do
{
xPos = random.nextInt(boardSize - (decks - 1));
yPos = random.nextInt(boardSize);
checkPlace = (checkPlaceHorizontally(xPos, yPos, board, decks));
} while (!checkPlace);
for (int i = xPos; i < (xPos + decks); i += 1)
board[i + (yPos * boardSize)] = decks;
} else
{
do
{
xPos = random.nextInt(boardSize);
yPos = random.nextInt(boardSize - (decks - 1));
checkPlace = checkPlaceVertically(xPos, yPos, board, decks);
} while (!checkPlace);
for (int i = (yPos * boardSize); i < ((yPos * boardSize) + (decks * boardSize)); i += boardSize)
board[i + xPos] = decks;
}
}
private static boolean checkPlaceHorizontally(int xPos, int yPos, int[] board, int decks) {
int[] checkList = new int[(decks + 2) * 3];
int counter = 0;
int result = 0;
for (int h = -boardSize; h < boardSize + 1; h += boardSize)
{
for (int i = xPos - 1; i < (xPos + decks + 1); i += 1)
{
result = ((i + h) + (yPos * boardSize));
if (xPos == 0 && i == xPos - 1)
result = -666; // FAKE RESULT (!)
if (xPos == (decks + 2) && result % boardSize == 0)
result = -666; // FAKE RESULT (!)
if (result > -1 && result < (boardSize * boardSize))
{
checkList[counter] = result;
counter++;
}
}
}
if (checkArea(board, checkList) > 0)
{
return false;
} else
{
return true;
}
}
private static boolean checkPlaceVertically(int xPos, int yPos, int[] board, int decks) {
int[] checkList = new int[(decks + 2) * 3];
int counter = 0;
int result = 0;
for (int h = -1; h < 2; h++)
{
for (int i = (yPos * boardSize) - boardSize;
i < ((yPos * boardSize) + (decks * boardSize) + boardSize); i += boardSize)
{
result = i + (xPos + h);
if (xPos == 0 && h == - 1)
{
result = -666; // FAKE RESULT (!)
}
if (xPos == boardSize - 1 && h == 1)
{
result = -666; // FAKE RESULT (!)
}
if (result > -1 && result < (boardSize * boardSize))
{
checkList[counter] = result;
counter ++;
}
}
}
if (checkArea(board, checkList) > 0)
{
return false;
} else
{
return true;
}
}
private static int checkArea(int[] board, int[] area) {
int checkSum = 0;
for (int i = 0; i < area.length; i++) {
if (i == 0) {
checkSum += board[area[i]];
} else {
if (area[i] > 0) {
checkSum += board[area[i]];
}
}
}
return checkSum;
}
private static void displayPattern(int[] board) {
String defaultMark = "~";
for (int i = 0; i < board.length; i++) {
if (board[i] != 0) {
defaultMark = String.valueOf(board[i]);
} else {
defaultMark = "~";
}
System.out.printf(defaultMark + ((i + 1) % boardSize == 0 ? "\n" : " "));
}
}
private static String printPattern(int[] board) {
String defaultMark = "0";
String pattern = "";
for (int i = 0; i < board.length; i++)
{
defaultMark = String.valueOf(board[i]);
if (board[i] > 0)
defaultMark = "1";
pattern = pattern + defaultMark;
}
return pattern;
}
private static void createEmptyBoard(int[] board) {
for (int i = 0; i < (boardSize * boardSize); i++)
board[i] = 0;
}
}
以上是关于java 海战模式发生器的主要内容,如果未能解决你的问题,请参考以下文章