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
16 2041
anyone? please help, will be greatly appriciated
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... -
>>> st = ''
-
>>> st += 'test'
-
>>> st += '4ex'
-
>>> st
-
'test4ex'
-
>>> len(st)
-
7
-
>>>
-
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. -
>>> import random
-
>>> colors = ['red', 'blue', 'green']
-
>>> random.shuffle(colors)
-
>>> colors
-
['green', 'red', 'blue']
-
>>> random.shuffle(colors)
-
>>> colors
-
['green', 'blue', 'red']
-
>>>
-
bvdet 2,851
Recognized Expert Moderator Specialist
I think this might get you started: - def boardStr(cols, rows, empty=1):
-
numList = [str(i) for i in range(1, (cols*rows-empty)+1)]
-
random.shuffle(numList)
-
numList += ['_' for i in range(empty)]
-
return ''.join(['%2s%s' % (s, ['|', '\n'][not (i+1)%cols or 0]) for i, s in enumerate(numList)])
-
-
# Without the list comprehension
-
def boardStr(cols, rows, empty=1):
-
numList = [str(i) for i in range(1, (cols*rows-empty)+1)]
-
random.shuffle(numList)
-
numList += ['_' for i in range(empty)]
-
outStr = ''
-
for i, s in enumerate(numList):
-
outStr += '%2s' % (s)
-
if not (i+1)%cols:
-
outStr += '\n'
-
else:
-
outStr += '|'
-
return outStr
-
-
print boardStr(3,3,1)
-
print boardStr(4,4,1)
-
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| _| _
>>>
I think this might get you started: - def boardStr(cols, rows, empty=1):
-
numList = [str(i) for i in range(1, (cols*rows-empty)+1)]
-
random.shuffle(numList)
-
numList += ['_' for i in range(empty)]
-
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
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.
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..
-
>>> n = range(1, 10)
-
>>> random.shuffle(n)
-
>>> 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)])
-
|2|8|9|
-
|3|7|4|
-
|5|6|1|
-
bvdet's List comprehension slightly modified for the output format specified in original post ;)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
______________________
|
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
|
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
|
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. ...
|
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
| |
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...
|
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:
...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| | |