Beispiel #1
0
def play_game(is_learning):
    game.setup(1)

    rAll = 0
    d = False
    j = 0

    race_timer = 0
    is_race_over = False

    while j < 99:
        j += 1

        # choose an action by greedily (with noise) picking from Q table
        game.players[0].racers[1].draw_hand()
        s = game.current_observation()
        a1 = RL.choose_action(s)
        game.players[0].racers[1].select_move(a1)
        if not is_learning:
            game.print_cards_for_racer(game.players[0].racers[1])

        game.players[0].racers[0].draw_hand()
        s2 = game.current_observation()
        a2 = RL.choose_action(s2)
        game.players[0].racers[0].select_move(a2)
        if not is_learning:
            game.print_cards_for_racer(game.players[0].racers[0])

        result = game.make_move()

        if not is_learning:
            racers = []
            for player in game.players:
                racers = [*racers, *player.racers]
            racers.sort()
            racers.reverse()
            game.draw_course(racers)
            if result:
                print(j, 'turns taken')

        r = 0
        if result:
            r = 25 - j

        s3 = game.current_observation()

        if is_learning:
            RL.store_transition(s, a1, r, s2)
            RL.store_transition(s2, a2, r, s3)
            if (counter > 200) and (counter % 10 == 0):
                RL.learn()

        rAll += r
        if result:
            break

    rList.append(rAll)
Beispiel #2
0
def main ():
    pygame.init()
    size = (800,600)

    game.setup()
    while True:
        game.events()
        game.loop()
        pygame.display.flip()
Beispiel #3
0
def play_game(is_learning):
    game.setup(1)

    rAll = 0
    d = False
    j = 0

    race_timer = 0
    is_race_over = False

    game.players[0].racers[1].draw_hand()

    while j < 99:
        j += 1
        # choose an action by greedily (with noise) picking from Q table
        s = game.current_observation()
        a1 = RL.choose_action(str(s))
        game.players[0].racers[1].select_move(a1)

        game.players[0].racers[0].draw_hand()
        s2 = game.current_observation()
        a2 = RL.choose_action(str(s2))
        game.players[0].racers[0].select_move(a2)

        result = game.make_move()

        if not is_learning:
            racers = []
            for player in game.players:
                racers = [*racers, *player.racers]
            racers.sort()
            racers.reverse()
            game.print_cards()
            game.draw_course(racers)
            if result:
                print(j, 'turns taken')

        r = 0
        if result:
            r = 1
            s3 = 'terminal'
        else:
            game.players[0].racers[1].draw_hand()
            s3 = game.current_observation()

        if is_learning:
            RL.learn(str(s2), a2, r, str((s3)))
            RL.learn(str(s), a1, r, str((s2)))

        rAll += r
        if result:
            break
    rList.append(rAll)
Beispiel #4
0
 def login(self, event):
     global passwordFound
     if password.entry.get() == "":
         messagebox.showinfo("Error", "Please enter a password")
         return
     try:
         if database[username.entry.get()] == password.entry.get():
             messagebox.showinfo("Success", "Logging in")
             setup(username.entry.get())
         else:
             messagebox.showinfo("Error", "Password/username not found")
     except KeyError:
         messagebox.showinfo("Error", "Password/username not found")
Beispiel #5
0
def parse_input(key):
	if var.state == 'game':
		if key in ['north','northeast','northwest','east','west','south','southeast','southwest']:
			var.player.direction = key
		
		if key == 'z':
			var.player.shoot('north')
		
		if key == 'p':
			if var.pause: var.pause = False
			else: var.pause = True
	elif var.state == 'menu':
		if key == 'south' and var.menu_select<len(var.main_menu)-1:
			var.menu_select+=1
		elif key == 'north' and var.menu_select>0:
			var.menu_select-=1
		elif key == 'enter' and var.state=='menu':
			var.state = var.main_menu[var.menu_select]['action']
			game.setup()
def play_game(is_learning):
    game.setup(2)

    if not is_learning:
        racers = []
        for player in game.players:
            racers = [*racers, *player.racers]
        racers.sort()
        racers.reverse()
        game.draw_course(racers)

    rAll = 0
    d = False
    j = 0

    race_timer = 0
    is_race_over = False

    while j < 99:
        j += 1

        s, a1 = play_racer_RL(game.players[0].racers[1], is_learning)

        s2, a2 = play_racer_RL(game.players[0].racers[0], is_learning)

        play_racer_max(game.players[1].racers[0])
        play_racer_max(game.players[1].racers[1])

        if not is_learning:
            game.print_cards_for_racer(game.players[1].racers[1])
            game.print_cards_for_racer(game.players[1].racers[0])

        result = game.make_move()

        if not is_learning:
            racers = []
            for player in game.players:
                racers = [*racers, *player.racers]
            racers.sort()
            racers.reverse()
            game.draw_course(racers)
            if result:
                print(result.name, ' won,', j, 'turns taken')

        r = 0
        if result and (result == game.players[0].racers[0]
                       or result == game.players[0].racers[1]):
            r = 1
        elif result:
            r = -1

        s3 = game.current_observation()

        if is_learning:
            RL.store_transition(s, a1, r, s2)
            RL.store_transition(s2, a2, r, s3)
            if (counter > 500) and (counter % 10 == 0):
                RL.learn()

        rAll += r
        if result:
            break

    if rAll == 0:
        racers = []
        for player in game.players:
            racers = [*racers, *player.racers]
        racers.sort()
        racers.reverse()
        game.print_cards()
        game.draw_course(racers)
        print(j)
        print()
        pass

    rList.append(rAll)
Beispiel #7
0
import classes
import game
# Setup
game = game.Game()
print("Welcome to python YaBlewIt!")
num_players = input("Please enter the number of players for this game: ")
game.setup(int(num_players))
for player in game.players:
    print(f"Welcome {player.name}! Your curse color is: {player.curse_color}")
game.loaddeck()

# Game play
game.begin()

# Score
#game.score()
Beispiel #8
0
class examplegame(game.game):

	def onstart(self):
		print('*' * 16 + ' - STARTING - ' + '*' * 16)

	def onquit(self, message):
		message += ('Quit from example.py',)
		print message
		print('*' * 16 + ' - QUITING - ' + '*' * 16)

	def logic(self):
		pass

	def render(self):
		pass



class exampleframe(frame.frame):
	pass

if __name__ == '__main__':

	game = examplegame()

	frame = exampleframe(800, 600)

	game.add(frame)
	game.setup()
	game.setfps(60)
	game.run()
Beispiel #9
0
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)

server.listen()
print("Server is looking for connections...")

conn, addr = server.accept()
print("Connection from {}".format(addr))

pygame.init()
pygame.display.set_caption("Player 1")
playing = True
last_click = None  # Tuple of the board coordinates of the piece you have selected (if there is one)
moving = False  # True when you have selected your own piece and can therefore move
current_player = 1  # This is player 1
screen, board = game.setup()
is_player_turn = True  # Means this player can make a move


def receive_data():
    global board, is_player_turn, playing
    while True:
        try:
            board = pickle.loads(conn.recv(4096))
            helper.update_screen(screen, board, 2)
            is_player_turn = True
        # The client has terminated the game
        except error:
            playing = False

Beispiel #10
0
def series (home, away, homeplayers, awayplayers):
	folder = 'scorecards'
	if not os.path.exists(folder): os.makedirs(folder)
	fileList = os.listdir(folder)
	for fileName in fileList: os.remove("{}/{}".format(folder,fileName))

	n = 0
	while n > 1000 or n < 1:
		n = input('Enter number of games to be played: ')
		try: n = int(n)
		except: 
			print ('Invalid input')
			n = 0

	s = seri()

	for i in range (n):
		g = setup(home = home, away = away)
		g.year = 2020
		g.home.active = homeplayers
		g.away.active = awayplayers
		g.no = i+1
		s.games.append(g)

	s.home = g.home
	s.away = g.away

	c = 1
	a, b, d = 0, 0, 0
	for i in s.games:
		x = game(i)
		s.results.append(x)
		s.inns = s.inns + x.inns
		s.bowls = s.bowls + x.bowls
		i.no = c
		c += 1

		if i.win in ['Draw', 'Tie']: d += 1
		elif i.win.name == s.home.name: a+= 1
		elif i.win.name == s.away.name: b += 1

		print ('',s.home.name, a, s.away.name, b, 'Draw', d)
		print ()
	
	for i in s.results: i.gamedesc()

	print ()

	print ('',s.home.name, a, s.away.name, b, 'Draw', d)
	print ()

	s.players.sort(key = lambda x: sum([y.runs for y in s.inns if y.player == x]), reverse = True)
	for i in s.players:
		r = sum([y.runs for y in s.inns if y.player == i])
		o = sum([y.out for y in s.inns if y.player == i])
		if o == 0: a = r
		else: a = round(r/o,2)
		if (a >= 50 and o > 1) or r >= 100*len(s.results) or i in s.players[:3]:
			print ('{} {} runs @ {}'.format(i.name, r, a), end = ', ')
	print()
	s.players.sort(key = lambda x: sum([y.wickets for y in s.bowls if y.player == x]), reverse = True)
	for i in s.players:
		r = sum([y.runs for y in s.bowls if y.player == i])
		o = sum([y.wickets for y in s.bowls if y.player == i])
		if o == 0: a = r
		else: a = round(r/o,2)
		if (a <= 25 and o > 5) or o >= (4*len(s.results)) or i in s.players[:3]:
			print (i.name, o, "wickets @", a, end = ', ')
	s.players.sort(key = lambda x: sum(20*[y.wickets for y in s.bowls if y.player == x]) +sum([y.runs for y in s.inns if y.player == x]) + 100*len([y for y in s.results if y.win == x.team]), reverse = True)
	print ()
	print ('Man of the series: {} ({})'.format(s.players[0].name, s.players[0].team))
	print ()

	statsdump (s.players, s.inns, s.bowls, folder = folder)
def league (t, n):
	folder = 'scorecards'
	if not os.path.exists(folder): os.makedirs(folder)
	fileList = os.listdir(folder)
	for fileName in fileList: os.remove("{}/{}".format(folder,fileName))

	log = []
	for i in t:
		log.append([i.name, 0, 0, 0])

	s = seri()
	for i in t:
		s.players = s.players + i.active

	c = 1
	for y in range (n):
		for i in t:
			for j in t:
				if j == i: continue
				print ()
				print ()
				#print (i.name, 'vs.', j.name)
				print ()
				g = setup (i.name, j.name)
				g.home.active = i.active
				g.away.active = j.active
				g.no = c
				x = game (g)
				s.results.append(x)
				s.inns = s.inns + x.inns
				s.bowls = s.bowls + x.bowls
				c += 1

				for k in log:
					if k[0] in [g.home.name, g.away.name]: 
						k[2] +=1
						if g.win in ['Draw','Tie']: 
							k[3] += 1
							continue
						if g.win.name == k[0]: k[1] += 1
				log.sort(key = lambda x: x[1] + x[3]/1000, reverse = True)
				for k in log:
					print ('{} {} games {} wins {} draws'.format(k[0].ljust(20), str(k[2]).rjust(3), str(k[1]).rjust(3), str(k[3]).rjust(3)))

	print ()
	print ()
	print ()
	for i in s.results: i.gamedesc()

	print ()
	for k in log:
		print ('{} {} games {} wins {} draws'.format(k[0].ljust(20), k[2], str(k[1]).rjust(3), str(k[3]).rjust(3)))	
	print ()
	s.players.sort(key = lambda x: x.runs, reverse = True)
	for i in s.players:
		if i.batav > 50 or i in s.players[:10]:
			print ('{} ({}) {} runs @ {}'.format(i.name, i.team, i.runs, i.batav), end = ', ')
	print()
	print ()
	s.players.sort(key = lambda x: x.wickets, reverse = True)
	for i in s.players:
		if (i.bowlav < 25 and i.wickets > 10) or i in s.players[:10]:
			print ('{} ({}) {} wickets @ {}'.format(i.name, i.team, i.wickets, i.bowlav), end = ', ')

	s.players.sort(key = lambda x: 20*x.wickets + x.runs + 100*[a[1] for a in log if a[0] == x.team][0], reverse = True)
	print ()
	print ()
	print ('Man of the series: {} ({})'.format(s.players[0].name, s.players[0].team))
	print ()

	statsdump (s.players, s.inns, s.bowls, folder)
Beispiel #12
0
 def medium():
     game.level = 'Medium'
     game.setup(16, 16, 40)
Beispiel #13
0
 def medium():
     game.level = 'Medium'
     game.start_time = time.time()
     game.setup(16, 16, 40)
Beispiel #14
0
import game

print('Random selection, 2 player game')

game.setup(2)
counter = 0
race_timer = 0
race_over = False
while not race_over:
    if counter == 5:
        counter = 0
        game.print_cards()

    for player in game.players:
        for racer in player.racers:
            racer.draw_hand()
            racer.select_move(0)

    race_over = game.make_move()
    print('Next Round')
    counter += 1
    race_timer += 1
    # sleep(2)

game.print_cards()
print('Finished in', race_timer, 'turns')
Beispiel #15
0
# Sets terminal title, size, font and font size
terminal.set("window: title='pyMastermind', size=24x18")
terminal.set("font: font/DejaVuSansMono.ttf, size=11")

# Shows the rules and welcome screen
for x in range(4):
    screen.show_welcome_screen(x)
    while terminal.read() != terminal.TK_ENTER:
        pass

# Calls for the terminal screen to be setup
screen.setup()

# Setups the game variables (i.e the secret code)
game.setup()

# Constantly listens for input and refreshes the screen 30 times a second
while not game.GAME_OVER:
    if terminal.has_input():
        input.handle(terminal.read())

    terminal.refresh()
    time.sleep(1 / 30)

# Checks if the player has won or not
if game.GAME_WON:
    # Waits 2 seconds and then shows win message
    time.sleep(2)
    screen.show_win_screen()
    terminal.refresh()
Beispiel #16
0
 def easy():
     game.level = 'Easy'
     game.setup(4, 4, 2)
Beispiel #17
0
 def hard():
     game.level = 'Hard'
     game.setup(16, 30, 90)
Beispiel #18
0
def start_game():
    game_board, current_player, next_player = game.setup()
    game.loop(game_board, current_player, next_player)
Beispiel #19
0
 def run_game(self, game_data):
     game_node = self.render.attachNewNode("Game Node")
     game_session = game.setup(game_node, game_data)
Beispiel #20
0
 def hard():
     game.level = 'Hard'
     game.start_time = time.time()
     game.setup(16, 30, 90)
Beispiel #21
0
on_right = 1
next_move = 1
qr_observation_count = (initial_cards + max_recycle_deck + cards_in_hand + position + on_right + next_move) * 2

# Init tables with all zeros
Q = np.zeros([qr_observation_count, 4])

lr = 0.8
y = 0.95
num_episodes = 2000

# reward list
rList = []

for i in range(num_episodes):
    game.setup(1)

    rAll = 0
    d = False
    j = 0

    race_timer = 0
    is_race_over = False

    game.players[0].racers[1].draw_hand()

    while j < 99:
        j += 1
        # choose an action by greedily (with noise) picking from Q table
        s = game.current_observation()
        a1 = np.argmax(Q[s, :] + np.random.randn(1, 4) * (1. / (i + 1)))
Beispiel #22
0
 def easy():
     game.level = 'Easy'
     game.start_time = time.time()
     game.setup(9, 9, 10)
Beispiel #23
0
 def run_game(self, game_data):
     game_node = self.render.attachNewNode("Game Node")
     game_session = game.setup(game_node, game_data)