473,788 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python help with taking in data, saving date, arrays, random.shuffle

7 New Member
im a beginner at comp science, and my prof is using python, which is totally new tom me i had a few questions with the program im writing, im having a few problems.

brief summary
i have to do a project where there are nine boxes filled with number 1-8, and 1 space is empty, the game needs to be scrambled, also all the moves need to be saved. the game can be paused anytime during thegame. the object is to move the numbers into the black spot till they are all in order.

the grid i have needs to contain variables so that can be moved by the players

this what i have and i know it is far off

def board():
board = [1,2,3,4,5,6,7,8 ,9]

for x in range(0, 9):
print "|"

if(board[x]==9):
print "_"
else:
print board[x]



if(x==8):
print "|"
board()




when i run that it looks like this

|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
_
|


and this is how i need it to look:

|1|2|3|
|4|5|6|
|7|8|_|

what am i doing wrong? also how do i use the random.shuffle( ) function so i can scramble the numbers?

help would be greatly appriciated...

also how would i save every move, also the random fuction isnt working so i can scramble the numbers.


i am probably doing everything wrong to make a game board, cause i need everything to to be interchanglable when the players wants to move and adjecent number to the blank spot, any help anyone???

thanks alot ahead of time

also i can't seem to import array or random
in addition my project is attached if anyone didnt understand what i needed to to do
Mar 4 '08 #1
16 2041
wokeup2sleep
7 New Member
anyone willing to help?
Mar 4 '08 #2
wokeup2sleep
7 New Member
anyone? please help, will be greatly appriciated
Mar 5 '08 #3
jlm699
314 Contributor
Your problem is that every time you issue a print statement, Python automatically appends a newline character to the end.

What you'll want to do is iterate through your loop and instead of printing outright, try storing the values to a string, which can be concatenated with the '+' operator.

On each iteration you could try checking the string for a certain length, and if it is the length that you want for a particular line you could print it then, and clear the string to start over with a new line.

example string concatenation and length check...

Expand|Select|Wrap|Line Numbers
  1. >>> st = ''
  2. >>> st += 'test'
  3. >>> st += '4ex'
  4. >>> st
  5. 'test4ex'
  6. >>> len(st)
  7. 7
  8. >>> 
  9.  
Mar 5 '08 #4
jlm699
314 Contributor
Additionally, please try to use code tags when you post code (it saves room and makes your post friendlier to read...)

random.shuffle( ) literally shuffles the order of number in a list.

Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> colors = ['red', 'blue', 'green']
  3. >>> random.shuffle(colors)
  4. >>> colors
  5. ['green', 'red', 'blue']
  6. >>> random.shuffle(colors)
  7. >>> colors
  8. ['green', 'blue', 'red']
  9. >>> 
  10.  
Mar 5 '08 #5
bvdet
2,851 Recognized Expert Moderator Specialist
I think this might get you started:
Expand|Select|Wrap|Line Numbers
  1. def boardStr(cols, rows, empty=1):
  2.     numList = [str(i) for i in range(1, (cols*rows-empty)+1)]
  3.     random.shuffle(numList)
  4.     numList += ['_' for i in range(empty)]
  5.     return ''.join(['%2s%s' % (s, ['|', '\n'][not (i+1)%cols or 0]) for i, s in enumerate(numList)])
  6.  
  7. # Without the list comprehension
  8. def boardStr(cols, rows, empty=1):
  9.     numList = [str(i) for i in range(1, (cols*rows-empty)+1)]
  10.     random.shuffle(numList)
  11.     numList += ['_' for i in range(empty)]
  12.     outStr = ''
  13.     for i, s in enumerate(numList):
  14.         outStr += '%2s' % (s)
  15.         if not (i+1)%cols:
  16.             outStr += '\n'
  17.         else:
  18.             outStr += '|'
  19.     return outStr
  20.  
  21. print boardStr(3,3,1)
  22. print boardStr(4,4,1)
  23. print boardStr(4,4,2)
The printed output looks like this:

>>> 8| 2| 6
1| 4| 7
3| 5| _

11| 9|14| 3
8|10|15| 6
4| 7| 2| 5
12| 1|13| _

7|13| 5|11
1| 6|14| 2
9|10| 4| 3
8|12| _| _

>>>
Mar 5 '08 #6
jlm699
314 Contributor
I think this might get you started:
Expand|Select|Wrap|Line Numbers
  1. def boardStr(cols, rows, empty=1):
  2.     numList = [str(i) for i in range(1, (cols*rows-empty)+1)]
  3.     random.shuffle(numList)
  4.     numList += ['_' for i in range(empty)]
  5.     return ''.join(['%2s%s' % (s, ['|', '\n'][not (i+1)%cols or 0]) for i, s in enumerate(numList)])

Oh bvdet... you and your list comprehensions never cease to amaze me
Mar 5 '08 #7
bvdet
2,851 Recognized Expert Moderator Specialist
Oh bvdet... you and your list comprehensions never cease to amaze me
I realize that sometimes a list comprehension may not the best way and can be difficult to read. That's why I included the second version of the function.
Mar 5 '08 #8
wokeup2sleep
7 New Member
The game is called the sliders game. The game starts a in a solved state, then the player types (s)scramble, to scramble the board. Then the player has to type a number that is adjacent to the empty box, to switch spot, they have to do this strategically to get all the number back in order 1-8.

Problems i have;

how to get a board so the number are interchangable when the player chooses. ie, if the board looks like
|1|2|3|
|4|5|6|
|7|8|_|
if the player types scramble i need board to look as if you handed it to some one and they made a thousand random moves. how do i do that;
so it look something like
|4|1|2|
|3|5|7|
|6|8|_|
then the player should be able to type in a number, the the empty slot if filled with that number, player continues to do this till the board is in order again. so say if the player typed 7 it should looked;
|4|1|2|
|3|5|_|
|6|8|7|
and if anytime the player types (p)pause the state of the board that it is in needs to be saved so it can be resumed

how to save every move in the system when a player makes it, so i can see a replay of the game if the game is won, also if the game is paused the game can be resumed.

so how would i make the numbers move, when a player types a number, and how do i save every move?

thanks alot guys..
Mar 5 '08 #9
jlm699
314 Contributor
Expand|Select|Wrap|Line Numbers
  1. >>> n = range(1, 10)
  2. >>> random.shuffle(n)
  3. >>> print ''.join(['%s%s' % (['|', '|' + s][not (i+1)%3 or 0], [s, '|\n'][not (i+1)%3 or 0]) for i, s in enumerate(n)])
  4. |2|8|9|
  5. |3|7|4|
  6. |5|6|1|
  7.  
bvdet's List comprehension slightly modified for the output format specified in original post ;)
Mar 5 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1896
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 241 open ( -6) / 2622 closed (+26) / 2863 total (+20) Bugs : 764 open ( +6) / 4453 closed (+38) / 5217 total (+44) RFE : 150 open ( +2) / 131 closed ( +0) / 281 total ( +2) New / Reopened Patches ______________________
13
11796
by: sf | last post by:
Just started thinking about learning python. Is there any place where I can get some free examples, especially for following kind of problem ( it must be trivial for those using python) I have files A, and B each containing say 100,000 lines (each line=one string without any space) I want to do
10
3692
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. Andrew dalke@dalkescientific.com
34
4128
by: Blake T. Garretson | last post by:
I want to save some sensitive data (passwords, PIN numbers, etc.) to disk in a secure manner in one of my programs. What is the easiest/best way to accomplish strong file encryption in Python? Any modern block cipher will do: AES, Blowfish, etc. I'm not looking for public key stuff; I just want to provide a pass-phrase. I found a few modules out there, but they seem to be all but abandoned. Most seem to have died several years ago. ...
10
1504
by: Grooooops | last post by:
Hi All, I've been lurking the list for a month and this is my first post. I am hoping this post is appropriate here, otherwise, my apologies. I'm somewhat new to Python, (I'm reading all the tutorials I can find, and have read through Andre Lessa's Developers Handbook.) I am trying to learn the Python way of thinking as well as the syntax. I popped this bit of code together for fun, based on a previous post
8
3041
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to what I really should do. I've had a look through Programming Python and the Python Cookbook, which have given me ideas, but nothing has gelled yet, so I thought I'd put the question to the community. But first, let me be a little more detailed...
3
1926
by: bb91915 | last post by:
I am taking a beginners class in C and am attempting to write code for extra problems my instructor gave us. These are not for grade and are only used solely for practice. I have a hard time taking pseudocode and turning it into code. Since I am in a beginners class, we have only learned so much to date, so what I am presenting will be really rough, so please pardon my ignorance. Here are my instructions, with my code to follow: ...
5
6780
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would cause. If I could create a large file that could be encrypted, and maybe add files to it by appending them and putting in some kind of delimiter between files, maybe a homemade version of truecrypt could be constructed. Any idea what it...
10
2096
by: connyledin | last post by:
Im trying to create a version of the game Wumpus. Mine is called Belzebub. But im STUCK! And its due tuesday 2 maj. Im panicing! Can some one help me?? here is the file: http://esnips.com/webfolder/b71bfe95-d363-4dd3-bfad-39999a9e36d0 What i have the biggest problems with now is between line 8 and 23. How i can move the character trough the game. Other parts of the game that have with the movement to do is between line 83-114 and...
0
10173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.