许久都没写博客了,些许是前段时间有些懈怠,今天来写博客,想记录下做过的事情,怕以后电脑换了,以前做的小项目也跟着丢了,总结下最近做的一个小游戏:

游戏目的:建立一个7X7的网格,选择其中的连续的三格来作为一个目标,一共有3个目标,对每次的猜数,给一个hit,miss,kill,如果三个目标都杀完了,返回猜测次数,给个分数

游戏核心:类之间的交流  private对属性设置后部分代码的重构   ArrayList的使用

要求:main()作为一个启动程序  基本上所有的代码都放到各类中

一个类里面:属性propety和方法method

property method main 三者居然就构建了java的世界 佩服OO

headfirst中提及极限编程:意在对课题有基本了解后,先写只重功能的测试码,却不去想怎么实现,再去一一写各类,在我写这个游戏时,确也不只一次的先联想到测试码,而后再去想各类的编写,竟是如此自然

注意:这其中却遇到了一个for循环,在逻辑上一定会对res变量赋值,愚蠢的电脑却只有在运行的时候才能知道,可是由于res的赋值在for循环中,因此编译器只能报错

因此如果以后遇到这种情况姑且给变量赋值,因为既然逻辑上会被赋值,想必也会更改他的值,我们就姑且打消编译器的疑虑吧,啊哈哈哈哈

Game类:

  1. package twoClass;
  2. import java.util.ArrayList;
  3.  
  4. class Game{
  5. int numOfGuess = 0;
  6. ArrayList<DotCom> ThreeDotCom;
  7. GameHelper helper;
  8.  
  9. void SetUp(){
  10. DotCom [] Three = new DotCom[3];
  11. helper = new GameHelper();
  12. ArrayList<DotCom> tmp= new ArrayList<DotCom>();
  13. for(int i = 0; i < 3; i++){
  14. Three[i] = new DotCom();
  15. }
  16. Three[0].nameSet("pet.com");
  17. Three[1].nameSet("amaoza.com");
  18. Three[2].nameSet("tmall.com");
  19. for(int i = 0; i < 3; i++){
  20. tmp.add(Three[i]);
  21. }
  22. ThreeDotCom = tmp;
  23. for(DotCom Cell: ThreeDotCom){
  24. ArrayList<String> generator = helper.placeDotCom(3);
  25. Cell.setLocation(generator);
  26. }
  27. }
  28. void startPlaying(){
  29. String guess;
  30. String res = " ";
  31. while(!ThreeDotCom.isEmpty()){
  32. guess = helper.getUserInput("Enter a num:");
  33. numOfGuess++;
  34. for(DotCom Cell : ThreeDotCom){
  35. res = Cell.checkUserGuess(guess);
  36. if(res == "kill"){
  37. ThreeDotCom.remove(Cell);
  38. }
  39. if(res != "miss")
  40. break;
  41. }
  42. System.out.println(res);
  43. }
  44. }
  45.  
  46. void finishGame(){
  47. System.out.println("you have sunk three dots by " + numOfGuess + "guess");
  48. }
  49. public static void main(String [] args){
  50. Game myGame = new Game();
  51. myGame.SetUp();
  52. myGame.startPlaying();
  53. myGame.finishGame();
  54. }
  55. }

DotCom类:

  1. package twoClass;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class DotCom{
  6. private ArrayList<String> LocCell;
  7. private String name;
  8.  
  9. public void nameSet(String webName){
  10. name = webName;
  11. }
  12.  
  13. public void setLocation(ArrayList<String> Loc){
  14. LocCell = Loc;
  15. }
  16.  
  17. public String checkUserGuess(String guess){
  18. String res = "miss";
  19. if(LocCell.contains(guess)){
  20. res = "hit";
  21. LocCell.remove(guess);
  22. if(LocCell.isEmpty())
  23. res = "kill";
  24. }
  25. if(res == "kill"){
  26. System.out.println("ouch! You have sunk " + name);
  27. }
  28. return res;
  29. }
  30.  
  31. }

以及搬来的砖:

  1. package twoClass;
  2. import java.util.*;
  3. import java.io.*;
  4. public class GameHelper {
  5. private static final String alphabet = "abcdefg";
  6. private int gridLength= 7;
  7. private int gridSize = 49;
  8. private int [] grid = new int[gridSize];
  9. private int comCount;
  10.  
  11. public String getUserInput(String prompt){
  12. String inputLine = null;
  13. System.out.print(prompt + " ");
  14. try{
  15. BufferedReader is = new BufferedReader(
  16. new InputStreamReader(System.in));
  17. inputLine = is.readLine();
  18. if(inputLine.length() == 0 ) return null;
  19. }catch (IOException e) {
  20. System.out.println("IOException: " + e);
  21. }
  22. return inputLine;
  23. }
  24.  
  25. public ArrayList<String> placeDotCom(int comSize) {
  26. ArrayList<String> alphaCells = new ArrayList<String>();
  27. String [] alphacoords = new String [comSize];
  28. String temp = null;
  29. int [] coords = new int[comSize];
  30. int attempts = 0;
  31. boolean success = false;
  32. int location = 0;
  33.  
  34. comCount++;
  35. int incr = 1;
  36. if((comCount % 2) == 1){
  37. incr = gridLength;
  38. }
  39.  
  40. while( !success & attempts++ < 200 ){
  41. location = (int) (Math.random() * gridSize);
  42. int x = 0;
  43. success = true;
  44. while(success && x<comSize){
  45. if(grid[location] == 0){
  46. coords[x++] = location;
  47. location += incr;
  48. if(location >= gridSize){
  49. success = false;
  50. }
  51. if(x>0 && (location % gridLength == 0)){
  52. success = false;
  53. }
  54. }else{
  55. success = false;
  56. }
  57. }
  58. }
  59.  
  60. int x = 0;
  61. int row = 0;
  62. int column = 0;
  63.  
  64. while(x < comSize){
  65. grid[coords[x]] = 1;
  66. row = (int) (coords[x] / gridLength);
  67. column = coords[x] % gridLength;
  68. temp = String.valueOf(alphabet.charAt(column));
  69.  
  70. alphaCells.add(temp.concat(Integer.toString(row)));
  71. x++;
  72.  
  73. }
  74.  
  75. return alphaCells;
  76. }
  77.  
  78. }

攻击DotCom小游戏的更多相关文章

  1. [安卓] 12、开源一个基于SurfaceView的飞行射击类小游戏

    前言  这款安卓小游戏是基于SurfaceView的飞行射击类游戏,采用Java来写,没有采用游戏引擎,注释详细,条理比较清晰,适合初学者了解游戏状态转化自动机和一些继承与封装的技巧. 效果展示    ...

  2. 自制Unity小游戏TankHero-2D(2)制作敌方坦克

    自制Unity小游戏TankHero-2D(2)制作敌方坦克 我在做这样一个坦克游戏,是仿照(http://game.kid.qq.com/a/20140221/028931.htm)这个游戏制作的. ...

  3. 12岁的少年教你用Python做小游戏

    首页 资讯 文章 频道 资源 小组 相亲 登录 注册       首页 最新文章 经典回顾 开发 设计 IT技术 职场 业界 极客 创业 访谈 在国外 - 导航条 - 首页 最新文章 经典回顾 开发 ...

  4. c++制作小游戏--雷电

    用c++实现了一个小游戏--雷电,貌似执行的还不错.贴图和声效也是Duang!Duang!的.整个项目我也会给出下载链接,有兴趣的能够编译执行一下.用到了C++11的新特性,最好是使用vs2013编译 ...

  5. 小游戏大智慧,10 个让人眼前一亮的 JavaScript 游戏

    摘要: JS还可以这么玩~ Fundebug经授权转载,版权归原作者所有. 这是一篇有趣的文章,我们精选了 JS13K 游戏编程挑战的优秀作品,与大家分享.JS13K 是专为 JavaScript 开 ...

  6. 原生JS实现的h5小游戏-植物大战僵尸

    代码地址如下:http://www.demodashi.com/demo/12755.html 项目介绍 本项目是利用原生js实现的h5小游戏-植物大战僵尸,主要结合了一下自己对于h5小游戏的理解,结 ...

  7. Chrome自带恐龙小游戏的源码研究(七)

    在上一篇<Chrome自带恐龙小游戏的源码研究(六)>中研究了恐龙的跳跃过程,这一篇研究恐龙与障碍物之间的碰撞检测. 碰撞盒子 游戏中采用的是矩形(非旋转矩形)碰撞.这类碰撞优点是计算比较 ...

  8. day22 01 初识面向对象----简单的人狗大战小游戏

    day22 01 初识面向对象----简单的人狗大战小游戏 假设有一个简单的小游戏:人狗大战   怎样用代码去实现呢? 首先得有任何狗这两个角色,并且每个角色都有他们自己的一些属性,比如任务名字nam ...

  9. c++小游戏

    #include <iostream> using namespace std; double shengmingli=2000;//定义主角初始生命力 int gongjili=150; ...

随机推荐

  1. linux case 语句

    #!/bin/bash #$ 表示脚本名 #$n 表示第n个参数(n>) in ") echo '--=> A' ;; ") echo '--=> B' ;; * ...

  2. SQL Server 为索引启动硬件加速(分区)的 2 方法

    背景知识: 如果你想看<三国>这部电视剧它有 假设它有400G这么大,现在你有两个朋友他们都已经把这部剧保存在自己的硬盘上了. A用一个硬盘就把这部剧保存了下来,B用了两个硬盘才保存了一下 ...

  3. 转载【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析

    ViewPager中切换界面Fragment被销毁的问题分析  原文链接 http://www.cnblogs.com/monodin/p/3866441.html 1.使用场景 ViewPager+ ...

  4. android widget->progressbar

    其中的indeterminteDrawable属性就是用来设置进度条颜色等属性的

  5. Storyboard、Nib文件和代码来实现UI的利与弊

    很清楚,这就是iOS里面两种可视化UI的方法.加上全部用代码来实现UI,总共有三种方法可以来实现. 我们先说一下全用代码来做,这个方法属于比较极端的程序员所推崇的,优点和缺点同样明显. 优点是可以实现 ...

  6. ASP.NET MVC4(Razor)从客户端中检测到有潜在危险的 Request.Form 值

    SP.NET MVC4(Razor)从客户端中检测到有潜在危险的 Request.Form 值 “/”应用程序中的服务器错误. 从客户端(Content=" sdfdddd ..." ...

  7. POJ 1811 Prime Test 素性测试 分解素因子

    题意: 给你一个数n(n <= 2^54),判断n是不是素数,如果是输出Prime,否则输出n最小的素因子 解题思路: 自然数素性测试可以看看Matrix67的  素数与素性测试 素因子分解利用 ...

  8. SQLite.dll混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。

    其他信息: V5.7.4.4 Can't find the System.Data.SQLite.dll more info : 混合模式程序集是针对"v2.0.50727"版的运 ...

  9. 关于 .crash 分析

    这里只给出其中 一种方式. 1. 建议 桌面 建 个文件夹  appxx  ,然后 将那个闪退 对应的 包  xxx.app 放入  appxx文件夹 2. 打开终端cd命令,进入该文件夹 3.在命令 ...

  10. Javascript自由拖拽类

    基本拖拽配置 new Dragdrop({target 拖拽元素 HTMLElemnt 必选bridge 指定鼠标按下哪个元素时开始拖拽,实现模态对话框时用到 dragable 是否可拖拽 (true ...