Example #1
0
class Game(object):

    def __init__(self, first='W'):
        super(Game, self).__init__()
        self.white, self.black = Player('W'), Player('B')
        self.players = [self.white, self.black] if first == 'W' else [self.black, self.white]
        self.plansza = Plansza()

    def is_finished(self):
        if sum(self.plansza.count_pawnsWB()) == 64:
            return True
        elif self.players[0].blocked and self.players[1].blocked:
            return True
        else:
            return False

    def get_result(self):
        whites, blacks = self.plansza.count_pawnsWB()
        if whites > blacks:
            return "White won: %d to %d" % (whites, blacks)
        elif blacks > whites:
            return "Blacks won: %d to %d" % (blacks, whites)
        else:
            return "Draw %d to %d" % (blacks, whites)

    def play(self):
#        print self.plansza
        for player in itertools.cycle(self.players):
            self.plansza = player.think_and_move(self.plansza)           
#            if player.blocked:
#                print "\n%s can't move" % player.color
#            else:
#                print '\n' + str(self.plansza)
            if self.is_finished():
                break
Example #2
0
class PlanszaTest(TestCase):
    def __init__(self, *args, **kwargs):
        """
        Tworzymy klasę PlanszaTest, która będzie wykorzystywana do przeprowadzenie testów jednostkowych
        :param args:
        :param kwargs:
        """
        super().__init__(*args, **kwargs)
        g = Saper()
        w = Okno(g)
        g.ustawWidok(w)
        wg = w.get_okno()
        g.setTestValues(8, 8, 12) #zgodnie z założeniami, rozmiar planszy to 8x8 a min jest 12

        self.winGame = mapaGry(wg, g)
        self.mapOfButtons = self.winGame.get_mapOfButtons(8, 8)
        self.board = Plansza(self.mapOfButtons)

    def testMarkedBombsIncreasedAfterSign(self):
        """
        Test sprawdza, czy po oznaczeniu pola na planszy, wzrasta licznik do liczenia oznaczonych pól
        """
        howMuchBombsIsMarkedBefore = self.board._howMuchBombsIsMarked
        self.board.flag(1, 1)
        howMuchBombsIsMarkedAfter = self.board._howMuchBombsIsMarked

        self.assertEqual(howMuchBombsIsMarkedBefore + 1, howMuchBombsIsMarkedAfter)
Example #3
0
def test_is_player_blocked_W_True():
    player = Player('W')
    plansza = Plansza()
    tekst = """ * * * * W * * *
                * * * * W * * *
                * * * * W * * *
                * * * * B * * *
                * * * B B * * *
                * * * B B * * *
                * * * * B * * *
                * * * * B * * * """
    plansza.load(tekst)
    player.think_and_move(plansza)
    assert player.blocked
Example #4
0
def test_make_move_B_5_3():
    player = Player('B')
    plansza = Plansza()
    expected = Plansza()
    tekst = """ * * * * * * * *
                * * * * * * * *
                * * * * * * * *
                * * * B W * * *
                * * * B B * * *
                * * * B * * * *
                * * * * * * * *
                * * * * * * * * """
    expected.load(tekst)
    assert player.make_move(5, 3, plansza) == expected
Example #5
0
def test_make_move_W_4_5():
    player = Player('W')
    plansza = Plansza()
    expected = Plansza()
    tekst = """ * * * * * * * *
                * * * * * * * *
                * * * * * * * *
                * * * B W * * *
                * * * W W W * *
                * * * * * * * *
                * * * * * * * *
                * * * * * * * * """
    expected.load(tekst)
    assert player.make_move(4, 5, plansza) == expected
Example #6
0
def test_make_move_W_2_3():
    player = Player('W')
    plansza = Plansza()
    expected = Plansza()
    tekst = """ * * * * * * * *
                * * * * * * * *
                * * * W * * * *
                * * * W W * * *
                * * * W B * * *
                * * * * * * * *
                * * * * * * * *
                * * * * * * * * """
    expected.load(tekst)
    assert player.make_move(2, 3, plansza) == expected
Example #7
0
def test_clean_board():
	tekst = '''	* * * * * * * *
				* * * * * * * *
				* * * * * * * *
				* * * B W * * *
				* * * W B * * *
				* * * * * * * *
				* * * * * * * *
				* * * * * * * * '''	
	
	clean = Plansza()
	clean.load(tekst)
	nowa = Plansza()
	assert nowa==clean, 'powinny być takie same'
Example #8
0
def test_is_finished_not_full():
    tekst = ''' B B B B B B B B
                B B B B B B B B
                B B B * B B B B
                B B B B W B B B
                W W W W B W W W
                W W W W W W W W
                W W W W W W W W
                W W W W W W W W ''' 
    game = Game()
    p1 = Plansza()
    p1.load(tekst)
    game.plansza = p1
    assert game.is_finished() == False
Example #9
0
def test_load_from_string():

		tekst = '''	* * * * * * * *
					* * * * * * * *
					* * * * W B * *
					* * * B B B * *
					* * * B W * * *
					* * * B W * * *
					* * * * * * * *
					* * * * * * * * '''

		plansza = Plansza()
		plansza.load(tekst)
		assert plansza[1,1]=='*', 'powinna być gwiazdka'
		assert plansza[3,3]=='B', 'powinno być B'
Example #10
0
    def __init__(self, *args, **kwargs):
        """
        Tworzymy klasę PlanszaTest, która będzie wykorzystywana do przeprowadzenie testów jednostkowych
        :param args:
        :param kwargs:
        """
        super().__init__(*args, **kwargs)
        g = Saper()
        w = Okno(g)
        g.ustawWidok(w)
        wg = w.get_okno()
        g.setTestValues(8, 8, 12) #zgodnie z założeniami, rozmiar planszy to 8x8 a min jest 12

        self.winGame = mapaGry(wg, g)
        self.mapOfButtons = self.winGame.get_mapOfButtons(8, 8)
        self.board = Plansza(self.mapOfButtons)
Example #11
0
def test_make_move_B_5_4():
    player = Player('B')

    plansza = Plansza()
    expected = Plansza()

    start = """ * * * * B * * *
                W * * * W * * *
                * B * * W * * B
                * * B B W * W *
                * * * B W W * *
                B W W W * W W B
                * * * W W W * *
                * * B * B * B * """

    end =   """ * * * * B * * *
                W * * * B * * *
                * B * * B * * B
                * * B B B * B *
                * * * B B B * *
                B B B B B B B B
                * * * B B B * *
                * * B * B * B * """
    plansza.load(start)
    expected.load(end)
    assert player.make_move(5, 4, plansza) == expected
Example #12
0
def play_game(n):
	wyniki = collections.defaultdict(int)
	for _ in xrange(n):
		cnt = 0
		last_moved = True
		plansza = Plansza()

		for color in itertools.cycle(['B', 'W']):
			any_moves = plansza.find_moves(color)
			if not any_moves:	
				print "%s can't make any move\n" % color
				if not last_moved:	# oboje gracze zablokowani
					break
			else:	# czyli są możliwe ruchy
				cnt += 1
				if color == 'B':
					plansza.make_random_move(color)
				elif color == 'W':
					plansza.pick_best_move(color)

			
			print plansza
			print 

			if not plansza._plansza.count('*'):		# cała plansza wypełniona
				break

			last_moved = any_moves

			# s=raw_input('?')
		# plansza.print_score()

		result = plansza.get_result().split()[0]
		print cnt
		wyniki[result] += 1

	print wyniki.items()
Example #13
0
 def __init__(self, first='W'):
     super(Game, self).__init__()
     self.white, self.black = Player('W'), Player('B')
     self.players = [self.white, self.black] if first == 'W' else [self.black, self.white]
     self.plansza = Plansza()
Example #14
0
def test_count_pawnsBW():
	tekst = '''	B B B * * * * *
				* * * * * * * *
				* * * * * * * *
				* * * B W * * *
				* * * W B * * *
				* * * * * * * *
				* * * * * * * *
				* * * * * W W W '''	
	plansza = Plansza()
	plansza.load(tekst)
	w, b = plansza.count_pawnsWB()
	assert w==5 and b==5


# class PlanszaTest(unittest.TestCase):

	# def test_znalezione_ruchy(self):
	# 	plansza = Plansza()
	# 	plansza.find_moves('B')
	# 	expected=set([(3, 5), (2, 4), (4, 2), (5, 3)])
	# 	actual = set(plansza.available_moves)
	# 	self.assertEqual(actual, expected)

	# def test_wykonal_ruch(self):
	# 	plansza = Plansza()
	# 	plansza.make_move(3, 5, 'B')

	# 	self.assertEqual(plansza[3,5], 'B')
	# 	self.assertEqual(plansza[3,4], 'B')

	# def test_wykonal_ruch2(self):
	# 	plansza = Plansza()
	# 	plansza.make_move(5, 4, 'W')

	# 	self.assertEqual(plansza[5,4], 'W')
	# 	self.assertEqual(plansza[4,4], 'W')

	# def test_wykonal_ruch3(self):
		
	# 	start = '''		* * * * * W * *
	# 					* * * * * B * *
	# 					* * * * W B * *
	# 					* * * B B B * *
	# 					* * * B W * W B
	# 					* * * B W B W *
	# 					* * * * * W * B
	# 					* * * * * * * * '''
		
	# 	expected = '''	* * * * * W * *
	# 					* * * * * B * *
	# 					* * * * W B * *
	# 					* * * B B B * *
	# 					* * * B B B B B
	# 					* * * B W B B *
	# 					* * * * * W * B
	# 					* * * * * * * *'''

		# plansza = Plansza()
		# plansza.load(start)
		# plansza.make_move(4,5,'B')

		# end=Plansza()
		# end.load(expected)

		# self.assertEqual(plansza, end)

	


#if __name__ == '__main__':
#	unittest.main()


		
		
		
		
Example #15
0
def test_count_pawnsBW_clean():
	plansza = Plansza()
	whites, blacks = plansza.count_pawnsWB()
	assert blacks==2 and whites==2, 'liczba pionów się nie zgadza'
Example #16
0
def main():
    # Uruchomienie programu
    pygame.init()
    ##
    # Ekran gry
    SZEROKOSC = 1400
    WYSOKOSC = 800
    SZEROKOSC_PLANSZY = 800
    WYSOKOSC_PLANSZY = 800
    CZARNE_POLE = (150, 80, 40)
    KOLOR_TLA = (255, 229, 180)
    EKRAN = pygame.display.set_mode((SZEROKOSC, WYSOKOSC))
    PLANSZOWKA = Plansza(WYSOKOSC_PLANSZY, SZEROKOSC_PLANSZY, EKRAN)

    BIALY = pygame.image.load("assets/bialy_pionek.png")
    CZARNY = pygame.image.load("assets/czarny_pionek.png")

    #Tytul i ikona
    pygame.display.set_caption("Warcaby Polskie by Maciej Walczyk")
    IKONA = pygame.image.load("assets/ikona.png")
    pygame.display.set_icon(IKONA)
    ODSWIEZ = 0
    GRACZ_BIALY = 1
    GRACZ_CZARNY = 2
    GRACZ_OBECNY = GRACZ_BIALY
    KOLOR = (155, 105, 10)
    WLACZONY = 1
    EKRAN.fill(KOLOR_TLA)
    PLANSZOWKA.rysuj_poczatek()
    prawda = True
    # TESTY
    #PLANSZOWKA.dodaj_czarny_pionek(Pionek(3, 4, 'B', EKRAN))
    #PLANSZOWKA.rysuj_poczatek()
    #PLANSZOWKA.przesuwaj(PLANSZOWKA.pola_bialych[2])
    #PLANSZOWKA.bicie_pionkiem(PLANSZOWKA.pola_bialych[0][1])
    #WSPOLRZEDNE_PIONKOW
    NUMER = 0

    # WYPISYWANIE WIADOMOSCI NA EKRAN
    font = pygame.font.SysFont(None, 25)

    def wiadomosc(tresc, kolor, liczba):
        wspol_x = 810
        wspol_y = 40 + (30 * liczba)
        tekst = font.render(tresc, True, kolor)
        EKRAN.blit(tekst, [wspol_x, wspol_y])
        if liczba <= 10:
            liczba += 1
        else:
            liczba = 0

    def wiadomosc2(tresc, kolor):
        wspol_x = 1060
        wspol_y = 40
        tekst = font.render(tresc, True, KOLOR)
        EKRAN.blit(tekst, [wspol_x, wspol_y])

    wiadomosc("Hej! Życzę miłej gry! ", KOLOR, -1)
    if GRACZ_OBECNY == 1:
        wiadomosc("Teraz kolej gracza Bialych.", KOLOR, 0)
    else:
        wiadomosc2("Teraz kolej gracza Czarnych.", KOLOR)

    # GLOWNA PETLA W KTOREJ TOCZY SIE GRA

    while WLACZONY:
        wybrany_pionek = None
        #PLANSZOWKA.rysuj_poczatek()
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                WLACZONY = 0
            elif event.type == pygame.MOUSEBUTTONDOWN:
                myszka = pygame.mouse.get_pos()
                wspolrzedna_x = myszka[0]
                wspolrzedna_y = myszka[1]
                wsp_x = wspolrzedna_x // (SZEROKOSC_PLANSZY //
                                          PLANSZOWKA.KOLUMNY)
                wsp_y = wspolrzedna_y // (WYSOKOSC_PLANSZY //
                                          PLANSZOWKA.WIERSZE)

                if PLANSZOWKA.czy_mozna_ruszyc(GRACZ_OBECNY, wsp_x,
                                               wsp_y) == True:
                    if GRACZ_OBECNY == 1:
                        for pioneczek in PLANSZOWKA.pola_bialych:
                            if pioneczek.wsp_x == wsp_x and pioneczek.wsp_y == wsp_y:
                                wybrany_pionek = pioneczek
                                PLANSZOWKA.podswietlaj(wybrany_pionek)
                                prawda = True
                    if GRACZ_OBECNY == 2:
                        for pioneczek in PLANSZOWKA.pola_czarnych:
                            if pioneczek.wsp_x == wsp_x and pioneczek.wsp_y == wsp_y:
                                wybrany_pionek = pioneczek
                                PLANSZOWKA.podswietlaj(wybrany_pionek)
                                prawda = True
                    while prawda:
                        event = pygame.event.wait()
                        if event.type == pygame.QUIT:
                            WLACZONY = 0
                        elif event.type == pygame.MOUSEBUTTONDOWN:
                            PLANSZOWKA.podswietlaj(wybrany_pionek)
                            pole_ruchu = pygame.mouse.get_pos()
                            pole_x = pole_ruchu[0] // (SZEROKOSC_PLANSZY //
                                                       PLANSZOWKA.KOLUMNY)
                            pole_y = pole_ruchu[1] // (SZEROKOSC_PLANSZY //
                                                       PLANSZOWKA.KOLUMNY)

                            if PLANSZOWKA.czy_mozna_bic(
                                    wybrany_pionek) == True:
                                temp = wybrany_pionek.wsp_x
                                PLANSZOWKA.bicie_pionkiem(
                                    GRACZ_OBECNY, wybrany_pionek, pole_x,
                                    pole_y)
                                EKRAN.fill(KOLOR_TLA)
                                PLANSZOWKA.rysuj_poczatek()
                                PLANSZOWKA.czy_krolowa(GRACZ_OBECNY,
                                                       wybrany_pionek)
                                prawda = False
                                if PLANSZOWKA.czy_mozna_bic(
                                        wybrany_pionek) == True:
                                    wiadomosc(
                                        "Wciąż masz jeszcze bicie! Tak, tak, zrób to!",
                                        KOLOR, 3)

                                if temp != wybrany_pionek.wsp_x:
                                    if PLANSZOWKA.czy_mozna_bic(
                                            wybrany_pionek) == False:
                                        if GRACZ_OBECNY == GRACZ_BIALY:
                                            if PLANSZOWKA.pola_czarnych == []:
                                                wiadomosc(
                                                    "Koniec gry, wygrały Białe! Gratuluję :)",
                                                    KOLOR, 4)
                                                pygame.display.update()
                                                time.sleep(6)
                                                WLACZONY = 0
                                            else:
                                                GRACZ_OBECNY = GRACZ_CZARNY
                                                EKRAN.fill(KOLOR_TLA)
                                                PLANSZOWKA.rysuj_poczatek()
                                                wiadomosc2(
                                                    "Teraz kolej gracza Czarnych.",
                                                    KOLOR)
                                                pygame.display.update()
                                                prawda = False
                                        else:
                                            if PLANSZOWKA.pola_bialych == []:
                                                EKRAN.fill(KOLOR_TLA)
                                                PLANSZOWKA.rysuj_poczatek()
                                                wiadomosc(
                                                    "Koniec gry, wygrały Czarne! Gratuluję :)",
                                                    KOLOR, 5)
                                                pygame.display.update()
                                                time.sleep(6)
                                                WLACZONY = 0
                                            else:
                                                GRACZ_OBECNY = GRACZ_BIALY
                                                EKRAN.fill(KOLOR_TLA)
                                                PLANSZOWKA.rysuj_poczatek()
                                                wiadomosc(
                                                    "Teraz kolej gracza Bialych.",
                                                    KOLOR, 0)
                                                pygame.display.update()
                                                prawda = False
                                else:
                                    EKRAN.fill(KOLOR_TLA)
                                    PLANSZOWKA.rysuj_poczatek()
                                    wiadomosc(
                                        "Masz bicie, zrób je! Na komputerze "
                                        "przecież wolno bić innych!", KOLOR, 6)
                                    pygame.display.update()

                            elif PLANSZOWKA.czy_mozna_postawic(
                                    wybrany_pionek, pole_x, pole_y):
                                pomocnicza = wybrany_pionek.wsp_x
                                PLANSZOWKA.rusz_pionkiem(
                                    wybrany_pionek, pole_x, pole_y)
                                PLANSZOWKA.czy_krolowa(GRACZ_OBECNY,
                                                       wybrany_pionek)
                                prawda = False
                                if pomocnicza != wybrany_pionek.wsp_x:
                                    if GRACZ_OBECNY == GRACZ_BIALY:
                                        if PLANSZOWKA.pola_czarnych == []:
                                            EKRAN.fill(KOLOR_TLA)
                                            PLANSZOWKA.rysuj_poczatek()
                                            wiadomosc(
                                                "Koniec gry, wygrały Białe! Gratuluję :)",
                                                KOLOR, 7)
                                            pygame.display.update()
                                            time.sleep(6)
                                            WLACZONY = 0
                                        else:
                                            EKRAN.fill(KOLOR_TLA)
                                            PLANSZOWKA.rysuj_poczatek()
                                            wiadomosc2(
                                                "Teraz kolej gracza Czarnych.",
                                                KOLOR)
                                            pygame.display.update()
                                            GRACZ_OBECNY = GRACZ_CZARNY
                                            prawda = False
                                    else:
                                        if PLANSZOWKA.pola_bialych == []:
                                            wiadomosc(
                                                "Koniec gry, wygrały Czarne! Gratuluję :)",
                                                KOLOR, 7)
                                            pygame.display.update()
                                            time.sleep(6)
                                            WLACZONY = 0
                                        else:
                                            GRACZ_OBECNY = GRACZ_BIALY
                                            EKRAN.fill(KOLOR_TLA)
                                            PLANSZOWKA.rysuj_poczatek()
                                            wiadomosc(
                                                "Teraz kolej gracza Bialych.",
                                                KOLOR, 0)
                                            pygame.display.update()
                                            prawda = False
                            elif PLANSZOWKA.czy_mozna_postawic(
                                    wybrany_pionek, pole_x, pole_y) == False:
                                EKRAN.fill(KOLOR_TLA)
                                PLANSZOWKA.rysuj_poczatek()
                                wiadomosc(
                                    "Wykonałeś nie do końca poprawny ruch, "
                                    "no ale dam Ci szansę ;) ", KOLOR, 8)
                                pygame.display.update()
                                prawda = False
                PLANSZOWKA.rysuj_poczatek()

        pygame.display.update()