控制台上简易的海战棋:

在这里插入代码片
import java.util.Scanner;public class Battleship {public static int pB = 7;public static int pC = 9;public static int pP = 5;public static int cB = 7;public static int cC = 9;public static int cP = 5;public static String[][] drawBoard(int width, int height) {String[][] board = new String[width][height];for (int i = 0; i < width; i++) {for (int j = 0; j < height; j++) {board[i][j] = "_";}}//draw the battleshipint finishBattleship = 1; while(finishBattleship > 0) {  int x = (int)(Math.random() * (height - 1)); int y = (int)(Math.random() * (width - 4)); board[x + 1][y] = "B";board[x + 1][y + 1] = "B";board[x + 1][y + 2] = "B";board[x + 1][y + 3] = "B";board[x + 1][y + 4] = "B";board[x][y + 1] = "B";board[x][y + 2] = "B";finishBattleship--;}//draw the chasersint finishChaser = 3;while (finishChaser > 0) {int x = (int)(Math.random() * (width - 3));int y = (int)(Math.random() * height);if (board[x][y] == "_" && board[x + 1][y] == "_" && board[x + 2][y] == "_") {  board[x][y] = "C";board[x + 1][y] = "C";board[x + 2][y] = "C";finishChaser--;}}//draw the planesint finishPlane = 5;while (finishPlane > 0) {int x = (int)(Math.random() * width);int y = (int)(Math.random() * height);if (board[x][y] == "_") {board[x][y] = "P";finishPlane--;}}return board;}public static String[][] controlBoard(int width, int height) {String[][] board = new String[width][height];for (int i = 0; i < width; i++) {for (int j = 0; j < height; j++) {board[i][j] = "+";}}return board;}public static int[] attack(String[][] computer, String[][] control) {boolean attack = false;    Scanner input = new Scanner(System.in);while (!attack) {   System.out.println("Please choose the row where the bomb is thrown(1-10)");int row = input.nextInt();System.out.println("Please choose the column where the bomb is thrown(1-10)");int col = input.nextInt();int[] result = new int[3];result[1] = row - 1;result[2] = col - 1;if (row >= 1 && row <= 10 && col >= 1 && col <= 10 && control[col - 1][row - 1] == "+") {if (computer[col - 1][row - 1] == "B") {System.out.println("You hit the enemy's battleship!");result[0] = 1;cB--;return result;  //1 stands for battleship} else if (computer[col - 1][row - 1] == "C") {System.out.println("You hit the enemy's chaser!");result[0] = 2;cC--;return result;  //2 stands for chaser} else if (computer[col - 1][row - 1] == "P") {System.out.println("You hit the enemy's plane!");result[0] = 3;cP--;return result; //3 stands for plane} else {System.out.println("You hit nothing");result[0] = -1;return result; //-1 stands for nothing}} else {System.out.println("Undefined position");}}return null;}public static int[] computerAttack (int width, int height, String[][] player, String[][] control) {boolean attack = false;int[] result = new int[3];while (!attack) {int col = (int)(Math.random() * width);int row = (int)(Math.random() * height);if (control[col][row] == "+") {result[1] = row;result[2] = col;if (player[col][row] == "B") {System.out.println("Your battleship was hit!");result[0] = 1;control[col][row] = "X";pB--;return result;} else if (player[col][row] == "C") {System.out.println("Your chaser was hit!");result[0] = 2;control[col][row] = "X";pC--;return result;} else if (player[col][row] == "P") {System.out.println("Your plane was hit!");result[0] = 3;control[col][row] = "X";pP--;return result;} else {result[0] = -1;control[col][row] = "X";return result;}}}return null;  }public static String[][] changeBoard(String side, String[][] board,int[] change) { if (change[0] == 1 || change[0] == 2 || change[0] == 3) {board[change[2]][change[1]] = "X";return board;} else {board[change[2]][change[1]] = "_";return board;}}public static int number (int[] change) {return change[3];}public static boolean process () {if (cB <= 0 && cC <= 0 && cP <= 0) {System.out.println("You win!");return false;} else if (pB <= 0 && pC <= 0 && pP <= 0){System.out.println("You lose!");return false;} else {if (cB == 0) {System.out.println("You have destoryed the enemy's battleship!");cB--;} if (cC == 0) {System.out.println("You have destoryed the enemy's chasers!");cC--;} if (cP == 0) {System.out.println("You have destoryed the enemy's planes!");cP--;}if (pB == 0) {System.out.println("Your battleship was destoryed!");pB--;} if (pC == 0) {System.out.println("Your chasers were destoryed!");pC--;} if (pP == 0) {System.out.println("Your planes were destoryed!");pP--;}return true;}}public static void main(String[] args) {// TODO Auto-generated method stubString[][] player = drawBoard(10, 10);String[][] computer = drawBoard(10, 10);String[][] playerControl = controlBoard(10, 10);String[][] computerControl = controlBoard(10, 10);while (process()) {for (int i = 0; i < 10; i++) {for (int j = 0; j < 10; j++) {System.out.print(player[i][j] + " ");}System.out.println();}System.out.println();  System.out.println();for (int i = 0; i < 10; i++) {for (int j = 0; j < 10; j++) {System.out.print(playerControl[i][j] + " ");}System.out.println();}  int[] playerChange = attack(computer, playerControl);playerControl = changeBoard("player", playerControl, playerChange);int[] computerChange = computerAttack(10, 10, player, computerControl);player = changeBoard("computer", player, computerChange);computerControl = changeBoard("computer", computerControl, computerChange);process();    }}}

java控制台制作海战棋相关推荐

  1. Java控制台游戏~600行代码实现打怪小游戏

    Java控制台游戏~600行代码实现打怪小游戏(多图预警) 一,先放个启动界面(一些英雄,怪物技能介绍跟装备属性都写在里边): 二,在这个简单的小游戏里,你可以体验到: 1.打怪: 2.随机玩法寻宝: ...

  2. 使用java swing制作人机五子棋

    使用java swing制作人机五子棋 背景 算法原理 棋盘分值更新范围 更新分值方法 判断分值方法 局势分数权重 设计模式 战局类Battle AI类 Integration UI设计 窗口类UIi ...

  3. java 柱状图下载_(JFreeChart)Java图表制作

    (JFreeChart)Java图表制作软件时一个灵活的设计,很容易扩展,和目标服务器端和客户端应用程序;,支持多种图表类型. 输出类型也是多样化.包括Swing组件.图像文件(包括PNG和JPEG) ...

  4. java word转html 烟火,Java多线程制作烟花效果.doc

    Java多线程制作烟花效果 Java多线程应用实例: 制作烟花效果 本例知识点一句话讲解新学知识使用Thread类管理线程已学知识Math类产生随机数使用Color类设置颜色使用Graphics类绘制 ...

  5. JasperReports是一个开源的java报表制作引擎

    JasperReports是一个开源的java报表制作引擎 http://jasperreports.sourceforge.net iReport是JasperReports的一个GUI工具,用来生 ...

  6. nbu无法运行java控制台_Netbackup:nbu常见错误及故障解决

    Veritas Netbackup 提供了强大的故障响应功能, 能够有效及时的处理 各种备份故障. 主要有备份状态码(status) .错误信息.报告信息及调试日志. 下面我们主要针对备份状态码讲解下 ...

  7. linux控制台单人五子棋简书,Java控制台版五子棋的简单实现方法

    这篇文章主要给大家介绍了关于Java控制台版五子棋的简单实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 设计一个10*1 ...

  8. 2048——Java控制台版本

    前情提要,祝2021年的蓝桥杯小伙伴成绩突出. 贪心.杂凑.递归.分类.评分.哈希.共识.聚类.分治.启发式.粒子群.最大期望.最小完成时间.序列最小优化....算法无数,但是基础类似,无非坐标处理. ...

  9. java相册_精致小巧的java相册制作方法

    本文实例为大家分享了java相册制作方法,供大家参考,具体内容如下 注: 1)html上的图片是静态指定的.当更新了新的图片时必须手工更新.所以使用Servlet读取本地images中的所有图片,动态 ...

最新文章

  1. day22 案例 发送邮箱激活码 购物车 分析
  2. oc 计算 带括号 式子
  3. java 图像处理 空白_使用Java进行图像处理的一些基础操作
  4. linux if 判断文件数量,linux if判断命令
  5. git: command not found
  6. Linux——JDK的部署
  7. linux 改目录前缀,Linux修改终端显示前缀及环境变量
  8. java使用Encoding导什么包_String getEncoding()
  9. [Ajax] jQuery中的Ajax -- 02-jQuery中的三级联动
  10. Leetcode -MySQL-178. 分数排名
  11. mysql大事务commit快_MYSQL事务他快你慢,都是你自己惹的祸
  12. steam授权_号商福利,Steam验号机器人上线,再也不用手动验号了
  13. SegNet(持续更新)
  14. 如何修改论文,能够避开查重?
  15. 【Robo 3T】MongoDB可视化工具-- Robo 3T使用教程
  16. Python读取文件找出重复元素
  17. 支付宝 alipay.fund.trans.uni.transfer(单笔转账接口)功能整合
  18. Excel换行显示的几种方法,你知道吗?
  19. PreScan 教程:0. PreScan与Matlab连接
  20. Unity 数学基础

热门文章

  1. APP自动化测试-5.触屏操作及toast处理
  2. Arduino框架下最便宜的开发芯片-CH552初探
  3. springMVC导入excle poi
  4. Nepxion Discovery学习笔记4 Sentinel流量防卫兵/服务容错综合方案
  5. 几种不同的系统重装方法有什么区别
  6. 程序员掌握新技术,知晓最新行业资讯的网站合集。
  7. python-使用百度AI开放平台实现人像动漫化、黑白图片上色、图像风格转化
  8. 0基础也能学!软件测试自学路线(附带学习资料)
  9. 【U8】凭证手工编号或者整理凭证按照凭证日期重排是灰色的
  10. 胡宏开:4.8数字货币操作建议及盘面分析总结