def __init__(self, numberOfPlayers, names): """ Initialize the Game Controller """ self.game = Game(numberOfPlayers, names) screen = GameScreen(self.game) ConsoleController.__init__(self, screen, commands={ENDL: self.nextMessage})
def add_turns(element: Element, game: game_models.Game): for turn in element: name = turn.attrib["id"] new_turn = game_models.Turn(action=game.actions[turn.attrib["action"]], name=name) game.turns[name] = new_turn if turn.attrib.get("initial") is not None: game.starting_turn = new_turn
def _play(self): self.frame_2 = Frame(self.main_window) self.frame_2.pack() try: self._rows = int(self.row_entr.get()) self._columns = int(self.col_entr.get()) if self._rows < 2 or self._rows > 20 or self._columns < 2 or self._columns > 20: raise GameException("This size of the board is not allowed!") self.frame_1.pack_forget() self.frame_play.pack_forget() self._game = Game([self._rows, self._columns]) self.buttons = [] count = 0 for row in range(self._rows): for col in range(self._columns): self.buttons.append(Button(self.frame_2,text = " ",\ width = 3, height = 1, bg = "thistle2",\ command = lambda col = col,row = row : self._move(row,col) )) self.buttons[count].grid(column=col, row=row) count += 1 except Exception as e: messagebox.showerror("Error!", str(e))
def setUp(self): unittest.TestCase.setUp(self) self.__rows = 4 self.__columns = 5 self.__10board = Board(10, 10) self.__board = Board(self.__rows, self.__columns) self.__game = Game([self.__rows, self.__columns]) self.__sq1 = Square(1, 1) self.__sq2 = Square(2, 2) self.__sq34 = Square(3, 4) self.__sq45 = Square(4, 5)
class Console: def _get_move(self): while True: try: row = int(input("row: ")) col = int(input("col: ")) return Square(row, col) except Exception as e: print("Invalid move! -> " + str(e)) def start(self): player_move = True while True: try: rows = int(input("Number of rows for the board: ")) columns = int(input("Number of columns for the board: ")) if rows < 2 or rows > 20 or columns < 2 or columns > 20: raise GameException( "The number of rows and columns must be between 2 and 20!" ) self.__game = Game([rows, columns]) break except Exception as e: print(str(e)) b = self.__game.board while b.won() == False: try: if player_move: print(b) move = self._get_move() self.__game.human_move(move) else: self.__game.computer_move() player_move = not player_move except Exception as e: print(str(e)) print("Game over!") print(b) if player_move == True: print("Computer wins!") else: print("Player wins!")
def add_pieces(element: Element, game: game_models.Game): for piece in element: name = piece.attrib["id"] new_piece = game_models.Piece(name, None) for attribute in piece: if attribute.tag == "attribute": try: attribute_value = int(attribute.text) except ValueError: if attribute.text.lower() == "true": attribute_value = True elif attribute.text.lower() == "false": attribute_value = False else: attribute_value = attribute.text new_piece.attributes[attribute.attrib["name"]] = attribute_value game.pieces[name] = new_piece for piece in element: for relation in piece: if relation.tag == "relation": related_name = relation.attrib["to"] if related_name in game.actions: related = game.actions[related_name] elif related_name in game.collections: related = game.collections[related_name] elif related_name in game.pieces: related = game.pieces[related_name] elif related_name in game.turns: related = game.turns[related_name] else: raise BadGameXML("Relation doesn't exist", relation) game.pieces[piece.attrib["id"]].attributes[relation.attrib["name"]] = related
def buildGame(self, lobby): """ Build the Game """ players = self.getGamePlayers(lobby) return Game(players, mainDeck=self.mainDeck, kickDeck=self.kickDeck, weaknessDeck=self.weaknessDeck, superVillainDeck=self.loadSupervillainDeck( number=self.numberOfVillains))
def __init__(self, gen_size=100, threshold=10): self.games = [ Game(fps=100, caption='Game ' + str(x)) for x in range(gen_size) ] self.curr_gen = [ AIPlayer(x, [10, 10], random=True) for x in self.games ] self.gen_size = gen_size self.threshold = threshold
class GameController(ConsoleController): """ Controller for a Game """ def __init__(self, numberOfPlayers, names): """ Initialize the Game Controller """ self.game = Game(numberOfPlayers, names) screen = GameScreen(self.game) ConsoleController.__init__(self, screen, commands={ENDL:self.nextMessage}) def nextMessage(self, event): """ Tell the screen to print the next message """ if self.game.over: self.stopRunning() else: self.runController(RoundController(self.game.getNewRound())) if self.game.over: self.screen.message = "{0} Won!".format(self.game.winningPlayer) else: self.screen.message = "Round Complete!\r\nGet Ready for the next one!"
class GameController(ConsoleController): """ Controller for a Game """ def __init__(self, numberOfPlayers, names): """ Initialize the Game Controller """ self.game = Game(numberOfPlayers, names) screen = GameScreen(self.game) ConsoleController.__init__(self, screen, commands={ENDL: self.nextMessage}) def nextMessage(self, event): """ Tell the screen to print the next message """ if self.game.over: self.stopRunning() else: self.runController(RoundController(self.game.getNewRound())) if self.game.over: self.screen.message = "{0} Won!".format( self.game.winningPlayer) else: self.screen.message = "Round Complete!\r\nGet Ready for the next one!"
async def _start_game(self, player_one, player_two): game = Game(player_one, player_two) player_one.state = PlayerState.IN_GAME player_two.state = PlayerState.IN_GAME player_one.game = game player_two.game = game if game.players_turn is player_one: player_one_msg = Messages.game_start(player_two.name, True) await self._send(player_one.socket, player_one_msg) player_two_msg = Messages.game_start(player_one.name, False) await self._send(player_two.socket, player_two_msg) else: player_one_msg = Messages.game_start(player_two.name, False) await self._send(player_one.socket, player_one_msg) player_two_msg = Messages.game_start(player_one.name, True) await self._send(player_two.socket, player_two_msg)
class GUI: def start(self): self.main_window = Tk() self.main_window.title("Obstruction - ") self.main_window.configure(background="thistle2") #Creating entries for the board size self.frame_1 = Frame(self.main_window) self.frame_1.pack(side=TOP) self.l_row = Label(self.frame_1, text="Nr. Rows: ", bg="thistle2") self.l_row.pack(side=LEFT) self.row_entr = Entry(self.frame_1, width=10) self.row_entr.pack(side=LEFT) self.l_col = Label(self.frame_1, text="Nr. Columns: ", bg="thistle2") self.l_col.pack(side=LEFT) self.col_entr = Entry(self.frame_1, width=10) self.col_entr.pack(side=LEFT) #Creating the board self.frame_play = Frame(self.main_window) self.frame_play.pack() self.play_btn = Button(self.frame_play, compound = CENTER,\ text = "START GAME",\ bg = "thistle1", \ command = self._play) self.play_btn.pack() self.main_window.mainloop() def _play(self): self.frame_2 = Frame(self.main_window) self.frame_2.pack() try: self._rows = int(self.row_entr.get()) self._columns = int(self.col_entr.get()) if self._rows < 2 or self._rows > 20 or self._columns < 2 or self._columns > 20: raise GameException("This size of the board is not allowed!") self.frame_1.pack_forget() self.frame_play.pack_forget() self._game = Game([self._rows, self._columns]) self.buttons = [] count = 0 for row in range(self._rows): for col in range(self._columns): self.buttons.append(Button(self.frame_2,text = " ",\ width = 3, height = 1, bg = "thistle2",\ command = lambda col = col,row = row : self._move(row,col) )) self.buttons[count].grid(column=col, row=row) count += 1 except Exception as e: messagebox.showerror("Error!", str(e)) def _move(self, row, col): try: self._game.human_move(Square(row, col)) self._show_change() if self._game.board.won(): messagebox.showinfo("Game won!", "Congratulation YOU WON this game!") self.main_window.destroy() else: self._game.computer_move() self._show_change() if self._game.board.won(): messagebox.showinfo("Game won!", "The Computer WON this game!") self.main_window.destroy() except Exception as e: messagebox.showerror("Error!", str(e)) def _show_change(self): count = 0 for r in range(self._rows): for c in range(self._columns): if self._game.board.get_square(r, c) == ".": self.buttons[count].config(bg="thistle4") else: self.buttons[count].config( text=self._game.board.get_square(r, c)) count += 1
from Game.game import Game PRISONER_DILEMMA = { "players": (("A", "testify", "refuse"), ("B", "testify", "refuse")), "matrix": (((-5, -5), (-10, 0)), ((0, -10), (-1, -1))) } g = Game(PRISONER_DILEMMA) print "Dominant strategy for A: %s" % g.dominant_strategy("A") print "Dominant strategy for B: %s" % g.dominant_strategy("B") print "Equilibrium Points: %s" % str(g.equilibrium()) CONSOLE_GAME = { "players": (("A", "blu", "dvd"), ("B", "blu", "dvd")), "matrix": (((9, 9), (-4, -1)), ((-3, -1), (5, 5))) } g = Game(CONSOLE_GAME) print "Dominant strategy for A: %s" % g.dominant_strategy("A") print "Dominant strategy for B: %s" % g.dominant_strategy("B") print "Equilibrium Points: %s" % str(g.equilibrium())
class Test_Game(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.__rows = 4 self.__columns = 5 self.__10board = Board(10, 10) self.__board = Board(self.__rows, self.__columns) self.__game = Game([self.__rows, self.__columns]) self.__sq1 = Square(1, 1) self.__sq2 = Square(2, 2) self.__sq34 = Square(3, 4) self.__sq45 = Square(4, 5) def tearDown(self): unittest.TestCase.tearDown(self) def test_board(self): self.assertEquals(len(self.__10board.empty_squares()), 10 * 10) self.assertEquals(len(self.__board.empty_squares()), self.__rows * self.__columns) self.assertEquals(self.__board.get_square(2, 3), " ") self.assertEquals(self.__board.won(), False) self.__board.move(Square(2, 3), "X") self.assertEquals(self.__board.get_square(2, 3), "X") self.assertEquals(len(self.__board.empty_squares()), 11) self.assertEquals(self.__board.get_square(1, 2), ".") try: self.__board.move(Square(2, 3), "X") assert False except GameException as ge: self.assertEquals(str(ge), "Square already taken!") self.__board.move(Square(2, 3), "E") self.assertEquals(self.__board.get_square(2, 3), " ") self.assertEquals(len(self.__board.empty_squares()), 4 * 5) self.assertEquals(self.__board.get_square(1, 2), " ") def test_game(self): self.assertEquals(len(self.__game.board.empty_squares()), self.__rows * self.__columns) self.__game.board.move(self.__sq1, "X") self.assertEquals(len(self.__game.board.empty_squares()), 11) try: self.__game.board.move(self.__sq45, "X") assert False except GameException as ge: self.assertEquals(str(ge), "Move outside board!") self.__game.board.move(self.__sq34, "O") self.assertEquals(len(self.__game.board.empty_squares()), 7) self.__game.board.move(Square(0, 4), "X") board = self.__game.board.copy() best_move = self.__game.computer_AI(board, "computer", 0, -100, 100) self.assertEquals(str(best_move[0]), str(Square(3, 0))) self.__game.computer_move() self.assertEquals(self.__game.board.get_square(3, 0), "O") self.__game.board.move(Square(3, 2), "X") self.assertEquals(self.__game.board.won(), True)
from Game.game import Game PRISONER_DILEMMA = { "players": ( ("A", "testify", "refuse"), ("B", "testify", "refuse") ), "matrix": ( ((-5, -5), (-10,0)), (( 0,-10), (-1,-1)) ) } g = Game(PRISONER_DILEMMA) print "Dominant strategy for A: %s" % g.dominant_strategy("A") print "Dominant strategy for B: %s" % g.dominant_strategy("B") print "Equilibrium Points: %s" % str(g.equilibrium()) CONSOLE_GAME = { "players": ( ("A", "blu", "dvd"), ("B", "blu", "dvd") ), "matrix": ( (( 9, 9), (-4,-1)), ((-3,-1), ( 5, 5)) ) } g = Game(CONSOLE_GAME) print "Dominant strategy for A: %s" % g.dominant_strategy("A") print "Dominant strategy for B: %s" % g.dominant_strategy("B")
def testGame(self): b = Board() a = Ai() g = Game(b, a) self.assertEquals(g.getAiPlaneCount(), 0) g.addPlane(0, 2, 'S') g.addPlane(0, 7, 'S') g.addPlane(4, 6, 'S') self.assertEquals(g.getFromPosition(0, 2), 2) g.chooseAiPlanes() self.assertEquals(g.getAiPlaneCount(), 3) while g.isGameWon() == False: g.makeAiMove() self.assertEquals(g.wonPlayer(), False)
from Server.server import Server from Ui.ui import Ui s = curses.initscr() uiState = -1 uiError = '' while True: ui = Ui(uiState, uiError) uiState = ui.state uiError = '' if ui.ip: # If connectin to a game try: game = Game(ui.ip[0], int(ui.ip[1])) game.connect() windowSize = s.getmaxyx() if windowSize[0] < game.size[0] or windowSize[1] < game.size[1]: uiError = f' Sorry, your terminal window has to be at least {game.size[1]}x{game.size[0]}. ' uiState = -1 else: game.start() break except: uiError = ' Error: Connection refused ' uiState = -1
def __init__(self, numberOfPlayers, names): """ Initialize the Game Controller """ self.game = Game(numberOfPlayers, names) screen = GameScreen(self.game) ConsoleController.__init__(self, screen, commands={ENDL:self.nextMessage})
import curses, time, sys from Game.game import Game if len(sys.argv) <= 1: print('Host address is required as an argument [ip:port]') sys.exit() else: ip, port = sys.argv[1].split(":") s = curses.initscr() curses.curs_set(0) curses.noecho() game = Game(ip, int(port)) game.connect() while game.game == {}: time.sleep(0.2) game.draw() while True: try: key = game.window.getch() if key == 27: # Quit if esc break if key == 87 or key == 119: # w/W, Move game.move(0) elif key == 68 or key == 100: # d/D
from Players.randomPlayer import RandomPlayer from Players.greedyPlayer import GreedyPlayer from Players.humanPlayer import HumanPlayer from Players.AIPlayer import AIPlayer from Game.game import Game HIDDEN = 100 boardsize = 3 #players = [RandomPlayer(1, boardsize), AIPlayer(3, 3, HIDDEN)] players = [RandomPlayer(1, boardsize), GreedyPlayer(3, boardsize)] g = Game(players, boardsize) g.play()
t = TransProb(OBSERVATIONS) t.report() print "\n=== Homework 6.2 ===" CHAIN = ("A", 1.0, {True: 0.9, False: 0.5}) c = MarkovChain(CHAIN) print "stationary distribution: P(A) = %.4f" % c.stationary_distribution(True) print "stationary distribution: P(B) = %.4f" % c.stationary_distribution(False) print "\n=== Homework 6.3 ===" MODEL = ("A", 0.5, {True: 0.5, False: 0.5}, "X", {True: 0.1, False: 0.8}) m = MarkovModel(MODEL) m.p({"A0": True}, {"X0": True}) m.p({"A1": True}, {"X0": True}) m.p({"A1": True}, {"X0": True, "X1": True}) print "\n=== Homework 6.11 ===" from Game.game import Game HW6_11 = { "players": (("B", "d", "e", "f"), ("A", "a", "b", "c")), "matrix": ( ((3, 3), (5, 0), (2, 1)), ((2, 4), (7, 8), (4, 6)), ((7, 5), (8, 5), (5, 3)), ) } g = Game(HW6_11) print "Dominant strategy for A: %s" % g.dominant_strategy("A") print "Dominant strategy for B: %s" % g.dominant_strategy("B") print "Equilibrium Points: %s" % str(g.equilibrium())
################# ''' ''' ####################### import pygame as PY from Engine.ace_engine import AceEngine from Game.game import Game PY.init() clock = PY.time.Clock() FPS = 30 G = Game(PY) AE = AceEngine.new(PY, G) def start(): while True: AE.get_input() AE.update() AE.render() clock.tick(FPS)
from Model.ship import Ship from Model.board import Board from Model.ship import Point from Game.game import Game from ui.ui import UI if __name__ == '__main__': b = Board() g = Game(b) u = UI(g) u.start()