Created
December 6, 2018 15:36
-
-
Save mackwic/fdef850645f92f40c09cf7bcad8e5285 to your computer and use it in GitHub Desktop.
Groupe 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
YELLOW = 1 | |
RED =2 | |
EMPTY =0 | |
COL = 7 | |
ROW = 6 | |
class Board: | |
def __init__(self): | |
self.state = [] | |
for i in range(COL): | |
self.state.append([]) | |
def get_state(self): | |
return self.state | |
def add_chip(self,col,color): | |
self.state[col].append(color) | |
def convert_state_to_string(self): | |
for i in range(COL): | |
for j in range(ROW): | |
self.state[j][i] = str(self.state[j][i]) | |
class Arbitre: | |
def __init__(self, board = None): | |
self.current_player = YELLOW | |
self.board = board | |
def play_and_change_players(self, column_index): | |
self.board.add_chip(column_index, self.current_player) | |
if self.current_player == YELLOW: | |
self.current_player = RED | |
else: | |
self.current_player = YELLOW | |
def get_player(self): | |
return self.current_player | |
def game_over(self): | |
return Analyseur.analyse(self.board) | |
class Application: | |
@staticmethod | |
def board_to_string(board): | |
state_as_list = board.get_state() | |
full_board = [] | |
for col in state_as_list: | |
col += ["." for i in range(ROW - len(col))] | |
print(state_as_list) | |
return state_as_list | |
class Analyseur: | |
@staticmethod | |
def analyse_vertical(board): | |
state = board.get_state() | |
for col in range(COL): | |
current_col = state[col] | |
if len(current_col) >= 4: | |
row = 0 | |
nb_same_color = 1 | |
current_col_len = len(current_col) | |
while row < current_col_len - 1: | |
current_chip = state[col][row] | |
next_chip = state[col][row + 1] | |
if current_chip != next_chip: | |
nb_same_color = 1 | |
if current_chip == next_chip: | |
nb_same_color += 1 | |
row += 1 | |
if nb_same_color >= 4: | |
return True | |
return False | |
@staticmethod | |
def analyse(board): | |
return Analyseur.analyse_vertical(board) | |
# Lignes | |
#for row in range(ROW): | |
# current_row = [state[col][i] for i in range(COL)] | |
def run(): | |
print("running interactive") | |
if __name__ == '__main__': | |
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from puissance4 import * | |
class TestBoard(unittest.TestCase): | |
def test_get_initial_state(self): | |
# given | |
board = Board() | |
# when | |
state = board.get_state() | |
# then | |
self.assertEqual(state, [[], [], [], [], [], [], []]) | |
def test_get_state(self): | |
# given | |
board = Board() | |
col = 0 | |
color = YELLOW | |
#action | |
board.add_chip(col, color) | |
state = board.get_state() | |
#assertion | |
self.assertAlmostEqual(state[0][0], YELLOW) | |
# def test_board_to_string(self): | |
# # given | |
# board = Board() | |
# #when | |
# for i in range(COL): | |
# board.add_chip(i, RED) | |
# board.convert_state_to_string1() | |
# state = board.get_state() | |
#then | |
#for i in | |
# self.assertIsInstance() | |
class TestApplication(unittest.TestCase): | |
@unittest.skip("prochaine fois merci") | |
def test_display_empty_board(self): | |
# given | |
board = Board() | |
expected_board_str = ". . . . . . . \n " \ | |
". . . . . . . \n " \ | |
". . . . . . . \n" \ | |
" . . . . . . . \n" \ | |
" . . . . . . . \n" \ | |
" . . . . . . . \n" | |
# when | |
actual_board_str = Application.board_to_string(board) | |
# then | |
self.assertEqual(actual_board_str,expected_board_str) | |
@unittest.skip("Osef") | |
def test_display_with_1_token(self): | |
# given | |
board = Board() | |
expected_board_str = ". . . . . . . \n " \ | |
". . . . . . . \n " \ | |
". . . . . . . \n" \ | |
" . . . . . . . \n" \ | |
" . . . . . . . \n" \ | |
" . 1 . . . . . \n" | |
# when | |
actual_board_str = Application.board_to_string(board) | |
# then | |
self.assertEqual(actual_board_str,expected_board_str) | |
class TestArbitre(unittest.TestCase): | |
def test_display_initial_player(self): | |
# given` | |
board = Board() | |
expected_player = YELLOW | |
# when | |
arbitre = Arbitre(board) | |
actual_player = arbitre.get_player() | |
# then | |
self.assertEqual(expected_player, actual_player) | |
def test_display_second_player(self): | |
# given | |
board = Board() | |
expected_player = RED | |
# when | |
arbitre = Arbitre(board) | |
arbitre.play_and_change_players(2) | |
actual_player = arbitre.get_player() | |
# then | |
self.assertEqual(expected_player, actual_player) | |
def test_display_third_player(self): | |
# given | |
board = Board() | |
expected_player = YELLOW | |
# when | |
arbitre = Arbitre(board) | |
arbitre.play_and_change_players(4) | |
arbitre.play_and_change_players(5) | |
actual_player = arbitre.get_player() | |
# then | |
self.assertEqual(expected_player, actual_player) | |
def test_if_arbitre_plays_for_a_the_right_player(self): | |
# given | |
board = Board() | |
expected_board = [[YELLOW], [], [], [], [], [], []] | |
# when | |
arbitre = Arbitre(board) | |
arbitre.play_and_change_players(0) | |
actual_board = board.get_state() | |
# then | |
self.assertEqual(expected_board, actual_board) | |
def test_check_if_game_is_on(self): | |
# given | |
board = Board() | |
expected_game_end= False | |
# when | |
arbitre = Arbitre(board) | |
arbitre.play_and_change_players(0) | |
arbitre.play_and_change_players(1) | |
arbitre.play_and_change_players(0) | |
arbitre.play_and_change_players(1) | |
arbitre.play_and_change_players(0) | |
arbitre.play_and_change_players(1) | |
actual_game_end = arbitre.game_over() | |
# then | |
self.assertEqual(expected_game_end, actual_game_end) | |
def test_check_if_game_is_over(self): | |
# given | |
board = Board() | |
expected_game_end= True | |
# when | |
arbitre = Arbitre(board) | |
arbitre.play_and_change_players(0) | |
arbitre.play_and_change_players(1) | |
arbitre.play_and_change_players(0) | |
arbitre.play_and_change_players(1) | |
arbitre.play_and_change_players(0) | |
arbitre.play_and_change_players(1) | |
arbitre.play_and_change_players(0) | |
actual_game_end = arbitre.game_over() | |
# then | |
self.assertEqual(expected_game_end, actual_game_end) | |
class TestAnalyseur(unittest.TestCase): | |
# def test_partie_en_cours_grille_vide(self): | |
# # given | |
# grille = Board() | |
# # when | |
# partie_terminee = Analyseur.analyse(grille) | |
# #then | |
# self.assertFalse(partie_terminee) | |
def test_victoire_verticale_4_pions(self): | |
# given | |
grille = Board() | |
for i in range(4): | |
grille.add_chip(0, YELLOW) | |
# when | |
partie_terminee = Analyseur.analyse(grille) | |
# then | |
self.assertTrue(partie_terminee) | |
def test_victoire_verticale_4_pions_colonne_3(self): | |
# given | |
grille = Board() | |
for i in range(4): | |
grille.add_chip(3, YELLOW) | |
# when | |
partie_terminee = Analyseur.analyse(grille) | |
# then | |
self.assertTrue(partie_terminee) | |
def test_partie_en_cours_4_pions_differents_verticaux(self): | |
# given | |
grille = Board() | |
for i in range(3): | |
grille.add_chip(3, YELLOW) | |
grille.add_chip(3, RED) | |
# when | |
partie_terminee = Analyseur.analyse(grille) | |
# then | |
self.assertFalse(partie_terminee) | |
def test_partie_en_cours_2_meme_pions_4_differents_verticaux(self): | |
# given | |
grille = Board() | |
for i in range(2): | |
grille.add_chip(4, YELLOW) | |
for i in range(4): | |
grille.add_chip(4, RED) | |
# when | |
partie_terminee = Analyseur.analyse(grille) | |
# then | |
self.assertTrue(partie_terminee) | |
def test_partie_en_cours_2_meme_pions_3_differents_verticaux(self): | |
# given | |
grille = Board() | |
for i in range(2): | |
grille.add_chip(4, YELLOW) | |
for i in range(3): | |
grille.add_chip(4, RED) | |
# when | |
partie_terminee = Analyseur.analyse(grille) | |
# then | |
self.assertFalse(partie_terminee) | |
# def test_victoire_horizontale_4_pions(self): | |
# # given | |
# grille = Board() | |
# grille.add_chip(0, YELLOW) | |
# grille.add_chip(1, YELLOW) | |
# grille.add_chip(2, YELLOW) | |
# grille.add_chip(3, YELLOW) | |
# # when | |
# partie_terminee = Analyseur.analyse(grille) | |
# # then | |
# self.assertTrue(partie_terminee) | |
""" | |
def test_victoire_horizontale_4_pions(self): | |
# given | |
grille = Board() | |
for i in range(4): | |
grille.add_chip(i, YELLOW) | |
# when | |
partie_terminee = Analyseur.analyse(grille) | |
# then | |
self.assertTrue(partie_terminee) | |
""" | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment