Created
December 6, 2018 15:35
-
-
Save mackwic/c1a59b6042b3b477619492f088937721 to your computer and use it in GitHub Desktop.
Groupe 1
This file contains hidden or 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]) | |
def display(self): | |
return """. . . . . . . | |
. . . . . . . | |
. . . . . . . | |
. . . . . . . | |
. . . . . . . | |
. . . . . . . """ | |
class Analyseur: | |
@staticmethod | |
def analyse(grille): | |
state = grille.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 | |
else: | |
return False | |
# Lignes | |
#for row in range(ROW): | |
# current_row = [state[col][i] for i in range(COL)] | |
return False | |
class Arbitre: | |
def __init__(self, board): | |
self.current_player = YELLOW | |
self.count_turns = 0 | |
self.board = board | |
self.analyseur = Analyseur() | |
def get_current_player(self): | |
if self.count_turns % 2 == 0: | |
self.current_player = YELLOW | |
if self.count_turns % 2 == 1: | |
self.current_player = RED | |
return self.current_player | |
def play_turn(self, COL): | |
self.board.add_chip(COL, self.get_current_player()) | |
self.count_turns += 1 | |
def declare_score(self): | |
win = self.analyseur.analyse(self.board) | |
if win: | |
return "WINNER !", YELLOW | |
return 'In Progress' | |
def run(): | |
print("running interactive") | |
if __name__ == '__main__': | |
run() |
This file contains hidden or 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 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) | |
class TestApplication(unittest.TestCase): | |
def test_display_string_board(self): | |
# given | |
board = Board() | |
expected = """. . . . . . . | |
. . . . . . . | |
. . . . . . . | |
. . . . . . . | |
. . . . . . . | |
. . . . . . . """ | |
# when | |
actual = board.display() | |
# then | |
self.assertEqual(expected, actual) | |
class TestArbitre(unittest.TestCase): | |
def test_first_player_is_yellow(self): | |
# given | |
expected = YELLOW | |
board = Board() | |
arbitre = Arbitre(board) | |
# when | |
actual = arbitre.get_current_player() | |
# then | |
self.assertEqual(expected, actual) | |
def test_turn_to_play(self): | |
# given | |
board = Board() | |
arbitre = Arbitre(board) | |
expected = RED | |
# when | |
arbitre.play_turn(0) | |
actual = arbitre.get_current_player() | |
# then | |
self.assertEqual(expected, actual) | |
def test_2_turns_to_play(self): | |
# given | |
board = Board() | |
arbitre = Arbitre(board) | |
expected = YELLOW | |
# when | |
arbitre.play_turn(0) | |
arbitre.play_turn(0) | |
actual = arbitre.get_current_player() | |
# then | |
self.assertEqual(expected, actual) | |
def test_enter_current_player_turn_empty_board(self): | |
# given | |
board = Board() | |
arbitre = Arbitre(board) | |
expected = [[YELLOW], [], [], [], [], [], []] | |
# when | |
arbitre.play_turn(0) | |
actual = board.get_state() | |
# then | |
self.assertEqual(expected, actual) | |
def test_enter_current_player_turn_not_empty_board(self): | |
# given | |
board = Board() | |
arbitre = Arbitre(board) | |
expected = [[YELLOW], [], [RED], [], [], [], []] | |
# when | |
arbitre.play_turn(0) | |
arbitre.play_turn(2) | |
actual = board.get_state() | |
# then | |
self.assertEqual(expected, actual) | |
def test_declare_if_in_progress(self): | |
# given | |
board = Board() | |
arbitre = Arbitre(board) | |
expected = "In Progress" | |
# when | |
arbitre.play_turn(0) | |
arbitre.play_turn(2) | |
actual = arbitre.declare_score() | |
# then | |
self.assertEqual(expected, actual) | |
def test_declare_if_game_is_won(self): | |
# given | |
board = Board() | |
board.state = [[YELLOW, YELLOW, YELLOW, YELLOW], [RED, RED, RED], [], [], [], [], []] | |
arbitre = Arbitre(board) | |
expected_status = "WINNER !" | |
expected_winner = YELLOW | |
# when | |
actual_status, actual_winner = arbitre.declare_score() | |
# then | |
self.assertEqual((expected_status, expected_winner), (actual_status, actual_winner)) | |
# def test_declare_if_game_is_null(self): | |
# # given | |
# board = Board() | |
# board.state = [[YELLOW, YELLOW, YELLOW, RED, RED, RED], | |
# [RED, RED, RED, YELLOW, YELLOW, YELLOW], | |
# [YELLOW, YELLOW, YELLOW, RED, RED, RED], | |
# [RED, RED, RED, YELLOW, YELLOW, YELLOW], | |
# [YELLOW, YELLOW, YELLOW, RED, RED, RED], | |
# [RED, RED, RED, YELLOW, YELLOW, YELLOW], | |
# [YELLOW, YELLOW, YELLOW, RED, RED, RED], | |
# [RED, RED, RED, YELLOW, YELLOW, YELLOW]] | |
# | |
# arbitre = Arbitre(board) | |
# expected_status = "NULLLLLLL" | |
# | |
# # when | |
# actual_status = arbitre.declare_score() | |
# | |
# # then | |
# self.assertEqual(expected_status, actual_status) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment