Java self-study road---DotCom

Source: Internet
Author: User

Introduction

I have been from the touch programming, has been a C + + program ape, because I like to program, the kind of grounding gas feeling, think only the use of their own management memory, psychological only practical. But with the increasing knowledge of the work, constantly from the blog and the source of new insights, I found that the era has changed, we have strong hardware resources, we have a proven framework and source code, our most valuable is spent on coding time. So I decided to take the experience of the previous C + +, to understand how Java is the language of the door, and how its programming ideas can bring me benefits.

How to learn

As I learn other languages, I mainly rely on books, e-books or paper books, so that learning is the most systematic and efficient, compared to video teaching may allow you to master more practical experience, but the efficiency is too slow.

Today's Theme

I am currently looking at a classic introductory book "Head first JAVA", the Chinese name is called "Learning JAVA in the easy Way", this book is indeed more suitable for beginners, and mainly from the stream of consciousness let you learn from the essence of JAVA, and other "textbook" like the study material compared to appear different. Because of the basis of C + +, the first few chapters on the data types, classes and objects, the operation of the object basically swept away, and today take out the summary of the exercises, it is time to start learning Java warm-up exercise bar.

This exercise is said to write a game called Dotcom, said is the game, in fact, the content is very simple, in a n*n grid, randomly generated 3 objects, the object occupies a grid of several grids, the user input grid coordinates as a hit object (the user does not know in advance which object in which lattice), If the user enters coordinates that are exactly the object's coordinates, it means hitting, where the object occupies all the squares, then the object is destroyed and the game is over.

The image comes from "Head first JAVA"

The rest of the game is not complicated, as long as the random generation of object coordinates, read user input, polling judgment, read, meet the end of the game, the only challenge is how to randomly generate a number of logical objects, that is, the object can not exceed the grid range, cannot cross the line, the object and the object cannot overlap , which is randomly generated each time.

Of course, the book gives the solution, but I think the algorithm is very cumbersome, we have the opportunity to see, in the Book of 152 pages, here is not posted code, because not directly copied ...

Because of the first knowledge of Java, just according to their own ideas to code, there must be a lot of flaws, but also ask you to help the Java great God.

Here's My Code:

1  PackageDotcomgame;2 Importjava.util.ArrayList;3  Public classGamehelper {4     Private intRow = 0;5     Private intCol = 0;6     Private intGridSize = 0;7     Private int[] Grid =NULL;8      PublicGamehelper (intnum) {9         if(num >= 0 | | num < 65535) {Tenrow =num; OneCol =num; A              -gridSize = row *Col; -Grid =New int[gridSize]; the              -              for(inti = 0; i < grid.length; i++) { -Grid[i] = 0; -             } +         } -     } +      A      PublicArraylist<integer> placedotcom (intnum) { at          -         int[]TMP =New int[gridSize]; -          -arraylist<integer> pos =NewArraylist<integer>(); -         if(num > Row | | num >Col) { -             return NULL; in         } -          to         intIncre = 1; +          -         if(Math.Round (Math.random () * 1) = = 0) { theIncre =Col; *         } $         Panax Notoginseng         BooleanSuccess =false; -         intAttempts = 0; the          while(!success && attempts++ < 512) { +System.arraycopy (grid, 0, tmp, 0, gridSize); A             //find the first place to store the             intLocation = (int) Math.ceil (Math.random () *gridSize); +             inti = 0; -              while(I <num) { $                  $                 //whether the border is more down -                 if(Location + (i * incre) >=gridSize) { -Success =false; the                      Break; -                 }Wuyi                  the                 //whether the more right border -                 if(0 = = (location + (i * incre))%Col) { WuSuccess =false; -                      Break; About                 } $                  -                 //whether it is already occupied -                 if(0! = tmp[location + (i *Incre)]) { -Success =false; A                      Break; +                 } the                  -Tmp[location + (I * incre)] = 1; $                  thePos.add (location + (i *incre)); the                  thei++; the             } -              in             if(i = =num) { theSuccess =true; theSystem.arraycopy (TMP, 0, grid, 0, gridSize); About                  Break; the             } the             Else { the pos.clear (); +             } -              the         }Bayi          the         if(Success) the             returnPos; -         Else -             return NULL; the     } the      the      Public voidDraw () { the          -          for(inti = 0; i < grid.length; i++) { the System.out.print (Grid[i]); the             if(0 = = (i+1)%Col) { the System.out.println ();94             } the         } the          the     }98      Public Static voidMain (String []args) { AboutGamehelper helper =NewGamehelper (15); -          for(inti = 0; I < 6; i++) {101Helper.placedotcom (3);102         }103 Helper. Draw ();104     } the}

The basic core of the algorithm is to randomly select an object's head coordinates, and then repeatedly determine whether each object node coordinates are legitimate, the object coordinates are all legitimate, then recorded in the grid.

Conclusion

I believe that regardless of whether or not to work in Java programming, Learning Java always has the advantage, and I do not agree, learn a language must rely on it to eat, the world of programming language a wide variety, each has his unique place, have his charm, like travel, you went to Thailand, Does not mean that you have to keep Thailand forever, and programming language is not all programming, language is only our way of solving problems and tools, we use different languages, sometimes just to achieve the same purpose: on the way to work, people on the subway, people crowded buses, people riding bicycles and even walking, The aim is to catch up with the company before it is late.

Java self-study road---DotCom

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.