From f5a7c71c338270a6a7b76901214bb00e1123a985 Mon Sep 17 00:00:00 2001 From: Liam Keegan Date: Thu, 18 Jul 2024 12:51:33 +0200 Subject: [PATCH] Add BetterEngine, moves in center if possible --- src/effective_software_testing/engine.py | 9 +++++++++ tests/test_engine.py | 9 ++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/effective_software_testing/engine.py b/src/effective_software_testing/engine.py index a397385..f74b240 100644 --- a/src/effective_software_testing/engine.py +++ b/src/effective_software_testing/engine.py @@ -24,3 +24,12 @@ def make_move(self) -> bool: if self._board.make_move(row, col, self._player): return True return False + + +class BetterEngine(Engine): + """A better tic-tac-toe engine that makes winning moves when possible""" + + def make_move(self) -> bool: + if self._board.make_move(1, 1, self._player): + return True + return super().make_move() diff --git a/tests/test_engine.py b/tests/test_engine.py index d441052..b2a0b15 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -1,7 +1,7 @@ from __future__ import annotations from effective_software_testing.player import Player from effective_software_testing.board import Board -from effective_software_testing.engine import Engine +from effective_software_testing.engine import Engine, BetterEngine from typing import Optional import pytest import numpy as np @@ -81,3 +81,10 @@ def test_two_engines_finish_game( assert _count_squares(board, None) >= 0 assert engine_cross.make_move() is False assert engine_circle.make_move() is False + + +def test_better_engine_first_move_in_center(): + board = Board(n=3) + engine = BetterEngine(Player.CROSS, board) + engine.make_move() + assert board.square(1, 1) == Player.CROSS \ No newline at end of file