java - Java语言的战舰游戏
问题描述
我需要一些关于我的学校项目代码的帮助。我用 Java 做了一些简单的游戏,它在 5x5 板上,随机定位的船只。
我被一些我无法独自管理的事情困住了,我需要更多的大脑来帮助我。我需要制作一艘需要将自己定位在船上 2 个位置的船,所以如果你正在与 PC 比赛,你需要两次射击才能击沉那艘船。
代码如下:
public static void main(String[] args) {
int[][] board = new int[5][5];
int[][] ships = new int[3][2];
int[] shoot = new int[2];
int attempts=0,
shotHit=0;
initBoard(board);
initShips(ships);
System.out.println();
do{
showBoard(board);
shoot(shoot);
attempts++;
if(hit(shoot,ships)){
hint(shoot,ships,attempts);
shotHit++;
}
else
hint(shoot,ships,attempts);
changeboard(shoot,ships,board);
}while(shotHit!=3);
System.out.println("\n\n\nBattleship Java game finished! You hit 3 ships in "+attempts+" attempts");
showBoard(board);
}
public static void initBoard(int[][] board){
for(int row=0 ; row < 5 ; row++ )
for(int column=0 ; column < 5 ; column++ )
board[row][column]=-1;
}
public static void showBoard(int[][] board){
System.out.println("\t1 \t2 \t3 \t4 \t5");
System.out.println();
for(int row=0 ; row < 5 ; row++ ){
System.out.print((row+1)+"");
for(int column=0 ; column < 5 ; column++ ){
if(board[row][column]==-1){
System.out.print("\t"+"~");
}else if(board[row][column]==0){
System.out.print("\t"+"*");
}else if(board[row][column]==1){
System.out.print("\t"+"X");
}
}
System.out.println();
}
}
public static void initShips(int[][] ships){
Random random = new Random();
for(int ship=0 ; ship < 3 ; ship++){
ships[ship][0]=random.nextInt(5);
ships[ship][1]=random.nextInt(5);
//let's check if that shot was already tried
//if it was, just finish the do...while when a new pair was randomly selected
for(int last=0 ; last < ship ; last++){
if( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) )
do{
ships[ship][0]=random.nextInt(5);
ships[ship][1]=random.nextInt(5);
}while( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) );
}
}
}
public static void shoot(int[] shoot){
Scanner input = new Scanner(System.in);
System.out.print("Row: ");
shoot[0] = input.nextInt();
shoot[0]--;
System.out.print("Column: ");
shoot[1] = input.nextInt();
shoot[1]--;
}
public static boolean hit(int[] shoot, int[][] ships){
for(int ship=0 ; ship<ships.length ; ship++){
if( shoot[0]==ships[ship][0] && shoot[1]==ships[ship][1]){
System.out.printf("You hit a ship located in (%d,%d)\n",shoot[0]+1,shoot[1]+1);
return true;
}
}
return false;
}
public static void hint(int[] shoot, int[][] ships, int attempt){
int row=0,
column=0;
for(int line=0 ; line < ships.length ; line++){
if(ships[line][0]==shoot[0])
row++;
if(ships[line][1]==shoot[1])
column++;
}
System.out.printf("\nHint %d: \nRow %d -> %d ships\n" +
"Column %d -> %d ships\n",attempt,shoot[0]+1,row,shoot[1]+1,column);
}
public static void changeboard(int[] shoot, int[][] ships, int[][] board){
if(hit(shoot,ships))
board[shoot[0]][shoot[1]]=1;
else
board[shoot[0]][shoot[1]]=0;
}
解决方案
跟随您的设计伙伴。
public static void showShips(int[][] ships)
{
for (int ship = 0; ship < ships[0].length; ship++) {
System.out.println("Ship: " + (ship + 1));
System.out.println("Position 1: (" + (ships[ship][0]+1) + "," + (ships[ship][1]+1) + ")");
}
System.out.println("\t1 \t2 \t3 \t4 \t5");
System.out.println();
for (int row = 0; row < 5; row++) {
System.out.print((row + 1) + "");
for (int column = 0; column < 5; column++) {
boolean flag = false;
for (int ship = 0; ship < ships[0].length; ship++) {
if (row == ships[ship][0] && column == ships[ship][1]) {
flag = true;
}
}
if (flag) {
System.out.print("\t" + "x");
} else {
System.out.print("\t" + "~");
}
}
System.out.println();
}
}
推荐阅读
- qt - Qt GUI 导致 CPU 开销
- airflow - 通过 WebUI 触发时,Airflow DAG 会因 RecursionError 而爆炸
- r - 自定义自动 R Markdown 报告
- php - Docker MySQL mysqli::real_connect():(HY000/2002):连接被拒绝
- python - Python + CPLEX:没有名为“docplex”的模块
- django - Django CreateView - 只允许创建 n 个对象。达到限制时重定向
- c# - 使用外部模型引用访问 WCF 服务的 Swagger UI 时出错
- python - 如果在 1/100 秒内请求相同的烧瓶 api,则插入后 sqlalchemy 不会更新
- python - 尝试用一个数据帧的元素做最少 2 个数据帧
- ios - 打印 tableview 的计数时出现问题