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.

Hi, post your code with proper tags attached in the future.

``````def board():
board = [1,2,3,4,5,6,7,8,9]
count = 1
for x in range(0,9):
c = '_' if board[x]==9 else board[x]
if count < 4:
print "|", c,
else:
print "|\n|",c,
count = 1
count += 1
print "|"``````

`print` usually includes new line at the end. you can avoid this by placing ',' at the end.

``````>>> import random
>>> l=[1,2,3,4,5,6,7,8]
>>> random.shuffle(l)
>>> l
[3, 5, 1, 8, 4, 2, 6, 7]``````

do you mean every move is `random.shuffle(board)` ?

kath

no, the player has to select an adjecent number, to the empty slot to fill, once the numbers are in order 1-8 from top to bottom in the 3 rows, then the game is won. i didnt under stand how to make the numbers so when i take an input from the player such as a number to switch with the empty spot, that the empty spot is replace by that number and the number is replaced but the empty spot, and the board has to be a 3x3 grid and the dividers have to be "|" and the empty spot is represented by "_"

in addition the game has to start spolved, then there is a scramble button, where the board is shuffle, starting a new game. the game can be paused and resumed, also after a game is won, and instant replay can be shown

also when i import a module, it say module doesnt exist..

any help is apreciated.. thanks

kath is right, you have to keep track of it as a list, really a number of lists.

thx so much

if a player of the game chooses to scrammble the board so they can start a new game, and have them be able to type a number that is adjacent the with the empty slot on the grid to switch spots, until the board is back in order 1-8

in addition i need the scrammble the board as if i handed it to someone and they made 1000 random moves.

also how would i save all the moves, so when the player wins i can show an instant replay, or resume a pause game.

thx ahead of time

p.s.

the game is call the sliders game, its where a player get a shuffled board they place a number in the the empty spot, keep doing the same tile the board is in order again, but the game starts solved, and the gmae doesnt begin till the player types, (s) to scrammble the board.

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 tille 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.

--------------------------------------------------------------------------------

can anyone lend a hand please, i need some help.

hi, the following code is simpler version of 'Slider game'. It does not accomplish all the points asked in the question(for eg. saving each move, quit in the middle, pause/resume.). I hope this is enough for a good start. You can also approach implementation using OO methodology. This is just to let you get started.

``````""" Slider game
map_adj - is a dict, which maps adjacent index for a given index of '_'.
for eg.
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | _ |, adjacent index for '_' is - (5,7).

board - is list holding the current game's number-order
index - holds the index of '_' at any point of game.
"""
import random

global map_adj, board, index

map_adj = {
0:(1,3),
1:(0,2,4),
2:(1,5),
3:(0,4,6),
4:(1,3,5,7),
5:(2,4,8),
6:(3,7),
7:(6,8,4),
8:(7,5)
}
board = [1,2,3,4,5,6,7,8,9]
index = board.index(9)

def show_board():
#board = [1,2,3,4,5,6,7,8,9]
global board
count = 1
for x in range(0,9):
c = '_' if board[x]==9 else board[x]
if count < 4:
print "|", c,
else:
print "|\n|",c,
count = 1
count += 1
print "|"

def isAdjacent(n):
global map_adj, board, index
if board.index(n) in map_adj[index]:
return True
else:
return False

def swap(n):
global map_adj, board, index
board[ board.index(n) ]= 9
board[index] = n
index = board.index(9)
show_board()

def main():
global map_adj, board, index
show_board()
c = raw_input("Enter 's' to shuffle the board and start the game: ")
if c == 's':
random.shuffle(board)
index = board.index(9)
show_board()
while 1:
try:
#print '_ index: ', index, ' adjacent index: ', map_adj[index]
n = int( raw_input("Enter a number which is adjacent to '_': ") )
if (0 < n < 9) and isAdjacent(n):
swap(n)
if board == [1,2,3,4,5,6,7,8,9]:
print 'You won!'
#sys.exit(0)
break
except ValueError:
pass

if __name__ == '__main__':
main()``````

Remarks:
1. There might be a better way to find the adjacent index.
2. You can also implement using Tkinter to enhance this game. A better GUI, a mouse click to swap numbers
3. I have used 3 global variables, which may not be a better approach, can be improved if a class is defined.

katharnakh.

thx that helps alot, but one of the issue i came up with is, if random.shuffle() is used, it could make the board into an unsovable one, so what i found was if i start the board in a soldved stat, than use the random functon to choose one of the two possible valid moves such as 6 or 7 then let i make that move with the empty slot, let it do that in 1000 times in a for loop, so it has to make vaild random moves, but i dont no how to implement, that how would i do that?\

thx alot.

ps, i would like to play on just one board instead of printing a board over and over, how would i use the clear screen function to use that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.