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

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

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

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

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

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

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

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

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

Game类:

package twoClass;
import java.util.ArrayList; class Game{
int numOfGuess = 0;
ArrayList<DotCom> ThreeDotCom;
GameHelper helper; void SetUp(){
DotCom [] Three = new DotCom[3];
helper = new GameHelper();
ArrayList<DotCom> tmp= new ArrayList<DotCom>();
for(int i = 0; i < 3; i++){
Three[i] = new DotCom();
}
Three[0].nameSet("pet.com");
Three[1].nameSet("amaoza.com");
Three[2].nameSet("tmall.com");
for(int i = 0; i < 3; i++){
tmp.add(Three[i]);
}
ThreeDotCom = tmp;
for(DotCom Cell: ThreeDotCom){
ArrayList<String> generator = helper.placeDotCom(3);
Cell.setLocation(generator);
}
}
void startPlaying(){
String guess;
String res = " ";
while(!ThreeDotCom.isEmpty()){
guess = helper.getUserInput("Enter a num:");
numOfGuess++;
for(DotCom Cell : ThreeDotCom){
res = Cell.checkUserGuess(guess);
if(res == "kill"){
ThreeDotCom.remove(Cell);
}
if(res != "miss")
break;
}
System.out.println(res);
}
} void finishGame(){
System.out.println("you have sunk three dots by " + numOfGuess + "guess");
}
public static void main(String [] args){
Game myGame = new Game();
myGame.SetUp();
myGame.startPlaying();
myGame.finishGame();
}
}

DotCom类:

package twoClass;

import java.util.ArrayList;

public class DotCom{
private ArrayList<String> LocCell;
private String name; public void nameSet(String webName){
name = webName;
} public void setLocation(ArrayList<String> Loc){
LocCell = Loc;
} public String checkUserGuess(String guess){
String res = "miss";
if(LocCell.contains(guess)){
res = "hit";
LocCell.remove(guess);
if(LocCell.isEmpty())
res = "kill";
}
if(res == "kill"){
System.out.println("ouch! You have sunk " + name);
}
return res;
} }

以及搬来的砖:

package twoClass;
import java.util.*;
import java.io.*;
public class GameHelper {
private static final String alphabet = "abcdefg";
private int gridLength= 7;
private int gridSize = 49;
private int [] grid = new int[gridSize];
private int comCount; public String getUserInput(String prompt){
String inputLine = null;
System.out.print(prompt + " ");
try{
BufferedReader is = new BufferedReader(
new InputStreamReader(System.in));
inputLine = is.readLine();
if(inputLine.length() == 0 ) return null;
}catch (IOException e) {
System.out.println("IOException: " + e);
}
return inputLine;
} public ArrayList<String> placeDotCom(int comSize) {
ArrayList<String> alphaCells = new ArrayList<String>();
String [] alphacoords = new String [comSize];
String temp = null;
int [] coords = new int[comSize];
int attempts = 0;
boolean success = false;
int location = 0; comCount++;
int incr = 1;
if((comCount % 2) == 1){
incr = gridLength;
} while( !success & attempts++ < 200 ){
location = (int) (Math.random() * gridSize);
int x = 0;
success = true;
while(success && x<comSize){
if(grid[location] == 0){
coords[x++] = location;
location += incr;
if(location >= gridSize){
success = false;
}
if(x>0 && (location % gridLength == 0)){
success = false;
}
}else{
success = false;
}
}
} int x = 0;
int row = 0;
int column = 0; while(x < comSize){
grid[coords[x]] = 1;
row = (int) (coords[x] / gridLength);
column = coords[x] % gridLength;
temp = String.valueOf(alphabet.charAt(column)); alphaCells.add(temp.concat(Integer.toString(row)));
x++; } return alphaCells;
} }

攻击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. PHP中的单例模式

    额,只是复习到这里,做点笔记吧. 单例模式.何谓也?我想就是唯一吧.唯一的意思大概希特勒已经说的很清楚了.就是我也说不明白--把代码贴上来了事. <?php // Single instance ...

  2. MVC WebApi 用户验证 (2)

    构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(66)-MVC WebApi 用户验证 (2)   前言: 构建ASP.NET MVC5+EF6+E ...

  3. 如何诊断crs 安装时 root.sh 脚本执行错误

    troubleshooting root.sh problem ------*for 10g and 11.1 1.查证公网,私网的节点名是可以互相ping通的 2.---查证OCR/Voting 文 ...

  4. #include <boost/shared_ptr.hpp>

    共享指针 这个智能指针命名为boost::shared_ptr,定义在boost/shared_ptr.hpp里.智能指针boost::shared_ptr基本上类似于boost::scoped_pt ...

  5. BZOJ 3674 可持久化并查集加强版 可持久化并查集

    题目大意:同3673 强制在线 同3673 仅仅只是慢了一些0.0 这道题仅仅写路径压缩比仅仅写启示式合并要快一点点 两个都写就慢的要死0.0 改代码RE的可能是内存不够 #include<cs ...

  6. OAuth2.0开发指南

    OAuth2.0开发指南 1.认证与登录 来往开放平台支持3种不同的OAuth 2.0验证与授权流程: 服务端流程(协议中Authorization Code Flow): 此流程适用于在Web服务端 ...

  7. jQuery下实现检测指定元素加载完毕

    检测元素出现方法.虽然是基于 jQuery 的,但是代码很简洁,可以修改成纯js版的. 文本 jQuery.fn.wait = function (func, times, interval) { v ...

  8. maven profile参数动态打入

    第一: 1,如果是resources目录下文件profile参数中动态打入,在pom.xml中的build标签中加入如下配置: <resources> <resource> & ...

  9. T-SQL事务

    事务 订火车票的时候,下一个订单,这个订单中,包含多个购买信息,要么全部执行,要么全部不执行,合作事务就是来处理这种模型的一种机制. --关键字:transaction 或 tran 简写形式 --开 ...

  10. [jQuery]无法获取隐藏元素(display:none)宽度(width)和高度(height)的新解决方案

    在做茶城网改版工作的时候,又遇到一个新问题,我需要用jQuery写一个通过点击左右图标来翻阅图片的小插件,写好后测试可以正常运行,但是放到Tab中后发现只有第一个Tab中的代码能够正常运行,其它全部罢 ...