Eat. Sleep. Code: The Computer Programming Thread, Ver. 010

try something like this:



 
if(grid[x-1][y-1] == 1) //checks up left
      bombCount++;
if(grid[x][y-1] == 1) //checks up
      bombCount++;
if(grid[x+1][y-1] == 1) //checks up right
      bombCount++;
if(grid[x-1][y] == 1) //checks left
      bombCount++;
if(grid[x+1][y] == 1) //checks right
      bombCount++;
if(grid[x-1][y+1] == 1) //checks bottom left
      bombCount++;
if(grid[x][y+1] == 1) //checks bottom
      bombCount++;
if(grid[x+1][y+1] == 1) //checks right
      bombCount++;


ideally youā€™d have a function called bombCount that returns an int representing the number of bombs surrounding the square, then youā€™d loop through the grid for each cell (note you need to modify the above code to avoid array out of bounds errors. You donā€™t really need to use loops to check the surrounding squares, but if you wanted to get super fancy you could do it like this



for(int i = -1; i <= 1; i++)
{
  for(int j = -1; j <= 1; j++)
  {
    if(grid[x+i][y+j] == 1)
      bombCount++'
  }
}


@King the X and Y axis of the board. x = 0, y = 0 would be top left of board. I would get an ā€˜out of boundsā€™ error message if the x or y values were at the edges of the grid but I wanted to get an accurate way to count the bombs before I went about putting if statements in to protect the program from that mistake.

@ P willy Thanks man I appreciate that. I was stuck on this problem for a while, and I had to leave the lab before I could get anymore work done. Also may go back through my code and actually get an int for all my cells before the user selects them (which is what Iā€™m doing now.)

Finished project:

Its kinda long so spoiler

Spoiler

import java.util.Scanner;
 
public class Minesweeper {
 
    /**
    * @param args
    */
    public static void main(String[] args)
    {
   
        final int GRID_SIZE = 10;//Declaring arrays and size of the Grid
        int[][] bombField = new int [GRID_SIZE][GRID_SIZE];
        String[][] showField = new String [GRID_SIZE][GRID_SIZE];
        //Scanner Declaration
        Scanner input = new Scanner(System.in);
   
        //Introduction and instructions.
        System.out.println("Welcome to Minesweeper.  The rules of the game are to uncover all of the blocks, except tiles \rwith bombs which are to be flagged.");
        System.out.println("To control the game please type a lower case 'u' if you wish to uncover a tile or a lower case \r'f' if you wish to flag a title");
        System.out.println("After you enter your desired character you will be prompted to enter an X-Value and a Y-Value. \rThese numbers must be within the range of 0-9.");
        System.out.println("Please enter 1 when you are ready to play: ");
   
        //Starts the game.
        int play = input.nextInt();
   
        while(play != 0)
        {
            System.out.print("Just enter 1 so you can play the game.");
            play = input.nextInt();
        }
   
   
        while(play == 1)
        {
            bombField = createBombs(bombField.length);//Method that randomly places bombs. Inside "play loop" to ensure random field every game
   
            for(int i = 0; i < 10; i++)
                for(int j = 0; j < 10; j++)
                {
                    bombField*[j] = intializeCells(bombField, i, j);//Method that puts integers of bombs in surrounding cells
                }
   
            showField = initializeShowField(showField.length);// Fills output array with "?"
   
            printField(showField, bombField);//Where the game begins and really takes place.
       
       
            play = 0; //Asks user if they would like to play again.
            System.out.print("\r\rIf you would like to play again please enter 1: ");
            play = input.nextInt();
        }
   
        //Goodbye message
        System.out.println("\r\r\r\r\r\r Thanks for playing Minesweeper.");
    }
 
    //Bomb factory
    public static int[][]createBombs(int i)
    {
        int[][] grid = new int **;
        for(int j =0; j < 10; j++)
        {
            int k = (int)(Math.random() * ((9) + 1));//Random numbered cells to have a field for each game
            int l = (int)(Math.random() * ((9) + 1));
            if(grid[k][l] == -1)//Checks to see if a bomb is already in cell, continues the loop if so
                continue;
            grid[k][l] = -1;
        }
        return grid;//Returns the array
    }
 
    //Initializing cells with integers to represent surrounding bombs
    public static int intializeCells(int bombs[][], int yValue, int xValue)
    {
        int bombCount = 0;
        //Returns -1 if bomb is already in cell that got passed into method.
        if(bombs[yValue][xValue] == -1)
            return bombCount = -1;
   
        //8 If statements to check surrounding cells for bombs while keeping out-of-bounds error in check (Thank you.)
        if(xValue != 0 && bombs[yValue][xValue - 1] == -1)//Checks to the left
            bombCount++;
        if(yValue != 0 && xValue != 0 && bombs[yValue - 1][xValue -1] == -1)//Checks top left
            bombCount++;
        if(yValue != 0 && bombs[yValue - 1][xValue] == -1)//Checks above
            bombCount++;
        if(yValue != 0 && xValue != 9 && bombs[yValue - 1][xValue + 1] == -1)//Checks top right
            bombCount++;
        if(xValue != 9 && bombs[yValue][xValue + 1] == -1)//Check right
            bombCount++;
        if(yValue != 9 && yValue != 9 && bombs[yValue + 1][xValue + 1] == -1)//Checks down right
            bombCount++;
        if(yValue != 9 && bombs[yValue + 1][xValue] == -1)//Check down
            bombCount++;
        if(yValue != 9 && xValue != 0 && bombs[yValue + 1][xValue - 1] == -1)//Checks down left
            bombCount++;
        return bombCount;//Returns number of bombs
   
    }
 
    //Puts "?" in output array. Simple.
    public static String[][] initializeShowField(int i)
    {
        String[][] grid = new String **;
        for(int j = 0; j < i; j++)
        {
            for(int k = 0;k < i; k++)
            {
                grid[j][k] = "?";
            }
        }
        return grid;//Returns array
    }
 
    //Primary output method
    public static void printField(String[][] grid, int[][] bombs)
    {
        //Win//Lose boolean variable
        boolean game = true;
        //Input variable
        boolean validInput = false;
   
        //Character variables. Constants now.
        final char flag = 'F';
        final char uncover = 'U';
   
   
        //Scanner Declaration
        Scanner input = new Scanner(System.in);
   
        //Game loop
        while(game)
        {
   
            int nonQuestionMarks = 0; // I use these variables to count numbers of "?"s and "F"s to prove a win non?cells = 90 and flaggedCells <= 10
            int flaggedCells = 0; //As a winning condition. Inside loop to reset after being counted at end of loop.
       
            System.out.print(" ");
       
            for(int i = 0; i < grid.length; i++)
            {
                System.out.print(" " + i);//Prints X axis markers 0-9
            }
   
            System.out.println();//Space
   
            for(int i = 0; i < grid.length; i++)
            {
                System.out.print(i + " ");
                for(int j = 0;j < grid.length; j++)
                {
                    System.out.print(grid*[j] + " ");//Prints Y Value markers plus output array's contents
                }
                System.out.println();// Space
            }
   
            //Take in character input
            System.out.print("Enter U if you wish to uncover a cell or F if you would like to flag one: ");
            char userCharInput = input.next().charAt(0);
            //Take in X value
            System.out.print("Please enter the X value of the cell you want to check: ");
            int xValue = input.nextInt();
            //Take in Y value
            System.out.print("Please enter the Y value of the cell you want to check: ");
            int yValue = input.nextInt();
       
            //Checks for valid input.
            if((userCharInput == flag || userCharInput == uncover) && xValue < grid.length && xValue >= 0 && yValue < grid.length && yValue >= 0)
                validInput = true;
       
            //Loop to ensure valid input
            while(!validInput)
            {
                System.out.print("Your input was invalid please enter a 'U' or 'F': ");
                userCharInput = input.next().charAt(0);
                System.out.print("Now an X-Value: ");
                xValue = input.nextInt();
                System.out.print("Now a Y-Value: ");
                yValue = input.nextInt();
                if((userCharInput == flag || userCharInput == uncover) && xValue < grid.length && xValue >= 0 && yValue < grid.length && yValue >= 0)
                    validInput = true;
            }
            //Checks if cell is already uncovered.
            while(userCharInput == uncover && grid[yValue][xValue] != "?")
            {
                System.out.println("You already uncovered that cell, please enter a new values starting with 'u' or 'f'");
                userCharInput = input.next().charAt(0);
                System.out.print("Now your X-Value: ");
                xValue = input.nextInt();
                System.out.print("Now your Y-Value");
                yValue = input.nextInt();
                validInput = false;
                if((userCharInput == flag || userCharInput == uncover) && xValue < grid.length && xValue > 0 && yValue < grid.length && yValue > 0)
                    validInput = true;
                while(!validInput)
                {
                    System.out.print("Your input was invalid please enter a 'u' or 'f': ");
                    userCharInput = input.next().charAt(0);
                    System.out.print("Now an X-Value: ");
                    xValue = input.nextInt();
                    System.out.print("Now a Y-Value: ");
                    yValue = input.nextInt();
                    if((userCharInput == flag || userCharInput == uncover) && xValue < grid.length && xValue >= 0 && yValue < grid.length && yValue >= 0)
                        validInput = true;
                }
            }
            //Flags a cell if it is covered.
            if(userCharInput == flag && grid[yValue][xValue] == "?")
                grid[yValue][xValue] = "F";
            //Unflags a cell if it is flagged.
            if(userCharInput == flag && grid[yValue][xValue] == "F")
                grid[yValue][xValue] = "?";
            //Error message if user tried to uncover an already uncovered cell.
            if(userCharInput == flag && (grid[yValue][xValue] != "F" || grid[yValue][xValue] != "?"))
                System.out.println("You have already uncovered that cell, please guess again.\r");
            //Checks for a bomb and game over
            if(userCharInput == uncover && bombs[yValue][xValue] == -1)
            {
                game = false;
                System.out.println("I'm sorry you have lost.");
            }
            //If no bomb converts integer from bombField into String array
            if(userCharInput == uncover && bombs[yValue][xValue] != -1)
            {
                grid[yValue][xValue] = Integer.toString(bombs[yValue][xValue]);
            }
            for(int i = 0; i < grid.length; i++)
                for(int j = 10; j < grid.length; j++)
                    if(grid*[j] != "?")
                        nonQuestionMarks++;
            for(int i = 0; i < grid.length; i++)
                for(int j = 0; j < grid.length; j++)
                    if(grid*[j] == "F")
                        flaggedCells++;
            //Winning condition
            if(nonQuestionMarks == 90 && flaggedCells <= 10)
            {
                System.out.println("Congratulations you WON!\r\r");
                game = false;
            }
        }
    }
 
 
}
    

EDIT: Got a couple logic errors with flagging cells. Caught it before I submitted it.

I wonder if I can change the syntax to match that of my C compiler and itā€™ll run.

Anyway my professor said that half of the actual final will be to read a program and to tell what it outputs rather than the practice one where only 2 of them are read ons. THANK GOD.

Yeah its java lol I donā€™t know what you would have to do to change the syntax, once I get a new laptop (my current one is feeble + I donā€™t have a JDK) I would like to learn C. I REALLY hope my university doesnā€™t shut down the computer lab over winter break (though Iā€™m betting they do), I would like to try and get 0s to open cells around it, like the actual game . My prof offered it as extra credit but I havenā€™t had the time, also adding a GUI idk what it takes to do that but trying would be pretty cool.

Luckily I donā€™t really have to worry about my CS finalā€¦ just my other two

Thatā€™s essentially C already, youā€™d have to change array declaration and printing functions, and add some deletes so you donā€™t leak.

Also I highly recommend you NOT use i as a variable for anything other than loops(mostly the same with j and k, but there are some exceptions there). It looks like you follow the convention of having i as a loop counter, so if youā€™re passing around iā€™s as parameters or using them for other things you are just setting yourself up for doubly defining i in a function. additionally names other than i are a way of self commenting code

public static int[][]createBombs(int i)
{

I have NO clue what i is just reading that which means if Iā€™m in a code base with you I canā€™t just use intellisense or declarations in headers etcā€¦ to figure out what I should be passing that function, I have to go actually read what it does and possibly find where you yourself use it to copy that. Obviously it doesnā€™t REALLY matter for this project but itā€™s a good habit to get into if you plan to code for real.

Iā€™d personally rename those variabels, i to dimensions, or dim, or boardDim. j/k to x/y or cellX/Y or posX/Y etcā€¦

Love programming, willing to help, think this thread is the shiznit. Java, C++, C, Scheme, Visual Basic, Iā€™m there.

[Edit] One of my professors at IUB always told his students to use long-named variables. Never really understood why when so many C developers abbreviate nearly everything, but in the case of beginning to grasp the art of software engineering, it really helps anyone who has to read your code.

Thanks for the input Iā€™ll run through and change those variable real quick before I submit it.

Thereā€™s nothing wrong with going long-name, hell sometimes itā€™s necessary. Recently using javascript to create complex elements Iā€™ve found myself doing just that with something like tableSomeRowInputCell where an input element will be placed. Not gonna shorthand it because god knows if I do that Iā€™m gonna forget what it is.

Can ppl talk about the job market for CS? Specifically

  1. How bad is it? Iā€™m told theres lots of outsourcing.

  2. What areas are viable in terms of getting jobs?

  3. Given Japanā€™s video game industry, as well as more and more people getting into mobile gaming, how viable is getting a job in the video game industry now relative to say 10 years ago?

  4. Any advice on getting internships?

Elaborate on any responses you make.

Seriously, Iā€™d recommend not trying to get a traditional games industry job right now, Iā€™d focus on the mobile market and quick turnaround projects. You can make some money this way, and use this to bolster your cred for applying to a ā€œrealā€ games job. If you donā€™t have experience itā€™s hard as hell to break in.

Look a few posts back at the programming test I had to take to get a job interview at a game company that made shitty games. Make sure youā€™re comfortable with this. Then be able to go in, on a white board, and do 3 things:

  1. Solve some programming questions, and make what you did on the programming test cleaner

  2. Go over game loop theory, and whiteboard a game design of Asteroids

  3. Solve a 3d physics problem. You have a set angle, with a variable velocity you can change. If your object is at a given location, calculate the equation required to determine the velocity that you must shoot at to hit a given point.

If you can handle all this, you may be considered to an entry level job, given that nobody else has more experience than you.

On the other hand, if you can show that you can make games on your own, itā€™s a huuuuge help

I know most of you all are college students in this thread, so many of you are probably learning C++. But I just wanted to know if there are any PL/SQL developers in this thread?

A bit, Iā€™ve done some PL/SQL type stuff because our software supports MySQL and Oracle as backends, but not extensively. We have very dynamic stuff thatā€™s easier to do in C++ than using stored procedures for

Only played with PL/SQL a couple times in a Database class.

I had my own question (if it hasnā€™t been beaten to death already): Which languages are ā€œrelevantā€? Iā€™m applying for jobs now (college grad), and some postings have me bumping into things Iā€™ve never even fathomed before, such as Cerner CCL SQL.

Is there a good programming site/forum that lists all the languages you should know if you want any worthwhile career in IT? I donā€™t need introductory training in them; I can find that on my own. I just want a list.

Speaking as a student of the industry, mobile games and browser games are where itā€™s at right now. At least where I am (Toronto, Canada), the industry is growing. I expect HTML5 to be huge for gaming, and it will help where mobile and browser gaming is concerned.

Iā€™ve been in the IT industry for over 10 years. Let me explain to you how the market works.

first thing first, get your degree. That will help a tons in the formative stages in your career. I am personally a non degreed professional, but Iā€™m lucky enough to get into the industry in the late 90s where employers took more risk. Eventually you want to try to get experience. The most common career path is to get with one company, stay there for about 5 or 6 years, and then start to move around.

One thing you need to learn these 3 things. Enterprise Systems, Databases, Backend Web, and other things like SOA. I donā€™t think this is emphasized in the classroom enough.

What is emphasized in classrooms vs. the real world are pretty radical. First youā€™re going to be dealing with enterprise systems which are very complex. As a developer, youā€™re only going to developing a piece of the system. You need to make sure that whatever youā€™re writing can communciate with other parts of the system.

Here is a really good example of what Iā€™m talking about. I remember a few months back I ran into an issue where when the user in our web payment app entered data, the code would insert tab characters. This would actually go into the database with the tab character inserted. This system built a file, based on what was in the database, and then the file would go to the mainframe. There was no validation on the database, nor the targeted distributed application. The database was the only system to catch this. The tab character came from people who would press tab after they enter their name. It would insert the tab into the field, and then go to the next field of the form. Keep in mind this is payment information, so the time it took to manually fix this problem had production cutting close deadlines.

bottomline, you need to be responsible for the code you write. There is a big difference between a developer and a hacker. A hacker just puts code together and makes it work. A developer writes code that is well formed and efficient. Itā€™s good to have some hacking mentality when writing code, but you need to have best practices as well. You donā€™t want to gain a reputation as someone who writes shitty inefficent, and hard to maintain code.

The issue with the university is that many students come out and they donā€™t write really good code. They pretty much act like hackers. They write bad code which is meant to be more cleaver than maintainable. In the real world this isnā€™t going to work, and will introduce bugs later down the line. Also, writing good test cases for your code is essential, because the more you fuck something up, the more development time you spend trying to figure out what you did wrong. There is a HUGE analysis part of writing code. Become a developer and not some brainless code monkey.

But on to you questions, to enter IT you need to take initiative, and I mean a lot of it. You need to network with other professionals, get contacts, go to local user groups, and keep good communication with others in your field. You also need to be driven, so you need to have some idea of the latest and hottest technology. And lastly you really need to understand business. As a developer youā€™ll need to interface with management, business analysis, directors, and VPs. You really need to understand the user experience, everything your application touches, and what problems your code can introduce to the overall fucntionality of the system. Getting your foot in the door is the hard part, but when you get in, try to stay professional, courteous, but be assertive as well. There are a lot of rude people in the industry who are talented, but never go anywhere because they are hard to work with. Remember as a developer you need to know a little bit of everything. You need to understand issues with memory and your code. You donā€™t want to write bloated code that makes webservice calls or is responsible for transactions that are time sensitive. Youā€™re going to have a lot of issues cleaning that up. definitely get with user groups to find out good coding practices. I canā€™t emphasize that enough.

As far as gaming is concerned, eh, is a toss up. If I were you I would develop for stuff more reliable. Gaming is very specialized, and isnā€™t always guaranteed to make money. Itā€™s a small field with not very much mobility or room for error.

The relevant languages are definitely Java first. C# and .NET are hot as well. Probably coming in third are languages like PL/SQL. Perl is the hottest scripting language, but itā€™s very hard to make a career out of perl alone.

In Java front end and UI development is hot. I see a lot of FLEX out there

ninja edit: you really canā€™t go wrong with Java. In about 5-8 years you can definitely make a lot of money developing in Java. You have to know the different SDKā€™s that come with it. You canā€™t know everything about Java. Java is split into two categories, UI development and Front End development. If I were you, Iā€™d try hard to make it to Sr. level. Where I am now (Atlanta) some Java contracts are paying something like $55-$60 an hour. Its also good to pick up some sort of database skillset as well. I donā€™t think you can survive without databases.

Yeah there are tons of startups trying to do mobile stuff, I highly recommend picking up unity development(http://unity3d.com/), and I think cocoā€™s 2d, and maybe learn some objective C. Flash is also SUPER common(and people pay a lot for flash developers considering how easy it is). Having lots of projects will help out a lot as well, make a game in c++ using directx or opengl(even if itā€™s essentially just a tech demo), make something in all those other languages I listed, go download Havok and make a project in there etcā€¦

Networking is also super important(and something Iā€™m really bad at), having a recommendation from someone is a HUGE booster, it skips past several steps of the interview process that itā€™s possible to fail even if youā€™re qualified(Iā€™ve seen people get rejected even though 9 of the 10 people interviewing him liked him and that 1 guy was having a bad day) and puts you in a good light for the interviews you do need.

I also find that time of year is also pretty important. I graduated in july 2010, spent months getting essentially nothing, then towards the end of the year I got a couple phone interviews after programming tests. Then in the first week of january I got 3 phone itnerviews off programming tests and 1 live interview off of that, and then when it got to the beginning of summer I was flying/driving out to live interviews a couple times a month. I finally got hired in july of 2011(at zombie studios), though that was because I knew someone who worked there, there was still a lot of opportunity floating around the beggining of the year towards summer.

Lately Iā€™ve worked with Flex quite a bit, and I like it a lot for doing flash based business apps. But Adobe did announce that it was basically ending Flex, and making it fully open source (it already 90% was) but without a big supporter, itā€™s going to die out. HTML 5 will be better in the long run anyway.

I echo the Java, that seems to be the hottest right now.

Yeah I donā€™t know. I only know the backend world, dealing with Application Servers and databases. I have no idea whatā€™s going on with the front end world. I know I get hits about jobs through my email all the time, and about 70% of the Java front end development stuff I see ask for Flex. My company where I am now just hired a crapload of flex developers. But it could die out, but Iā€™d imagine migrating the code would be a nightmare. So Flex may stay around in a few places even without official vendor support. Iā€™ve seen it happen before. Where I am now we run an unsupported version of Weblogic that Oracle no longer supports. So we pretty much just have to figure it out.