Skip to content

Instantly share code, notes, and snippets.

@mackwic
Created December 6, 2018 11:15
Show Gist options
  • Save mackwic/051d93c08789cf3792fe04e97d66083f to your computer and use it in GitHub Desktop.
Save mackwic/051d93c08789cf3792fe04e97d66083f to your computer and use it in GitHub Desktop.
import unittest
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 Analyser:
def __init__(self,board):
self.state = board.get_state()
def iswinner(self):
if self.state == [[], [], [], [], [], [], []]:
return False
else:
return True
"""
def get_vertical_alignment(self):
if len(self.state[0]) == 4:
if len(set(self.state[0])) == 1:
return True
else:
return False
else:
return False
"""
def get_vertical_alignment(self):
if len(self.state[0]) >= 4:
alignment_bool = False
token_yellow_count =0
token_red_count = 0
last_token = None
for token in self.state[0]:
if token == YELLOW and last_token==YELLOW:
token_yellow_count +=1
if token_yellow_count == 4:
alignment_bool = True
if token == RED:
token_red_count +=1
if token_red_count == 4:
alignment_bool = True
last_token=token
return alignment_bool
else:
return False
class TestAnalyser(unittest.TestCase):
def test_if_five_tokens_but_red_color_not_alignement_in_the_first_column(self):
# given
board = Board()
analyser = Analyser(board)
col = 0
# when
color = RED
board.add_chip(col, color)
board.add_chip(col, color)
color = YELLOW
board.add_chip(col, color)
color = RED
board.add_chip(col, color)
board.add_chip(col, color)
is_vertical_alignment = analyser.get_vertical_alignment()
# then
self.assertEqual(is_vertical_alignment, False)
def test_if_five_tokens_but_red_color_in_the_first_column(self):
# given
board = Board()
analyser = Analyser(board)
col = 0
color = RED
# when
board.add_chip(col, color)
board.add_chip(col, color)
board.add_chip(col, color)
board.add_chip(col, color)
color = YELLOW
board.add_chip(col, color)
is_vertical_alignment = analyser.get_vertical_alignment()
# then
self.assertEqual(is_vertical_alignment, True)
def test_if_five_tokens_but_yellow_color_in_the_first_column(self):
# given
board = Board()
analyser = Analyser(board)
col = 0
color = YELLOW
# when
board.add_chip(col, color)
board.add_chip(col, color)
board.add_chip(col, color)
board.add_chip(col, color)
color = RED
board.add_chip(col, color)
is_vertical_alignment = analyser.get_vertical_alignment()
# then
self.assertEqual(is_vertical_alignment, True)
def test_if_four_tokens_but_not_same_color(self):
# given
board = Board()
analyser = Analyser(board)
col = 0
color = YELLOW
# when
board.add_chip(col, color)
board.add_chip(col, color)
board.add_chip(col, color)
color = RED
board.add_chip(col, color)
is_vertical_alignment = analyser.get_vertical_alignment()
# then
self.assertEqual(is_vertical_alignment, False)
def test_if_vertical_alignment_with_three_tokens_in_the_first_column(self):
# given
board = Board()
analyser = Analyser(board)
col = 0
color = YELLOW
# when
board.add_chip(col, color)
board.add_chip(col, color)
board.add_chip(col, color)
is_vertical_alignment = analyser.get_vertical_alignment()
# then
self.assertEqual(is_vertical_alignment, False)
def test_if_vertical_alignment_with_four_tokens_in_the_first_column(self):
# given
board = Board()
analyser = Analyser(board)
col = 0
color = YELLOW
# when
board.add_chip(col, color)
board.add_chip(col, color)
board.add_chip(col, color)
board.add_chip(col, color)
is_vertical_alignment = analyser.get_vertical_alignment()
# then
self.assertEqual(is_vertical_alignment, True)
def test_is_winner_4_token_same_color_in_the_same_column_should_be_true(self):
# given
board = Board()
analyser = Analyser(board)
col = 0
color = YELLOW
# when
board.add_chip(col, color)
board.add_chip(col, color)
board.add_chip(col, color)
board.add_chip(col, color)
is_winner = analyser.iswinner()
# then
self.assertEqual(is_winner, True)
def test_is_winner_when_there_is_less_4_token_should_be_false(self):
# given
board = Board()
analyser = Analyser(board)
# when
is_winner = analyser.iswinner()
# then
self.assertEqual(is_winner, False)
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)
if __name__ == '__main__':
unittest.main()
import unittest
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 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)
class Analyser:
def __init__(self, board):
self.board = board
def check(self):
result = False
state = self.board.get_state()
for list_to_check in state:
count = 1
if list_to_check:
current_color = list_to_check[0]
for i in range(1, len(list_to_check)):
if list_to_check[i] == current_color:
count += 1
else:
current_color = list_to_check[i]
count = 1
if count == 4:
return True
current_color = None
count = 1
transposed_state = [[None for i in range(COL)] for j in range(ROW)]
for i, col in enumerate(state):
for j, row in enumerate(col):
transposed_state[j][i] = state[i][j]
'''
for list_to_check in state:
if list_to_check:
if not current_color:
current_color = list_to_check[0]
elif current_color == list_to_check[0]:
count += 1
if count == 4:
return True
'''
for list_to_check in transposed_state:
count = 1
if list_to_check:
current_color = list_to_check[0]
for i in range(1, len(list_to_check)):
if list_to_check[i]:
if list_to_check[i] == current_color:
count += 1
else:
current_color = list_to_check[i]
count = 1
if count == 4:
return True
return result
# =============================================================================
# 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 TestAnalyser(unittest.TestCase):
def test_vertical_winner_from_bottom(self):
# Arrange
board = Board()
for i in range(4):
board.add_chip(0,YELLOW)
analyser = Analyser(board)
# action
result = analyser.check()
# assertion
self.assertTrue(result)
def test_vertical_winner_anywhere_first_column(self):
# arrange
board = Board()
board.add_chip(0, RED)
for i in range(4):
board.add_chip(0,YELLOW)
analyser = Analyser(board)
# action
result = analyser.check()
# assertion
self.assertTrue(result)
def test_vertical_winner_anywhere(self):
# arrange
board = Board()
board.add_chip(2, RED)
for i in range(4):
board.add_chip(2,YELLOW)
analyser = Analyser(board)
# action
result = analyser.check()
# assertion
self.assertTrue(result)
def test_horizontal_winner_of_first_line(self):
# arrange
board = Board()
for i in range(4):
board.add_chip(i, YELLOW)
analyser = Analyser(board)
# action
result = analyser.check()
# assertion
self.assertTrue(result)
def test_horizontal_winner_of_any_line(self):
# arrange
board = Board()
board.add_chip(0, RED)
board.add_chip(1, RED)
board.add_chip(2, YELLOW)
board.add_chip(3, YELLOW)
for i in range(4):
board.add_chip(i, YELLOW)
analyser = Analyser(board)
# action
result = analyser.check()
# assertion
self.assertTrue(result)
if __name__ == '__main__':
unittest.main()
import unittest
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 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 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 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)
if __name__ == '__main__':
unittest.main()
import unittest
YELLOW = 1
RED = 2
EMPTY = 0
COL = 7
ROW = 6
class Board:
def __init__(self):
self.state = []
for i in range(ROW):
self.state.append([0, 0, 0, 0, 0, 0, 0])
def get_state(self):
return self.state
def add_chip(self,col,color):
self.state[0][col] = color
for i in range(ROW):
if self.state[i][col] != 0:
pass
else:
return self.state
return "col pleine"
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 Analyzer:
def __init__(self, board):
self.board = board
def check_horizontal_win(self):
for i in self.board:
pass
return True
class TestBoard(unittest.TestCase):
def test_get_initial_state(self):
# given
board = Board()
# when
state = board.get_state()
# then
self.assertEqual(state, [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]])
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_add_chip_in_2_columns(self):
# given
board = Board()
col = 0
col2 = 1
color = YELLOW
#action
board.add_chip(col, color)
board.add_chip(col2, color)
state = board.get_state()
#assertion
self.assertEqual((state[0][col], state[0][col2]), (YELLOW,YELLOW))
def test_add_chip_stack(self):
# given
board = Board()
col = 1
color = YELLOW
#action
board.add_chip(col, color)
board.add_chip(col, color)
state = board.get_state()
#assertion
self.assertEqual((state[0][col], state[1][col]), (YELLOW,YELLOW))
# def test_set_predefined_state(self):
#
# board = Board()
# expected = [[0, 1, 1, 1, 1, 1, 0], [0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0]]
# actual = board.set_predefined_state(expected)
#
# self.assertEqual(actual, expected)
class TestAnalyze(unittest.TestCase):
# def test_horizontal_win(self):
#
# #Arrange
# board = Board()
# #board = [[0,1,1,1,1,1,0], [0,0,1,0,0,0], [0,0,1,0,0,0], [0,0,1,0,0,0], [0,0,1,0,0,0], [0,0,1,0,0,0]]
# analyzer = Analyzer(board)
# expected_result = True
#
# #Act
# actual_result = analyzer.check_horizontal_win()
#
# #Assert
# self.assertEqual(expected_result, actual_result)
pass
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment