Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the TicTacToe environment #1192

Merged
merged 27 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b12f6ba
Simplify TicTacToe reward accumulation
dm-ackerman Mar 9, 2024
24f7569
Don't update TicTacToe agent on winning step
dm-ackerman Mar 9, 2024
e3f9cc9
Move TicTacToe test for valid moves to Board
dm-ackerman Mar 9, 2024
6708fdb
Hard code winning lines in TicTacToe board
dm-ackerman Mar 9, 2024
ea15ddf
Simplify win check in TicTacToe
dm-ackerman Mar 9, 2024
967caae
Clean up tictactoe board functions
dm-ackerman Mar 9, 2024
e6851a1
Add reset to TicTacToe board
dm-ackerman Mar 11, 2024
42794bc
Update TicTacToe masking and observe
dm-ackerman Mar 11, 2024
2847c17
Update TicTacToe winning code
dm-ackerman Mar 11, 2024
eda5033
Minor cleanups of TicTacToe code
dm-ackerman Mar 11, 2024
364c307
Don't create screen if not rending in TicTacToe
dm-ackerman Mar 11, 2024
758e6a2
Add legal_moves() to TicTacToe board
dm-ackerman Mar 13, 2024
8b7ce5e
Remove win detection short-cut in TicTacToe
dm-ackerman Mar 20, 2024
5bebafa
Remove unneeded variable in TicTacToe
dm-ackerman Mar 20, 2024
8e33bf7
Add test cases for TicTacToe board
dm-ackerman Mar 20, 2024
df4c950
Update TicTacToe code comments
dm-ackerman Mar 20, 2024
f750754
Merge branch 'master' into ttt_update
dm-ackerman Mar 20, 2024
72ef62f
Bump TicTacToe environment to version 4
dm-ackerman Mar 20, 2024
e4bd228
Add __future__ annotations to TicTacToe tests
dm-ackerman Mar 20, 2024
4f35a22
Merge branch 'master' into ttt_update
dm-ackerman Mar 20, 2024
1f95299
Change TicTacToe from medium to easy in SB3 test.
dm-ackerman Mar 22, 2024
39de495
Replace TicTacToe exceptions with asserts
dm-ackerman Mar 22, 2024
6336393
Check messages of assert errors in tictactoe test
dm-ackerman Mar 22, 2024
71ca217
Fix agent swap in TicTacToe
dm-ackerman May 3, 2024
4170f2b
revert TicTacToe version to 3
dm-ackerman May 3, 2024
da8373e
Merge branch 'master' into ttt_update
dm-ackerman May 3, 2024
44716c3
Merge branch 'master' into ttt_update
elliottower May 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions pettingzoo/classic/tictactoe/board.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
class BadTicTacToeMoveException(Exception):
"""Exception raised when a bad move is made on TicTacToe board."""

def __init__(self, message="Bad TicTacToe move"):
super().__init__(message)


TTT_PLAYER1_WIN = 0
TTT_PLAYER2_WIN = 1
TTT_TIE = -1
Expand Down Expand Up @@ -80,15 +73,11 @@ def play_turn(self, agent, pos):
* The spot must be be empty.
* The spot must be in the board (integer: 0 <= spot <= 8)

If any of those are not true, a BadTicTacToeMoveException
will be raised.
If any of those are not true, an assertion will fail.
"""
if pos < 0 or pos > 8:
raise BadTicTacToeMoveException("Invalid move location")
if agent != 0 and agent != 1:
raise BadTicTacToeMoveException("Invalid agent")
if self.squares[pos] != 0:
raise BadTicTacToeMoveException("Location is not empty")
assert pos >= 0 and pos <= 8, "Invalid move location"
assert agent in [0, 1], "Invalid agent"
assert self.squares[pos] == 0, "Location is not empty"

# agent is [0, 1]. board values are stored as [1, 2].
self.squares[pos] = agent + 1
Expand Down
7 changes: 3 additions & 4 deletions pettingzoo/classic/tictactoe/test_board.py
elliottower marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
TTT_PLAYER1_WIN,
TTT_PLAYER2_WIN,
TTT_TIE,
BadTicTacToeMoveException,
Board,
)

Expand Down Expand Up @@ -113,16 +112,16 @@ def test_tictactoe_bad_move() -> None:
board = Board()
# 1) move out of bounds should be rejected
for outside_space in [-1, 9]:
with pytest.raises(BadTicTacToeMoveException):
with pytest.raises(AssertionError):
board.play_turn(0, outside_space)

# 2) move by unknown agent should be rejected
for unknown_agent in [-1, 2]:
with pytest.raises(BadTicTacToeMoveException):
with pytest.raises(AssertionError):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why pytest raises? Haven’t seen that before

Copy link
Contributor Author

@dm-ackerman dm-ackerman Mar 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, that's the recommended pytest way to confirm that a block of code raises a specific assertion. Pytest itself doesn't raise the exception. It catches and ignores that exception (because it's expected) but raises an error if the code in that block doesn't raise the expected error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok fair, we should probably also go and make the code adhere to that as well, because I’m pretty sure elsewhere that isn’t done, but I can see it being good practice. Not a problem here tbh since it’s so small I think it’s fine to have here and then we can do an issue or future prs to fix other stuff to adhere to this practice

board.play_turn(unknown_agent, 0)

# 3) move in occupied space by either agent should be rejected
board.play_turn(0, 4) # this is fine
for agent in [0, 1]:
with pytest.raises(BadTicTacToeMoveException):
with pytest.raises(AssertionError):
elliottower marked this conversation as resolved.
Show resolved Hide resolved
board.play_turn(agent, 4) # repeating move is not valid
Loading