Python Forum
Building a patterned grid from a dictionary...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Building a patterned grid from a dictionary...
#1
I'm making a peg solitaire type grid, with the peg holes being displayed as an 'O', and the corners where the pegs can't go (but must still have displayed something for the grid displayed to look like a cross) be displayed as an "X".


xx000xx
xx000xx
0000000
0000000
0000000
xx000xx
xx000xx



What I am trying to do is set up a class which will take lists of groups of three inputs like follows:


      # X : Y : Boolean
         1 : 1 : False 
         1 : 2 : False 
         1 : 3 : True
         1 : 4 : True
         1 : 5 : True
         1 : 6 : False 
         1 : 7 : False 
       
         2 : 1 : False 
         2 : 2 : False 
         2 : 3 : True
         2 : 4 : True
         2 : 5 : True
         2 : 6 : False 
         2 : 7 : False 

         So on, and so on....

This will then be read through and printed out, with False co-ordinates displayed as an X, and True Co-ordinate as an O. But I need to do this in batch, I can't just assign 49 variables calling the readGrid() class. So is there any way of cycling through large three input dictionary type variables? Or any ideas on how I might go about this. (I'm quite tired so I'm having a tough time think by myself XD)
Reply
#2
Hello,

What have you tried so far?

A nested list might be better, and why not store character 'X' or 'O' that way there need not be a conversion.


Larz60+
Reply
#3
I often use a dictionary for game boards. Tuples are hashable, so they can be used as keys in a dictionary. So you use a tuple of the coordinates of the peg holes as the keys of the dictionary, and the state of each peg hole is the value.

And Larz60+ is right that boolean may not be the best way to store the state. If your peg holes can have more than two states (empty, unusable, peg), than you want something that can handle more than two states. You can use 'X' and 'O', and then it's easy to print the board on the screen. However, it might be that the way you are processing the peg holes would make integer values easier to use in the processing. So you want to think about where in your program you want to interpret the values, and where in your program you want to make use of them as they are.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Oct-01-2016, 05:14 AM)Larz60+ Wrote: Hello,

What have you tried so far?

A nested list might be better, and why not store character 'X' or 'O' that way there need not be a conversion.


Larz60+
Over night I was able to make a list with multiple list within, each with four attributes [x, y, useable, occupied], but looking back (I've done too much to go back now), I probably would've had better luck you're way, storing X and O's. Thanks for the answer anyway though,  I'll have to redo this for an A level assignment, probably will save me a lot of time!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting a patterned string or text into excel table soup1987 1 2,097 Oct-03-2019, 01:37 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020