MineSweeperGame powered by Java Swing

First Entry on is-programmer.com

Kavinyao posted @ Feb 16, 2010 08:01:25 PM in 学习心得 with tags Learning java c , 14889 阅读

My first semester in college ended a month ago and the next is just a few days ahead. As a student of software engineering department, programming is definitely a skill you must acquire. This blog serves as a place to share my studies, findings and essays of programming, maybe a few other topics included.

 

I love programming. Teacher not covering much, I learned C mostly by myself last semester and find it a powerful and elegant language. Many say that so old a language C is that it is out of fashion. I'm not an expert for the time being, but I can say that after learning C, I found learning Java easier. I just skipped the syntax style part as Java inherited that mostly from C. That doesn’t mean syntax style is not important. On the contrary, it’s rather important because it helps people who read the code understand what it is and how it works. A program can be excellent when the style is horrible at the same time. Just take the example of winners of IOOOC, look at one and your first impression must (oh, will probably) be “damn, I can’t believe this is a piece of code!” But all of them worked out well, some being tiny but powerful! Back to main topic, C is still an important language. Just take a peek at the Tiobe Index , C is still a popular language with strong power. Generally known, C is a free style language and a language so free that the compiler may think different about the code from you. It has various kinds of traps and pitfalls and makes beginners confused. Sometimes even a senior programmer makes mistakes. But once you can produce a clear-style, high-quality C code, you will take a much fast step forward whether to other languages or deeper into C, just because languages born after C are more abstract, which means they have more and more restrictions and make things that difficult to deal with in C easier to handle at the same time.

 

Clarify again, I’m still a student, not an expert, all what I said just stand for my opinion. You may wonder why I do this. To show off? No. I wrote this for future reference. A student I am, though, I still try my best to make my points clear, accurate and strict. And even I tried to, after a few months, I read the entry again and I will find that there may be points wrong or groundless.  Then I’ll revise it. Everyone needs a process of improvement, don’t they? And you, please don’t bother to point out my mistakes and speak out your own opinion. You are welcomed to do that.

 

Currently I’m learning Java, which will be included in Computing and Software Engineering, one of the lessons I’ll take next semester. Java is different from C as it is an Object-Oriented Language but C is a procedural-based language. Changing from C to Java can be painful, oh, is painful, more exactly as I find, as the angle to view a problem is wholly changed. So my Blog subtitle is Understanding OOP currently. I’ll keep my effort on it.

 

Last word I’d like to say is, I’m using English to practice it.

 

Now, let me test the code highlighter. :)

 

 

package chap05;

import java.io.*;
import java.util.*;

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 = 0;


  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.toLowerCase();
  }

  
  
 public ArrayList<String> placeDotCom(int comSize) {                 // line 19
    ArrayList<String> alphaCells = new ArrayList<String>();
    String [] alphacoords = new String [comSize];      // holds 'f6' type coords
    String temp = null;                                // temporary String for concat
    int [] coords = new int[comSize];                  // current candidate coords
    int attempts = 0;                                  // current attempts counter
    boolean success = false;                           // flag = found a good location ?
    int location = 0;                                  // current starting location
    
    comCount++;                                        // nth dot com to place
    int incr = 1;                                      // set horizontal increment
    if ((comCount % 2) == 1) {                         // if odd dot com  (place vertically)
      incr = gridLength;                               // set vertical increment
    }

    while ( !success & attempts++ < 200 ) {             // main search loop  (32)
	location = (int) (Math.random() * gridSize);      // get random starting point
        //System.out.print(" try " + location);
	int x = 0;                                        // nth position in dotcom to place
        success = true;                                 // assume success
        while (success && x < comSize) {                // look for adjacent unused spots
          if (grid[location] == 0) {                    // if not already used
             coords[x++] = location;                    // save location
             location += incr;                          // try 'next' adjacent
             if (location >= gridSize){                 // out of bounds - 'bottom'
               success = false;                         // failure
             }
             if (x>0 & (location % gridLength == 0)) {  // out of bounds - right edge
               success = false;                         // failure
             }
          } else {                                      // found already used location
              // System.out.print(" used " + location);  
              success = false;                          // failure
          }
        }
    }                                                   // end while

    int x = 0;                                          // turn good location into alpha coords
    int row = 0;
    int column = 0;
    // System.out.println("\n");
    while (x < comSize) {
      grid[coords[x]] = 1;                              // mark master grid pts. as 'used'
      row = (int) (coords[x] / gridLength);             // get row value
      column = coords[x] % gridLength;                  // get numeric column value
      temp = String.valueOf(alphabet.charAt(column));   // convert to alpha
      
      alphaCells.add(temp.concat(Integer.toString(row)));
      x++;

      // System.out.print("  coord "+x+" = " + alphaCells.get(x-1));
      
    }
    // System.out.println("\n");
    
    return alphaCells;
   }
}

 

 

 

kavinyao 说:
Mar 03, 2012 09:53:10 PM

Hi guys,
How time flies! I'm now in the second semester of my junior year. Though I only posted articles sporadically here, the accumulated pieces remind me of some precious memories. I think I've got something to write.

During the two years, I learned various things like desperate (though I haven't mastered few). After I've learned about and used different technologies by myself, I have some preliminary conclusions to share with you here.

First is the comparison of languages. In the post above, the past me had learned only (rudimentary) C and Java and the comparison made was limited on the syntax. After then, I've learned C++, Python, JavaScript, C#, PHP, shell scripts, tried Scheme, CoffeeScript, R, and had shallow impressions on an assort of others. Admittedly, I'm a language enthusiast. But the point here I want to make is, there's no perfect language and every existing language has its unique strengths and weaknesses. Currently, my favorite language is Python, but it sucks in some cases as well.

Syntax may be the most obvious, but never the fundamental, difference between languages. The philosophy is. C is intended to be a higher level assembly with hardware in mind. C++ is designed with an emphasis on efficiency comparable to C and thus chose the more practical simula approach to OO. Java's designer thinks developers are all idiots and are prone to make mistakes, so they make Java as rigid and strict as possible. C# is an revenge on Java and has outpaced the latter after release 2.0. Python and Ruby put emphasis on programmer productivity, abstract away tedious parts of daily job and bring much more fun to programming. PHP is quicker and dirtier than Perl and is hotchpotch of shell, perl, C, Java and some other languages. JavaScript's handicapped FP with a disguise of Java. The list can go on and on.

However, jumping from one language from another regularly is far more evil than sticking to a degraded language. As a language has philosophy behind, one stick to some language shares the philosophy too. And a language vagrant has no philosophy at all.

I'm also happy to inform you all that thinking and writing has become a habit. I find them both fantastic. I've got a personal blog: http://hackab.it. Feel free to drop in and keep in touch. :)

jimy 说:
Oct 20, 2022 07:36:10 AM

Good to reach your post. I was looking for comments on https://www.huffpost.com/archive/ca/entry/to-pay-or-not-to-pay-someone-to-write-my-essay-for-me_b_14793970  forum for some quality tip for my next exams.Your work is really incredible. Keep it up.

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter