Exemplo n.º 1
0
 def testGetNeighbourCells(self):
     conway = Conway('rayas')
     neighbours = conway.getNeighbourCells([1, 1])
     values = []
     for neighbour in neighbours:
         values.append(conway.currentGeneration[neighbour[0]][neighbour[1]])
     expected = [1, 1, 1, -1, -1, 1, 1, 1]
     self.assertEqual(expected, values)
Exemplo n.º 2
0
 def test_Conway1(self):
     c = Conway('1')
     self.assertEqual(c.getCurrent(), '1')
     self.assertEqual(c.computeNext(), '11')
     self.assertEqual(c.computeNext(), '21')
     self.assertEqual(c.computeNext(), '1211')
     self.assertEqual(c.computeNext(), '111221')
     self.assertEqual(c.computeNext(), '312211')
     self.assertEqual(c.computeNext(), '13112221')
def main():
    # Initialize pygame
    pygame.init()

    # Start application

    config = [
        Config((640, 640), "Conway's Game of Life", 64, Conway()),
        Config((640, 640), "Langston's Ant", 64, Ant()),
        Config((640, 640), "Brian's Brain", 64, Brian()),
        Config((640, 640), "Rule 90", 64, Rule90()),
        Config((640, 640), "Rule 110", 64, Rule110()),
        Config((640, 640), "Fluid Simulation", 64, Fluid())
    ]

    Application(config[5])
Exemplo n.º 4
0
def player(init_config):
    """ plays optimally and tries to win if there exists a winning
    strategy """
    global GSG_VAL, CONWAY, CONWAY_state
    _row = len(init_config)
    if _row == 0:
        return
    _column = len(init_config[0])
    if _column == 0:
        return
    calculate_gsg(_row, _column)
    graph = GSG_VAL[0]
    val_l = GSG_VAL[1]
    val_c = GSG_VAL[2]
    start_state = CONWAY.get_state_index(init_config)
    
    if (val_l[start_state] == 0):
        print "No winning move exist from this position "
    else:
        print "There exists a non-loosing move from this position"
    
    pos = start_state    
    
    while (True):
        print_position(pos)
        
        n_nodes = graph[pos]

        options = [i for i in n_nodes if val_l[i] == 0]
        if options == [] :
            options = [i for i in n_nodes if val_l[i] == float('inf')]
        min_c_val = float('inf')
        feasible_option = None
        for option in options:
            if val_c[option] < min_c_val:
                min_c_val = val_c[option]
                feasible_option = option
        
        print
        print "Computer plays ... " , feasible_option
        print 

               
        
        pos = feasible_option
        print_position(pos)
        state_pos = CONWAY_state[pos]
        while True:
            row = input("Enter the row:")
            col = input("Enter the col:")
            if state_pos[row][col] == 0:
                state_pos[row][col] = 1
                break
            print "Cell Already ALIVE !!! "

        if "Windows" in platform.platform() : 
            os.system('cls')
        elif "Linux" in platform.platform() :
            os.system("clear")
        else:
            print "Unknown OS"
            exit()
        print_position(CONWAY.get_state_index(state_pos))        
        
        temp = Conway(ROW_SIZE, COL_SIZE)
        temp.board = state_pos
        state_pos = temp.evolve()
        pos = CONWAY.get_state_index(state_pos)
        
Exemplo n.º 5
0
 def testRefineDrawing(self):
     conway = Conway("dosPorDos")
     expected = [[1, -1], [-1, 1]]
     self.assertEqual(expected, conway.initialCondition)
Exemplo n.º 6
0
 def testSwitchCell(self):
     conway = Conway("dosPorDos")
     conway.switchCell([[0, 0], [0, 1]])
     expected = [[-1, 1], [-1, 1]]
     self.assertEqual(expected, conway.currentGeneration)
Exemplo n.º 7
0
 def testCurrentActiveCells(self):
     conway = Conway("dosPorDos")
     expected = [[0, 0], [1, 1]]
     self.assertEqual(expected, conway.currentActiveCells)
Exemplo n.º 8
0
 def testCorrectSize(self):
     conway = Conway("rayas")
     self.assertEqual(10, conway.gridSize)
     conway = Conway("dosPorDos")
     self.assertEqual(2, conway.gridSize)