Skip to content

Instantly share code, notes, and snippets.

@mackwic
Created December 6, 2018 15:36
Show Gist options
  • Save mackwic/055790af3b88b8d31e895917dda218d5 to your computer and use it in GitHub Desktop.
Save mackwic/055790af3b88b8d31e895917dda218d5 to your computer and use it in GitHub Desktop.
Groupe 3
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 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):
self.next_player = YELLOW
def get_next_player(self):
return self.next_player
def next_turn(self):
if(self.next_player == YELLOW):
self.next_player = RED
else:
self.next_player = YELLOW
def verify_winner(self, analyser, grille):
return analyser.analyse(grille)
def play(self, grille, column):
grille.add_chip(column, self.next_player)
return grille.get_state()
def run():
print("running interactive")
if __name__ == '__main__':
run()
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 TestArbitre(unittest.TestCase):
def test_if_begins_by_yellow(self):
# arrange
arbitre = Arbitre()
# action
next_player = arbitre.next_player
# assertion
self.assertEqual(next_player,YELLOW)
def test_2_iterations(self):
# arrange
arbitre = Arbitre()
# action
arbitre.next_turn()
next_player = arbitre.next_player
# assertion
self.assertEqual(next_player,RED)
def test_4_iterations(self):
# arrange
arbitre = Arbitre()
# action
hist_game =[]
for i in range(3):
arbitre.next_turn()
hist_game.append(arbitre.next_player)
# assertion
self.assertEqual(hist_game[-1],RED)
def test_if_there_is_winner(self):
grille = Board()
arbitre = Arbitre()
analyser = Analyseur()
# action
actual_result = arbitre.verify_winner(analyser, grille)
# assertion
self.assertFalse(actual_result)
def test_first_play(self):
# Arrange
grille = Board()
arbitre = Arbitre()
expected_result = [[],[],[],[1],[],[],[]]
# Action
grille = Board()
actual_result = arbitre.play(grille, 3)
# assertion
self.assertEqual(expected_result, actual_result)
def test_update_two_plays(self):
# Arrange
grille = Board()
arbitre = Arbitre()
expected_result = [[],[],[],[1],[2],[],[]]
# Action
grille = Board()
for i in [3,4]:
actual_result = arbitre.play(grille, i)
arbitre.next_turn()
# assertion
self.assertEqual(expected_result, actual_result)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment