Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/grid.py
blob: 7b4e096904ae92bb47d63e0b25fdf68475d5f4fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#Copyright (c) 2009, Walter Bender

#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:

#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.

import pygtk
pygtk.require('2.0')
import gtk
import gobject
import random

from sprites import *
from card import *
from deck import *

from constants import *

ROW = 5
COL = 3

#
# class for managing 3x5 matrix of cards
#
class Grid:
    def __init__(self, width, height, card_width, card_height):
        # the playing surface is a 3x5 grid
        self.grid = []
        # how many cards are on the playing field
        self.cards = 0
        # card spacing
        self.left = int((width-(card_width*2))/2)
        self.xinc = int(card_width*1.2)
        # self.top = int((height-(card_height*3.5))/2)
        self.top = 10
        self.yinc = int(card_height*1.33)

    # deal the initial deck of cards
    def deal(self, deck):
        # find upper left corner of grid
        self.cards = 0
        self.grid = []
        x = self.left
        y = self.top
        for r in range(0, ROW-1):
            for c in range(0, COL):
                a = deck.deal_next_card()
                self.grid.append(a)
                self.place_a_card(a, x, y)
                x += self.xinc
                self.cards += 1
            x = self.left
            y += self.yinc
        for c in range(0,COL):
            # leave a blank row for extra cards
            self.grid.append(None)

    # add cards when there is no match
    def deal_extra_cards(self, deck):
        # if there are still cards in the deck and only 12 cards in the grid
        if deck.empty() is False and self.cards == DEAL:
            # add three extra cards to the playing field
            for c in range(0,COL):
                i = self.grid.index(None)
                self.grid[i] = deck.deal_next_card()
                x = self.left+self.xinc*(i%COL)
                y = self.top+self.yinc*int(i/COL)
                self.place_a_card(self.grid[i], x, y)
                self.cards += 1

    # remove a match from the grid and deal new cards from the deck
    def remove_and_replace(self, clicked_set, deck):
        for a in clicked_set:
            # find the position in the grid of the clicked card
            i = self.xy_to_grid(a.x,a.y)
            # only add new cards if we are down to 12 cards
            if self.cards == DEAL:
                if deck.empty():
                    self.grid[i] = None
                else:
                    # save card in grid position of card we are replacing
                    self.grid[i] = deck.deal_next_card()
                    self.place_a_card(self.grid[i], a.x, a.y)
            else:
                self.cards -= 1
                # mark grid positions of cards we are not replacing
                self.grid[i] = None
            # move clicked card to the match area
            a.x = 10
            a.y = self.top + clicked_set.index(a)*self.yinc
            deck.spr_to_card(a).show_card()

    # if we have removed cards from a grid of 15, we may need to consolidate
    def consolidate(self):
        for j in range(12,15):
            i = 0
            while(self.grid[j] is not None):
                if self.grid[i] is None:
                    self.grid[i] = self.grid[j]
                    self.grid[i].spr.move(self.grid_to_xy(i))
                    self.grid[j] = None
                else:
                    i+=1

    # place a card at position x,y and display it
    def place_a_card(self, c, x, y):
        if c is not None:
            c.spr.x = x
            c.spr.y = y
            c.show_card()

    # convert from sprite x,y to grid index
    def xy_to_grid(self, x, y):
        return int(COL*(y-self.top)/self.yinc) + int((x-self.left)/self.xinc)

    # convert from grid index to sprite x,y
    def grid_to_xy(self, i):
        return ((self.left+i%COL*self.xinc),(self.top+(i/COL)*self.yinc))

    # return sprite in grid element i
    def grid_to_spr(self, i):
        print self.grid[i].spr
        return self.grid[i].spr