We have looked at one possible data structure for a game so far. The adjacency matrix. Any structure that defines a graph is a good canditate for a game with some connected geography, such as a floor plan.
How about a deck of cards or a chess board? What might be a data structure for those?
import numpy as np
import unicodedata as ud
def initialize():
"""
Return a chess board in the form of a 3D numpy array
"""
board = np.full((2,8,8),' ', dtype=str) # start with all spaces, shape of 8 by 8
board[0, 0::2, 0::2] ='W' # every other row and column starting at (0,0), layer 0
board[0, 1::2, 1::2] ='W' # every other row and column starting at (1,1), layer 0
board[0, board[0]!='W'] = 'B' # black everywhere on layer 0 that isn't white
# https://qwerty.dev/chess-symbols-to-copy-and-paste/
white_pieces = '♖♘♗♕♔♗♘♖'
white_pawns = 8 * '♙'
black_pieces = '♜♞♝♛♚♝♞♜'
black_pawns = 8 * '♟'
board[1,7,0:] = list(white_pieces)
board[1,6,0:] = list(white_pawns)
board[1,0,0:] = list(black_pieces)
board[1,1,0:] = list(black_pawns)
return board
wk = '♘'
ord(wk)
9816
chess2 = initialize()
chess2
array([[['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'], ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'], ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'], ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W']], [['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'], ['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙'], ['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖']]], dtype='<U1')
ud.name('♟')
'BLACK CHESS PAWN'
ud.name('♘')
'WHITE CHESS KNIGHT'
my_chess_board[1,0,0]
'♜'
my_chess_board[1,7,7]
'♖'
def get_piece(loc, game_board):
file = 'abcdefgh'.index(loc[0])
rank = 8 - loc[1]
return game_board[1,rank, file]
get_piece(('d',8), my_chess_board)
'♛'
def put_piece(loc, the_piece, game_board):
file = 'abcdefgh'.index(loc[0])
rank = 8 - loc[1]
game_board[1,rank, file] = the_piece
def move_piece(start, end, game_board):
piece = get_piece(start, game_board)
piece_name = ud.name(piece)
print(f"Moving {piece_name} from {start} to {end}")
put_piece(start, ' ', game_board)
put_piece(end, piece, game_board)
my_chess_board[1]
array([['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'], ['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙'], ['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖']], dtype='<U1')
move_piece(('a',2), ('a',3), my_chess_board)
Moving WHITE CHESS PAWN from ('a', 2) to ('a', 3)
my_chess_board[1]
array([['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'], ['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['♙', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', '♙', '♙', '♙', '♙', '♙', '♙', '♙'], ['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖']], dtype='<U1')
move_piece(('b',8), ('c',6), my_chess_board)
Moving BLACK CHESS KNIGHT from ('b', 8) to ('c', 6)
my_chess_board[1]
array([['♜', ' ', '♝', '♛', '♚', '♝', '♞', '♜'], ['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'], [' ', ' ', '♞', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['♙', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', '♙', '♙', '♙', '♙', '♙', '♙', '♙'], ['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖']], dtype='<U1')
my_chess_board[my_chess_board=="W"] # using a filter to select all the 'W's
array(['W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W'], dtype='<U1')
import pandas as pd
pdchess = pd.DataFrame(my_chess_board[1], columns=list("abcdefgh"))
pdchess
a | b | c | d | e | f | g | h | |
---|---|---|---|---|---|---|---|---|
0 | ♜ | ♞ | ♝ | ♛ | ♚ | ♝ | ♞ | ♜ |
1 | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ |
2 | ||||||||
3 | ||||||||
4 | ||||||||
5 | ||||||||
6 | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ |
7 | ♖ | ♘ | ♗ | ♕ | ♔ | ♗ | ♘ | ♖ |
pdchess.loc[0,'b']
'♞'
pdchess.loc[0,'a':'h']
a ♜ b ♞ c ♝ d ♛ e ♚ f ♝ g ♞ h ♜ Name: 0, dtype: object
spades = [chr(x) for x in range(0x1F0A1, 0x1F0A1+13)]
hearts = [chr(x) for x in range(0x1F0B1, 0x1F0B1+13)]
diamonds = [chr(x) for x in range(0x1F0C1, 0x1F0C1+13)]
clubs = [chr(x) for x in range(0x1F0D1, 0x1F0D1+13)]
from random import shuffle
class Deck:
"""
Deck of Cards: planning our methods
"""
def __init__(self):
self._cards = spades + hearts + diamonds + clubs
def shuffle(self):
shuffle(self._cards)
def deal(self, n):
pass
def add(self, cards):
pass
def show(self):
return self._cards
def __repr__(self):
how_many = len(self._cards)
return f"Deck of {how_many} cards"
deck1 = Deck()
deck1
Deck of 52 cards
Deck.shuffle(deck1)
Deck.shuffle(deck1)
print(deck1.show())
['🃆', '🂣', '🂬', '🂻', '🃜', '🂺', '🃅', '🃙', '🂲', '🃃', '🃈', '🂸', '🃛', '🂫', '🂦', '🂥', '🃊', '🂭', '🃑', '🂼', '🂳', '🃒', '🂩', '🃘', '🂡', '🃌', '🃁', '🂢', '🂹', '🂽', '🂵', '🃂', '🃄', '🃝', '🂧', '🂷', '🂤', '🃉', '🃚', '🂴', '🃕', '🂱', '🃗', '🃓', '🂨', '🃇', '🂶', '🃔', '🃖', '🃋', '🃍', '🂪']
spades = [chr(x) for x in range(0x1F0A1, 0x1F0A1+13)]
hearts = [chr(x) for x in range(0x1F0B1, 0x1F0B1+13)]
diamonds = [chr(x) for x in range(0x1F0C1, 0x1F0C1+13)]
clubs = [chr(x) for x in range(0x1F0D1, 0x1F0D1+13)]
deck1
Deck of 52 cards
deck1.__dict__
{'_cards': ['🃆', '🂣', '🂬', '🂻', '🃜', '🂺', '🃅', '🃙', '🂲', '🃃', '🃈', '🂸', '🃛', '🂫', '🂦', '🂥', '🃊', '🂭', '🃑', '🂼', '🂳', '🃒', '🂩', '🃘', '🂡', '🃌', '🃁', '🂢', '🂹', '🂽', '🂵', '🃂', '🃄', '🃝', '🂧', '🂷', '🂤', '🃉', '🃚', '🂴', '🃕', '🂱', '🃗', '🃓', '🂨', '🃇', '🂶', '🃔', '🃖', '🃋', '🃍', '🂪']}
chess1
array([[['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'], ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'], ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'], ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W']], [['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'], ['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙'], ['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖']]], dtype='<U1')
move_piece(('b',1), ('c',3), chess1)
Moving WHITE CHESS KNIGHT from ('b', 1) to ('c', 3)
chess1
array([[['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'], ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'], ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'], ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W']], [['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'], ['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', '♘', ' ', ' ', ' ', ' ', ' '], ['♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙'], ['♖', ' ', '♗', '♕', '♔', '♗', '♘', '♖']]], dtype='<U1')
move_piece(('e',2),('e',4), chess2)
Moving WHITE CHESS PAWN from ('e', 2) to ('e', 4)
chess2
array([[['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'], ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'], ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'], ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'], ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W']], [['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'], ['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', '♙', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['♙', '♙', '♙', '♙', ' ', '♙', '♙', '♙'], ['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖']]], dtype='<U1')