def __init__(self):
     self.board = Board()
     self.myShip = Spaceship('$', 56, self.board)
     self.miss1 = []
     self.miss2 = []
     self.aliens = []
     self.score = 0
     self.start = time.time()
     self.getch = GetInput._getChUnix()
def big_move_test(r=3, c=1):
    states = []
    board = Board.Board(7)
    print("Initial board:")
    board.print()
    states.append(board)

    # available_move_test
    print("available move test")
    d = available_move_test(board, r, c)

    # move_test
    print("move test")
    if (len(d) != 0):
        print(d)
        b = move_test(board, int(d[0]), r, c)
        states.append(b)
        # b.print()
    else:
        print("no move")
    b = move_test(b, 4, 3, 4)
    states.append(b)
    # b.print()

    path_print(states)
    print("in")
    sted = AStar.h2(states)
    path_print(sted)
def iterative_bfs_test():
    board = Board.Board(7)
    bfs = Bfs.Bfs(board)
    result, queue, path = bfs.iterative_bfs()
    print("Final state: ")
    result.print()
    print("Peg count is: ", result.count_pegs())
    # print("Path is: ")
    # for i in path:
    #     i.print()

    print(bfs.get_gen_cnt(), " nodes are generated")
    print(bfs.get_ext_cnt(), " nodes are extended")
class Engine(Board, Spaceship, Missile, Alien):
    '''game engine that inherits from classes
       Board, Spaceship, Missile, Alien'''
    def __init__(self):
        self.board = Board()
        self.myShip = Spaceship('$', 56, self.board)
        self.miss1 = []
        self.miss2 = []
        self.aliens = []
        self.score = 0
        self.start = time.time()
        self.getch = GetInput._getChUnix()

    #method that returns the alien 
    #object at a given position 
    #when a missile collides with 
    #the alien at that position
    def findalien(self, pos):
        for i in self.aliens:
            if i.pos == pos:
                return i

    #method to update the gameboard
    def updateboard(self):
        delmiss = []
        delaliens = []

        #update positions of missiles
        for i in self.miss1:

            if time.time() - i.time >= 1:

                if self.board.grid[i.pos] == i.ch:
                    self.board.grid[i.pos] = ' '
                i.pos -= i.jump

                if i.pos < 0:
                    delmiss.append(i)
                    continue

                if self.board.grid[i.pos] == 'A':
                    delmiss.append(i)
                    self.score += 10
                    self.board.grid[i.pos] = ' '
                    alien_obj = self.findalien(i.pos)
                    delaliens.append(alien_obj)
                    continue

                elif self.board.grid[i.pos] == '@':
                    delmiss.append(i)
                    self.score += 5
                    self.board.grid[i.pos] = ' '
                    alien_obj = self.findalien(i.pos)
                    delaliens.append(alien_obj)
                    continue

                else:
                    i.time = time.time()
                    self.board.grid[i.pos] = i.ch

        for i in delmiss:
            self.miss1.remove(i)

        delmiss = []
        for i in self.miss2:

            if time.time() - i.time >= 1:

                if self.board.grid[i.pos] == i.ch:
                    self.board.grid[i.pos] = ' '

                i.pos -= (i.jump) / 2

                if i.pos < 0:
                    delmiss.append(i)
                    continue

                if self.board.grid[i.pos] == 'A':
                    delmiss.append(i)
                    self.board.grid[i.pos] = '@'
                    alien_obj = self.findalien(i.pos)
                    alien_obj.time += 5
                    continue

                elif self.board.grid[i.pos] == '@':
                    delmiss.append(i)
                    self.board.grid[i.pos] = ' '
                    alien_obj = self.findalien(i.pos)
                    alien_obj.time += 5
                    continue

                else:
                    pass

                i.pos -= (i.jump) / 2

                if i.pos < 0:
                    delmiss.append(i)
                    continue

                if self.board.grid[i.pos] == 'A':
                    delmiss.append(i)
                    self.board.grid[i.pos] = '@'
                    alien_obj = self.findalien(i.pos)
                    alien_obj.time += 5
                    continue

                elif self.board.grid[i.pos] == '@':
                    delmiss.append(i)
                    self.board.grid[i.pos] = ' '
                    alien_obj = self.findalien(i.pos)
                    alien_obj.time += 5
                    continue

                else:
                    i.time = time.time()
                    self.board.grid[i.pos] = i.ch

        for i in delmiss:
            self.miss2.remove(i)

        #remove aliens that have been
        #on the board for ten seconds
        for i in self.aliens:
            if time.time() - i.time >= 10:
                delaliens.append(i)
                self.board.grid[i.pos] = ' '

        for i in delaliens:
            try:
                self.aliens.remove(i)
            except ValueError:
                pass

        #spawning three aliens randomly
        #after every seven seconds
        if time.time() - self.start >= 7:
            self.start = time.time()
            for _ in range(3):
                newalien = Alien('A', '@', randint(0, 15), time.time())
                self.board.grid[newalien.pos] = newalien.ch1
                self.aliens.append(newalien)

        del delaliens
        del delmiss

    def run(self):

        def alarmhandler(signum, frame):
            raise TypeError

        def getinp(timeout=0.5):
            signal.signal(signal.SIGALRM, alarmhandler)
            signal.setitimer(signal.ITIMER_REAL, timeout)
            try:
                ch = self.getch()
                signal.alarm(0)
                return ch
            except TypeError:
                pass
            signal.signal(signal.SIGALRM, signal.SIG_IGN)
            return ''

        for _ in range(4):
            newalien = Alien('A', '@', randint(0, 15), time.time())
            self.board.grid[newalien.pos] = newalien.ch1
            self.aliens.append(newalien)

        while True:

            self.board.render()

            print 'YOUR SCORE:', self.score

            inp = getinp()

            if inp == 'a':
                self.myShip.moveLeft()

            elif inp == 'd':
                self.myShip.moveRight()

            elif inp == 'w':
                newmissile = Missile('i', self.myShip.pos-8, time.time(), 1)
                self.board.grid[self.myShip.pos-8] = 'i'
                self.board.render()
                self.miss1.append(newmissile)

            elif inp == 's':
                newmissile = Missile('l', self.myShip.pos-8, time.time(), 2)
                self.board.grid[self.myShip.pos-8] = 'l'
                self.board.render()
                self.miss2.append(newmissile)

            elif inp == 'q':
                print('Come back again!')
                exit()

            else:
                pass

            self.updateboard()
        if board.three_in_a_row() == 1:
            print("X Win!")
            return
        elif board.three_in_a_row() == 2:
            print("O Win!")
            return

        # KI turn
        best = MCTS.choose_best_move(board, 1500)
        print(best)
        board.make_move(best)
        print(board)

        if board.three_in_a_row() == 1:
            print("X Win!")
            return
        elif board.three_in_a_row() == 2:
            print("O Win!")
            return


print("Start")

board = Board.Board()
print(board)

play(board)


Exemple #6
0
how_many_pellets = 20




bot = Bot(start_loc=(0,0))

finish = Finish()

actors = [bot, finish]
for _ in range(how_many_pellets):
    actors.append(Pellet())

dimension = 20
display_size = 600


board = Board(dimension, display_size, initial_randomness, randomness_decay, gamma=gamma, FPS=FPS)
board.set_actors(actors)

pygame.init()
pygame.display.set_caption("TIMBERLEE'S Journey")

for i in range(total_iterations):
    print(i)
    print(board.randomness)
    if i >= total_iterations - 2:
        show_board = True
    board.run_episode(show=show_board)
    board.reset_board()
        "                     Welcome to the memory game! \n"
        "                 Please indicate how many pairs you want!\n"
        "            Remember that this game only accept numbers as answers\n"
        "-------------------------------------------------------------------------"
    )
    Cards = int(input("Number of pairs: "))

    return Cards


A = Cards(menu())
x, y = Cards.tablero(A)

Pt_1 = 0  #Jugadores
Pt_2 = 0
B = Board(x, y)

B.Print()

Turn = False  #False jugador 1 True jugador 2
while True:
    if Turn:
        print("\nPlayer 2 turn!\n")
    else:
        print("\nPlayer 1 turn!\n")

    Comp, puntos, Symbols, Matrix = B.Coords()

    if Turn:
        Pt_2 += puntos
    else:
Exemple #8
0
import sys
import datetime
import os

sys.path.append('Classes')

from Classes import Board, Interface

CURRENT_OS = os.name
BOARD_SIZE_ROW = 300
BOARD_SIZE_COLUMN = 300
# Make a X,Y game board. 0,0 is the upper left corner.
GameBoard = Board.Board(size_row=BOARD_SIZE_ROW, size_column=BOARD_SIZE_COLUMN)

# Open HWInterface only on the pi
if CURRENT_OS == 'posix':
    HWInterface = Interface.HWInterface(size_row=BOARD_SIZE_ROW,
                                        size_column=BOARD_SIZE_COLUMN)

# Seed the board with a pattern
# TODO: Randomize a starting patern or import/create a known pattern
GameBoard.Create_Glider(5, 5)
a = datetime.datetime.now()
round_num = 0

while round_num < 15:
    b = datetime.datetime.now()
    if ((b - a).seconds % 2 == 1):  # Delay the processing
        game_state = GameBoard.Process()

        # If on posix then we are in the raspberrypi
def bfs_test():
    sys.setrecursionlimit(10000)
    board = Board.Board(7)
    bfs = Bfs.Bfs(board)
    result, queue, path = bfs.bfs()
    print_results(result, bfs)
def dfs_test():
    board = Board.Board(7)
    dfs = Dfs.Dfs(board)
    result, stack, path = dfs.dfs()
    print_results(result, dfs)
def a_star_test(hn):
    sys.setrecursionlimit(10000)
    board = Board.Board(7)
    a_star = AStar.AStar(board, hn)
    result, queue, path = a_star.a_star()
    print_results(result, a_star)
def peg_indexes_test():
    m = Board.Board(7)
    print(m.peg_indexes())
Exemple #13
0
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 150, 0)
RED = (255, 0, 0)

# This sets the WIDTH and HEIGHT of each grid location
WIDTH = 40
HEIGHT = 40

# This sets the margin between each cell
MARGIN = 5
WINDOW_SIZE = [365, 420]

# Initializes the board
board = Board()

# Score text
BLACK_SCORE = 'Black\n{}'

WHITE_SCORE = 'White\n{}'


def turn(position, player, p1_pos, p2_pos):
    '''
        structures the turn for each player
    '''
    board.show_valid(player)


def text_obj(text, font, color):