Jump to content

Python game problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
23 replies to this topic

#1
DrPepperMan34

DrPepperMan34

    Newbie

  • Members
  • PipPip
  • 13 posts
Hi Guys,

Just a quick question regarding what data structure would be best to implement a board game structure. I want to create a board of size n where a player draws a line from one horizontal or vertical line of the board to another. One way of doing this would be to record the x/y co-ordinates which the line is adjacent to e.g in a n = 6 board [(2,2), (3,2)] or [(5,3),(5,4)]. Also since The horizontal/vertical lines around the board can be specified like [(0,4),(1,4) or (5,6),(5,7)] would a good idea when creating the board to set the size to n + 1 to accommodate these extra x/y co-ordinates on the board. Would using sets or vectors be possible, and how should I go about starting it too? Oh and I'm using version 2.6 of python.

I'm guessing using Lists would be better because I can keep the order of the co-ordinates in the same order I want. Although I'm still confused about how to start the **** thing!

Cheers for any help!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It will depend a little bit on the exact rules of the game. Can lines intersect each other? How easily can you detect line collisions? etc.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
DrPepperMan34

DrPepperMan34

    Newbie

  • Members
  • PipPip
  • 13 posts
Thanks for the reply. Basicaly the game consists of each player (one human, other AI) drawing lines from point to point and the objective is to create boxes. If a player completes a box then they get another turn to draw another line. The first player with the most completed boxes at the end wins. Its a popular game, I use to play it during lectures years ago can't remember the name though!

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You may be better off with an array of some sort.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
DrPepperMan34

DrPepperMan34

    Newbie

  • Members
  • PipPip
  • 13 posts
Thanks for the reply.

Orginaly I thought about an array but how would I construct it?

I am looking to implement a user-defined size: e.g: a size of 1 would create a board of one by one empty sqaure and a size of 2 would create a board of two by two squares etc. Also how would I implement the edges around the board?

I would prefer to have a co-ordinate based board because I could quickly build a version to run without a GUI first, and have the user enter their choice of co-ordinates on their move as paramaters. Which is why I thought of using sets.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Have an array of vertices, not squares. Then a line is along the edges of the squares. A 1x1 square would use a 2x2 array, 2x2 uses 3x3, etc.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
DrPepperMan34

DrPepperMan34

    Newbie

  • Members
  • PipPip
  • 13 posts
Cheers.

So for example the 2 by 2 board would consist of:

.___.___.
!___!___!
!___!___!

Where the dots are the vertices.

So the array would consist of:

>>array_level1[0.0,1.0,2.0]
>>array_level2[0.1,1.1,2.1]
>>array_level3[0.2,1.2,2.2]

As x/y co-ordinates.

I could have an if statement which (because the board can't be too big) roughly says:

If user input = 1 (number of squares along)
create 1 x 1 array:
array_level1[...]
array_level2[...]

else if user input = 2
create 2 x 2 array::
array_level1[...]
array_level2[...]
array_level3[...]

else if user input = 3
create 3 x 3 array:
array_level1[...]
array_level2[...]
array_level3[...]
array_level4[...]
...
...
...

and so on till user input 5 or 6, because if I implement minimax/alpha beta only relatively small board sizes will really work.

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
That looks like it should work well.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
DrPepperMan34

DrPepperMan34

    Newbie

  • Members
  • PipPip
  • 13 posts
I have a problem. When I use this code:

if str(size_of_board) == '1':
    board_level1 = [0.0,1.0,2.0]
    board_level2 = [0.1,1.1,2.1]
    print "Board is " + str(str(size_of_board)) + " by " + str(size_of_board)

and then type board_level1 I get:

>>> board_level1
[0.0, 1.0, 2.0]

which is correct, but when I type:

board_level2
[0.10000000000000001, 1.1000000000000001, 2.1000000000000001]

which is wrong. Is there anyway round this with out putting the co-ordinates in ' ' ?

#10
DrPepperMan34

DrPepperMan34

    Newbie

  • Members
  • PipPip
  • 13 posts
Ok I've made some changes but now I'm completely stuck. This is my code:

def int_size_of_board():

    size_of_board = int(raw_input("How many squares across do you want the game to be?"))

    if size_of_board == 1 or 2 or 3 or 4 or 5 or 6:

        dX,dY = size_of_board

        board = [[(i,j) for i in range(dX + 2)] for j in range(dY + 1)]

        print size_of_board

        print dX

    else:

        print 'Size too big'

    return board

But when I run it I get this:

line 4, in int_size_of_board

    dX,dY = size_of_board

TypeError: 'int' object is not iterable

Does anyone know why this is?

Cheers

#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I don't know Python, sorry. That said, it seems likely it should be:
        dX = size_of_board
        dY = size_of_board

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#12
DrPepperMan34

DrPepperMan34

    Newbie

  • Members
  • PipPip
  • 13 posts
Thanks for the reply. As with programming 5 mins after I posted my reply I tried what you suggest and it worked. Cheers