6 Replies - 349 Views - Last Post: 04 May 2014 - 10:07 AM Rate Topic: -----

#1 efes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-May 14

Code Puzzle

Posted 04 May 2014 - 07:14 AM

Hi, can anyone tell me how to open a file for example file.txt and be able to edit it? Meaning that the file first opens, then the words in the file are shown and then I type in a symbol and a letter to replace it with. I've tried a few ways but then it just didn't show the list after replacing the symbols.
Is This A Good Question/Topic? 0
  • +

Replies To: Code Puzzle

#2 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 705
  • View blog
  • Posts: 3,034
  • Joined: 03-December 12

Re: Code Puzzle

Posted 04 May 2014 - 07:17 AM

Let's start with what have you tried? Post your complete relavent code, please. Then what issues and/ or errors you are receiving.

This post has been edited by astonecipher: 04 May 2014 - 07:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 efes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-May 14

Re: Code Puzzle

Posted 04 May 2014 - 08:28 AM

Grrr. All my code is now gone. So basically I've tried something like this now:

def puzzle():
    codepuzzle = open('words.txt', mode="r",encoding="utf-8")
    print(codepuzzle)
    


And this is what comes out:

<_io.TextIOWrapper name='words.txt' mode='r' encoding='cp1252'>

Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,201
  • Joined: 19-March 11

Re: Code Puzzle

Posted 04 May 2014 - 08:36 AM

View Postefes, on 04 May 2014 - 09:14 AM, said:

Hi, can anyone tell me how to open a file for example file.txt and be able to edit it? Meaning that the file first opens, then the words in the file are shown and then I type in a symbol and a letter to replace it with. I've tried a few ways but then it just didn't show the list after replacing the symbols.


You mean you want to write a simple text editor?
Was This Post Helpful? 0
  • +
  • -

#5 efes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-May 14

Re: Code Puzzle

Posted 04 May 2014 - 09:11 AM

This is how the 'Game-like' puzzle will go:
- 10 coded words from words.txt will apear on screen
E.g.
&$^$^$
)$!
#@$\]
- Then I enter a symbol and letter to replace the symbols with, e.g.
Please enter a symbol: %
Please enter a letter S
It replaces the % symbol in every word with S
- During the puzzle, every 2-3 minutes a message will come up saying 'Do you want to use the hints (Y/N)?' then if I type in Y, then something like this will appear:
A %
B ^
C @
If I type in N, then it will come up again until I do type the 'Y' to show me the hints - the hints can only be used once.
- I will keep on replacing the symbols with letter, until all symbols are replaced. After all of them are replaced, then it will check my solution with the solved.txt file - The solved.txt file contains the uncoded words.
E.g.
Banana
Hat
Snake
- If symbols still need replacing, then it will keep on doing until I do so.
Was This Post Helpful? 0
  • +
  • -

#6 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 705
  • View blog
  • Posts: 3,034
  • Joined: 03-December 12

Re: Code Puzzle

Posted 04 May 2014 - 09:56 AM

Since you know a base line of what you want to do, start breaking down the problem into smalled does 1 thing functions.

You are basically doing a decryption program if that gives you a place to start looking.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,201
  • Joined: 19-March 11

Re: Code Puzzle

Posted 04 May 2014 - 10:07 AM

Okay, that's a reasonably well specified game. I think it's possible to proceed from there.

The first thing I would observe is that you don't need to edit the text file at all. You want to keep the text that the user is working on in memory.

I would start by breaking this down into smaller pieces that you can put together in useful ways. I usually do this from a mostly top-down approach. Here's an example of what I mean. When I think about this problem, I can see that I'm going to need to arrange for the user to "make a move". I would usually represent this as a function that takes a state as input, and returns a state as its output. A "state" is simply "everything you need to know about the state of play", and in python it can often be represented as a dictionary, for simple games, or as a class, for more complex ones. This "more" function is pretty abstract, and it's going to be about the same for all single player games (ie, solitaire, or hangman, or a cryptogram game of this sort) or single-player-versus computer games (for example, you could use it for tic-tac-toe - but we'll see later why you might not want to do that). It will generally want to get a move, verify that the move is legal given the state of play, and if the move is legal in the current state of play, it will apply that move to the state and return the new state. This function runs in a master loop that goes until the game state says the game is over.

So your "play the game" function looks like this:

state = some_initial_state  # you have to work on this part
while not state['game_over']:       # suppose that state is a dict with an entry for 'game_over' which is initially False     
  state = player_move(state)
report (state)    # here you would say something about how the game came out - for example, "you won!"


And player_move looks something like:

def player_move(state):
  move  = player_input (state) # pass in the state so the input can tell the player where things stand
  if validate(move):
    state = apply(move, state)
  else:
    # don't need to do anything here, but you might want to set an "alert" in the state so the player knows why it didn't change
    # if you do this, remember to clear the alert after displaying it!
  return state



So that's a top-down design decision. You're starting from the top, and making decisions about the whole structure which help you simplify the things you need to do. The trouble is, you can't run this code, because there's functions here you haven't defined. So you should probably define some "stub" functions that don't do anything and just return stereotyped values - for example, your apply() function could return some bogus state for any input it receives, for now. The returned value won't be correct, of course, but this allows you to work on other parts of the program. Having those stub functions, you'd then think about how to implement pieces of them. For example, what does your player_input function do? Well, if I were writing it, it would report the current state of the game, prompt for input and retrieve that input, then do some sanity checking to make sure the move is potentially a legal move, but not validate against the current board. If the move isn't a valid move, then it doesn't even return it, it just complains and asks for another input. When it gets a plausible move, it returns that. So you can express that in similarly abstract language, describing everyhing that happens in terms of helper functions, which have not yet been defined.
You continue this all the way down until the helper functions are so simple that you can do them in a few lines of actual python, and then you just do them.

So go ahead and try doing some design work and see what you can come up with.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1