Jeph Jacques's comics discussion forums

  • 20 Feb 2019, 09:06
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 2 [3] 4   Go Down

Author Topic: A programming thread!  (Read 16371 times)

Pilchard123

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,814
  • I always name them Bitey.
Re: A programming thread!
« Reply #100 on: 07 Jun 2014, 01:51 »

Have you installed a webserver and PHP interpreter?
Logged
Piglet wondered how it was that every conversation with Eeyore seemed to go wrong.

pwhodges

  • Admin emeritus
  • Awakened
  • *
  • Offline Offline
  • Posts: 15,718
  • I'll only say this once...
    • My home page
Re: A programming thread!
« Reply #101 on: 07 Jun 2014, 05:22 »

Php is installed for the browser

Um, php is a server-side technology.  You need a web server to handle it, not a file server like FileZilla.
Logged
"Being human, having your health; that's what's important."  (from: Magical Shopping Arcade Abenobashi )
"As long as we're all living, and as long as we're all having fun, that should do it, right?"  (from: The Eccentric Family )

hedgie

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,566
  • Can't be buggered at all
Re: A programming thread!
« Reply #102 on: 07 Jun 2014, 07:17 »

It's been a couple of years since I've touched PHP, but isn't it just easy enough to install a LAMP stack on a stripped-down linux install in a VM?
Logged
"A person who may charitably described as 'not entirely a waste of resources, but mostly so.'"  -- Momo

"Pray that there's intelligent life somewhere out in space, 'Cause there's bugger all down here on Earth!"  -- The Pythons

Pilchard123

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,814
  • I always name them Bitey.
Re: A programming thread!
« Reply #103 on: 07 Jun 2014, 07:34 »

It's easy enough to install it on Windows, as well. Only without the Linux bit and with a few language changes too.
Logged
Piglet wondered how it was that every conversation with Eeyore seemed to go wrong.

cesium133

  • Methuselah's mentor
  • *****
  • Online Online
  • Posts: 5,341
  • Has a fucked-up browser history
    • Cesium Comics
Re: A programming thread!
« Reply #104 on: 07 Jun 2014, 07:48 »

Logged
The nerdy comic I update on Saturdays: Cesium Comics

Unofficial character tag thingy for QC

Pilchard123

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,814
  • I always name them Bitey.
Re: A programming thread!
« Reply #105 on: 07 Jun 2014, 07:53 »

Logged
Piglet wondered how it was that every conversation with Eeyore seemed to go wrong.

hedgie

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,566
  • Can't be buggered at all
Re: A programming thread!
« Reply #106 on: 07 Jun 2014, 17:13 »

It's easy enough to install it on Windows, as well. Only without the Linux bit and with a few language changes too.
For a test environment, I could see using Windows.  But I never would think of using it for a production environment.  The only thing I can think of using a Windows server for is Exchange.
Logged
"A person who may charitably described as 'not entirely a waste of resources, but mostly so.'"  -- Momo

"Pray that there's intelligent life somewhere out in space, 'Cause there's bugger all down here on Earth!"  -- The Pythons

pwhodges

  • Admin emeritus
  • Awakened
  • *
  • Offline Offline
  • Posts: 15,718
  • I'll only say this once...
    • My home page
Re: A programming thread!
« Reply #107 on: 08 Jun 2014, 00:01 »

For a test environment, I could see using Windows.   But I never would think of using it for a production environment.

There's nothing like a good set of blinkers! (I run Apache, Tomcat, PostgreSQL, etc on Windows at work, and the same plus Deluge and Kerio Connect at home.  But I use Open BSD for firewalls).

Quote
The only thing I can think of using a Windows server for is Exchange.

I hate Exchange with a passion, though I've been running it for 15 years; it's just that for the user Exchange plus Outlook is still unrivalled by any alternative.
Logged
"Being human, having your health; that's what's important."  (from: Magical Shopping Arcade Abenobashi )
"As long as we're all living, and as long as we're all having fun, that should do it, right?"  (from: The Eccentric Family )

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #108 on: 08 Jun 2014, 12:13 »

Huh. The programming task for this week is actually a lot more complex than I expected.

Our task is to model the game Battleships. Given are a ShipType and a Direction enum. We've got a number of methods to implement, and at first I thought it would be a relatively easy task. But the more I look into it, the more modelling decisions I need to make. So far I've got an FieldType[][], which is basically the field, and gets initialised with water in every element. (the size is a parameter of the constructor and can't be changed afterwards, as specified in the task)

Now I'm working on the demanded "addShip" method. I guess it'd be the right decision to create a Ship class, and make every ship an instance of it, so I know, which fields belong to which ship and don't have to iterate over the whole array to find out whether there are still ships left. I'm thinking about saving direction and starting point of each ship (which are the parameters given to the addShip method), so every ship knows, which fields in the array belong to it.

Any better ideas?

yeah, and I need to be finished with this task tomorrow at 13:00.

Edit: and once again I'm not allowed to use anything but Java.lang
« Last Edit: 08 Jun 2014, 12:21 by ankhtahr »
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #109 on: 08 Jun 2014, 14:22 »

argh. Accessing multidimensional object arrays without making them public sucks. Especially if you can't use java.util.

Array.copyOf and things like that could be useful here, but I have to do everything by hand.

Maybe I'll even do a setVerticalLine/getVerticalLine and setHorizontalLine/getHorizontalLine…
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,362
  • No time for Claireification
    • Things To Remember
Re: A programming thread!
« Reply #110 on: 08 Jun 2014, 14:25 »

Why can't you iterate over the array? It only has 10x10 fields anyway.

If you do it the other way around, you'd have to iterate over the list of boats, which would have to iterate over the fields they occupy, which just seems more, well, cumbersome.

Do you have to make a visual representation of the field? In that case I guess you could do the boat thing. In any other case, I guess it would be irrelevant to the code if the field you hit was a cruiser or an airship.

Beware that I'm only musing and am not a reliable reference.

If you do need to know which ship you're using, how about using a Map? In this case, I would create a Point -> Ship kind of map, aka, you input a point (for example, 4,6) and it either returns the reference to the ship that occupies that point, or a Null reference (as a stand-in for water). Then you can do fancy things like check if the ship is still alive after that shot, and removing that ship from the list of alive ships (which may be valuable if you want to check for a game-ended condition).

If you have any questions to my line of thinking, please ask. I'm not sure I know how I'd implement it in detail, but I'd love to work it out together with you. What are the methods you have to implement? What are their parameters, and their return values (and throws declarations, while we're at it).

If you want to, I can go online and we can have a chat on mumble. I love practical programming exercises like these.

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,362
  • No time for Claireification
    • Things To Remember
Re: A programming thread!
« Reply #111 on: 08 Jun 2014, 14:27 »

Hmm. I didn't read that part about only being allowed access to java.lang libraries. Which ones do they entail?

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,362
  • No time for Claireification
    • Things To Remember
Re: A programming thread!
« Reply #112 on: 08 Jun 2014, 14:28 »

By the way, if you really want to chat, send me a pm as i'll react to that quicker than to a forum post.

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #113 on: 08 Jun 2014, 14:44 »

At first: Thanks!

java.lang

What I'm worried about is, that the tutors correcting these tasks value modelling above everything.

I need to implement two toString methods, creating the opponents and the players view. In the players view I need to differentiate between the different types of ships.

Here's an example output (toStringWithShips() on the top, toStringWithBombs() on the bottom. Their names, not mine.):
Code: [Select]
.1.......
.....2...
..3..2...
..3......
4.3.4444.
4....1...
4......22
4........
.........
..1.333..


......XO..
..X.......
......O...
.O.O......
..........
.......O..
...O......
.......O..
..X.......

And as this is supposed to work for any field sizes and any number of ships, and there are always less ships than fields, I thought it a good idea to iterate over the ships only.

The way I'm currently working on it is that every ship is an object, I have an array of ships, and every ship has a getter which tells me, whether it's sunk or not (by checking every field it's placed on, an information I can get by saving the end point, direction and ship type with every ship).

What I'm currently struggling with is changing the type of the fields the ship is placed on in the constructor, as the field is private in the "Sea" class.

And I actually just now remembered how arrays work in Java, and now things got a little bit more complicated.
« Last Edit: 08 Jun 2014, 15:06 by ankhtahr »
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,362
  • No time for Claireification
    • Things To Remember
Re: A programming thread!
« Reply #114 on: 08 Jun 2014, 15:08 »

Not being allowed to use util is MEAN. I'm afraid I can't help much here.

Wait, what's the sea class?

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #115 on: 08 Jun 2014, 15:12 »

the class which is actually my task to write. It's supposed to model the field and the rules of the game.

Here's the whole task if anybody's interested. I don't feel like translating it this time…

(click to show/hide)
« Last Edit: 08 Jun 2014, 15:26 by ankhtahr »
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,362
  • No time for Claireification
    • Things To Remember
Re: A programming thread!
« Reply #116 on: 08 Jun 2014, 15:14 »

Oh man I'm reading through the Map-API and IT WOULD HAVE WORKED SO WELL HERE.

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,362
  • No time for Claireification
    • Things To Remember
Re: A programming thread!
« Reply #117 on: 08 Jun 2014, 15:24 »

Oh but that is easy! If the access to the actual array is private, just write a public 'setCell' method. This could look like this:

(I am assuming an int array as a datastructure)

Code: [Select]
private int[][] field;

...

public void setCell(int x, int y, int state) throws IndexOutOfBoundsException{

     this.field[x][y] = state;

}

The Index out of Bounds exception is just for debug, if you falsely input a higher index into the array than it has indices.

You could also give the setCell method different parameters:

Code: [Select]
private int[][] field;

...

public void setCell(int x, int y, Ship ship) throws IndexOutOfBoundsException{

     this.field[x][y] = ship.toString();

}

Everything's possible!

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #118 on: 08 Jun 2014, 15:28 »

In addition to this I have now created a getter and setter which set an entire row. Makes some things easier, and I don't have to write that iteration multiple times.

Also I added the task in my previous message.

Edit:

seriously, not using java.util makes anything ridiculous:
Code: (java) [Select]
Ship[] afterAdd = new Ship[this.shipCount + 1];
for (int i = 0; i < this.shipCount; i++) {
    afterAdd[i] = this.ships[i];
}
afterAdd[this.shipCount + 1] = new Ship(type, dir, x, y, this);
this.ships = afterAdd;
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,362
  • No time for Claireification
    • Things To Remember
Re: A programming thread!
« Reply #119 on: 08 Jun 2014, 15:42 »

can you tell me how ShipType and Direction look like? I'm writing a solution for your task, it's not that difficult.

edit: Nevermind, I wrote them on my own.

second edit: It would be so great if you could modify the shiptype enum.
« Last Edit: 08 Jun 2014, 15:50 by Masterpiece »
Logged

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #120 on: 08 Jun 2014, 15:46 »

You could find them here together with the original task PDF and the checkstyle xml.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,362
  • No time for Claireification
    • Things To Remember
Re: A programming thread!
« Reply #121 on: 08 Jun 2014, 15:52 »

ugh I hate the way they wrote the shiptype enum

I mean, it does what it has to, but so sloppily. You could write the same class like this:

Code: [Select]
enum ShipType{
MINENSUCHER(1),
SCHLACHTSCHIFF(2),
GROSSKAMPSCHIFF(3),
FLUGZEUGTRAEGER(4);

public final int length;

private ShipType(int length){
this.length = length;
}

public int getLength(){
return this.length;
}
}

which has a lot less redundant code.

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,362
  • No time for Claireification
    • Things To Remember
Re: A programming thread!
« Reply #122 on: 08 Jun 2014, 16:50 »

There you go! I haven't checked if it's 100% correct, but if you're struck this should give you a good starting point. Have fun!

Code: [Select]
package battleship;

public class Sea {

/**
* A representation of the ships in the coordinate field.
*/
private int[][] shipField;
/**
* A representation of the bomb-runs in the coordinate field.
*/
private String[][] bombField;
/**
* The width of the coordinate field.
*/
private int width;
/**
* The height of the coordinate field.
*/
private int height;

public Sea(int width, int height){

this.width = width;
this.height = height;

this.shipField = new int[width][height];
this.bombField = new String[width][height];

for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
this.shipField[x][y] = 0;
this.bombField[x][y] = null;
}
}
}

/**
* Adds a ship to the field. Only works if the ship fits in the field,
* the field is not occupied by another ship, and no bombs
* have been thrown yet.
* @param type The ship type.
* @param dir The direction the ship is facing.
* @param x The x - coordinate of the ship-rear
* @param y The y - coordinate of the ship-rear
* @return Returns true, if the ship could be placed.
*/
public boolean addShip(ShipType type, Direction dir, int x, int y){

// Checking if the ship rear is within coordinate field.
if (x < width && x >= 0 && y < height && y >= 0){

// Checking if no bombs have been thrown yet.
for(int n = 0; n < width; n++){
for(int m = 0; m < height; m++){
if(this.bombField[n][m] != null){
return false;
}
}
}

// Getting the ship length.
int shipLength = type.getLength();
// A helper variable used in the for-loops.
int iterator;

switch(dir){
case NORTH:
// Checking if the ship fits in the coordinate field.
if((y - shipLength) >= 0){
// Checking if no other ship occupies space.
for(iterator = 0; iterator < shipLength; iterator++){
if(this.shipField[x][y - iterator] != 0){
return false;
}
}
// Placing ship.
for(iterator = 0; iterator < shipLength; iterator++){
this.shipField[x][y - iterator] = type.getLength();
}
return true;
}else return false;
// Rinse and repeat for every direction.
case EAST:
if((x + shipLength) < this.width){
for(iterator = 0; iterator < shipLength; iterator ++){
if(this.shipField[x + iterator][y] != 0){
return false;
}
}
for(iterator = 0; iterator < shipLength; iterator ++){
this.shipField[x + iterator][y] = type.getLength();
}
return true;
}else return false;
case SOUTH:
if((y + shipLength) < this.height){
for(iterator = 0; iterator < shipLength; iterator++){
if(this.shipField[x][y + iterator] != 0){
return false;
}
}
for(iterator = 0; iterator < shipLength; iterator++){
this.shipField[x][y + iterator] = type.getLength();
}
return true;
}else return false;
case WEST:
if((x - shipLength) >= 0){
for(iterator = 0; iterator < shipLength; iterator ++){
if(this.shipField[x - iterator][y] != 0){
return false;
}
}
for(iterator = 0; iterator < shipLength; iterator ++){
this.shipField[x - iterator][y] = type.getLength();
}
return true;
}else return false;
default:
return false;
}
}else return false;

}

/**
* Places a bomb on the field. Sets {@link #bombField} to X or O,
* depending on whether a boat is hit.
* @param x X-Coordinate of the bomb hit.
* @param y Y-Coordinate of the bomb hit.
* @return Returns true if a ship has been hit.
*/
public boolean dropBomb(int x, int y){
// Checks if the field is occupied, and if it is, lays an X.
if(this.shipField[x][y] != 0){
this.bombField[x][y] = "X";
return true;
}else{
this.bombField[x][y] = "O";
return false;
}

}

/**
* A method that returns true if all the ships that were
* placed in the field were sunk.
* @return True if all ships are down.
*/
public boolean allShipsSunk(){

/* Helper Variables. Ship counter counts
* every cell with a ship and sunk counter
* counts every cell with a hit mark.
*/
int shipCounter = 0, sunkCounter = 0;

// Iterate over field
for(int x = 0; x < this.width; x ++){
for(int y = 0; y < this.height; y ++){
// If cell contains ship
if(this.shipField[x][y] != 0){
// Increment shipCounter
shipCounter ++;
// If cell contains hit marker
if(this.bombField[x][y].equals("X")){
// Increment sunkCounter
sunkCounter ++;
}
}
}
}

// If shipcounter and sunkcounter are the same,
// every ship has been sunk.
if(shipCounter == sunkCounter){
return true;
}else return false;
}

public String toStringWithShips(){
String stringWithShips = "";
String concat;

for(int y = 0; y < this.height; y ++){
for(int x = 0; x < this.width; x ++){
if(this.shipField[x][y] == 0){
concat = ".";
}else{
concat = String.valueOf(this.shipField[x][y]);
}
stringWithShips.concat(concat);
}
stringWithShips.concat("\n");
}
return stringWithShips;
}

public String toStringWithBombs(){
String stringWithBombs = "";
String concat;

for(int y = 0; y < this.height; y ++){
for(int x = 0; x < this.width; x ++){
if(this.bombField[x][y].equals(null)){
concat =  ".";
}else{
concat = this.bombField[x][y];
}
stringWithBombs.concat(concat);
}
stringWithBombs.concat("\n");
}
return stringWithBombs;
}

}

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #123 on: 08 Jun 2014, 16:59 »

Wow.

It's a very different approach to mine (and so incredibly much shorter) but it looks great! It'll be a great help if I run into trouble! Thanks a lot!

If we ever meet I owe you at least one beer! And I want us to meet one day!
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,362
  • No time for Claireification
    • Things To Remember
Re: A programming thread!
« Reply #124 on: 08 Jun 2014, 17:02 »

This is short? I hate the way I wrote "addShip" but I can't think of a better way of doing it (but seriously, so much redundant code, SO MANY ITERATIONS OVER THE FIELD :'( )

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #125 on: 08 Jun 2014, 17:07 »

Yeah, short in the sense that I have multiple additional methods, and one additional class. So lots of whitespaces and formatting.

And these additional methods all have checks for invalid parameters. So lots of redundant checks. But they're public, and my tutor likes to poke public methods until they break, so one should make sure they don't.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,362
  • No time for Claireification
    • Things To Remember
Re: A programming thread!
« Reply #126 on: 08 Jun 2014, 17:09 »

I would much rather work with try-catch expressions, but since none of the methods you're supposed to write have any throw declaration I wasn't able to.

If your tutor likes poking methods, you should rewrite dropBomb in my method implementation. That method doesn't check if the coordinate given is in the field.

I'm tired! I'm off to sleep now. If you still have questions, PM me ( I should hear the e-mail sound.)

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #127 on: 08 Jun 2014, 17:16 »

I'll try to get my approach to work first, and I think I'm checking for valid coordinates everywhere. (Actually one of my methods is for checking whether a row (given with length, direction and coordinates) goes out of bounds, because I'm doing that everywhere.

The way my code looks right now, I've just thrown in everything I thought I could use, and will have to throw away around a quarter of it again, but I'm positive that it might do what it's supposed to do.

Also I think you've done enough for me by now, so I can finish without too much trouble. Thanks again!
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #128 on: 09 Jun 2014, 01:51 »

Sometimes you just want to throw your notebook against a wall…



So I guess I won't get the bonus points. I won't sit here for another three hours, when I should be sleeping, only to do work I already did.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,362
  • No time for Claireification
    • Things To Remember
Re: A programming thread!
« Reply #129 on: 09 Jun 2014, 02:58 »

If you lost your work, just submit mine

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #130 on: 09 Jun 2014, 03:18 »

All that was lost was the work for the bonus points (improved dropping of bombs in the test class), so it's no big deal, just annoying.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

cesium133

  • Methuselah's mentor
  • *****
  • Online Online
  • Posts: 5,341
  • Has a fucked-up browser history
    • Cesium Comics
Re: A programming thread!
« Reply #131 on: 09 Jun 2014, 05:18 »

Sometimes you just want to throw your notebook against a wall…



So I guess I won't get the bonus points. I won't sit here for another three hours, when I should be sleeping, only to do work I already did.
How on earth did you manage to get Linux to do that? I've only ever gotten a kernel panic when I had screwed up something royally...
Logged
The nerdy comic I update on Saturdays: Cesium Comics

Unofficial character tag thingy for QC

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #132 on: 09 Jun 2014, 22:46 »

Probably a hardware fault. I've had HDD issues in the past, and I had noticed first signs of bad RAM before. Both could have cause this.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

hedgie

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,566
  • Can't be buggered at all
Re: A programming thread!
« Reply #133 on: 09 Jun 2014, 23:08 »

For a test environment, I could see using Windows.   But I never would think of using it for a production environment.

There's nothing like a good set of blinkers! (I run Apache, Tomcat, PostgreSQL, etc on Windows at work, and the same plus Deluge and Kerio Connect at home.  But I use Open BSD for firewalls).

Aah, I'm more Apache and MySQL on Linux, myself.  Although I'm a bit of a recent (4 years) convert to Linux after I switched from OS X.

Quote
The only thing I can think of using a Windows server for is Exchange.

I hate Exchange with a passion, though I've been running it for 15 years; it's just that for the user Exchange plus Outlook is still unrivalled by any alternative.
[/quote]

Agreed.  It's an abomination against any gods that may exist, but I haven't seen any replacement for it.
Logged
"A person who may charitably described as 'not entirely a waste of resources, but mostly so.'"  -- Momo

"Pray that there's intelligent life somewhere out in space, 'Cause there's bugger all down here on Earth!"  -- The Pythons

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,362
  • No time for Claireification
    • Things To Remember
Re: A programming thread!
« Reply #134 on: 20 Jun 2014, 08:34 »

Our final task for our multimedia programming module is:

Write the game "Flappy Bird".

I'm writing in Python and it's SO FUN TO DO THIS I CAN'T BEGIN TO EXPLAIN

Like, every time I start coding I remember how AWESOME it is.

GarandMarine

  • Awakened
  • *****
  • Offline Offline
  • Posts: 10,308
  • Kawaii in the streets, Senpai in the sheets
Re: A programming thread!
« Reply #135 on: 01 Aug 2014, 19:28 »

Logged
I built the walls that make my life a prison, I built them all and cannot be forgiven... ...Sold my soul to carry your vendetta, So let me go before you can regret it, You've made your choice and now it's come to this, But that's price you pay when you're a monster with no name.

hedgie

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,566
  • Can't be buggered at all
Re: A programming thread!
« Reply #136 on: 01 Aug 2014, 20:03 »

Well, at least he wasn't flinging poo.
Logged
"A person who may charitably described as 'not entirely a waste of resources, but mostly so.'"  -- Momo

"Pray that there's intelligent life somewhere out in space, 'Cause there's bugger all down here on Earth!"  -- The Pythons

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #137 on: 27 Aug 2014, 11:45 »

I'm sitting in front of my IDE, having to do one of the two final tasks which are counted as exams until Sunday. I can't really motivate myself to do it.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #138 on: 28 Aug 2014, 15:40 »

Found out that using java.util.regex apparently isn't forbidden and makes the first part of the task a lot easier. Yay!

Edit: To specify: Finding every link in a long string marked with brackets ([[link]]) is a lot easier with regexes: \\[\\[(.*?)\\]\\]
« Last Edit: 28 Aug 2014, 15:54 by ankhtahr »
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #139 on: 29 Aug 2014, 11:28 »

Anyone here who knows their (Java) regexes better than I do?

I need to extract every "word" from a very long string (the strings may only contain the symbols mentioned in the description here). A word delimiter is either a space or any of the following symbols:
Code: [Select]
.,;:_-?!Words which are connected with a hyphen are considered one word. (e.g. this-is-all-one-word) A word may contain numbers and umlauts, but must consist of at least one letter. My current regex for that is this one:
Code: [Select]
[A-ZÄÖÜ]+[^.,;:_?!\ ]*(I use CASE_INSENSITIVE of course)
I still need a way to cut off a hyphen at the end of a word. I tried it with a "-?" at the end, putting the rest in parenthesis, but that doesn't work of course, because the "not these special characters" is greedy.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

Pilchard123

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,814
  • I always name them Bitey.
Re: A programming thread!
« Reply #140 on: 29 Aug 2014, 11:35 »

Try http://www.regexr.com/ - it's not perfect (it uses a JavaScript regex engine which can be a bit flaky with lookarounds), but does the job for most things I've needed.


EDIT:

Must it be all regex? Could you use something like (code not checked, but hopefully you get the idea)
Code: [Select]
String toGetWords = "a long string of tokens that blah blah blah blah blah"
String delimiters = "a string containing all the delimiter characters"
StringTokenizer st = new StringTokenizer(toGetWords, delimiters)

while(st.hasMoreTokens()){
    String token = st.nextToken();
    //Trim trailing hyphen, or discard the token if the trailing hyphen disqualifies it.

    //Then use a regex to check if the token contains [A-ZÄÖÜ], case insensitive.
}
« Last Edit: 29 Aug 2014, 11:50 by Pilchard123 »
Logged
Piglet wondered how it was that every conversation with Eeyore seemed to go wrong.

Pilchard123

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,814
  • I always name them Bitey.
Re: A programming thread!
« Reply #141 on: 29 Aug 2014, 11:53 »

Doublepost for great justice!

http://codegolf.stackexchange.com/
Logged
Piglet wondered how it was that every conversation with Eeyore seemed to go wrong.

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #142 on: 29 Aug 2014, 12:01 »

I think it should be relatively easy, My idea is to allow hyphens only if they are followed by another symbol. I just don't know, how to put this into a regex. And I feel like the other solution would stumble over a few other cases.

Huh, I just noticed that my regex has another problem. Words which start with a number won't be recognised.

Edit: found a solution:
Code: [Select]
\d*[a-zäöü]+([a-zäöü0-9]|\-[a-zäöü0-9])*
« Last Edit: 29 Aug 2014, 12:18 by ankhtahr »
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

Pilchard123

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,814
  • I always name them Bitey.
Re: A programming thread!
« Reply #143 on: 29 Aug 2014, 14:13 »

Have look at lookaheads, though I can't remember how to also capture those.
Logged
Piglet wondered how it was that every conversation with Eeyore seemed to go wrong.

Tea For The Tillerman

  • Not quite a lurker
  • Offline Offline
  • Posts: 9
Re: A programming thread!
« Reply #144 on: 30 Aug 2014, 01:26 »

Anyone here who knows their (Java) regexes better than I do?

I need to extract every "word" from a very long string (the strings may only contain the symbols mentioned in the description here). A word delimiter is either a space or any of the following symbols:
Code: [Select]
.,;:_-?!Words which are connected with a hyphen are considered one word. (e.g. this-is-all-one-word) A word may contain numbers and umlauts, but must consist of at least one letter. My current regex for that is this one:
Code: [Select]
[A-ZÄÖÜ]+[^.,;:_?!\ ]*(I use CASE_INSENSITIVE of course)
I still need a way to cut off a hyphen at the end of a word. I tried it with a "-?" at the end, putting the rest in parenthesis, but that doesn't work of course, because the "not these special characters" is greedy.

If this regex works:
Code: [Select]
[A-ZÄÖÜ]+[^.,;:_?!\ ]*(gonna call this block "word" from now on)

Couldn't you write something like this:
Code: [Select]
<word>[-<word>]*Basically saying "must have one word, but can have multiples as long as subsequent words start with a hyphen".
Logged
Everybody, get radical!!!

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #145 on: 31 Aug 2014, 13:55 »

Any tips for easy parsing of commandline arguments? I could of course use more regexes, but that feels like a little bit of overkill. I'm free to use java.util, but not much more. Switch cases seem impractical because the arguments can occur in any order, but not multiple times.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

Loki

  • comeback tour!
  • *****
  • Offline Offline
  • Posts: 5,515
  • The mischief that dwells within
Re: A programming thread!
« Reply #146 on: 31 Aug 2014, 14:01 »

Never done that, but my approach would be: stuff all of them into an array, do linear search on the array (you only have a set universe of arguments, right?)

For determining the pairs of arguments and values, I'd indeed use regexes - or, more naively, indexOf(' ') plus substring.
Logged
The future is a weird place and you never know where it will take you.
the careful illusion of shit-togetherness

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #147 on: 31 Aug 2014, 16:35 »

Yeah, I've decided to just use regexes anyway. I think with this task I learnt a lot about those. Anyway, I now have around 11.5 hours until I have to hand this task in. This might be a long night.

And now I discovered that I can apply another design pattern to this task. Good thing I studied for the software engineering exam. I'm now using the Composite and the Stategy pattern.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #148 on: 31 Aug 2014, 19:56 »

AAAAAAARGH

uhm, anyone know a good way to divide a search string based on the following format into nice parts?

A search string consists of the command "search ", followed by either a single word, or multiple words put together using "AND(word1, word2)" or "OR(word1, word2)". You can nest ANDs and ORs. I wanted to model this using the composite design pattern, so I have "AndTerm", "OrTerm" and "FinalTerm", all extending "Term", with AndTerm and OrTerm consisting of two terms. Somehow I have to break a string down into a format I can use for these classes.

Edit:
Hmm, maybe I chose the wrong way to model this… This is difficult. I need to look into this.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,679
  • A hacker spathe night owl
Re: A programming thread!
« Reply #149 on: 31 Aug 2014, 20:51 »

ok, I realised that my modelling is pretty much shit, and I'm starting to panic. I have 7 hours left to make this a running, Checkstyle compliant solution.

It would go into too much detail if I'd explain everything here, but here's the task.

I'm currently implementing the search for single search terms. I'm now at a point where I can get an ArrayList of Sites in which the term occurs, and I can get a ranking for each. Basically for the single term search to work I just need to put the results in a structure together with the rankings and sort it by the rankings. My mind is too full of fuck to manage that currently. I'm trying to do it anyway though.

And then I have no idea what to do about the multiple search term things.

Edit: okay, I think I'm writing a class for the results, which implements comparable to sort the results by the ranking. Still no idea about the multiple search terms.
Edit2: Slightly less panicky now that I at least have the search for single terms working in theory. I still need to write the parser for the commandline interface for that, but that shouldn't be so difficult. Now I need to understand how the hell the ranking with multiple search terms is supposed to work.
« Last Edit: 31 Aug 2014, 21:08 by ankhtahr »
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.
Pages: 1 2 [3] 4   Go Up