[Tutor] 15 puzzle (fwd)
by Daniel Yoo other posts by this author
Apr 29 2001 12:32AM messages near this date
Re: [Tutor] how can i
|
Re: [Tutor] 15 puzzle (fwd)
Dear Maritza,
Unfortunately, I'm unable to read word documents, so I'm forwarding this
to the other python tutors, in hopes that someone out there has Windows.
*grin* By the way, when you're replying to messages from tutor, make sure
you're doing a reply-to-all, so that other people can volunteer their
insight to the problem.
We can translate much of the meaning of the Java applet into a Python
program. However, it's not going to be a line-by-line translation: there
are certain things that are easier to do in Python than Java, and vice
versa. Python makes working with lists very convenient, and it's this
versetility that encourages idioms that might not translate well without
some effort.
Just to give an example, when we're trying to represent something
two-dimensionally in Java, the language offers a structure called the
array, which is something analogous to a Python list. Let's talk about
how we can possible represent a board position in Java and Python. As a
warning, my Java's very rusty, so some of this code may offend Java
purists. At the same time, I threaten to offend Python programmers with
what must look like an eyesore. Either way, I apologize in advance.
*grin*
Here's a snippet of code that explores one possible way we can represent
the 15-puzzle board, as a 2-dimensional array:
///
public class test {
static String[][] makeBoard() {
String[][] board = new String[4][4];
int counter = 1;
for(int row = 0; row < board.length; row++) {
for(int col = 0; col < board[row].length; col++) {
board[row][col] = (counter++)+"";
}
}
board[3][3] = " ";
return board;
}
static public void main(String[] args) {
String[][] board = makeBoard();
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
}
///
Here's a bit of Python code that does a similar task:
###
def makeBoard():
board = [None] * 4
for i in range(len(board)):
board[i] = map(str, range(4*i+1, 4*i + 5))
board[3][3] = ' '
return board
if __name__ == '__main__':
board = makeBoard();
for row in range(len(board)):
print board[row]
###
As you can see, there are a lot of concepts that transfer cleanly between
both languages, but there are also substantial differences. Some of these
owe to the fact that I haven't played around enough with Java to feel
casual with it; at the same time, Java isn't really meant for casual use
anyway. *grin*
I guess I'm trying to say that, yes, you can borrow some ideas from the
Java applet, but to really learn Python, you'll want to experiment with
the "Pythonic" way of doing things, and this you'll learn as you play with
examples and programs.
I hope that we can talk more about your program. Feel free to email us
your progress as you learn Python. Good luck!
---------- Forwarded message ----------
Date: Sat, 28 Apr 2001 23:08:03 -0500
From: Maritza Rodriguez <maritza_rodz@[...].com>
To: dyoo@[...].edu
Subject: 15 puzzle
I found this information on the internet. It appears to be the actual
program generating the game, but it is in another language. Is it possible
to translate it to python?
Please help.
Maritza
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com
Attachments:
The 15 Applet.doc
Thread:
Daniel Yoo
Sheila King
|