Skip to content

Commit

Permalink
Add BetterEngine, moves in center if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
lkeegan committed Jul 18, 2024
1 parent 4cf1fda commit f5a7c71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/effective_software_testing/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
9 changes: 8 additions & 1 deletion tests/test_engine.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit f5a7c71

Please sign in to comment.